From db5c6b5e45134f3cce21ee7b9b5ba4ca8d1e909c Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 28 Jun 2012 23:01:26 +0200 Subject: Updated README-DEV-OPENGL.txt --- README-DEV-OPENGL.txt | 88 ++------------------------------------------------- 1 file changed, 3 insertions(+), 85 deletions(-) diff --git a/README-DEV-OPENGL.txt b/README-DEV-OPENGL.txt index 9952325..6656047 100644 --- a/README-DEV-OPENGL.txt +++ b/README-DEV-OPENGL.txt @@ -1,86 +1,4 @@ -README for developers +README for Developers -This file outlines the most important things for developers. - -1. Goal. - - This branch is dedicated to rewriting the graphics engine as well as window & event handling, sound and all other parts of the project dependent on WinAPI or DirectX, to new platform-independent libraries: SDL, OpenGL and fmod (for sound). - The general rule is to rewrite the code so that it resembles the old one as closely as possible so that later on, it can be joined easily with the other modules, which will not be touched yet. Of course, it doesn't mean to sacrifice the usability or flexibility of new interfaces, so some cleaning-up is welcome. - - -2. Build system and organisation of directories. - - The directories in the repository are as following: - src/CBot separate CBot library - src/app class CApplication and everything concerned with SDL plus other system-dependent code such as displaying a message box, finding files, etc. - src/common shared structs, enums, defines, etc.; should not have any external dependencies - src/graphics/common interface of graphics engine (CEngine) and device (CDevice), without concrete implementation, shared structs such as Vertex, Material, etc., "effects" classes: CCamera, CLight, CParticle that will use the graphics engine interface - src/graphics/opengl concrete implementation of CEngine and CDevice classes in OpenGL: CGLEngine and CGLDevice - src/graphics/d3d in (far) future - perhaps a newer implementation in DirectX (9? 10?) - src/math mathematical structures and functions - src/object non-graphical game engine, that is robots, buildings, etc.; dependent only on interface of graphics engine, not on concrete implementation - src/ui 2D user interface (menu, buttons, check boxes, etc.); also without dependencies to concrete implementation of graphics engine - src/sound sound and music engine written using fmod library - src/physics physics engine - src/script link with the CBot library - src/old old modules that will be replaced by new code - - Other directories, not very important right now, include: - src/doc contains the Doxygen mainpage text; it will probably be removed to app/main.cpp or some other place soon - src/metafile a separate program for packing data files to .dat format - - The build system is as follows: - /CMakeLists.txt - definition of project, build type setup, general compiler options and reference to src subdirectory - /src/CMakeLists.txt - defines the colobot target - new implementation in SDL, compiles and runs on Windows and Linux, for now only "hello world"-like - /src/CBot/CMakeLists.txt - defines the CBot library target, currently not referenced by colobot target because it is still WinAPI-dependent - - There is also a generated header common/config.h with #defines set by CMake. - - -3. Plan of work. - - What is done so far: - - changes in the build system - - rewriting of math module and reaching independence from old FPOINT, D3DVECTOR and D3DMATRIX structs - - first draft of class and struct templates in src/graphics/common - - What remains to be done: - - in CBot library - remove dependencies to WinAPI and translate the comments from French to English - - write CApplication class, including handling of SDL events, mouse, joystick, etc. - - (connected with the above) remove dependencies from src/common - - complete the CDevice and CEngine interfaces and write the concrete implementations of CGLDevice and CGLEngine - - write the implementation of other classes in graphics engine, matching the old implementation but using only the new interface - - write the implementation of new sound module - - rewrite the old UI classes to use new graphics interface - - rewrite the old src/object classes to use new graphics interface - - Certain tasks regarding the work should be assigned to each developer using Issues on github, so that the division is clear. - - -3. Rewriting modules. - - The rewriting rule is the following: every old module/header that needs to be replaced by a new one with all declarations either changed to other names or, preferrably, enclosed in a separate namespace. This way, the new code will be separated from the old one, for the time being, thus making it possible to work on new code and not break anything else. Once the functionality of new code matches the old one, it can be replaced in old code with ease. - - -4. Documentation. - - All new code should be documented in Doxygen. Also, don't hesitate to add comments in the old code, where you deciphered some use or detail that you want to share. - - -5. Tests. - - Whenever possible, please write unit tests for your code. Tests should go into test subdirectory in each of the code directories and for now, should be independent of main build system (later on, we will combine them, but for now it will be easier this way). - - -6. Commiting code. - - Code commited to the repository should not break compilation nor tests. Breaking the compilation of old code can be tolerated but the new one - no! If you are uncertain, or want to make partial commit or something like that, commit to your own fork, or if you must, commit but comment out the code that breaks the build. - - -7. Whitespace rules. - - Please indent with spaces, 1 indentation level = 4 spaces. Unix line endings. And don't leave whitespace at the end of lines. Thank you :) - - - -I will probably think of something more in the future, but that's it for now. Thanks for reading and good luck :) +Information contained in this file was moved to developer wiki. Its current address is: +http://colobot.info/wiki/doku.php?id=developers -- cgit v1.2.3-1-g7c22 From e37019943cb77d8c0735b330339a139430202fd8 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 30 Jun 2012 00:12:04 +0200 Subject: Event handling, CApplication and switch to c++-11 - added/changed event structs and event queue in common/event.h - added event handling and some minor functions in CApplication - switched to --std=c++11 because of union in Event struct --- CMakeLists.txt | 4 +- src/app/app.cpp | 332 ++++++++++++++++++++++++++----------------- src/app/app.h | 80 +++++++---- src/common/event.cpp | 164 +++++++++------------ src/common/event.h | 269 +++++++++++++++++++++++++---------- src/common/key.h | 34 +++++ src/common/misc.h | 2 +- src/graphics/common/engine.h | 2 +- 8 files changed, 546 insertions(+), 341 deletions(-) create mode 100644 src/common/key.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d24e0a..b825022 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,8 @@ find_package(SDL_image REQUIRED) set(CMAKE_BUILD_TYPE debug) # Global compile flags -set(CMAKE_CXX_FLAGS_RELEASE "-O2") -set(CMAKE_CXX_FLAGS_DEBUG "-w -g -O0") +set(CMAKE_CXX_FLAGS_RELEASE "-std=c++11 -O2") +set(CMAKE_CXX_FLAGS_DEBUG "-std=c++11 -w -g -O0") # Subdirectory with sources add_subdirectory(src bin) diff --git a/src/app/app.cpp b/src/app/app.cpp index 4005707..4367852 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -42,14 +42,14 @@ struct ApplicationPrivate //! Joystick SDL_Joystick *joystick; //! Index of joystick device - int joystickDevice; + int joystickIndex; ApplicationPrivate() { memset(¤tEvent, 0, sizeof(SDL_Event)); surface = NULL; joystick = NULL; - joystickDevice = 0; + joystickIndex = 0; } }; @@ -60,21 +60,22 @@ CApplication::CApplication() m_exitCode = 0; m_iMan = new CInstanceManager(); - m_event = new CEvent(m_iMan); - m_engine = 0; - m_robotMain = 0; - m_sound = 0; + m_eventQueue = new CEventQueue(m_iMan); + + m_engine = NULL; + m_robotMain = NULL; + m_sound = NULL; m_keyState = 0; m_axeKey = Math::Vector(0.0f, 0.0f, 0.0f); m_axeJoy = Math::Vector(0.0f, 0.0f, 0.0f); - m_vidMemTotal = 0; - m_active = false; - m_activateApp = false; - m_ready = false; - m_joystick = false; + m_active = false; + m_activateApp = false; + m_ready = false; + m_joystickEnabled = false; + m_time = 0.0f; for (int i = 0; i < 32; i++) @@ -84,13 +85,8 @@ CApplication::CApplication() m_windowTitle = "COLOBOT"; - m_appUseZBuffer = true; - m_appUseStereo = true; m_showStats = false; m_debugMode = false; - m_audioState = true; - m_audioTrack = true; - m_niceMouse = false; m_setupMode = true; ResetKey(); @@ -101,6 +97,9 @@ CApplication::~CApplication() delete m_private; m_private = NULL; + delete m_eventQueue; + m_eventQueue = NULL; + delete m_iMan; m_iMan = NULL; } @@ -116,14 +115,6 @@ Error CApplication::ParseArguments(int argc, char *argv[]) m_showStats = true; SetDebugMode(true); } - else if (arg == "-audiostate") - { - m_audioState = false; - } - else if (arg == "-audiotrack") - { - m_audioTrack = false; - } // TODO else {} report invalid argument } @@ -132,43 +123,32 @@ Error CApplication::ParseArguments(int argc, char *argv[]) bool CApplication::Create() { -/* -TODO - Full screen by default unless in debug mode - if (! m_debugMode) - m_deviceConfig.fullScreen = true; - - int full = 0; - if (GetProfileInt("Device", "FullScreen", full)) - m_deviceConfig.fullScreen = full == 1; -*/ - // Temporarily -- only in windowed mode m_deviceConfig.fullScreen = false; -/* -TODO // Create the 3D engine. - m_engine = new CEngine(m_iMan, this); + m_engine = new Gfx::CEngine(m_iMan, this); + /* TODO // Initialize the app's custom scene stuff if (! m_engine->OneTimeSceneInit()) { SystemDialog(SDT_ERROR, "COLOBOT - Error", m_engine->RetError()); return false; - } + }*/ - // Create the sound instance. +/* // Create the sound instance. m_sound = new CSound(m_iMan); // Create the robot application. - m_robotMain = new CRobotMain(m_iMan); -*/ + m_robotMain = new CRobotMain(m_iMan); */ - Uint32 initFlags = SDL_INIT_VIDEO; - if (m_joystick) - initFlags |= SDL_INIT_JOYSTICK; + + /* SDL initialization sequence */ + + + Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; if (SDL_Init(initFlags) < 0) { @@ -177,7 +157,7 @@ TODO } const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); - if (! videoInfo) + if (videoInfo == NULL) { SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL error while getting video info:\n " + std::string(SDL_GetError()) ); return false; @@ -219,7 +199,7 @@ TODO m_private->surface = SDL_SetVideoMode(m_deviceConfig.width, m_deviceConfig.height, m_deviceConfig.bpp, videoFlags); - if (! m_private->surface) + if (m_private->surface == NULL) { SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("SDL error while setting video mode:\n") + std::string(SDL_GetError()) ); @@ -228,38 +208,19 @@ TODO SDL_WM_SetCaption(m_windowTitle.c_str(), m_windowTitle.c_str()); + // Enable translating key codes of key press events to unicode chars SDL_EnableUNICODE(1); + // Enable joystick event generation + SDL_JoystickEventState(SDL_ENABLE); -/* -TODO - InitJoystick(); + // For now, enable joystick for testing + SetJoystickEnabled(true); - if ( !GetProfileInt("Setup", "Sound3D", b3D) ) - { - b3D = true; - } - m_pSound->SetDebugMode(m_bDebugMode); - m_pSound->Create(m_hWnd, b3D); - m_pSound->CacheAll(); - m_pSound->SetState(m_bAudioState); - m_pSound->SetAudioTrack(m_bAudioTrack); - m_pSound->SetCDpath(m_CDpath); - - // First execution? - if ( !GetProfileInt("Setup", "ObjectDirty", iValue) ) - { - m_pD3DEngine->FirstExecuteAdapt(true); - } - - // Creates the file colobot.ini at the first execution. - m_pRobotMain->CreateIni(); + // TODO ... - m_pRobotMain->ChangePhase(PHASE_WELCOME2); - m_engine->TimeInit(); -*/ // The app is ready to go m_ready = true; @@ -269,6 +230,9 @@ TODO void CApplication::Destroy() { + delete m_engine; + m_engine = NULL; + if (m_private->joystick != NULL) { SDL_JoystickClose(m_private->joystick); @@ -283,6 +247,19 @@ void CApplication::Destroy() SDL_Quit(); } +bool CApplication::OpenJoystick() +{ + m_private->joystick = SDL_JoystickOpen(m_private->joystickIndex); + if (m_private->joystick == NULL) + return false; +} + + +void CApplication::CloseJoystick() +{ + SDL_JoystickClose(m_private->joystick); +} + int CApplication::Run() { m_active = true; @@ -312,9 +289,9 @@ int CApplication::Run() if (m_active && m_ready) { Event event; - while (m_event->GetEvent(event)) + while (m_eventQueue->GetEvent(event)) { - if (event.event == EVENT_QUIT) + if (event.type == EVENT_QUIT) { goto end; // exit both loops } @@ -322,11 +299,6 @@ int CApplication::Run() //m_robotMain->EventProcess(event); } - //if ( !RetNiceMouse()) - //{ - // SetMouseType(m_engine->RetMouseType()); - //} - // If an error occurs, push quit event to the queue if (! Render()) { @@ -345,52 +317,135 @@ end: return m_exitCode; } +//! Translates SDL press state to PressState +PressState TranslatePressState(unsigned char state) +{ + if (state == SDL_PRESSED) + return STATE_PRESSED; + else + return STATE_RELEASED; +} + +/** Conversion of the position of the mouse to the following coordinates: + +x: 0=left, 1=right + +y: 0=down, 1=up +*/ +Math::Point CApplication::WindowToInterfaceCoords(int x, int y) +{ + return Math::Point((float)x / (float)m_deviceConfig.width, + 1.0f - (float)y / (float)m_deviceConfig.height); +} + + void CApplication::ParseEvent() { -/* Event event; + Event event; - if (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN) + if ( (m_private->currentEvent.type == SDL_KEYDOWN) || + (m_private->currentEvent.type == SDL_KEYUP) ) { - if (m_private->currentEvent.button.button == SDL_BUTTON_LEFT) - event.event = EVENT_LBUTTONDOWN; - else if (m_private->currentEvent.button.button == SDL_BUTTON_RIGHT) - event.event = EVENT_RBUTTONDOWN; + if (m_private->currentEvent.type == SDL_KEYDOWN) + event.type = EVENT_KEY_DOWN; + else + event.type = EVENT_KEY_UP; + + event.data.key.key = m_private->currentEvent.key.keysym.sym; + event.data.key.mod = m_private->currentEvent.key.keysym.mod; + event.data.key.state = TranslatePressState(m_private->currentEvent.key.state); + event.data.key.unicode = m_private->currentEvent.key.keysym.unicode; } - else if (m_private->currentEvent.type == SDL_MOUSEBUTTONUP) + else if ( (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN) || + (m_private->currentEvent.type == SDL_MOUSEBUTTONUP) ) { - if (m_private->currentEvent.button.button == SDL_BUTTON_LEFT) - event.event = EVENT_LBUTTONUP; - else if (m_private->currentEvent.button.button == SDL_BUTTON_RIGHT) - event.event = EVENT_RBUTTONUP; + if (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN) + event.type = EVENT_MOUSE_BUTTON_DOWN; + else + event.type = EVENT_MOUSE_BUTTON_UP; + + event.data.mouseButton.button = m_private->currentEvent.button.button; + event.data.mouseButton.state = TranslatePressState(m_private->currentEvent.button.state); + event.data.mouseButton.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); } else if (m_private->currentEvent.type == SDL_MOUSEMOTION) { - event.event = EVENT_MOUSEMOVE; + event.type = EVENT_MOUSE_MOVE; + + event.data.mouseMove.state = TranslatePressState(m_private->currentEvent.button.state); + event.data.mouseMove.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); } - else if (m_private->currentEvent.type == SDL_KEYDOWN) + // TODO: joystick state polling instead of getting events + else if (m_private->currentEvent.type == SDL_JOYAXISMOTION) { - event.event = EVENT_KEYDOWN; + event.type = EVENT_JOY_AXIS; + + event.data.joyAxis.axis = m_private->currentEvent.jaxis.axis; + event.data.joyAxis.value = m_private->currentEvent.jaxis.value; } - else if (m_private->currentEvent.type == SDL_KEYUP) + else if ( (m_private->currentEvent.type == SDL_JOYBUTTONDOWN) || + (m_private->currentEvent.type == SDL_JOYBUTTONUP) ) { - event.event = EVENT_KEYUP; - } + if (m_private->currentEvent.type == SDL_JOYBUTTONDOWN) + event.type = EVENT_JOY_BUTTON_DOWN; + else + event.type = EVENT_JOY_BUTTON_UP; - if (m_robotMain != NULL && event.event != EVENT_NULL) - { - m_robotMain->EventProcess(event); + event.data.joyButton.button = m_private->currentEvent.jbutton.button; + event.data.joyButton.state = TranslatePressState(m_private->currentEvent.jbutton.state); } - if (m_engine != NULL) + + + if (m_robotMain != NULL && event.type != EVENT_NULL) { - m_engine->MsgProc( hWnd, uMsg, wParam, lParam ); + //m_robotMain->EventProcess(event); } - ProcessEvent(event);*/ + ProcessEvent(event); } void CApplication::ProcessEvent(Event event) { - + // Print the events in debug mode to test the code + if (m_debugMode) + { + switch (event.type) + { + case EVENT_KEY_DOWN: + case EVENT_KEY_UP: + printf("EVENT_KEY_%s:\n", (event.type == EVENT_KEY_DOWN) ? "DOWN" : "UP"); + printf(" key = %4x\n", event.data.key.key); + printf(" state = %s\n", (event.data.key.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" mod = %4x\n", event.data.key.mod); + printf(" unicode = %4x\n", event.data.key.unicode); + break; + case EVENT_MOUSE_MOVE: + printf("EVENT_MOUSE_MOVE:\n"); + printf(" state = %s\n", (event.data.mouseMove.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" pos = (%f, %f)\n", event.data.mouseMove.pos.x, event.data.mouseMove.pos.y); + break; + case EVENT_MOUSE_BUTTON_DOWN: + case EVENT_MOUSE_BUTTON_UP: + printf("EVENT_MOUSE_BUTTON_%s:\n", (event.type == EVENT_MOUSE_BUTTON_DOWN) ? "DOWN" : "UP"); + printf(" button = %d\n", event.data.mouseButton.button); + printf(" state = %s\n", (event.data.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" pos = (%f, %f)\n", event.data.mouseButton.pos.x, event.data.mouseButton.pos.y); + break; + case EVENT_JOY_AXIS: + printf("EVENT_JOY_AXIS:\n"); + printf(" axis = %d\n", event.data.joyAxis.axis); + printf(" value = %d\n", event.data.joyAxis.value); + break; + case EVENT_JOY_BUTTON_DOWN: + case EVENT_JOY_BUTTON_UP: + printf("EVENT_JOY_BUTTON_%s:\n", (event.type == EVENT_JOY_BUTTON_DOWN) ? "DOWN" : "UP"); + printf(" button = %d\n", event.data.joyButton.button); + printf(" state = %s\n", (event.data.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + break; + default: + break; + } + } } bool CApplication::Render() @@ -405,14 +460,27 @@ bool CApplication::Render() return true; } +/** Called in to toggle the pause state of the app. */ void CApplication::Pause(bool pause) { - // TODO -} + static long appPausedCount = 0L; -void CApplication::SetMousePos(Math::Point pos) -{ - // TODO + appPausedCount += ( pause ? +1 : -1 ); + m_ready = appPausedCount == 0; + + // Handle the first pause request (of many, nestable pause requests) + if( pause && ( 1 == appPausedCount ) ) + { + // Stop the scene from animating + //m_engine->TimeEnterGel(); + } + + // Final pause request done + if (appPausedCount == 0) + { + // Restart the scene + //m_engine->TimeExitGel(); + } } void CApplication::StepSimulation(float rTime) @@ -420,32 +488,29 @@ void CApplication::StepSimulation(float rTime) // TODO } -void SetShowStat(bool show) +void CApplication::SetShowStat(bool show) { - // TODO + m_showStats = show; } bool CApplication::RetShowStat() { - // TODO - return false; + return m_showStats; } void CApplication::SetDebugMode(bool mode) { - // TODO + m_debugMode = mode; } bool CApplication::RetDebugMode() { - // TODO - return false; + return m_debugMode; } bool CApplication::RetSetupMode() { - // TODO - return false; + return m_setupMode; } void CApplication::FlushPressKey() @@ -469,35 +534,36 @@ int CApplication::RetKey(int keyRank, int option) return 0; } -void CApplication::SetJoystick(bool enable) -{ - // TODO -} - -bool CApplication::RetJoystick() +void CApplication::SetMousePos(Math::Point pos) { // TODO - return false; } -void SetMouseType(Gfx::MouseType type) +void CApplication::SetMouseType(Gfx::MouseType type) { // TODO } -void SetNiceMouse(bool nice) +void CApplication::SetJoystickEnabled(bool enable) { - // TODO -} + m_joystickEnabled = enable; -bool CApplication::RetNiceMouse() -{ - return false; + if (m_joystickEnabled) + { + if (! OpenJoystick()) + { + m_joystickEnabled = false; + } + } + else + { + CloseJoystick(); + } } -bool CApplication::RetNiceMouseCap() +bool CApplication::RetJoystickEnabled() { - return false; + return m_joystickEnabled; } bool CApplication::WriteScreenShot(char *filename, int width, int height) diff --git a/src/app/app.h b/src/app/app.h index e83652c..9d689e2 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -60,44 +60,55 @@ public: //! Main event loop int Run(); -protected: - //! Cleans up before exit - void Destroy(); - //! Processes an SDL event to Event struct - void ParseEvent(); - //! Handles some incoming events - void ProcessEvent(Event event); - //! Renders the image in window - bool Render(); - -public: + //! Enters the pause mode void Pause(bool pause); - void StepSimulation(float rTime); - void SetMousePos(Math::Point pos); + //! Updates the simulation state + void StepSimulation(float rTime); void SetShowStat(bool show); bool RetShowStat(); + void SetDebugMode(bool mode); bool RetDebugMode(); + bool RetSetupMode(); + void SetJoystickEnabled(bool enable); + bool RetJoystickEnabled(); + void FlushPressKey(); void ResetKey(); void SetKey(int keyRank, int option, int key); int RetKey(int keyRank, int option); - void SetJoystick(bool enable); - bool RetJoystick(); - void SetMouseType(Gfx::MouseType type); - void SetNiceMouse(bool nice); - bool RetNiceMouse(); - bool RetNiceMouseCap(); + void SetMousePos(Math::Point pos); + + //? void SetNiceMouse(bool nice); + //? bool RetNiceMouse(); + //? bool RetNiceMouseCap(); bool WriteScreenShot(char *filename, int width, int height); protected: + //! Cleans up before exit + void Destroy(); + //! Processes an SDL event to Event struct + void ParseEvent(); + //! Handles some incoming events + void ProcessEvent(Event event); + //! Renders the image in window + bool Render(); + + //! Opens the joystick device + bool OpenJoystick(); + //! Closes the joystick device + void CloseJoystick(); + + //! Converts window coords to interface coords + Math::Point WindowToInterfaceCoords(int x, int y); + //HRESULT ConfirmDevice( DDCAPS* pddDriverCaps, D3DDEVICEDESC7* pd3dDeviceDesc ); //HRESULT Initialize3DEnvironment(); //HRESULT Change3DEnvironment(); @@ -113,14 +124,20 @@ protected: void OutputText(long x, long y, char* str); protected: + CInstanceManager* m_iMan; //! Private (SDL-dependent data) ApplicationPrivate* m_private; - CInstanceManager* m_iMan; + //! Global event queue + CEventQueue* m_eventQueue; + //! Current configuration of display device Gfx::DeviceConfig m_deviceConfig; + //! Graphics engine Gfx::CEngine* m_engine; - CEvent* m_event; - CRobotMain* m_robotMain; + //! Sound subsystem CSound* m_sound; + //! Main class of the proper game engine + CRobotMain* m_robotMain; + //! Code to return at exit int m_exitCode; @@ -128,19 +145,22 @@ protected: bool m_active; bool m_activateApp; bool m_ready; - bool m_joystick; - std::string m_windowTitle; - long m_vidMemTotal; - bool m_appUseZBuffer; - bool m_appUseStereo; bool m_showStats; bool m_debugMode; - bool m_audioState; - bool m_audioTrack; - bool m_niceMouse; bool m_setupMode; + bool m_joystickEnabled; + + std::string m_windowTitle; + + //? long m_vidMemTotal; + //? bool m_appUseZBuffer; + //? bool m_appUseStereo; + //? bool m_audioState; + //? bool m_audioTrack; + //? bool m_niceMouse; + int m_keyState; Math::Vector m_axeKey; Math::Vector m_axeJoy; diff --git a/src/common/event.cpp b/src/common/event.cpp index 9af4691..e8595d0 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -1,96 +1,68 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// event.cpp - -#include "common/iman.h" -#include "common/event.h" - -#include - - -Event::Event() -{ - event = EVENT_NULL; - param = 0; - axeX = 0.0f; - axeY = 0.0f; - axeZ = 0.0f; - keyState = 0; - rTime = 0.0f; -} - - -// Object's constructor. - -CEvent::CEvent(CInstanceManager* iMan) -{ - m_iMan = iMan; - m_iMan->AddInstance(CLASS_EVENT, this); - - Flush(); -} - -// Object's destructor. - -CEvent::~CEvent() -{ -} - - -// Empty the FIFO of events. - -void CEvent::Flush() -{ - m_head = 0; - m_tail = 0; - m_total = 0; -} - -// Produces an event. - -void CEvent::MakeEvent(Event &event, EventMsg msg) -{ - memset(&event, 0, sizeof(Event)); - event.event = msg; -} - -// Adds an event in the FIFO. - -bool CEvent::AddEvent(const Event &event) -{ - if ( m_total >= MAXEVENT ) return false; - - m_fifo[m_head++] = event; - if ( m_head >= MAXEVENT ) m_head = 0; - m_total ++; - - return true; -} - -// Removes an event from the FIFO. - -bool CEvent::GetEvent(Event &event) -{ - if ( m_head == m_tail ) return false; - - event = m_fifo[m_tail++]; - if ( m_tail >= MAXEVENT ) m_tail = 0; - m_total --; - - return true; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// event.cpp + +#include "common/event.h" +#include "common/iman.h" + + + +CEventQueue::CEventQueue(CInstanceManager* iMan) +{ + m_iMan = iMan; + m_iMan->AddInstance(CLASS_EVENT, this); + + Flush(); +} + +CEventQueue::~CEventQueue() +{ +} + +void CEventQueue::Flush() +{ + m_head = 0; + m_tail = 0; + m_total = 0; +} + +/** If the maximum size of queue has been reached, returns \c false. + Else, adds the event to the queue and returns \c true. */ +bool CEventQueue::AddEvent(const Event &event) +{ + if ( m_total >= MAX_EVENT_QUEUE ) return false; + + m_fifo[m_head++] = event; + if ( m_head >= MAX_EVENT_QUEUE ) m_head = 0; + m_total ++; + + return true; +} + +/** If the queue is empty, returns \c false. + Else, gets the event from the front, puts it into \a event and returns \c true. */ +bool CEventQueue::GetEvent(Event &event) +{ + if ( m_head == m_tail ) return false; + + event = m_fifo[m_tail++]; + if ( m_tail >= MAX_EVENT_QUEUE ) m_tail = 0; + m_total --; + + return true; +} + diff --git a/src/common/event.h b/src/common/event.h index 8174fab..e7ff552 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -19,40 +19,50 @@ #pragma once +#include "common/key.h" #include "math/point.h" +#include -#if !defined (WM_XBUTTONDOWN) -#define WM_XBUTTONDOWN 0x020B -#define WM_XBUTTONUP 0x020C -#define XBUTTON1 0x0001 -#define XBUTTON2 0x0002 -#endif +class CInstanceManager; +/** + \enum EventType + \brief Type of event message + */ +enum EventType +{ -class CInstanceManager; +// TODO: document the meaning of each value + EVENT_NULL = 0, -const int MAXEVENT = 100; + //! Event sent on user or system quit request + EVENT_QUIT = 1, -// Events. + //? EVENT_FRAME = 2, -enum EventMsg -{ - EVENT_NULL = 0, + //! Event sent after pressing a mouse button + EVENT_MOUSE_BUTTON_DOWN = 3, + //! Event sent after releasing a mouse button + EVENT_MOUSE_BUTTON_UP = 4, + //! Event sent after moving the mouse + EVENT_MOUSE_MOVE = 7, + //! Event sent after pressing a key + EVENT_KEY_DOWN = 8, + //! Event sent after releasing a key + EVENT_KEY_UP = 9, + + //? EVENT_CHAR = 10, + //? EVENT_FOCUS = 11, - EVENT_QUIT = 1, - EVENT_FRAME = 2, - EVENT_LBUTTONDOWN = 3, - EVENT_RBUTTONDOWN = 4, - EVENT_LBUTTONUP = 5, - EVENT_RBUTTONUP = 6, - EVENT_MOUSEMOVE = 7, - EVENT_KEYDOWN = 8, - EVENT_KEYUP = 9, - EVENT_CHAR = 10, - EVENT_FOCUS = 11, + //! Event sent after moving joystick axes + EVENT_JOY_AXIS = 12, + //! Event sent after pressing a joystick button + EVENT_JOY_BUTTON_DOWN = 13, + //! Event sent after releasing a joystick button + EVENT_JOY_BUTTON_UP = 14, EVENT_UPDINTERFACE = 20, EVENT_WIN = 30, @@ -525,60 +535,151 @@ enum EventMsg EVENT_STUDIO_STEP = 2053, EVENT_USER = 10000, - EVENT_FORCE_DWORD = 0x7fffffff + EVENT_FORCE_LONG = 0x7fffffff +}; + + +/** \enum PressState + \brief State of key/mouse button */ +enum PressState +{ + STATE_PRESSED, + STATE_RELEASED +}; + + +/** \struct KeyEventData + \brief Additional data for keyboard event */ +struct KeyEventData +{ + //! STATE_PRESSED or STATE_RELEASED */ + PressState state; + //! Key symbol: KEY(...) macro value (from common/key.h) + unsigned int key; + //! Keyboard modifiers: a bitmask made of KEY_MOD(...) macro values (from common/key.h) + unsigned int mod; + //! Unicode character + unsigned int unicode; + + KeyEventData() + : state(STATE_PRESSED), key(0), mod(0), unicode(0) {} +}; + +/** \struct MouseMotionEventData + \brief Additional data for mouse move event */ +struct MouseMoveEventData +{ + //! Current button state + unsigned char state; + //! Position of mouse in normalized coordinates (0..1) + Math::Point pos; + + MouseMoveEventData() + : state(STATE_PRESSED) {} +}; + +/** \struct MouseButtonEventData + \brief Additional data mouse button event */ +struct MouseButtonEventData +{ + //! The mouse button index + unsigned char button; + //! STATE_PRESSED or STATE_RELEASED + PressState state; + //! Position of mouse in normalized coordinates (0..1) + Math::Point pos; + + MouseButtonEventData() + : button(0), state(STATE_PRESSED) {} +}; + +/** \struct JoyAxisEventData + \brief Additional data for joystick axis event */ +struct JoyAxisEventData +{ + //! The joystick axis index + unsigned char axis; + //! The axis value (range: -32768 to 32767) + int value; + + JoyAxisEventData() + : axis(axis), value(value) {} +}; + +/** \struct JoyButtonEventData + \brief Joystick button event structure */ +struct JoyButtonEventData +{ + //! The joystick button index + unsigned char button; + //! STATE_PRESSED or STATE_RELEASED + PressState state; + + JoyButtonEventData() + : button(0), state(STATE_PRESSED) {} }; +// TODO: JoyHatEventData? JoyBallEventData? + + +/** + \struct Event + \brief Event sent by system, interface or game + + Event is described by its type (EventType) and the union + \a data contains additional data about the event. + Different members of the union are filled with different event types. + With some events, nothing is filled (it's zeroed out). + The union contains roughly the same information as SDL_Event struct + but packaged to independent structs and fields. + **/ struct Event { - EventMsg event; // event (EVENT *) - long param; // parameter - Math::Point pos; // mouse position (0 .. 1) - float axeX; // control the X axis (-1 .. 1) - float axeY; // control of the Y axis (-1 .. 1) - float axeZ; // control the Z axis (-1 .. 1) - short keyState; // state of the keyboard (KS_ *) - float rTime; // relative time - - Event(); + //! Type of event (EVENT_*) + EventType type; + /** + \union EventDataUnion + \brief Additional data associated with some events + + For the listed event, the given member is filled with data. + For other event types, it is filled with zeros. + */ + union EventDataUnion + { + //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP + KeyEventData key; + //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP + MouseButtonEventData mouseButton; + //! Additional data for EVENT_MOUSE_MOVE + MouseMoveEventData mouseMove; + //! Additional data for EVENT_JOY + JoyAxisEventData joyAxis; + //! Additional data for EVENT_JOY_AXIS + JoyButtonEventData joyButton; + + EventDataUnion() + { memset(this, 0, sizeof(EventDataUnion)); } + ~EventDataUnion() + {} + } data; + //? long param; // parameter + //? Math::Point pos; // mouse position (0 .. 1) + //? float axeX; // control the X axis (-1 .. 1) + //? float axeY; // control of the Y axis (-1 .. 1) + //? float axeZ; // control the Z axis (-1 .. 1) + //? short keyState; // state of the keyboard (KS_ *) + //? float rTime; // relative time + + Event(EventType aType = EVENT_NULL) : type(aType) {} }; -const int VK_BUTTON1 = (0x100+1); // joystick button 1 -const int VK_BUTTON2 = (0x100+2); // joystick button 2 -const int VK_BUTTON3 = (0x100+3); // joystick button 3 -const int VK_BUTTON4 = (0x100+4); // joystick button 4 -const int VK_BUTTON5 = (0x100+5); // joystick button 5 -const int VK_BUTTON6 = (0x100+6); // joystick button 6 -const int VK_BUTTON7 = (0x100+7); // joystick button 7 -const int VK_BUTTON8 = (0x100+8); // joystick button 8 -const int VK_BUTTON9 = (0x100+9); // joystick button 9 -const int VK_BUTTON10 = (0x100+10); // joystick button 10 -const int VK_BUTTON11 = (0x100+11); // joystick button 11 -const int VK_BUTTON12 = (0x100+12); // joystick button 12 -const int VK_BUTTON13 = (0x100+13); // joystick button 13 -const int VK_BUTTON14 = (0x100+14); // joystick button 14 -const int VK_BUTTON15 = (0x100+15); // joystick button 15 -const int VK_BUTTON16 = (0x100+16); // joystick button 16 -const int VK_BUTTON17 = (0x100+17); // joystick button 17 -const int VK_BUTTON18 = (0x100+18); // joystick button 18 -const int VK_BUTTON19 = (0x100+19); // joystick button 19 -const int VK_BUTTON20 = (0x100+20); // joystick button 20 -const int VK_BUTTON21 = (0x100+21); // joystick button 21 -const int VK_BUTTON22 = (0x100+22); // joystick button 22 -const int VK_BUTTON23 = (0x100+23); // joystick button 23 -const int VK_BUTTON24 = (0x100+24); // joystick button 24 -const int VK_BUTTON25 = (0x100+25); // joystick button 25 -const int VK_BUTTON26 = (0x100+26); // joystick button 26 -const int VK_BUTTON27 = (0x100+27); // joystick button 27 -const int VK_BUTTON28 = (0x100+28); // joystick button 28 -const int VK_BUTTON29 = (0x100+29); // joystick button 29 -const int VK_BUTTON30 = (0x100+30); // joystick button 30 -const int VK_BUTTON31 = (0x100+31); // joystick button 31 -const int VK_BUTTON32 = (0x100+32); // joystick button 32 - -const int VK_WHEELUP = (0x200+1); // Mousewheel up -const int VK_WHEELDOWN = (0x200+2); // Mousewheel down +/** + \enum KeyRank + \brief Slots for key assignment of user controls + */ +// TODO: move to global.h ? enum KeyRank { @@ -609,25 +710,37 @@ enum KeyRank }; +/** + \class CEventQueue + \brief Global event queue -class CEvent + Provides an interface to a global FIFO queue with events (both system- and user-generated). + The queue has a fixed maximum size but it should not be a problem. + */ +class CEventQueue { public: - CEvent(CInstanceManager* iMan); - ~CEvent(); + //! Constant maximum size of queue + static const int MAX_EVENT_QUEUE = 100; + +public: + //! Object's constructor + CEventQueue(CInstanceManager* iMan); + //! Object's destructor + ~CEventQueue(); + //! Empties the FIFO of events void Flush(); - void MakeEvent(Event &event, EventMsg msg); + //! Adds an event to the queue bool AddEvent(const Event &event); bool GetEvent(Event &event); protected: CInstanceManager* m_iMan; - - Event m_fifo[MAXEVENT]; - int m_head; - int m_tail; - int m_total; + Event m_fifo[MAX_EVENT_QUEUE]; + int m_head; + int m_tail; + int m_total; }; diff --git a/src/common/key.h b/src/common/key.h new file mode 100644 index 0000000..0ed6b54 --- /dev/null +++ b/src/common/key.h @@ -0,0 +1,34 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// key.h + +#pragma once + + +#include "SDL/SDL_keysym.h" + +/* Key definitions are specially defined here so that it is clear in other parts of the code + that these are used. It is to avoid having SDL-related enum values or #defines lying around + unchecked. With this approach it will be easier to maintain the code later on. */ + +// Key symbol defined as concatenation to SDLK_... +// If need arises, it can be changed to custom function or anything else +#define KEY(x) SDLK_ # x + +// Key modifier defined as concatenation to KMOD_... +// If need arises, it can be changed to custom function or anything else +#define KEY_MOD(x) KMOD_ # x diff --git a/src/common/misc.h b/src/common/misc.h index e863b69..4853856 100644 --- a/src/common/misc.h +++ b/src/common/misc.h @@ -219,7 +219,7 @@ const int KS_NUMMINUS = (1<<15); // Procedures. -extern EventMsg GetUniqueEventMsg(); +extern EventType GetUniqueEventType(); extern char RetNoAccent(char letter); extern char RetToUpper(char letter); diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index 6d2937c..0d93ea2 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -481,7 +481,7 @@ public: void SetLightMode(bool present); bool RetLightMode(); - void SetEditIndentMode(bool auto); + void SetEditIndentMode(bool autoIndent); bool RetEditIndentMode(); void SetEditIndentValue(int value); -- cgit v1.2.3-1-g7c22 From 9a268f553869ee59288873e059ca938d404378c4 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 30 Jun 2012 10:16:52 +0200 Subject: Switched back to old standard; addded -Wall - removed -std=c++11 - added -Wall and removed most reported warnings --- CMakeLists.txt | 4 +-- src/app/app.cpp | 57 +++++++++++++++++++++++------------------- src/common/event.h | 37 +++++++++------------------ src/graphics/common/camera.h | 3 --- src/graphics/common/particle.h | 2 -- 5 files changed, 45 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b825022..d0fc02e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,8 @@ find_package(SDL_image REQUIRED) set(CMAKE_BUILD_TYPE debug) # Global compile flags -set(CMAKE_CXX_FLAGS_RELEASE "-std=c++11 -O2") -set(CMAKE_CXX_FLAGS_DEBUG "-std=c++11 -w -g -O0") +set(CMAKE_CXX_FLAGS_RELEASE "-O2") +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") # Subdirectory with sources add_subdirectory(src bin) diff --git a/src/app/app.cpp b/src/app/app.cpp index 4367852..5612c17 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -26,6 +26,8 @@ #include #include +#include + /** * \struct ApplicationPrivate @@ -252,6 +254,8 @@ bool CApplication::OpenJoystick() m_private->joystick = SDL_JoystickOpen(m_private->joystickIndex); if (m_private->joystick == NULL) return false; + + return true; } @@ -351,10 +355,10 @@ void CApplication::ParseEvent() else event.type = EVENT_KEY_UP; - event.data.key.key = m_private->currentEvent.key.keysym.sym; - event.data.key.mod = m_private->currentEvent.key.keysym.mod; - event.data.key.state = TranslatePressState(m_private->currentEvent.key.state); - event.data.key.unicode = m_private->currentEvent.key.keysym.unicode; + event.key.key = m_private->currentEvent.key.keysym.sym; + event.key.mod = m_private->currentEvent.key.keysym.mod; + event.key.state = TranslatePressState(m_private->currentEvent.key.state); + event.key.unicode = m_private->currentEvent.key.keysym.unicode; } else if ( (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN) || (m_private->currentEvent.type == SDL_MOUSEBUTTONUP) ) @@ -364,24 +368,24 @@ void CApplication::ParseEvent() else event.type = EVENT_MOUSE_BUTTON_UP; - event.data.mouseButton.button = m_private->currentEvent.button.button; - event.data.mouseButton.state = TranslatePressState(m_private->currentEvent.button.state); - event.data.mouseButton.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); + event.mouseButton.button = m_private->currentEvent.button.button; + event.mouseButton.state = TranslatePressState(m_private->currentEvent.button.state); + event.mouseButton.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); } else if (m_private->currentEvent.type == SDL_MOUSEMOTION) { event.type = EVENT_MOUSE_MOVE; - event.data.mouseMove.state = TranslatePressState(m_private->currentEvent.button.state); - event.data.mouseMove.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); + event.mouseMove.state = TranslatePressState(m_private->currentEvent.button.state); + event.mouseMove.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); } // TODO: joystick state polling instead of getting events else if (m_private->currentEvent.type == SDL_JOYAXISMOTION) { event.type = EVENT_JOY_AXIS; - event.data.joyAxis.axis = m_private->currentEvent.jaxis.axis; - event.data.joyAxis.value = m_private->currentEvent.jaxis.value; + event.joyAxis.axis = m_private->currentEvent.jaxis.axis; + event.joyAxis.value = m_private->currentEvent.jaxis.value; } else if ( (m_private->currentEvent.type == SDL_JOYBUTTONDOWN) || (m_private->currentEvent.type == SDL_JOYBUTTONUP) ) @@ -391,8 +395,8 @@ void CApplication::ParseEvent() else event.type = EVENT_JOY_BUTTON_UP; - event.data.joyButton.button = m_private->currentEvent.jbutton.button; - event.data.joyButton.state = TranslatePressState(m_private->currentEvent.jbutton.state); + event.joyButton.button = m_private->currentEvent.jbutton.button; + event.joyButton.state = TranslatePressState(m_private->currentEvent.jbutton.state); } @@ -414,33 +418,33 @@ void CApplication::ProcessEvent(Event event) case EVENT_KEY_DOWN: case EVENT_KEY_UP: printf("EVENT_KEY_%s:\n", (event.type == EVENT_KEY_DOWN) ? "DOWN" : "UP"); - printf(" key = %4x\n", event.data.key.key); - printf(" state = %s\n", (event.data.key.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); - printf(" mod = %4x\n", event.data.key.mod); - printf(" unicode = %4x\n", event.data.key.unicode); + printf(" key = %4x\n", event.key.key); + printf(" state = %s\n", (event.key.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" mod = %4x\n", event.key.mod); + printf(" unicode = %4x\n", event.key.unicode); break; case EVENT_MOUSE_MOVE: printf("EVENT_MOUSE_MOVE:\n"); - printf(" state = %s\n", (event.data.mouseMove.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); - printf(" pos = (%f, %f)\n", event.data.mouseMove.pos.x, event.data.mouseMove.pos.y); + printf(" state = %s\n", (event.mouseMove.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" pos = (%f, %f)\n", event.mouseMove.pos.x, event.mouseMove.pos.y); break; case EVENT_MOUSE_BUTTON_DOWN: case EVENT_MOUSE_BUTTON_UP: printf("EVENT_MOUSE_BUTTON_%s:\n", (event.type == EVENT_MOUSE_BUTTON_DOWN) ? "DOWN" : "UP"); - printf(" button = %d\n", event.data.mouseButton.button); - printf(" state = %s\n", (event.data.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); - printf(" pos = (%f, %f)\n", event.data.mouseButton.pos.x, event.data.mouseButton.pos.y); + printf(" button = %d\n", event.mouseButton.button); + printf(" state = %s\n", (event.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" pos = (%f, %f)\n", event.mouseButton.pos.x, event.mouseButton.pos.y); break; case EVENT_JOY_AXIS: printf("EVENT_JOY_AXIS:\n"); - printf(" axis = %d\n", event.data.joyAxis.axis); - printf(" value = %d\n", event.data.joyAxis.value); + printf(" axis = %d\n", event.joyAxis.axis); + printf(" value = %d\n", event.joyAxis.value); break; case EVENT_JOY_BUTTON_DOWN: case EVENT_JOY_BUTTON_UP: printf("EVENT_JOY_BUTTON_%s:\n", (event.type == EVENT_JOY_BUTTON_DOWN) ? "DOWN" : "UP"); - printf(" button = %d\n", event.data.joyButton.button); - printf(" state = %s\n", (event.data.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" button = %d\n", event.joyButton.button); + printf(" state = %s\n", (event.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); break; default: break; @@ -569,6 +573,7 @@ bool CApplication::RetJoystickEnabled() bool CApplication::WriteScreenShot(char *filename, int width, int height) { // TODO + return false; } void CApplication::InitText() diff --git a/src/common/event.h b/src/common/event.h index e7ff552..70b110d 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -637,31 +637,18 @@ struct Event { //! Type of event (EVENT_*) EventType type; - /** - \union EventDataUnion - \brief Additional data associated with some events - - For the listed event, the given member is filled with data. - For other event types, it is filled with zeros. - */ - union EventDataUnion - { - //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP - KeyEventData key; - //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP - MouseButtonEventData mouseButton; - //! Additional data for EVENT_MOUSE_MOVE - MouseMoveEventData mouseMove; - //! Additional data for EVENT_JOY - JoyAxisEventData joyAxis; - //! Additional data for EVENT_JOY_AXIS - JoyButtonEventData joyButton; - - EventDataUnion() - { memset(this, 0, sizeof(EventDataUnion)); } - ~EventDataUnion() - {} - } data; + + //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP + KeyEventData key; + //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP + MouseButtonEventData mouseButton; + //! Additional data for EVENT_MOUSE_MOVE + MouseMoveEventData mouseMove; + //! Additional data for EVENT_JOY + JoyAxisEventData joyAxis; + //! Additional data for EVENT_JOY_AXIS + JoyButtonEventData joyButton; + //? long param; // parameter //? Math::Point pos; // mouse position (0 .. 1) //? float axeX; // control the X axis (-1 .. 1) diff --git a/src/graphics/common/camera.h b/src/graphics/common/camera.h index f17ecef..be52061 100644 --- a/src/graphics/common/camera.h +++ b/src/graphics/common/camera.h @@ -24,9 +24,6 @@ class CInstanceManager; -class Gfx::CEngine; -class Gfx::CTerrain; -class Gfx::CWater; class CObject; namespace Gfx { diff --git a/src/graphics/common/particle.h b/src/graphics/common/particle.h index e430e2a..dd9f4e3 100644 --- a/src/graphics/common/particle.h +++ b/src/graphics/common/particle.h @@ -25,8 +25,6 @@ class CInstanceManager; class CRobotMain; -class Gfx::CTerrain; -class Gfx::CWater; class CObject; -- cgit v1.2.3-1-g7c22 From 00c737b880865e874391af5681d8247f1cd6d4ad Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 30 Jun 2012 12:26:40 +0200 Subject: Joystick polling with timer - added joystick polling through timer - updated documentation on CApplication class --- src/app/app.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++----- src/app/app.h | 46 +++++++++++++++++++++--- 2 files changed, 138 insertions(+), 14 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 5612c17..2be58ff 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -29,6 +29,13 @@ #include +//! Interval of timer called to update joystick state +const int JOYSTICK_TIMER_INTERVAL = 1000/30; + +//! Function called by the timer +Uint32 JoystickTimerCallback(Uint32 interval, void *); + + /** * \struct ApplicationPrivate * \brief Private data of CApplication class @@ -45,6 +52,8 @@ struct ApplicationPrivate SDL_Joystick *joystick; //! Index of joystick device int joystickIndex; + //! Id of joystick timer + SDL_TimerID joystickTimer; ApplicationPrivate() { @@ -52,12 +61,19 @@ struct ApplicationPrivate surface = NULL; joystick = NULL; joystickIndex = 0; + joystickTimer = 0; } }; +CApplication* CApplication::m_appInstance = NULL; + + CApplication::CApplication() { + assert(m_appInstance == NULL); + m_appInstance = this; + m_private = new ApplicationPrivate(); m_exitCode = 0; @@ -80,11 +96,6 @@ CApplication::CApplication() m_time = 0.0f; - for (int i = 0; i < 32; i++) - { - m_joyButton[i] = false; - } - m_windowTitle = "COLOBOT"; m_showStats = false; @@ -150,7 +161,7 @@ bool CApplication::Create() /* SDL initialization sequence */ - Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; + Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_TIMER; if (SDL_Init(initFlags) < 0) { @@ -213,8 +224,8 @@ bool CApplication::Create() // Enable translating key codes of key press events to unicode chars SDL_EnableUNICODE(1); - // Enable joystick event generation - SDL_JoystickEventState(SDL_ENABLE); + // Don't generate joystick events + SDL_JoystickEventState(SDL_IGNORE); // For now, enable joystick for testing @@ -255,13 +266,90 @@ bool CApplication::OpenJoystick() if (m_private->joystick == NULL) return false; + // Create the vectors with joystick axis & button states to exactly the required size + m_joyAxeState = std::vector(SDL_JoystickNumAxes(m_private->joystick), 0); + m_joyButtonState = std::vector(SDL_JoystickNumButtons(m_private->joystick), false); + + // Create a timer for polling joystick state + m_private->joystickTimer = SDL_AddTimer(JOYSTICK_TIMER_INTERVAL, JoystickTimerCallback, NULL); + return true; } void CApplication::CloseJoystick() { + // Timer will remove itself automatically + SDL_JoystickClose(m_private->joystick); + m_private->joystick = NULL; +} + +Uint32 JoystickTimerCallback(Uint32 interval, void *) +{ + CApplication *app = CApplication::RetInstance(); + if ((app == NULL) || (! app->RetJoystickEnabled())) + return 0; // don't run the timer again + + app->UpdateJoystick(); + + return interval; // run for the same interval again +} + +/** Updates the state info in CApplication and on change, creates SDL events and pushes them to SDL event queue. + This way, the events get handled properly in the main event loop and besides, SDL_PushEvent() ensures thread-safety. */ +void CApplication::UpdateJoystick() +{ + if (! m_joystickEnabled) + return; + + SDL_JoystickUpdate(); + + for (int axis = 0; axis < (int) m_joyAxeState.size(); ++axis) + { + int newValue = SDL_JoystickGetAxis(m_private->joystick, axis); + + if (m_joyAxeState[axis] != newValue) + { + m_joyAxeState[axis] = newValue; + + SDL_Event joyAxisEvent; + + joyAxisEvent.jaxis.type = SDL_JOYAXISMOTION; + joyAxisEvent.jaxis.which = 0; + joyAxisEvent.jaxis.axis = axis; + joyAxisEvent.jaxis.value = newValue; + + SDL_PushEvent(&joyAxisEvent); + } + } + + for (int button = 0; button < (int) m_joyButtonState.size(); ++button) + { + bool newValue = SDL_JoystickGetButton(m_private->joystick, button) == 1; + + if (m_joyButtonState[button] != newValue) + { + m_joyButtonState[button] = newValue; + + SDL_Event joyButtonEvent; + + if (newValue) + { + joyButtonEvent.jbutton.type = SDL_JOYBUTTONDOWN; + joyButtonEvent.jbutton.state = SDL_PRESSED; + } + else + { + joyButtonEvent.jbutton.type = SDL_JOYBUTTONUP; + joyButtonEvent.jbutton.state = SDL_RELEASED; + } + joyButtonEvent.jbutton.which = 0; + joyButtonEvent.jbutton.button = button; + + SDL_PushEvent(&joyButtonEvent); + } + } } int CApplication::Run() @@ -444,7 +532,7 @@ void CApplication::ProcessEvent(Event event) case EVENT_JOY_BUTTON_UP: printf("EVENT_JOY_BUTTON_%s:\n", (event.type == EVENT_JOY_BUTTON_DOWN) ? "DOWN" : "UP"); printf(" button = %d\n", event.joyButton.button); - printf(" state = %s\n", (event.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + printf(" state = %s\n", (event.joyButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); break; default: break; diff --git a/src/app/app.h b/src/app/app.h index 9d689e2..b2d9135 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -25,6 +25,7 @@ #include "graphics/common/engine.h" #include +#include class CInstanceManager; @@ -39,10 +40,32 @@ struct ApplicationPrivate; * \class CApplication * \brief Main application * - * This class is responsible for creating and handling main application window, - * receiving events, etc. + * This class is responsible for main application execution, including creating + * and handling main application window, receiving events, etc. + * + * It is a singleton class with only one instance that can be created. + * + * Creation of other main objects + * + * The class creates the only instance of CInstanceManager, CEventQueue, CEngine, + * CRobotMain and CSound classes. + * + * Window management + * + * The class is responsible for creating app window, setting and changing the video mode, + * setting the position of mouse and changing the cursor, grabbing and writing screenshots. + * + * Events + * + * Events are taken from SDL event queue and either handled by CApplication or translated + * to common events from src/common.h and pushed to global event queue CEventQueue. + * Joystick events are generated somewhat differently, by running a separate timer, + * polling the device for changes and synthesising events on change. It avoids flooding + * the event queue with too many joystick events and the granularity of the timer can be + * adjusted. + * + * The events are further handled in CRobotMain class. * - * ... */ class CApplication { @@ -52,6 +75,10 @@ public: //! Destructor ~CApplication(); + //! Returns the only CApplication instance + static CApplication* RetInstance() + { return m_appInstance; } + public: //! Parses commandline arguments Error ParseArguments(int argc, char *argv[]); @@ -66,6 +93,9 @@ public: //! Updates the simulation state void StepSimulation(float rTime); + //! Polls the state of joystick axes and buttons + void UpdateJoystick(); + void SetShowStat(bool show); bool RetShowStat(); @@ -124,6 +154,9 @@ protected: void OutputText(long x, long y, char* str); protected: + //! The only instance of CApplication + static CApplication* m_appInstance; + //! Instance manager CInstanceManager* m_iMan; //! Private (SDL-dependent data) ApplicationPrivate* m_private; @@ -138,7 +171,6 @@ protected: //! Main class of the proper game engine CRobotMain* m_robotMain; - //! Code to return at exit int m_exitCode; @@ -164,10 +196,14 @@ protected: int m_keyState; Math::Vector m_axeKey; Math::Vector m_axeJoy; - bool m_joyButton[32]; Math::Point m_mousePos; long m_mouseWheel; + //! Current state of joystick axes; may be updated from another thread + std::vector m_joyAxeState; + //! Current state of joystick buttons; may be updated from another thread + std::vector m_joyButtonState; + float m_time; long m_key[50][2]; }; -- cgit v1.2.3-1-g7c22 From 5bbf897fb1d54df0dc84a0cea35775083591b88f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 30 Jun 2012 21:57:58 +0200 Subject: Removed README-DEV-OPENGL to avoid confusion --- README-DEV-OPENGL.txt | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 README-DEV-OPENGL.txt diff --git a/README-DEV-OPENGL.txt b/README-DEV-OPENGL.txt deleted file mode 100644 index 6656047..0000000 --- a/README-DEV-OPENGL.txt +++ /dev/null @@ -1,4 +0,0 @@ -README for Developers - -Information contained in this file was moved to developer wiki. Its current address is: -http://colobot.info/wiki/doku.php?id=developers -- cgit v1.2.3-1-g7c22 From 9bd4ec03b272e7925b11c3efc2bd8460894ea589 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 1 Jul 2012 01:37:30 +0200 Subject: CDevice interface and stub of implementation - added CDevice abstract interface - began implementation of CGLDevice - added stub for Texture struct - created CGLDeviceConfig - changed particule -> particle & other minor changes --- CMakeLists.txt | 2 + src/app/app.cpp | 44 +++++---- src/app/app.h | 3 - src/graphics/common/device.cpp | 29 +++++- src/graphics/common/device.h | 134 ++++++++++++++++++++++++++- src/graphics/common/engine.cpp | 13 +++ src/graphics/common/engine.h | 76 +++++++++------- src/graphics/common/particle.h | 104 ++++++++++----------- src/graphics/common/text.h | 4 +- src/graphics/common/texture.h | 28 ++++++ src/graphics/common/water.h | 4 +- src/graphics/opengl/gldevice.cpp | 192 ++++++++++++++++++++++++++++++++++++++- src/graphics/opengl/gldevice.h | 94 ++++++++++++++++++- 13 files changed, 608 insertions(+), 119 deletions(-) create mode 100644 src/graphics/common/texture.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d0fc02e..3a50bcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,8 @@ find_package(OpenGL REQUIRED) find_package(SDL REQUIRED) find_package(SDL_image REQUIRED) +# TODO: check for SDL version. Should be >= 1.2.10 + # Build with debugging symbols set(CMAKE_BUILD_TYPE debug) diff --git a/src/app/app.cpp b/src/app/app.cpp index 2be58ff..2e1948f 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -21,6 +21,7 @@ #include "app/system.h" #include "common/iman.h" +#include "graphics/opengl/gldevice.h" #include @@ -54,6 +55,8 @@ struct ApplicationPrivate int joystickIndex; //! Id of joystick timer SDL_TimerID joystickTimer; + //! Current configuration of OpenGL display device + Gfx::GLDeviceConfig deviceConfig; ApplicationPrivate() { @@ -137,18 +140,18 @@ Error CApplication::ParseArguments(int argc, char *argv[]) bool CApplication::Create() { // Temporarily -- only in windowed mode - m_deviceConfig.fullScreen = false; + m_private->deviceConfig.fullScreen = false; // Create the 3D engine. m_engine = new Gfx::CEngine(m_iMan, this); - /* TODO + // Initialize the app's custom scene stuff if (! m_engine->OneTimeSceneInit()) { SystemDialog(SDT_ERROR, "COLOBOT - Error", m_engine->RetError()); return false; - }*/ + } /* // Create the sound instance. m_sound = new CSound(m_iMan); @@ -178,7 +181,7 @@ bool CApplication::Create() Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; - if (m_deviceConfig.resizeable) + if (m_private->deviceConfig.resizeable) videoFlags |= SDL_RESIZABLE; // Use hardware surface if available @@ -191,16 +194,25 @@ bool CApplication::Create() if (videoInfo->blit_hw) videoFlags |= SDL_HWACCEL; - if (m_deviceConfig.fullScreen) + if (m_private->deviceConfig.fullScreen) videoFlags |= SDL_FULLSCREEN; - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + // Set OpenGL attributes + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, m_private->deviceConfig.redSize); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, m_private->deviceConfig.greenSize); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, m_private->deviceConfig.blueSize); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, m_private->deviceConfig.alphaSize); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, m_private->deviceConfig.depthSize); + + if (m_private->deviceConfig.doubleBuf) + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + /* If hardware acceleration specifically requested, this will force the hw accel + and fail with error if not available */ + if (m_private->deviceConfig.hardwareAccel) + SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0) { @@ -209,8 +221,8 @@ bool CApplication::Create() return false; } - m_private->surface = SDL_SetVideoMode(m_deviceConfig.width, m_deviceConfig.height, - m_deviceConfig.bpp, videoFlags); + m_private->surface = SDL_SetVideoMode(m_private->deviceConfig.width, m_private->deviceConfig.height, + m_private->deviceConfig.bpp, videoFlags); if (m_private->surface == NULL) { @@ -426,8 +438,8 @@ y: 0=down, 1=up */ Math::Point CApplication::WindowToInterfaceCoords(int x, int y) { - return Math::Point((float)x / (float)m_deviceConfig.width, - 1.0f - (float)y / (float)m_deviceConfig.height); + return Math::Point((float)x / (float)m_private->deviceConfig.width, + 1.0f - (float)y / (float)m_private->deviceConfig.height); } @@ -546,7 +558,7 @@ bool CApplication::Render() if (! result) return false; - if (m_deviceConfig.doubleBuf) + if (m_private->deviceConfig.doubleBuf) SDL_GL_SwapBuffers(); return true; diff --git a/src/app/app.h b/src/app/app.h index b2d9135..4cb6976 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -21,7 +21,6 @@ #include "common/misc.h" -#include "graphics/common/device.h" #include "graphics/common/engine.h" #include @@ -162,8 +161,6 @@ protected: ApplicationPrivate* m_private; //! Global event queue CEventQueue* m_eventQueue; - //! Current configuration of display device - Gfx::DeviceConfig m_deviceConfig; //! Graphics engine Gfx::CEngine* m_engine; //! Sound subsystem diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp index 53274b3..fcd4318 100644 --- a/src/graphics/common/device.cpp +++ b/src/graphics/common/device.cpp @@ -1,14 +1,33 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// device.cpp + + #include "graphics/common/device.h" -//! Sets the default values -Gfx::DeviceConfig::DeviceConfig() + +void Gfx::DeviceConfig::LoadDefault() { width = 800; height = 600; - bpp = 16; + bpp = 32; fullScreen = false; resizeable = false; - hardwareAccel = true; doubleBuf = true; noFrame = false; -} \ No newline at end of file +} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 5900570..4604e88 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -20,8 +20,22 @@ #pragma once +#include "graphics/common/color.h" +#include "graphics/common/light.h" +#include "graphics/common/material.h" +#include "graphics/common/texture.h" +#include "graphics/common/vertex.h" +#include "math/matrix.h" + + namespace Gfx { +/** + \struct DeviceConfig + \brief General config for graphics device + + These settings are common window options set by SDL. +*/ struct DeviceConfig { //! Screen width @@ -34,19 +48,131 @@ struct DeviceConfig bool fullScreen; //! Resizeable window bool resizeable; - //! Hardware acceleration - bool hardwareAccel; //! Double buffering bool doubleBuf; //! No window frame (also set with full screen) bool noFrame; - DeviceConfig(); + //! Constructor calls LoadDefault() + DeviceConfig() { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/** + \enum TransformType + \brief Type of transformation in rendering pipeline + + Corresponds directly to DirectX's transformation types. Listed are only the used types. */ +enum TransformType +{ + TRANSFORM_WORLD, + TRANSFORM_VIEW, + TRANSFORM_PROJECTION +}; + +/** + \enum RenderState + \brief Render states that can be enabled/disabled + + Corresponds to DirectX's render states. Listed are only the used modes. + + TODO: replace with functions in CDevice */ +enum RenderState +{ + RENDER_STATE_ALPHABLENDENABLE, + RENDER_STATE_ALPHAFUNC, + RENDER_STATE_ALPHAREF, + RENDER_STATE_ALPHATESTENABLE, + RENDER_STATE_AMBIENT, + RENDER_STATE_CULLMODE, + RENDER_STATE_DESTBLEND, + RENDER_STATE_DITHERENABLE, + RENDER_STATE_FILLMODE, + RENDER_STATE_FOGCOLOR, + RENDER_STATE_FOGENABLE, + RENDER_STATE_FOGEND, + RENDER_STATE_FOGSTART, + RENDER_STATE_FOGVERTEXMODE, + RENDER_STATE_LIGHTING, + RENDER_STATE_SHADEMODE, + RENDER_STATE_SPECULARENABLE, + RENDER_STATE_SRCBLEND, + RENDER_STATE_TEXTUREFACTOR, + RENDER_STATE_WRAP, + RENDER_STATE_ZBIAS, + RENDER_STATE_ZENABLE, + RENDER_STATE_ZFUNC, + RENDER_STATE_ZWRITEENABLE +}; + +/** + \enum PrimitiveType + \brief Type of primitive to render + + Only these two types are used. */ +enum PrimitiveType +{ + PRIMITIVE_TRIANGLES, + PRIMITIVE_TRIANGLE_STRIP }; +/** + \class CDevice + \brief Abstract interface of graphics device + + It is based on DIRECT3DDEVICE class from DirectX to make it easier to port existing code. + It encapsulates the general graphics device state and provides a common interface + to graphics-specific functions which will be used throughout the program, + both in CEngine class and in UI classes. Note that it doesn't contain all functions from DirectX, + only those that were used in old code. + + */ class CDevice { - // TODO +public: + //! Initializes the device, setting the initial state + virtual void Initialize() = 0; + //! Destroys the device, releasing every acquired resource + virtual void Destroy() = 0; + + // TODO: documentation + + virtual void BeginScene() = 0; + virtual void EndScene() = 0; + + virtual void Clear() = 0; + + virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0; + virtual const Math::Matrix& GetTransform(TransformType type) = 0; + virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix) = 0; + + virtual void SetMaterial(const Gfx::Material &material) = 0; + virtual const Gfx::Material& GetMaterial() = 0; + + virtual int GetMaxLightCount() = 0; + virtual void SetLight(int index, const Gfx::Light &light) = 0; + virtual const Gfx::Light& GetLight(int index) = 0; + virtual void SetLightEnabled(int index, bool enabled) = 0; + virtual bool GetLightEnabled(int index) = 0; + + virtual int GetMaxTextureCount() = 0; + virtual const Gfx::Texture& GetTexture(int index) = 0; + virtual void SetTexture(int index, const Gfx::Texture &texture) = 0; + + // TODO: + // virtual void GetTextureStageState() = 0; + // virtual void SetTextureStageState() = 0; + + virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; + virtual bool GetRenderState(Gfx::RenderState state) = 0; + + // TODO: + // virtual void ComputeSphereVisibility() = 0; + + virtual void DrawPrimitive(PrimitiveType, Vertex *vertices, int vertexCount) = 0; + virtual void DrawPrimitive(PrimitiveType, VertexTex2 *vertices, int vertexCount) = 0; }; }; // namespace Gfx diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index 3b9a89d..9906a0a 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -35,6 +35,17 @@ Gfx::CEngine::~CEngine() // TODO } +std::string Gfx::CEngine::RetError() +{ + return m_error; +} + +int Gfx::CEngine::OneTimeSceneInit() +{ + // TODO + return 1; +} + int Gfx::CEngine::Render() { /* Just a hello world for now */ @@ -71,3 +82,5 @@ int Gfx::CEngine::Render() return 1; } + + diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index 0d93ea2..df8f16b 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -29,10 +29,16 @@ #include "math/vector.h" +#include + + class CApplication; class CInstanceManager; class CObject; + +namespace Snd { class CSound; +}; namespace Gfx { @@ -117,46 +123,46 @@ enum ShadowType SHADOW_WORM = 1 }; -enum RenderState +enum EngineRenderState { //! Normal opaque materials - RSTATE_NORMAL = 0, + ENG_RSTATE_NORMAL = 0, //! The transparent texture (black = no) - RSTATE_TTEXTURE_BLACK = (1<<0), + ENG_RSTATE_TTEXTURE_BLACK = (1<<0), //! The transparent texture (white = no) - RSTATE_TTEXTURE_WHITE = (1<<1), + ENG_RSTATE_TTEXTURE_WHITE = (1<<1), //! The transparent diffuse color - RSTATE_TDIFFUSE = (1<<2), + ENG_RSTATE_TDIFFUSE = (1<<2), //! Texture wrap - RSTATE_WRAP = (1<<3), + ENG_RSTATE_WRAP = (1<<3), //! Texture borders with solid color - RSTATE_CLAMP = (1<<4), + ENG_RSTATE_CLAMP = (1<<4), //! Light texture (ambient max) - RSTATE_LIGHT = (1<<5), + ENG_RSTATE_LIGHT = (1<<5), //! Double black texturing - RSTATE_DUAL_BLACK = (1<<6), + ENG_RSTATE_DUAL_BLACK = (1<<6), //! Double white texturing - RSTATE_DUAL_WHITE = (1<<7), + ENG_RSTATE_DUAL_WHITE = (1<<7), //! Part 1 (no change in. MOD!) - RSTATE_PART1 = (1<<8), + ENG_RSTATE_PART1 = (1<<8), //! Part 2 - RSTATE_PART2 = (1<<9), + ENG_RSTATE_PART2 = (1<<9), //! Part 3 - RSTATE_PART3 = (1<<10), + ENG_RSTATE_PART3 = (1<<10), //! Part 4 - RSTATE_PART4 = (1<<11), + ENG_RSTATE_PART4 = (1<<11), //! Double-sided face - RSTATE_2FACE = (1<<12), + ENG_RSTATE_2FACE = (1<<12), //! Image using alpha channel - RSTATE_ALPHA = (1<<13), + ENG_RSTATE_ALPHA = (1<<13), //! Always use 2nd floor texturing - RSTATE_SECOND = (1<<14), + ENG_RSTATE_SECOND = (1<<14), //! Causes the fog - RSTATE_FOG = (1<<15), + ENG_RSTATE_FOG = (1<<15), //! The transparent color (black = no) - RSTATE_TCOLOR_BLACK = (1<<16), + ENG_RSTATE_TCOLOR_BLACK = (1<<16), //! The transparent color (white = no) - RSTATE_TCOLOR_WHITE = (1<<17) + ENG_RSTATE_TCOLOR_WHITE = (1<<17) }; @@ -289,6 +295,8 @@ public: CEngine(CInstanceManager *iMan, CApplication *app); ~CEngine(); + std::string RetError(); + void SetDevice(Gfx::CDevice *device); Gfx::CDevice* RetDevice(); @@ -439,7 +447,7 @@ public: void RetBackground(char *name, Gfx::Color &up, Gfx::Color &down, Gfx::Color &cloudUp, Gfx::Color &cloudDown, bool &full, bool &quarter); void SetFrontsizeName(char *name); void SetOverFront(bool front); - void SetOverColor(const Gfx::Color &color=Gfx::Color(), int mode=RSTATE_TCOLOR_BLACK); + void SetOverColor(const Gfx::Color &color=Gfx::Color(), int mode=ENG_RSTATE_TCOLOR_BLACK); void SetParticuleDensity(float value); float RetParticuleDensity(); @@ -584,18 +592,20 @@ protected: void DrawSprite(Math::Point pos, Math::Point dim, int icon); protected: - CInstanceManager* m_iMan; - CApplication* m_app; - Gfx::CDevice* m_device; - Gfx::CText* m_text; - Gfx::CLight* m_light; - Gfx::CParticle* m_particule; - Gfx::CWater* m_water; - Gfx::CCloud* m_cloud; - Gfx::CLightning* m_blitz; - Gfx::CPlanet* m_planet; - Gfx::CTerrain* m_terrain; - CSound* m_sound; + CInstanceManager* m_iMan; + CApplication* m_app; + Gfx::CDevice* m_device; + Gfx::CText* m_text; + Gfx::CLight* m_light; + Gfx::CParticle* m_particle; + Gfx::CWater* m_water; + Gfx::CCloud* m_cloud; + Gfx::CLightning* m_lightning; + Gfx::CPlanet* m_planet; + Gfx::CTerrain* m_terrain; + Snd::CSound* m_sound; + + std::string m_error; int m_blackSrcBlend[2]; int m_blackDestBlend[2]; diff --git a/src/graphics/common/particle.h b/src/graphics/common/particle.h index dd9f4e3..62d001d 100644 --- a/src/graphics/common/particle.h +++ b/src/graphics/common/particle.h @@ -50,7 +50,7 @@ const short SH_MAX = 3; // type == 4 -> text (white background) -enum ParticuleType +enum ParticleType { PARTIEXPLOT = 1, // technology explosion PARTIEXPLOO = 2, // organic explosion @@ -193,20 +193,20 @@ enum ParticuleType PARTITRACE19 = 159, // trace }; -enum ParticulePhase +enum ParticlePhase { PARPHSTART = 0, PARPHEND = 1, }; -struct Particule +struct Particle { char bUsed; // TRUE -> particle used char bRay; // TRUE -> ray with goal unsigned short uniqueStamp; // unique mark short sheet; // sheet (0..n) - ParticuleType type; // type PARTI* - ParticulePhase phase; // phase PARPH* + ParticleType type; // type PARTI* + ParticlePhase phase; // phase PARPH* float mass; // mass of the particle (in rebounding) float weight; // weight of the particle (for noise) float duration; // length of life @@ -233,7 +233,7 @@ struct Particule struct Track { char bUsed; // TRUE -> drag used - char bDrawParticule; + char bDrawParticle; float step; // duration of not float last; // increase last not memorized float intensity; // intensity at starting (0..1) @@ -246,7 +246,7 @@ struct Track struct WheelTrace { - ParticuleType type; // type PARTI* + ParticleType type; // type PARTI* Math::Vector pos[4]; // rectangle positions float startTime; // beginning of life }; @@ -261,16 +261,16 @@ public: void SetGLDevice(CDevice device); - void FlushParticule(); - void FlushParticule(int sheet); - int CreateParticule(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticuleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreateFrag(Math::Vector pos, Math::Vector speed, Triangle *triangle, ParticuleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreatePart(Math::Vector pos, Math::Vector speed, ParticuleType type, float duration=1.0f, float mass=0.0f, float weight=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreateRay(Math::Vector pos, Math::Vector goal, ParticuleType type, Math::Point dim, float duration=1.0f, int sheet=0); - int CreateTrack(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticuleType type, float duration=1.0f, float mass=0.0f, float length=10.0f, float width=1.0f); - void CreateWheelTrace(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3, const Math::Vector &p4, ParticuleType type); - void DeleteParticule(ParticuleType type); - void DeleteParticule(int channel); + void FlushParticle(); + void FlushParticle(int sheet); + int CreateParticle(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreateFrag(Math::Vector pos, Math::Vector speed, Triangle *triangle, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreatePart(Math::Vector pos, Math::Vector speed, ParticleType type, float duration=1.0f, float mass=0.0f, float weight=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreateRay(Math::Vector pos, Math::Vector goal, ParticleType type, Math::Point dim, float duration=1.0f, int sheet=0); + int CreateTrack(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float length=10.0f, float width=1.0f); + void CreateWheelTrace(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3, const Math::Vector &p4, ParticleType type); + void DeleteParticle(ParticleType type); + void DeleteParticle(int channel); void SetObjectLink(int channel, CObject *object); void SetObjectFather(int channel, CObject *object); void SetPosition(int channel, Math::Vector pos); @@ -279,57 +279,57 @@ public: void SetAngle(int channel, float angle); void SetIntensity(int channel, float intensity); void SetParam(int channel, Math::Vector pos, Math::Point dim, float zoom, float angle, float intensity); - void SetPhase(int channel, ParticulePhase phase, float duration); + void SetPhase(int channel, ParticlePhase phase, float duration); bool GetPosition(int channel, Math::Vector &pos); Gfx::Color RetFogColor(Math::Vector pos); void SetFrameUpdate(int sheet, bool bUpdate); - void FrameParticule(float rTime); - void DrawParticule(int sheet); + void FrameParticle(float rTime); + void DrawParticle(int sheet); bool WriteWheelTrace(char *filename, int width, int height, Math::Vector dl, Math::Vector ur); protected: void DeleteRank(int rank); bool CheckChannel(int &channel); - void DrawParticuleTriangle(int i); - void DrawParticuleNorm(int i); - void DrawParticuleFlat(int i); - void DrawParticuleFog(int i); - void DrawParticuleRay(int i); - void DrawParticuleSphere(int i); - void DrawParticuleCylinder(int i); - void DrawParticuleWheel(int i); - CObject* SearchObjectGun(Math::Vector old, Math::Vector pos, ParticuleType type, CObject *father); - CObject* SearchObjectRay(Math::Vector pos, Math::Vector goal, ParticuleType type, CObject *father); + void DrawParticleTriangle(int i); + void DrawParticleNorm(int i); + void DrawParticleFlat(int i); + void DrawParticleFog(int i); + void DrawParticleRay(int i); + void DrawParticleSphere(int i); + void DrawParticleCylinder(int i); + void DrawParticleWheel(int i); + CObject* SearchObjectGun(Math::Vector old, Math::Vector pos, ParticleType type, CObject *father); + CObject* SearchObjectRay(Math::Vector pos, Math::Vector goal, ParticleType type, CObject *father); void Play(Snd::Sound sound, Math::Vector pos, float amplitude); bool TrackMove(int i, Math::Vector pos, float progress); - void TrackDraw(int i, ParticuleType type); + void TrackDraw(int i, ParticleType type); protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CDevice* m_pDevice; - CRobotMain* m_main; - CTerrain* m_terrain; - CWater* m_water; - CSound* m_sound; + CInstanceManager* m_iMan; + CEngine* m_engine; + CDevice* m_pDevice; + CRobotMain* m_main; + CTerrain* m_terrain; + CWater* m_water; + Snd::CSound* m_sound; - Particule m_particule[MAXPARTICULE*MAXPARTITYPE]; - Gfx::Triangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 - Track m_track[MAXTRACK]; - int m_wheelTraceTotal; - int m_wheelTraceIndex; - WheelTrace m_wheelTrace[MAXWHEELTRACE]; - int m_totalInterface[MAXPARTITYPE][SH_MAX]; - bool m_bFrameUpdate[SH_MAX]; - int m_fogTotal; - int m_fog[MAXPARTIFOG]; - int m_uniqueStamp; - int m_exploGunCounter; - float m_lastTimeGunDel; - float m_absTime; + Gfx::Particle m_particule[MAXPARTICULE*MAXPARTITYPE]; + Gfx::Triangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 + Track m_track[MAXTRACK]; + int m_wheelTraceTotal; + int m_wheelTraceIndex; + WheelTrace m_wheelTrace[MAXWHEELTRACE]; + int m_totalInterface[MAXPARTITYPE][SH_MAX]; + bool m_bFrameUpdate[SH_MAX]; + int m_fogTotal; + int m_fog[MAXPARTIFOG]; + int m_uniqueStamp; + int m_exploGunCounter; + float m_lastTimeGunDel; + float m_absTime; }; diff --git a/src/graphics/common/text.h b/src/graphics/common/text.h index 00b73f2..f96dc61 100644 --- a/src/graphics/common/text.h +++ b/src/graphics/common/text.h @@ -73,7 +73,7 @@ public: CText(CInstanceManager *iMan, Gfx::CEngine* engine); ~CText(); - void SetGLDevice(Gfx::CDevice device); + void SetDevice(Gfx::CDevice *device); void DrawText(char *string, char *format, int len, Math::Point pos, float width, int justif, float size, float stretch, int eol); void DrawText(char *string, char *format, Math::Point pos, float width, int justif, float size, float stretch, int eol); @@ -106,7 +106,7 @@ protected: protected: CInstanceManager* m_iMan; Gfx::CEngine* m_engine; - Gfx::CDevice m_pDevice; + Gfx::CDevice* m_device; }; diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h new file mode 100644 index 0000000..ab894db --- /dev/null +++ b/src/graphics/common/texture.h @@ -0,0 +1,28 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// texture.h + +#pragma once + +namespace Gfx { + +struct Texture +{ + // TODO +}; + +}; // namespace Gfx diff --git a/src/graphics/common/water.h b/src/graphics/common/water.h index 5999eac..f5224a4 100644 --- a/src/graphics/common/water.h +++ b/src/graphics/common/water.h @@ -48,7 +48,7 @@ const short MAXWATVAPOR = 10; struct WaterVapor { bool bUsed; - ParticuleType type; + ParticleType type; Math::Vector pos; float delay; float time; @@ -96,7 +96,7 @@ protected: bool CreateLine(int x, int y, int len); void VaporFlush(); - bool VaporCreate(ParticuleType type, Math::Vector pos, float delay); + bool VaporCreate(ParticleType type, Math::Vector pos, float delay); void VaporFrame(int i, float rTime); protected: diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 7938e62..9ef376f 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -18,4 +18,194 @@ #include "graphics/opengl/gldevice.h" -// TODO +#include +#include + + +void Gfx::GLDeviceConfig::LoadDefault() +{ + Gfx::DeviceConfig::LoadDefault(); + + hardwareAccel = true; + + redSize = 8; + blueSize = 8; + greenSize = 8; + alphaSize = 8; + depthSize = 24; +} + +Gfx::CGLDevice::CGLDevice() +{ + m_renderState = 0; +} + + +Gfx::CGLDevice::~CGLDevice() +{ +} + +void Gfx::CGLDevice::Initialize() +{ + // TODO +} + +void Gfx::CGLDevice::Destroy() +{ + // TODO +} + +void Gfx::CGLDevice::BeginScene() +{ + // TODO +} + +void Gfx::CGLDevice::EndScene() +{ + // TODO +} + +void Gfx::CGLDevice::Clear() +{ + // TODO +} + +void Gfx::CGLDevice::SetTransform(Gfx::TransformType type, const Math::Matrix &matrix) +{ + switch (type) + { + case Gfx::TRANSFORM_WORLD: + m_worldMat = matrix; + // TODO + break; + case Gfx::TRANSFORM_VIEW: + m_viewMat = matrix; + // TODO + break; + case Gfx::TRANSFORM_PROJECTION: + m_projectionMat = matrix; + // TODO + break; + default: + assert(false); + } +} + +const Math::Matrix& Gfx::CGLDevice::GetTransform(Gfx::TransformType type) +{ + switch (type) + { + case Gfx::TRANSFORM_WORLD: + return m_worldMat; + case Gfx::TRANSFORM_VIEW: + return m_viewMat; + case Gfx::TRANSFORM_PROJECTION: + return m_projectionMat; + default: + assert(false); + } + + return m_worldMat; // to avoid warning +} + +void Gfx::CGLDevice::MultiplyTransform(Gfx::TransformType type, const Math::Matrix &matrix) +{ + // TODO +} + +void Gfx::CGLDevice::SetMaterial(const Gfx::Material &material) +{ + m_material = material; + + // TODO +} + +const Gfx::Material& Gfx::CGLDevice::GetMaterial() +{ + return m_material; +} + +int Gfx::CGLDevice::GetMaxLightCount() +{ + return m_lights.size(); +} + +void Gfx::CGLDevice::SetLight(int index, const Gfx::Light &light) +{ + assert(index >= 0); + assert(index < (int)m_lights.size()); + + m_lights[index] = light; + + // TODO +} + +const Gfx::Light& Gfx::CGLDevice::GetLight(int index) +{ + assert(index >= 0); + assert(index < (int)m_lights.size()); + + return m_lights[index]; +} + +void Gfx::CGLDevice::SetLightEnabled(int index, bool enabled) +{ + assert(index >= 0); + assert(index < (int)m_lightsEnabled.size()); + + m_lightsEnabled[index] = enabled; + + // TODO +} + +bool Gfx::CGLDevice::GetLightEnabled(int index) +{ + assert(index >= 0); + assert(index < (int)m_lights.size()); + + return m_lightsEnabled[index]; +} + +int Gfx::CGLDevice::GetMaxTextureCount() +{ + return m_textures.size(); +} + +const Gfx::Texture& Gfx::CGLDevice::GetTexture(int index) +{ + assert(index >= 0); + assert(index < (int)m_textures.size()); + + return m_textures[index]; +} + +void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) +{ + assert(index >= 0); + assert(index < (int)m_textures.size()); + + m_textures[index] = texture; + + // TODO +} + +void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) +{ + // TODO +} + +bool Gfx::CGLDevice::GetRenderState(Gfx::RenderState state) +{ + // TODO + return false; +} + +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType, Vertex *vertices, int vertexCount) +{ + // TODO +} + +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType, VertexTex2 *vertices, int vertexCount) +{ + // TODO +} diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index a2fb4a0..0b4702a 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -21,11 +21,103 @@ #include "graphics/common/device.h" + +#include + + namespace Gfx { +/** + \struct GLDeviceConfig + \brief Additional config with OpenGL-specific settings */ +struct GLDeviceConfig : public DeviceConfig +{ + //! Size of red channel in bits + int redSize; + //! Size of green channel in bits + int greenSize; + //! Size of blue channel in bits + int blueSize; + //! Size of alpha channel in bits + int alphaSize; + //! Color depth in bits + int depthSize; + + //! Force hardware acceleration (video mode set will fail on lack of hw accel) + bool hardwareAccel; + + //! Constructor calls LoadDefaults() + GLDeviceConfig() { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/** + \class CGLDevice + \brief Implementation of CDevice interface in OpenGL + + Provides the concrete implementation of 3D device in OpenGL. + + This class should be initialized (by calling Initialize() ) only after + setting the video mode by CApplication, once the OpenGL context is defined. + Because of that, CGLDeviceConfig is outside the CDevice class and must be set + in CApplication. +*/ class CGLDevice : public Gfx::CDevice { - // TODO +public: + CGLDevice(); + virtual ~CGLDevice(); + + virtual void Initialize(); + virtual void Destroy(); + + virtual void BeginScene(); + virtual void EndScene(); + + virtual void Clear(); + + virtual void SetTransform(Gfx::TransformType type, const Math::Matrix &matrix); + virtual const Math::Matrix& GetTransform(Gfx::TransformType type); + virtual void MultiplyTransform(Gfx::TransformType type, const Math::Matrix &matrix); + + virtual void SetMaterial(const Gfx::Material &material); + virtual const Gfx::Material& GetMaterial(); + + virtual int GetMaxLightCount(); + virtual void SetLight(int index, const Gfx::Light &light); + virtual const Gfx::Light& GetLight(int index); + virtual void SetLightEnabled(int index, bool enabled); + virtual bool GetLightEnabled(int index); + + virtual int GetMaxTextureCount(); + virtual const Gfx::Texture& GetTexture(int index); + virtual void SetTexture(int index, const Gfx::Texture &texture); + + virtual void SetRenderState(Gfx::RenderState state, bool enabled); + virtual bool GetRenderState(Gfx::RenderState state); + + virtual void DrawPrimitive(Gfx::PrimitiveType, Vertex *vertices, int vertexCount); + virtual void DrawPrimitive(Gfx::PrimitiveType, VertexTex2 *vertices, int vertexCount); + +private: + //! Current world matrix + Math::Matrix m_worldMat; + //! Current view matrix + Math::Matrix m_viewMat; + //! Current projection matrix + Math::Matrix m_projectionMat; + //! The current material + Gfx::Material m_material; + //! Current lights + std::vector m_lights; + //! Current lights enable status + std::vector m_lightsEnabled; + //! Current textures + std::vector m_textures; + //! Current render state + unsigned long m_renderState; }; }; // namespace Gfx -- cgit v1.2.3-1-g7c22 From d9c5a439d09211ec210195709d275596c6c3c9ba Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 1 Jul 2012 22:59:22 +0200 Subject: CGLDevice implementation - extended Gfx::CDevice interface - written OpenGL implementation in Gfx::CGLDevice - rewrote color and light module - added Gfx::VertexCol - added array casts to Math::Vector, Math::Matrix and Gfx::Color --- src/app/app.cpp | 95 +++++-- src/app/app.h | 23 +- src/app/main.cpp | 5 +- src/graphics/common/color.cpp | 173 +++++------- src/graphics/common/color.h | 25 +- src/graphics/common/device.h | 206 +++++++++++--- src/graphics/common/engine.cpp | 106 +++++--- src/graphics/common/engine.h | 138 +++++----- src/graphics/common/light.h | 64 +++-- src/graphics/common/vertex.h | 35 ++- src/graphics/opengl/gldevice.cpp | 567 +++++++++++++++++++++++++++++++++++---- src/graphics/opengl/gldevice.h | 65 ++++- src/math/geometry.h | 18 ++ src/math/matrix.h | 6 + src/math/point.h | 6 + src/math/vector.h | 6 + 16 files changed, 1173 insertions(+), 365 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 2e1948f..b73adee 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -85,6 +85,7 @@ CApplication::CApplication() m_eventQueue = new CEventQueue(m_iMan); m_engine = NULL; + m_device = NULL; m_robotMain = NULL; m_sound = NULL; @@ -118,6 +119,8 @@ CApplication::~CApplication() delete m_iMan; m_iMan = NULL; + + m_appInstance = NULL; } Error CApplication::ParseArguments(int argc, char *argv[]) @@ -142,14 +145,14 @@ bool CApplication::Create() // Temporarily -- only in windowed mode m_private->deviceConfig.fullScreen = false; - // Create the 3D engine. + // Create the 3D engine m_engine = new Gfx::CEngine(m_iMan, this); - - // Initialize the app's custom scene stuff - if (! m_engine->OneTimeSceneInit()) + // Initialize the app's custom scene stuff, but before initializing the graphics device + if (! m_engine->BeforeCreateInit()) { - SystemDialog(SDT_ERROR, "COLOBOT - Error", m_engine->RetError()); + SystemDialog(SDT_ERROR, "COLOBOT - Error", std::string("Error in CEngine::BeforeCreateInit() :\n") + + std::string(m_engine->GetError()) ); return false; } @@ -168,14 +171,16 @@ bool CApplication::Create() if (SDL_Init(initFlags) < 0) { - SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL initialization error:\n" + std::string(SDL_GetError()) ); + SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL initialization error:\n" + + std::string(SDL_GetError()) ); return false; } const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); if (videoInfo == NULL) { - SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL error while getting video info:\n " + std::string(SDL_GetError()) ); + SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL error while getting video info:\n " + + std::string(SDL_GetError()) ); return false; } @@ -243,8 +248,30 @@ bool CApplication::Create() // For now, enable joystick for testing SetJoystickEnabled(true); - // TODO ... + // The video is ready, we can create and initalize the graphics device + m_device = new Gfx::CGLDevice(); + if (! m_device->Create() ) + { + SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CDevice::Create() :\n") + + std::string(m_device->GetError()) ); + return false; + } + + m_engine->SetDevice(m_device); + if (! m_engine->Create() ) + { + SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CEngine::Create() :\n") + + std::string(m_engine->GetError()) ); + return false; + } + + if (! m_engine->AfterDeviceSetInit() ) + { + SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CEngine::AfterDeviceSetInit() :\n") + + std::string(m_engine->GetError()) ); + return false; + } // The app is ready to go @@ -255,8 +282,35 @@ bool CApplication::Create() void CApplication::Destroy() { - delete m_engine; - m_engine = NULL; + /*if (m_robotMain != NULL) + { + delete m_robotMain; + m_robotMain = NULL; + } + + if (m_sound != NULL) + { + delete m_sound; + m_sound = NULL; + }*/ + + if (m_engine != NULL) + { + if (m_engine->GetWasInit()) + m_engine->Destroy(); + + delete m_engine; + m_engine = NULL; + } + + if (m_device != NULL) + { + if (m_device->GetWasInit()) + m_device->Destroy(); + + delete m_device; + m_device = NULL; + } if (m_private->joystick != NULL) { @@ -264,8 +318,11 @@ void CApplication::Destroy() m_private->joystick = NULL; } - SDL_FreeSurface(m_private->surface); - m_private->surface = NULL; + if (m_private->surface != NULL) + { + SDL_FreeSurface(m_private->surface); + m_private->surface = NULL; + } IMG_Quit(); @@ -299,8 +356,8 @@ void CApplication::CloseJoystick() Uint32 JoystickTimerCallback(Uint32 interval, void *) { - CApplication *app = CApplication::RetInstance(); - if ((app == NULL) || (! app->RetJoystickEnabled())) + CApplication *app = CApplication::GetInstance(); + if ((app == NULL) || (! app->GetJoystickEnabled())) return 0; // don't run the timer again app->UpdateJoystick(); @@ -597,7 +654,7 @@ void CApplication::SetShowStat(bool show) m_showStats = show; } -bool CApplication::RetShowStat() +bool CApplication::GetShowStat() { return m_showStats; } @@ -607,12 +664,12 @@ void CApplication::SetDebugMode(bool mode) m_debugMode = mode; } -bool CApplication::RetDebugMode() +bool CApplication::GetDebugMode() { return m_debugMode; } -bool CApplication::RetSetupMode() +bool CApplication::GetSetupMode() { return m_setupMode; } @@ -632,7 +689,7 @@ void CApplication::SetKey(int keyRank, int option, int key) // TODO } -int CApplication::RetKey(int keyRank, int option) +int CApplication::GetKey(int keyRank, int option) { // TODO return 0; @@ -665,7 +722,7 @@ void CApplication::SetJoystickEnabled(bool enable) } } -bool CApplication::RetJoystickEnabled() +bool CApplication::GetJoystickEnabled() { return m_joystickEnabled; } diff --git a/src/app/app.h b/src/app/app.h index 4cb6976..098f0ad 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -21,6 +21,7 @@ #include "common/misc.h" +#include "graphics/common/device.h" #include "graphics/common/engine.h" #include @@ -75,7 +76,7 @@ public: ~CApplication(); //! Returns the only CApplication instance - static CApplication* RetInstance() + static CApplication* GetInstance() { return m_appInstance; } public: @@ -83,6 +84,8 @@ public: Error ParseArguments(int argc, char *argv[]); //! Initializes the application bool Create(); + //! Cleans up before exit + void Destroy(); //! Main event loop int Run(); @@ -96,33 +99,31 @@ public: void UpdateJoystick(); void SetShowStat(bool show); - bool RetShowStat(); + bool GetShowStat(); void SetDebugMode(bool mode); - bool RetDebugMode(); + bool GetDebugMode(); - bool RetSetupMode(); + bool GetSetupMode(); void SetJoystickEnabled(bool enable); - bool RetJoystickEnabled(); + bool GetJoystickEnabled(); void FlushPressKey(); void ResetKey(); void SetKey(int keyRank, int option, int key); - int RetKey(int keyRank, int option); + int GetKey(int keyRank, int option); void SetMouseType(Gfx::MouseType type); void SetMousePos(Math::Point pos); //? void SetNiceMouse(bool nice); - //? bool RetNiceMouse(); - //? bool RetNiceMouseCap(); + //? bool GetNiceMouse(); + //? bool GetNiceMouseCap(); bool WriteScreenShot(char *filename, int width, int height); protected: - //! Cleans up before exit - void Destroy(); //! Processes an SDL event to Event struct void ParseEvent(); //! Handles some incoming events @@ -163,6 +164,8 @@ protected: CEventQueue* m_eventQueue; //! Graphics engine Gfx::CEngine* m_engine; + //! Graphics device + Gfx::CDevice* m_device; //! Sound subsystem CSound* m_sound; //! Main class of the proper game engine diff --git a/src/app/main.cpp b/src/app/main.cpp index 54d305e..1e102d7 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -35,7 +35,10 @@ int main(int argc, char *argv[]) } if (! app.Create()) - return 0; + { + app.Destroy(); // ensure a clean exit + return 1; + } return app.Run(); } diff --git a/src/graphics/common/color.cpp b/src/graphics/common/color.cpp index dd502f9..c1b7337 100644 --- a/src/graphics/common/color.cpp +++ b/src/graphics/common/color.cpp @@ -21,130 +21,83 @@ #include "math/func.h" -// Returns the color corresponding long. - -long Gfx::RetColor(float intensity) -{ - long color; - - if ( intensity <= 0.0f ) return 0x00000000; - if ( intensity >= 1.0f ) return 0xffffffff; - - color = (int)(intensity*255.0f)<<24; - color |= (int)(intensity*255.0f)<<16; - color |= (int)(intensity*255.0f)<<8; - color |= (int)(intensity*255.0f); - - return color; -} - -// Returns the color corresponding long. - -long Gfx::RetColor(Gfx::Color intensity) +Gfx::ColorHSV Gfx::RGB2HSV(Gfx::Color color) { - long color; - - color = (int)(intensity.a*255.0f)<<24; - color |= (int)(intensity.r*255.0f)<<16; - color |= (int)(intensity.g*255.0f)<<8; - color |= (int)(intensity.b*255.0f); + Gfx::ColorHSV result; - return color; -} + float min = Math::Min(color.r, color.g, color.b); + float max = Math::Max(color.r, color.g, color.b); -// Returns the color corresponding Color. + result.v = max; // intensity -Gfx::Color Gfx::RetColor(long intensity) -{ - Gfx::Color color; - - color.r = (float)((intensity>>16)&0xff)/256.0f; - color.g = (float)((intensity>>8 )&0xff)/256.0f; - color.b = (float)((intensity>>0 )&0xff)/256.0f; - color.a = (float)((intensity>>24)&0xff)/256.0f; + if ( max == 0.0f ) + { + result.s = 0.0f; // saturation + result.h = 0.0f; // undefined color! + } + else + { + float delta = max-min; + result.s = delta/max; // saturation + + if ( color.r == max ) // between yellow & magenta + { + result.h = (color.g-color.b)/delta; + } + else if ( color.g == max ) // between cyan & yellow + { + result.h = 2.0f+(color.b-color.r)/delta; + } + else // between magenta & cyan + { + result.h = 4.0f+(color.r-color.g)/delta; + } + + result.h *= 60.0f; // in degrees + if ( result.h < 0.0f ) result.h += 360.0f; + result.h /= 360.0f; // 0..1 + } - return color; + return result; } - -// RGB to HSV conversion. - -void Gfx::RGB2HSV(Gfx::Color src, Gfx::ColorHSV &dest) +Gfx::Color Gfx::HSV2RGB(Gfx::ColorHSV color) { - float min, max, delta; + Gfx::Color result; - min = Math::Min(src.r, src.g, src.b); - max = Math::Max(src.r, src.g, src.b); + color.h = Math::Norm(color.h)*360.0f; + color.s = Math::Norm(color.s); + color.v = Math::Norm(color.v); - dest.v = max; // intensity - - if ( max == 0.0f ) - { - dest.s = 0.0f; // saturation - dest.h = 0.0f; // undefined color! - } - else - { - delta = max-min; - dest.s = delta/max; // saturation - - if ( src.r == max ) // between yellow & magenta - { - dest.h = (src.g-src.b)/delta; - } - else if ( src.g == max ) // between cyan & yellow + if ( color.s == 0.0f ) // zero saturation? { - dest.h = 2.0f+(src.b-src.r)/delta; + result.r = color.v; + result.g = color.v; + result.b = color.v; // gray } - else // between magenta & cyan + else { - dest.h = 4.0f+(src.r-src.g)/delta; + if ( color.h == 360.0f ) color.h = 0.0f; + color.h /= 60.0f; + int i = (int)color.h; // integer part (0 .. 5) + float f = color.h-i; // fractional part + + float v = color.v; + float p = color.v*(1.0f-color.s); + float q = color.v*(1.0f-(color.s*f)); + float t = color.v*(1.0f-(color.s*(1.0f-f))); + + switch (i) + { + case 0: result.r=v; result.g=t; result.b=p; break; + case 1: result.r=q; result.g=v; result.b=p; break; + case 2: result.r=p; result.g=v; result.b=t; break; + case 3: result.r=p; result.g=q; result.b=v; break; + case 4: result.r=t; result.g=p; result.b=v; break; + case 5: result.r=v; result.g=p; result.b=q; break; + } } - dest.h *= 60.0f; // in degrees - if ( dest.h < 0.0f ) dest.h += 360.0f; - dest.h /= 360.0f; // 0..1 - } -} - -// HSV to RGB conversion. - -void Gfx::HSV2RGB(Gfx::ColorHSV src, Gfx::Color &dest) -{ - int i; - float f,v,p,q,t; - - src.h = Math::Norm(src.h)*360.0f; - src.s = Math::Norm(src.s); - src.v = Math::Norm(src.v); - - if ( src.s == 0.0f ) // zero saturation? - { - dest.r = src.v; - dest.g = src.v; - dest.b = src.v; // gray - } - else - { - if ( src.h == 360.0f ) src.h = 0.0f; - src.h /= 60.0f; - i = (int)src.h; // integer part (0 .. 5) - f = src.h-i; // fractional part - - v = src.v; - p = src.v*(1.0f-src.s); - q = src.v*(1.0f-(src.s*f)); - t = src.v*(1.0f-(src.s*(1.0f-f))); - - switch (i) - { - case 0: dest.r=v; dest.g=t; dest.b=p; break; - case 1: dest.r=q; dest.g=v; dest.b=p; break; - case 2: dest.r=p; dest.g=v; dest.b=t; break; - case 3: dest.r=p; dest.g=q; dest.b=v; break; - case 4: dest.r=t; dest.g=p; dest.b=v; break; - case 5: dest.r=v; dest.g=p; dest.b=q; break; - } - } + return result; } diff --git a/src/graphics/common/color.h b/src/graphics/common/color.h index 12f008f..3b19cf2 100644 --- a/src/graphics/common/color.h +++ b/src/graphics/common/color.h @@ -21,15 +21,28 @@ namespace Gfx { +/** + \struct Color + \brief RGBA color */ struct Color { + //! Red, green, blue and alpha components float r, g, b, a; + //! Constructor; default values are (0,0,0,0) = black Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f) : r(aR), g(aG), b(aB), a(aA) {} -}; + //! Returns the struct cast to \c float* array; use with care! + inline float* Array() + { + return (float*)this; + } +}; +/** + \struct ColorHSV + \brief HSV color */ struct ColorHSV { float h, s, v; @@ -38,13 +51,11 @@ struct ColorHSV : h(aH), s(aS), v(aV) {} }; +//! Converts a RGB color to HSV color +Gfx::ColorHSV RGB2HSV(Gfx::Color color); -long RetColor(float intensity); -long RetColor(Color intensity); -Color RetColor(long intensity); - -void RGB2HSV(Color src, ColorHSV &dest); -void HSV2RGB(ColorHSV src, Color &dest); +//! Converts a HSV color to RGB color +Gfx::Color HSV2RGB(Gfx::ColorHSV color); }; // namespace Gfx diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 4604e88..961fb6b 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -27,6 +27,8 @@ #include "graphics/common/vertex.h" #include "math/matrix.h" +#include + namespace Gfx { @@ -60,11 +62,12 @@ struct DeviceConfig void LoadDefault(); }; + /** \enum TransformType \brief Type of transformation in rendering pipeline - Corresponds directly to DirectX's transformation types. Listed are only the used types. */ + These correspond to DirectX's three transformation matrices. */ enum TransformType { TRANSFORM_WORLD, @@ -74,37 +77,84 @@ enum TransformType /** \enum RenderState - \brief Render states that can be enabled/disabled - - Corresponds to DirectX's render states. Listed are only the used modes. - - TODO: replace with functions in CDevice */ + \brief Render states that can be enabled/disabled */ enum RenderState { - RENDER_STATE_ALPHABLENDENABLE, - RENDER_STATE_ALPHAFUNC, - RENDER_STATE_ALPHAREF, - RENDER_STATE_ALPHATESTENABLE, - RENDER_STATE_AMBIENT, - RENDER_STATE_CULLMODE, - RENDER_STATE_DESTBLEND, - RENDER_STATE_DITHERENABLE, - RENDER_STATE_FILLMODE, - RENDER_STATE_FOGCOLOR, - RENDER_STATE_FOGENABLE, - RENDER_STATE_FOGEND, - RENDER_STATE_FOGSTART, - RENDER_STATE_FOGVERTEXMODE, RENDER_STATE_LIGHTING, - RENDER_STATE_SHADEMODE, - RENDER_STATE_SPECULARENABLE, - RENDER_STATE_SRCBLEND, - RENDER_STATE_TEXTUREFACTOR, - RENDER_STATE_WRAP, - RENDER_STATE_ZBIAS, - RENDER_STATE_ZENABLE, - RENDER_STATE_ZFUNC, - RENDER_STATE_ZWRITEENABLE + RENDER_STATE_TEXTURING, + RENDER_STATE_BLENDING, + RENDER_STATE_FOG, + RENDER_STATE_DEPTH_TEST, + RENDER_STATE_DEPTH_WRITE, + RENDER_STATE_ALPHA_TEST, + RENDER_STATE_DITHERING +}; + +/** + \enum CompFunc + \brief Type of function used to compare values */ +enum CompFunc +{ + COMP_FUNC_NEVER, + COMP_FUNC_LESS, + COMP_FUNC_EQUAL, + COMP_FUNC_NOTEQUAL, + COMP_FUNC_LEQUAL, + COMP_FUNC_GREATER, + COMP_FUNC_GEQUAL, + COMP_FUNC_ALWAYS +}; + +/** + \enum BlendFunc + \brief Type of blending function */ +enum BlendFunc +{ + BLEND_ZERO, + BLEND_ONE, + BLEND_SRC_COLOR, + BLEND_INV_SRC_COLOR, + BLEND_DST_COLOR, + BLEND_INV_DST_COLOR, + BLEND_SRC_ALPHA, + BLEND_INV_SRC_ALPHA, + BLEND_DST_ALPHA, + BLEND_INV_DST_ALPHA, + BLEND_SRC_ALPHA_SATURATE +}; + +/** + \enum FogMode + \brief Type of fog calculation function */ +enum FogMode +{ + FOG_LINEAR, + FOG_EXP, + FOG_EXP2 +}; + +/** + \enum CullMode + \brief Culling mode for polygons */ +enum CullMode +{ + //! Cull clockwise side + CULL_CW, + //! Cull counter-clockwise side + CULL_CCW +}; + +/** + \enum FillMode + \brief Polygon fill mode */ +enum FillMode +{ + //! Draw only points + FILL_POINT, + //! Draw only lines + FILL_LINES, + //! Draw full polygons + FILL_FILL }; /** @@ -114,6 +164,7 @@ enum RenderState Only these two types are used. */ enum PrimitiveType { + PRIMITIVE_LINES, PRIMITIVE_TRIANGLES, PRIMITIVE_TRIANGLE_STRIP }; @@ -132,47 +183,124 @@ enum PrimitiveType class CDevice { public: + virtual ~CDevice() {} + //! Initializes the device, setting the initial state - virtual void Initialize() = 0; + virtual bool Create() = 0; //! Destroys the device, releasing every acquired resource virtual void Destroy() = 0; - // TODO: documentation + //! Returns whether the device has been initialized + virtual bool GetWasInit() = 0; + //! Returns the last encountered error + virtual std::string GetError() = 0; + //! Begins drawing the 3D scene virtual void BeginScene() = 0; + //! Ends drawing the 3D scene virtual void EndScene() = 0; + //! Clears the screen to blank virtual void Clear() = 0; + //! Sets the transform matrix of given type virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0; + //! Returns the current transform matrix of given type virtual const Math::Matrix& GetTransform(TransformType type) = 0; + //! Multiplies the current transform matrix of given type by given matrix virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix) = 0; - virtual void SetMaterial(const Gfx::Material &material) = 0; + //! Sets the current material + virtual void SetMaterial(Gfx::Material &material) = 0; + //! Returns the current material virtual const Gfx::Material& GetMaterial() = 0; + //! Returns the maximum number of lights available virtual int GetMaxLightCount() = 0; - virtual void SetLight(int index, const Gfx::Light &light) = 0; + //! Sets the light at given index + virtual void SetLight(int index, Gfx::Light &light) = 0; + //! Returns the current light at given index virtual const Gfx::Light& GetLight(int index) = 0; + //! Enables/disables the light at given index virtual void SetLightEnabled(int index, bool enabled) = 0; + //! Returns the current enable state of light at given index virtual bool GetLightEnabled(int index) = 0; + // TODO: + // virtual Gfx::Texture* CreateTexture(CImage *image) = 0; + // virtual void DestroyTexture(Gfx::Texture *texture) = 0; + + //! Returns the maximum number of multitexture units virtual int GetMaxTextureCount() = 0; - virtual const Gfx::Texture& GetTexture(int index) = 0; - virtual void SetTexture(int index, const Gfx::Texture &texture) = 0; + //! Sets the (multi)texture at given index + virtual void SetTexture(int index, Gfx::Texture *texture) = 0; + //! Returns the (multi)texture at given index + virtual Gfx::Texture* GetTexture(int index) = 0; // TODO: // virtual void GetTextureStageState() = 0; // virtual void SetTextureStageState() = 0; - virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; - virtual bool GetRenderState(Gfx::RenderState state) = 0; + //! Renders primitive composed of vertices with single texture + virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::Vertex *vertices, int vertexCount) = 0; + //! Renders primitive composed of vertices with color information and single texture + virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount) = 0; + //! Renders primitive composed of vertices with multitexturing (2 textures) + virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexTex2 *vertices, int vertexCount) = 0; // TODO: // virtual void ComputeSphereVisibility() = 0; - virtual void DrawPrimitive(PrimitiveType, Vertex *vertices, int vertexCount) = 0; - virtual void DrawPrimitive(PrimitiveType, VertexTex2 *vertices, int vertexCount) = 0; + + //! Enables/disables the given render state + virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; + //! Returns the current setting of given render state + virtual bool GetRenderState(Gfx::RenderState state) = 0; + + //! Sets the function of depth test + virtual void SetDepthTestFunc(Gfx::CompFunc func) = 0; + //! Returns the current function of depth test + virtual Gfx::CompFunc GetDepthTestFunc() = 0; + + //! Sets the depth bias (constant value added to Z-coords) + virtual void SetDepthBias(float factor) = 0; + //! Returns the current depth bias + virtual float GetDepthBias() = 0; + + //! Sets the alpha test function and reference value + virtual void SetAlphaTestFunc(Gfx::CompFunc func, float refValue) = 0; + //! Returns the current alpha test function and reference value + virtual void GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue) = 0; + + //! Sets the blending functions for source and destination operations + virtual void SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend) = 0; + //! Returns the current blending functions for source and destination operations + virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) = 0; + + //! Sets the clear color + virtual void SetClearColor(Gfx::Color color) = 0; + //! Returns the current clear color + virtual Gfx::Color GetClearColor() = 0; + + //! Sets the global ambient color + virtual void SetGlobalAmbient(Gfx::Color color) = 0; + //! Returns the global ambient color + virtual Gfx::Color GetGlobalAmbient() = 0; + + //! Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) + virtual void SetFogParams(Gfx::FogMode mode, Gfx::Color color, float start, float end, float density) = 0; + //! Returns the current fog parameters: mode, color, start distance, end distance and density (for exp models) + virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) = 0; + + //! Sets the current cull mode + virtual void SetCullMode(Gfx::CullMode mode) = 0; + //! Returns the current cull mode + virtual Gfx::CullMode GetCullMode() = 0; + + //! Sets the current fill mode + virtual void SetFillMode(Gfx::FillMode mode) = 0; + //! Returns the current fill mode + virtual Gfx::FillMode GetFillMode() = 0; }; }; // namespace Gfx diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index 9906a0a..0b92224 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -19,68 +19,110 @@ #include "graphics/common/engine.h" -#include -#include +#include "graphics/common/device.h" +#include "math/geometry.h" -// TODO implementation Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) { + m_iMan = iMan; + m_app = app; + m_device = NULL; + + m_wasInit = false; + // TODO } Gfx::CEngine::~CEngine() { + m_iMan = NULL; + m_app = NULL; + m_device = NULL; + // TODO } -std::string Gfx::CEngine::RetError() +bool Gfx::CEngine::GetWasInit() +{ + return m_wasInit; +} + +std::string Gfx::CEngine::GetError() { return m_error; } -int Gfx::CEngine::OneTimeSceneInit() +bool Gfx::CEngine::BeforeCreateInit() { // TODO - return 1; + return true; } -int Gfx::CEngine::Render() +bool Gfx::CEngine::Create() { - /* Just a hello world for now */ + m_wasInit = true; - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glShadeModel(GL_SMOOTH); - glDisable(GL_DEPTH_TEST); - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + // TODO + return true; +} - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f); +void Gfx::CEngine::Destroy() +{ + // TODO - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + m_wasInit = false; +} - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +void Gfx::CEngine::SetDevice(Gfx::CDevice *device) +{ + m_device = device; +} - //glTranslatef(0.0f, 0.0f, -6.0f); +Gfx::CDevice* Gfx::CEngine::GetDevice() +{ + return m_device; +} - glBegin(GL_TRIANGLES); +bool Gfx::CEngine::AfterDeviceSetInit() +{ + m_device->SetClearColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)); + + // TODO + + return true; +} + +bool Gfx::CEngine::Render() +{ + m_device->BeginScene(); + + Math::Matrix world; + world.LoadIdentity(); + m_device->SetTransform(Gfx::TRANSFORM_WORLD, world); + + Math::Matrix view; + view.LoadIdentity(); + m_device->SetTransform(Gfx::TRANSFORM_VIEW, view); + + Math::Matrix proj; + Math::LoadOrthoProjectionMatrix(proj, -10.0f, 10.0f, -10.0f, 10.0f); + m_device->SetTransform(Gfx::TRANSFORM_PROJECTION, proj); + + Gfx::VertexCol vertices[3] = { - glColor3f(1.0f, 0.0f, 0.0f); - glVertex2f(-2.0f, -1.0f); - glColor3f(0.0f, 1.0f, 0.0f); - glVertex2f(2.0f, -1.0f); - glColor3f(0.0f, 0.0f, 1.0f); - glVertex2f(0.0f, 1.5f); - } - glEnd(); - - glFlush(); - - return 1; + Gfx::VertexCol(Math::Vector(-2.0f, -1.0f, 0.0f), Gfx::Color(1.0f, 0.0f, 0.0f)), + Gfx::VertexCol(Math::Vector( 2.0f, -1.0f, 0.0f), Gfx::Color(0.0f, 1.0f, 0.0f)), + Gfx::VertexCol(Math::Vector( 0.0f, 1.5f, 0.0f), Gfx::Color(0.0f, 0.0f, 1.0f)) + }; + + m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, vertices, 3); + + m_device->EndScene(); + + return true; } diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index df8f16b..04c478c 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -295,23 +295,35 @@ public: CEngine(CInstanceManager *iMan, CApplication *app); ~CEngine(); - std::string RetError(); + bool GetWasInit(); + std::string GetError(); + + bool BeforeCreateInit(); + bool Create(); + void Destroy(); void SetDevice(Gfx::CDevice *device); - Gfx::CDevice* RetDevice(); + Gfx::CDevice* GetDevice(); + + bool AfterDeviceSetInit(); + + + bool Render(); + + void SetTerrain(Gfx::CTerrain* terrain); bool WriteProfile(); void SetPause(bool pause); - bool RetPause(); + bool GetPause(); void SetMovieLock(bool lock); - bool RetMovieLock(); + bool GetMovieLock(); void SetShowStat(bool show); - bool RetShowStat(); + bool GetShowStat(); void SetRenderEnable(bool enable); @@ -319,17 +331,16 @@ public: int InitDeviceObjects(); int DeleteDeviceObjects(); int RestoreSurfaces(); - int Render(); int FrameMove(float rTime); void StepSimul(float rTime); int FinalCleanup(); void AddStatisticTriangle(int nb); - int RetStatisticTriangle(); + int GetStatisticTriangle(); void SetHiliteRank(int *rankList); bool GetHilite(Math::Point &p1, Math::Point &p2); bool GetSpriteCoord(int &x, int &y); void SetInfoText(int line, char* text); - char * RetInfoText(int line); + char * GetInfoText(int line); //LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void FirstExecuteAdapt(bool first); //int GetVidMemTotal(); @@ -337,19 +348,19 @@ public: //bool IsVideo32MB(); bool EnumDevices(char *bufDevices, int lenDevices, char *bufModes, int lenModes, int &totalDevices, int &selectDevices, int &totalModes, int &selectModes); - bool RetFullScreen(); + bool GetFullScreen(); bool ChangeDevice(char *device, char *mode, bool full); - Math::Matrix* RetMatView(); - Math::Matrix* RetMatLeftView(); - Math::Matrix* RetMatRightView(); + Math::Matrix* GetMatView(); + Math::Matrix* GetMatLeftView(); + Math::Matrix* GetMatRightView(); void TimeInit(); void TimeEnterGel(); void TimeExitGel(); float TimeGet(); - int RetRestCreate(); + int GetRestCreate(); int CreateObject(); void FlushObject(); bool DeleteObject(int objRank); @@ -361,7 +372,7 @@ public: Gfx::ObjLevel6* SearchTriangle(int objRank, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max); void ChangeLOD(); bool ChangeSecondTexture(int objRank, char* texName2); - int RetTotalTriangles(int objRank); + int GetTotalTriangles(int objRank); int GetTriangles(int objRank, float min, float max, Gfx::Triangle* buffer, int size, float percent); bool GetBBox(int objRank, Math::Vector &min, Math::Vector &max); bool ChangeTextureMapping(int objRank, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max, Gfx::Mapping mode, float au, float bu, float av, float bv); @@ -369,7 +380,7 @@ public: bool SetObjectTransform(int objRank, const Math::Matrix &transform); bool GetObjectTransform(int objRank, Math::Matrix &transform); bool SetObjectType(int objRank, Gfx::ObjectType type); - Gfx::ObjectType RetObjectType(int objRank); + Gfx::ObjectType GetObjectType(int objRank); bool SetObjectTransparency(int objRank, float value); bool ShadowCreate(int objRank); @@ -382,7 +393,7 @@ public: bool SetObjectShadowRadius(int objRank, float radius); bool SetObjectShadowIntensity(int objRank, float intensity); bool SetObjectShadowHeight(int objRank, float h); - float RetObjectShadowRadius(int objRank); + float GetObjectShadowRadius(int objRank); void GroundSpotFlush(); int GroundSpotCreate(); @@ -405,109 +416,109 @@ public: bool LoadAllTexture(); void SetLimitLOD(int rank, float limit); - float RetLimitLOD(int rank, bool last=false); + float GetLimitLOD(int rank, bool last=false); void SetTerrainVision(float vision); void SetGroundSpot(bool mode); - bool RetGroundSpot(); + bool GetGroundSpot(); void SetShadow(bool mode); - bool RetShadow(); + bool GetShadow(); void SetDirty(bool mode); - bool RetDirty(); + bool GetDirty(); void SetFog(bool mode); - bool RetFog(); - bool RetStateColor(); + bool GetFog(); + bool GetStateColor(); void SetSecondTexture(int texNum); - int RetSecondTexture(); + int GetSecondTexture(); void SetRankView(int rank); - int RetRankView(); + int GetRankView(); void SetDrawWorld(bool draw); void SetDrawFront(bool draw); void SetAmbiantColor(const Gfx::Color &color, int rank=0); - Gfx::Color RetAmbiantColor(int rank=0); + Gfx::Color GetAmbiantColor(int rank=0); void SetWaterAddColor(const Gfx::Color &color); - Gfx::Color RetWaterAddColor(); + Gfx::Color GetWaterAddColor(); void SetFogColor(const Gfx::Color &color, int rank=0); - Gfx::Color RetFogColor(int rank=0); + Gfx::Color GetFogColor(int rank=0); void SetDeepView(float length, int rank=0, bool ref=false); - float RetDeepView(int rank=0); + float GetDeepView(int rank=0); void SetFogStart(float start, int rank=0); - float RetFogStart(int rank=0); + float GetFogStart(int rank=0); void SetBackground(char *name, Gfx::Color up=Gfx::Color(), Gfx::Color down=Gfx::Color(), Gfx::Color cloudUp=Gfx::Color(), Gfx::Color cloudDown=Gfx::Color(), bool full=false, bool quarter=false); - void RetBackground(char *name, Gfx::Color &up, Gfx::Color &down, Gfx::Color &cloudUp, Gfx::Color &cloudDown, bool &full, bool &quarter); + void GetBackground(char *name, Gfx::Color &up, Gfx::Color &down, Gfx::Color &cloudUp, Gfx::Color &cloudDown, bool &full, bool &quarter); void SetFrontsizeName(char *name); void SetOverFront(bool front); void SetOverColor(const Gfx::Color &color=Gfx::Color(), int mode=ENG_RSTATE_TCOLOR_BLACK); void SetParticuleDensity(float value); - float RetParticuleDensity(); + float GetParticuleDensity(); float ParticuleAdapt(float factor); void SetClippingDistance(float value); - float RetClippingDistance(); + float GetClippingDistance(); void SetObjectDetail(float value); - float RetObjectDetail(); + float GetObjectDetail(); void SetGadgetQuantity(float value); - float RetGadgetQuantity(); + float GetGadgetQuantity(); void SetTextureQuality(int value); - int RetTextureQuality(); + int GetTextureQuality(); void SetTotoMode(bool present); - bool RetTotoMode(); + bool GetTotoMode(); void SetLensMode(bool present); - bool RetLensMode(); + bool GetLensMode(); void SetWaterMode(bool present); - bool RetWaterMode(); + bool GetWaterMode(); void SetBlitzMode(bool present); - bool RetBlitzMode(); + bool GetBlitzMode(); void SetSkyMode(bool present); - bool RetSkyMode(); + bool GetSkyMode(); void SetBackForce(bool present); - bool RetBackForce(); + bool GetBackForce(); void SetPlanetMode(bool present); - bool RetPlanetMode(); + bool GetPlanetMode(); void SetLightMode(bool present); - bool RetLightMode(); + bool GetLightMode(); void SetEditIndentMode(bool autoIndent); - bool RetEditIndentMode(); + bool GetEditIndentMode(); void SetEditIndentValue(int value); - int RetEditIndentValue(); + int GetEditIndentValue(); void SetSpeed(float speed); - float RetSpeed(); + float GetSpeed(); void SetTracePrecision(float factor); - float RetTracePrecision(); + float GetTracePrecision(); void SetFocus(float focus); - float RetFocus(); - Math::Vector RetEyePt(); - Math::Vector RetLookatPt(); - float RetEyeDirH(); - float RetEyeDirV(); - Math::Point RetDim(); + float GetFocus(); + Math::Vector GetEyePt(); + Math::Vector GetLookatPt(); + float GetEyeDirH(); + float GetEyeDirV(); + Math::Point GetDim(); void UpdateMatProj(); void ApplyChange(); @@ -515,14 +526,14 @@ public: void FlushPressKey(); void ResetKey(); void SetKey(int keyRank, int option, int key); - int RetKey(int keyRank, int option); + int GetKey(int keyRank, int option); void SetJoystick(bool enable); - bool RetJoystick(); + bool GetJoystick(); void SetDebugMode(bool mode); - bool RetDebugMode(); - bool RetSetupMode(); + bool GetDebugMode(); + bool GetSetupMode(); bool IsVisiblePoint(const Math::Vector &pos); @@ -533,16 +544,16 @@ public: void MoveMousePos(Math::Point pos); void SetMousePos(Math::Point pos); - Math::Point RetMousePos(); + Math::Point GetMousePos(); void SetMouseType(Gfx::MouseType type); - Gfx::MouseType RetMouseType(); + Gfx::MouseType GetMouseType(); void SetMouseHide(bool hide); - bool RetMouseHide(); + bool GetMouseHide(); void SetNiceMouse(bool nice); - bool RetNiceMouse(); - bool RetNiceMouseCap(); + bool GetNiceMouse(); + bool GetNiceMouseCap(); - CText* RetText(); + CText* GetText(); bool ChangeColor(char *name, Gfx::Color colorRef1, Gfx::Color colorNew1, Gfx::Color colorRef2, Gfx::Color colorNew2, float tolerance1, float tolerance2, Math::Point ts, Math::Point ti, Math::Point *pExclu=0, float shift=0.0f, bool hSV=false); bool OpenImage(char *name); @@ -605,6 +616,7 @@ protected: Gfx::CTerrain* m_terrain; Snd::CSound* m_sound; + bool m_wasInit; std::string m_error; int m_blackSrcBlend[2]; diff --git a/src/graphics/common/light.h b/src/graphics/common/light.h index dec9912..f85d8da 100644 --- a/src/graphics/common/light.h +++ b/src/graphics/common/light.h @@ -20,6 +20,7 @@ #pragma once +#include "graphics/common/engine.h" #include "graphics/common/color.h" #include "math/vector.h" @@ -30,9 +31,9 @@ namespace Gfx { * \brief Type of light */ enum LightType { - LT_Point, - LT_Spot, - LT_Directional + LIGHT_POINT, + LIGHT_SPOT, + LIGHT_DIRECTIONAL }; /** @@ -46,9 +47,13 @@ enum LightType struct Light { //! Type of light source - Gfx::LightType type; - //! Color of light - Gfx::Color color; + Gfx::LightType type; + //! Color of ambient light + Gfx::Color ambient; + //! Color of diffuse light + Gfx::Color diffuse; + //! Color of specular light + Gfx::Color specular; //! Position in world space Math::Vector position; //! Direction in world space @@ -57,29 +62,42 @@ struct Light float range; //! Falloff float falloff; + //! Inner angle of spotlight cone + float theta; + //! Outer angle of spotlight cone + float phi; //! Constant attenuation float attenuation0; //! Linear attenuation float attenuation1; //! Quadratic attenuation float attenuation2; - //! Inner angle of spotlight cone - float theta; - //! Outer angle of spotlight cone - float phi; - Light() : type(LT_Point), range(0.0f), falloff(0.0f), - attenuation0(0.0f), attenuation1(0.0f), attenuation2(0.0f), - theta(0.0f), phi(0.0f) {} + Light() + { + type = LIGHT_POINT; + range = falloff = theta = phi = attenuation0 = attenuation1 = attenuation2 = 0.0f; + } }; -struct LightProg +/** + * \struct LightProgression + * \brief Describes the progression of light parameters change + * + * TODO documentation + */ +struct LightProgression { float starting; float ending; float current; float progress; float speed; + + LightProgression() + { + starting = ending = current = progress = speed = 0.0f; + } }; /** @@ -91,23 +109,23 @@ struct LightProg struct SceneLight { //! true -> light exists - bool used; + bool used; //! true -> light turned on - bool enable; + bool enabled; //! Type of all objects included - //D3DTypeObj incluType; + Gfx::ObjectType includeType; //! Type of all objects excluded - //D3DTypeObj excluType; + Gfx::ObjectType excludeType; //! Configuration of the light - Gfx::Light light; + Gfx::Light light; //! intensity (0 .. 1) - Gfx::LightProg intensity; - Gfx::LightProg colorRed; - Gfx::LightProg colorGreen; - Gfx::LightProg colorBlue; + Gfx::LightProgression intensity; + Gfx::LightProgression colorRed; + Gfx::LightProgression colorGreen; + Gfx::LightProgression colorBlue; }; // TODO CLight diff --git a/src/graphics/common/vertex.h b/src/graphics/common/vertex.h index 0cc6402..430c84c 100644 --- a/src/graphics/common/vertex.h +++ b/src/graphics/common/vertex.h @@ -19,6 +19,7 @@ #pragma once +#include "graphics/common/color.h" #include "math/vector.h" #include "math/point.h" @@ -47,6 +48,33 @@ struct Vertex : coord(aCoord), normal(aNormal), texCoord(aTexCoord) {} }; +/** + * \struct VertexCol + * \brief Vertex with color information + * + * This structure was created as analog to DirectX's D3DLVERTEX. + * + * It contains: + * - vertex coordinates (x,y,z) as Math::Vector, + * - RGBA color as Gfx::Color, + * - RGBA specular color as Gfx::Color, + * - texture coordinates (u,v) as Math::Point. + */ +struct VertexCol +{ + Math::Vector coord; + Gfx::Color color; + Gfx::Color specular; + Math::Point texCoord; + + VertexCol(Math::Vector aCoord = Math::Vector(), + Gfx::Color aColor = Gfx::Color(), + Gfx::Color aSpecular = Gfx::Color(), + Math::Point aTexCoord = Math::Point()) + : coord(aCoord), color(aColor), specular(aSpecular), texCoord(aTexCoord) {} +}; + + /** * \struct VertexTex2 * \brief Vertex with secondary texture coordinates @@ -54,15 +82,18 @@ struct Vertex * In addition to fields from Gfx::Vector, it contains * secondary texture coordinates (u2, v2) as Math::Point */ -struct VertexTex2 : public Gfx::Vertex +struct VertexTex2 { + Math::Vector coord; + Math::Vector normal; + Math::Point texCoord; Math::Point texCoord2; VertexTex2(Math::Vector aCoord = Math::Vector(), Math::Vector aNormal = Math::Vector(), Math::Point aTexCoord = Math::Point(), Math::Point aTexCoord2 = Math::Point()) - : Vertex(aCoord, aNormal, aTexCoord), texCoord2(aTexCoord2) {} + : coord(aCoord), normal(aNormal), texCoord(aTexCoord), texCoord2(aTexCoord2) {} }; }; // namespace Gfx diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 9ef376f..d105a93 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -20,6 +20,35 @@ #include #include +#include + +#include + +#include + +namespace Gfx { + +struct GLDevicePrivate +{ + void (APIENTRY* glMultiTexCoord1fARB)(GLenum target, GLfloat s); + void (APIENTRY* glMultiTexCoord2fARB)(GLenum target, GLfloat s, GLfloat t); + void (APIENTRY* glMultiTexCoord3fARB)(GLenum target, GLfloat s, GLfloat t, GLfloat r); + void (APIENTRY* glMultiTexCoord4fARB)(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q); + void (APIENTRY* glActiveTextureARB)(GLenum texture); + void (APIENTRY* glClientActiveTextureARB)(GLenum texture); + + GLDevicePrivate() + { + glMultiTexCoord1fARB = NULL; + glMultiTexCoord2fARB = NULL; + glMultiTexCoord3fARB = NULL; + glMultiTexCoord4fARB = NULL; + glActiveTextureARB = NULL; + glClientActiveTextureARB = NULL; + } +}; + +}; // namespace Gfx void Gfx::GLDeviceConfig::LoadDefault() @@ -35,89 +64,194 @@ void Gfx::GLDeviceConfig::LoadDefault() depthSize = 24; } + + + Gfx::CGLDevice::CGLDevice() { - m_renderState = 0; + m_private = new Gfx::GLDevicePrivate(); + m_wasInit = false; } Gfx::CGLDevice::~CGLDevice() { + delete m_private; + m_private = NULL; } -void Gfx::CGLDevice::Initialize() +bool Gfx::CGLDevice::GetWasInit() { - // TODO + return m_wasInit; +} + +std::string Gfx::CGLDevice::GetError() +{ + return m_error; +} + +bool Gfx::CGLDevice::Create() +{ + m_wasInit = true; + + // TODO: move to functions? + glShadeModel(GL_SMOOTH); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + + std::string extensions = std::string( (char*) glGetString(GL_EXTENSIONS)); + + if (extensions.find("GL_ARB_multitexture") == std::string::npos) + { + m_error = "Required extension GL_ARB_multitexture not supported"; + return false; + } + + if (extensions.find("GL_EXT_texture_env_combine") == std::string::npos) + { + m_error = "Required extension GL_EXT_texture_env_combine not supported"; + return false; + } + + int maxTextures = 0; + glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures); + m_textures = std::vector(maxTextures, NULL); + + m_lights = std::vector(GL_MAX_LIGHTS, Gfx::Light()); + m_lightsEnabled = std::vector(GL_MAX_LIGHTS, false); + + m_private->glMultiTexCoord1fARB = (PFNGLMULTITEXCOORD1FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord1fARB"); + m_private->glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord2fARB"); + m_private->glMultiTexCoord3fARB = (PFNGLMULTITEXCOORD3FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord3fARB"); + m_private->glMultiTexCoord4fARB = (PFNGLMULTITEXCOORD4FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord4fARB"); + m_private->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB"); + m_private->glClientActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glClientActiveTextureARB"); + + return true; } void Gfx::CGLDevice::Destroy() { - // TODO + m_private->glMultiTexCoord1fARB = NULL; + m_private->glMultiTexCoord2fARB = NULL; + m_private->glMultiTexCoord3fARB = NULL; + m_private->glMultiTexCoord4fARB = NULL; + m_private->glActiveTextureARB = NULL; + m_private->glClientActiveTextureARB = NULL; + + // Delete the remaining textures + std::set::iterator it; + for (it = m_allTextures.begin(); it != m_allTextures.end(); ++it) + delete *it; + m_allTextures.clear(); + + m_wasInit = false; } void Gfx::CGLDevice::BeginScene() { - // TODO + Clear(); + + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(m_projectionMat.Array()); + + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(m_modelviewMat.Array()); } void Gfx::CGLDevice::EndScene() { - // TODO + glFlush(); } void Gfx::CGLDevice::Clear() { - // TODO + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void Gfx::CGLDevice::SetTransform(Gfx::TransformType type, const Math::Matrix &matrix) { - switch (type) + if (type == Gfx::TRANSFORM_WORLD) + { + m_worldMat = matrix; + m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(m_modelviewMat.Array()); + } + else if (type == Gfx::TRANSFORM_VIEW) + { + m_viewMat = matrix; + m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(m_modelviewMat.Array()); + } + else if (type == Gfx::TRANSFORM_PROJECTION) { - case Gfx::TRANSFORM_WORLD: - m_worldMat = matrix; - // TODO - break; - case Gfx::TRANSFORM_VIEW: - m_viewMat = matrix; - // TODO - break; - case Gfx::TRANSFORM_PROJECTION: - m_projectionMat = matrix; - // TODO - break; - default: - assert(false); + m_projectionMat = matrix; + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(m_projectionMat.Array()); + } + else + { + assert(false); } } const Math::Matrix& Gfx::CGLDevice::GetTransform(Gfx::TransformType type) { - switch (type) - { - case Gfx::TRANSFORM_WORLD: - return m_worldMat; - case Gfx::TRANSFORM_VIEW: - return m_viewMat; - case Gfx::TRANSFORM_PROJECTION: - return m_projectionMat; - default: - assert(false); - } + if (type == Gfx::TRANSFORM_WORLD) + return m_worldMat; + else if (type == Gfx::TRANSFORM_VIEW) + return m_viewMat; + else if (type == Gfx::TRANSFORM_PROJECTION) + return m_projectionMat; + else + assert(false); return m_worldMat; // to avoid warning } void Gfx::CGLDevice::MultiplyTransform(Gfx::TransformType type, const Math::Matrix &matrix) { - // TODO + if (type == Gfx::TRANSFORM_WORLD) + { + m_worldMat = Math::MultiplyMatrices(m_worldMat, matrix); + m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(m_modelviewMat.Array()); + } + else if (type == Gfx::TRANSFORM_VIEW) + { + m_viewMat = Math::MultiplyMatrices(m_viewMat, matrix); + m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(m_modelviewMat.Array()); + } + else if (type == Gfx::TRANSFORM_PROJECTION) + { + m_projectionMat = Math::MultiplyMatrices(m_projectionMat, matrix); + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(m_projectionMat.Array()); + } + else + { + assert(false); + } } -void Gfx::CGLDevice::SetMaterial(const Gfx::Material &material) +void Gfx::CGLDevice::SetMaterial(Gfx::Material &material) { m_material = material; - // TODO + glMaterialfv(GL_AMBIENT, GL_FRONT_AND_BACK, m_material.ambient.Array()); + glMaterialfv(GL_DIFFUSE, GL_FRONT_AND_BACK, m_material.diffuse.Array()); + glMaterialfv(GL_SPECULAR, GL_FRONT_AND_BACK, m_material.specular.Array()); } const Gfx::Material& Gfx::CGLDevice::GetMaterial() @@ -130,14 +264,35 @@ int Gfx::CGLDevice::GetMaxLightCount() return m_lights.size(); } -void Gfx::CGLDevice::SetLight(int index, const Gfx::Light &light) +void Gfx::CGLDevice::SetLight(int index, Gfx::Light &light) { assert(index >= 0); assert(index < (int)m_lights.size()); m_lights[index] = light; - // TODO + // Indexing from GL_LIGHT0 should always work + glLightfv(GL_LIGHT0 + index, GL_AMBIENT, light.ambient.Array()); + glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, light.diffuse.Array()); + glLightfv(GL_LIGHT0 + index, GL_SPECULAR, light.specular.Array()); + + GLfloat position[4] = { light.position.x, light.position.y, light.position.z, 0.0f }; + if (light.type == LIGHT_DIRECTIONAL) + position[3] = 0.0f; + else + position[3] = 1.0f; + glLightfv(GL_LIGHT0 + index, GL_POSITION, position); + + GLfloat direction[4] = { light.direction.x, light.direction.y, light.direction.z, 0.0f }; + glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, direction); + + glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, light.range); + + // TODO: falloff?, phi?, theta? + + glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, light.attenuation0); + glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, light.attenuation1); + glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, light.attenuation2); } const Gfx::Light& Gfx::CGLDevice::GetLight(int index) @@ -155,7 +310,7 @@ void Gfx::CGLDevice::SetLightEnabled(int index, bool enabled) m_lightsEnabled[index] = enabled; - // TODO + glEnable(GL_LIGHT0 + index); } bool Gfx::CGLDevice::GetLightEnabled(int index) @@ -171,7 +326,7 @@ int Gfx::CGLDevice::GetMaxTextureCount() return m_textures.size(); } -const Gfx::Texture& Gfx::CGLDevice::GetTexture(int index) +Gfx::Texture* Gfx::CGLDevice::GetTexture(int index) { assert(index >= 0); assert(index < (int)m_textures.size()); @@ -179,7 +334,7 @@ const Gfx::Texture& Gfx::CGLDevice::GetTexture(int index) return m_textures[index]; } -void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) +void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) { assert(index >= 0); assert(index < (int)m_textures.size()); @@ -189,23 +344,339 @@ void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) // TODO } +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount) +{ + if (type == Gfx::PRIMITIVE_LINES) + glBegin(GL_LINES); + else if (type == Gfx::PRIMITIVE_TRIANGLES) + glBegin(GL_TRIANGLES); + else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) + glBegin(GL_TRIANGLE_STRIP); + + for (int i = 0; i < vertexCount; ++i) + { + glNormal3fv((GLfloat*)vertices[i].normal.Array()); + glTexCoord2fv((GLfloat*)vertices[i].texCoord.Array()); + glVertex3fv((GLfloat*)vertices[i].coord.Array()); + } + + glEnd(); +} + +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount) +{ + if (type == Gfx::PRIMITIVE_LINES) + glBegin(GL_LINES); + else if (type == Gfx::PRIMITIVE_TRIANGLES) + glBegin(GL_TRIANGLES); + else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) + glBegin(GL_TRIANGLE_STRIP); + + for (int i = 0; i < vertexCount; ++i) + { + // TODO: specular? + glColor4fv((GLfloat*)vertices[i].color.Array()); + glTexCoord2fv((GLfloat*)vertices[i].texCoord.Array()); + glVertex3fv((GLfloat*)vertices[i].coord.Array()); + } + + glEnd(); +} + +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount) +{ + if (type == Gfx::PRIMITIVE_LINES) + glBegin(GL_LINES); + else if (type == Gfx::PRIMITIVE_TRIANGLES) + glBegin(GL_TRIANGLES); + else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) + glBegin(GL_TRIANGLE_STRIP); + + for (int i = 0; i < vertexCount; ++i) + { + glNormal3fv((GLfloat*) vertices[i].normal.Array()); + // TODO glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y); + // TODO glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y); + glVertex3fv((GLfloat*) vertices[i].coord.Array()); + } + + glEnd(); +} + void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) { - // TODO + if (state == RENDER_STATE_DEPTH_WRITE) + { + glDepthMask(enabled ? GL_TRUE : GL_FALSE); + return; + } + else if (state == RENDER_STATE_TEXTURING) + { + if (enabled) + { + glEnable(GL_TEXTURE_2D); + // TODO multitexture + } + else + { + glDisable(GL_TEXTURE_2D); + } + return; + } + + GLenum flag = 0; + + switch (state) + { + case Gfx::RENDER_STATE_LIGHTING: flag = GL_DEPTH_WRITEMASK; break; + case Gfx::RENDER_STATE_BLENDING: flag = GL_BLEND; break; + case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; + case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; + case Gfx::RENDER_STATE_ALPHA_TEST: flag = GL_ALPHA_TEST; break; + case Gfx::RENDER_STATE_DITHERING: flag = GL_DITHER; break; + default: assert(false); break; + } + + if (enabled) + glEnable(flag); + else + glDisable(flag); } bool Gfx::CGLDevice::GetRenderState(Gfx::RenderState state) { - // TODO - return false; + GLenum flag = 0; + + switch (state) + { + case Gfx::RENDER_STATE_DEPTH_WRITE: flag = GL_DEPTH_WRITEMASK; break; + case Gfx::RENDER_STATE_TEXTURING: flag = GL_TEXTURE_2D; break; + case Gfx::RENDER_STATE_LIGHTING: flag = GL_DEPTH_WRITEMASK; break; + case Gfx::RENDER_STATE_BLENDING: flag = GL_BLEND; break; + case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; + case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; + case Gfx::RENDER_STATE_ALPHA_TEST: flag = GL_ALPHA_TEST; break; + case Gfx::RENDER_STATE_DITHERING: flag = GL_DITHER; break; + default: assert(false); break; + } + + GLboolean result = GL_FALSE; + glGetBooleanv(flag, &result); + + return result == GL_TRUE; } -void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType, Vertex *vertices, int vertexCount) +Gfx::CompFunc TranslateGLCompFunc(GLenum flag) { - // TODO + switch (flag) + { + case GL_NEVER: return Gfx::COMP_FUNC_NEVER; + case GL_LESS: return Gfx::COMP_FUNC_LESS; + case GL_EQUAL: return Gfx::COMP_FUNC_EQUAL; + case GL_NOTEQUAL: return Gfx::COMP_FUNC_NOTEQUAL; + case GL_LEQUAL: return Gfx::COMP_FUNC_LEQUAL; + case GL_GREATER: return Gfx::COMP_FUNC_GREATER; + case GL_GEQUAL: return Gfx::COMP_FUNC_GEQUAL; + case GL_ALWAYS: return Gfx::COMP_FUNC_ALWAYS; + default: assert(false); break; + } + return Gfx::COMP_FUNC_NEVER; } -void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType, VertexTex2 *vertices, int vertexCount) +GLenum TranslateGfxCompFunc(Gfx::CompFunc func) { - // TODO + switch (func) + { + case Gfx::COMP_FUNC_NEVER: return GL_NEVER; + case Gfx::COMP_FUNC_LESS: return GL_LESS; + case Gfx::COMP_FUNC_EQUAL: return GL_EQUAL; + case Gfx::COMP_FUNC_NOTEQUAL: return GL_NOTEQUAL; + case Gfx::COMP_FUNC_LEQUAL: return GL_LEQUAL; + case Gfx::COMP_FUNC_GREATER: return GL_GREATER; + case Gfx::COMP_FUNC_GEQUAL: return GL_GEQUAL; + case Gfx::COMP_FUNC_ALWAYS: return GL_ALWAYS; + default: assert(false); break; + } + return 0; +} + +void Gfx::CGLDevice::SetDepthTestFunc(Gfx::CompFunc func) +{ + glDepthFunc(TranslateGfxCompFunc(func)); +} + +Gfx::CompFunc Gfx::CGLDevice::GetDepthTestFunc() +{ + GLenum flag = 0; + glGetIntegerv(GL_DEPTH_FUNC, (GLint*)&flag); + return TranslateGLCompFunc(flag); +} + +void Gfx::CGLDevice::SetDepthBias(float factor) +{ + glPolygonOffset(factor, 0.0f); +} + +float Gfx::CGLDevice::GetDepthBias() +{ + GLfloat result = 0.0f; + glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &result); + return result; +} + +void Gfx::CGLDevice::SetAlphaTestFunc(Gfx::CompFunc func, float refValue) +{ + glAlphaFunc(TranslateGfxCompFunc(func), refValue); +} + +void Gfx::CGLDevice::GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue) +{ + GLenum flag = 0; + glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint*)&flag); + func = TranslateGLCompFunc(flag); + + glGetFloatv(GL_ALPHA_TEST_REF, (GLfloat*) &refValue); +} + +Gfx::BlendFunc TranslateGLBlendFunc(GLenum flag) +{ + switch (flag) + { + case GL_ZERO: return Gfx::BLEND_ZERO; + case GL_ONE: return Gfx::BLEND_ONE; + case GL_SRC_COLOR: return Gfx::BLEND_SRC_COLOR; + case GL_ONE_MINUS_SRC_COLOR: return Gfx::BLEND_INV_SRC_COLOR; + case GL_DST_COLOR: return Gfx::BLEND_DST_COLOR; + case GL_ONE_MINUS_DST_COLOR: return Gfx::BLEND_INV_DST_COLOR; + case GL_SRC_ALPHA: return Gfx::BLEND_SRC_ALPHA; + case GL_ONE_MINUS_SRC_ALPHA: return Gfx::BLEND_INV_SRC_ALPHA; + case GL_DST_ALPHA: return Gfx::BLEND_DST_ALPHA; + case GL_ONE_MINUS_DST_ALPHA: return Gfx::BLEND_INV_DST_ALPHA; + case GL_SRC_ALPHA_SATURATE: return Gfx::BLEND_SRC_ALPHA_SATURATE; + default: assert(false); break; + } + + return Gfx::BLEND_ZERO; +} + +GLenum TranslateGfxBlendFunc(Gfx::BlendFunc func) +{ + switch (func) + { + case Gfx::BLEND_ZERO: return GL_ZERO; + case Gfx::BLEND_ONE: return GL_ONE; + case Gfx::BLEND_SRC_COLOR: return GL_SRC_COLOR; + case Gfx::BLEND_INV_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR; + case Gfx::BLEND_DST_COLOR: return GL_DST_COLOR; + case Gfx::BLEND_INV_DST_COLOR: return GL_ONE_MINUS_DST_COLOR; + case Gfx::BLEND_SRC_ALPHA: return GL_SRC_ALPHA; + case Gfx::BLEND_INV_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA; + case Gfx::BLEND_DST_ALPHA: return GL_DST_ALPHA; + case Gfx::BLEND_INV_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA; + case Gfx::BLEND_SRC_ALPHA_SATURATE: return GL_SRC_ALPHA_SATURATE; + default: assert(false); break; + } + return 0; +} + +void Gfx::CGLDevice::SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend) +{ + glBlendFunc(TranslateGfxBlendFunc(srcBlend), TranslateGfxBlendFunc(dstBlend)); +} + +void Gfx::CGLDevice::GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) +{ + GLenum srcFlag = 0; + glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint*)&srcFlag); + srcBlend = TranslateGLBlendFunc(srcFlag); + + GLenum dstFlag = 0; + glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint*)&dstFlag); + dstBlend = TranslateGLBlendFunc(dstFlag); +} + +void Gfx::CGLDevice::SetClearColor(Gfx::Color color) +{ + glClearColor(color.r, color.g, color.b, color.a); +} + +Gfx::Color Gfx::CGLDevice::GetClearColor() +{ + float color[4] = { 0.0f }; + glGetFloatv(GL_COLOR_CLEAR_VALUE, color); + return Gfx::Color(color[0], color[1], color[2], color[3]); +} + +void Gfx::CGLDevice::SetGlobalAmbient(Gfx::Color color) +{ + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color.Array()); +} + +Gfx::Color Gfx::CGLDevice::GetGlobalAmbient() +{ + float color[4] = { 0.0f }; + glGetFloatv(GL_LIGHT_MODEL_AMBIENT, color); + return Gfx::Color(color[0], color[1], color[2], color[3]); +} + +void Gfx::CGLDevice::SetFogParams(Gfx::FogMode mode, Gfx::Color color, float start, float end, float density) +{ + if (mode == Gfx::FOG_LINEAR) glFogi(GL_FOG_MODE, GL_LINEAR); + else if (mode == Gfx::FOG_EXP) glFogi(GL_FOG_MODE, GL_EXP); + else if (mode == Gfx::FOG_EXP2) glFogi(GL_FOG_MODE, GL_EXP2); + else assert(false); + + glFogf(GL_FOG_START, start); + glFogf(GL_FOG_END, end); + glFogf(GL_FOG_DENSITY, density); +} + +void Gfx::CGLDevice::GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) +{ + GLenum flag = 0; + glGetIntegerv(GL_FOG_MODE, (GLint*)&flag); + if (flag == GL_LINEAR) mode = Gfx::FOG_LINEAR; + else if (flag == GL_EXP) mode = Gfx::FOG_EXP; + else if (flag == GL_EXP2) mode = Gfx::FOG_EXP2; + else assert(false); + + glGetFloatv(GL_FOG_START, (GLfloat*)&start); + glGetFloatv(GL_FOG_END, (GLfloat*)&end); + glGetFloatv(GL_FOG_DENSITY, (GLfloat*)&density); +} + +void Gfx::CGLDevice::SetCullMode(Gfx::CullMode mode) +{ + if (mode == Gfx::CULL_CW) glCullFace(GL_CW); + else if (mode == Gfx::CULL_CCW) glCullFace(GL_CCW); + else assert(false); +} + +Gfx::CullMode Gfx::CGLDevice::GetCullMode() +{ + GLenum flag = 0; + glGetIntegerv(GL_CULL_FACE, (GLint*)&flag); + if (flag == GL_CW) return Gfx::CULL_CW; + else if (flag == GL_CCW) return Gfx::CULL_CCW; + else assert(false); + return Gfx::CULL_CW; +} + +void Gfx::CGLDevice::SetFillMode(Gfx::FillMode mode) +{ + if (mode == Gfx::FILL_POINT) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); + else if (mode == Gfx::FILL_LINES) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else if (mode == Gfx::FILL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + else assert(false); +} + +Gfx::FillMode Gfx::CGLDevice::GetFillMode() +{ + GLenum flag = 0; + glGetIntegerv(GL_POLYGON_MODE, (GLint*)&flag); + if (flag == GL_POINT) return Gfx::FILL_POINT; + else if (flag == GL_LINE) return Gfx::FILL_LINES; + else if (flag == GL_FILL) return Gfx::FILL_FILL; + else assert(false); + return Gfx::FILL_POINT; } diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 0b4702a..298eb56 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -21,8 +21,9 @@ #include "graphics/common/device.h" - +#include #include +#include namespace Gfx { @@ -53,6 +54,8 @@ struct GLDeviceConfig : public DeviceConfig void LoadDefault(); }; +struct GLDevicePrivate; + /** \class CGLDevice \brief Implementation of CDevice interface in OpenGL @@ -70,7 +73,10 @@ public: CGLDevice(); virtual ~CGLDevice(); - virtual void Initialize(); + virtual bool GetWasInit(); + virtual std::string GetError(); + + virtual bool Create(); virtual void Destroy(); virtual void BeginScene(); @@ -82,30 +88,67 @@ public: virtual const Math::Matrix& GetTransform(Gfx::TransformType type); virtual void MultiplyTransform(Gfx::TransformType type, const Math::Matrix &matrix); - virtual void SetMaterial(const Gfx::Material &material); + virtual void SetMaterial(Gfx::Material &material); virtual const Gfx::Material& GetMaterial(); virtual int GetMaxLightCount(); - virtual void SetLight(int index, const Gfx::Light &light); + virtual void SetLight(int index, Gfx::Light &light); virtual const Gfx::Light& GetLight(int index); virtual void SetLightEnabled(int index, bool enabled); virtual bool GetLightEnabled(int index); virtual int GetMaxTextureCount(); - virtual const Gfx::Texture& GetTexture(int index); - virtual void SetTexture(int index, const Gfx::Texture &texture); + virtual void SetTexture(int index, Gfx::Texture *texture); + virtual Gfx::Texture* GetTexture(int index); + + virtual void DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount); + virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount); + virtual void DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount); + virtual void SetRenderState(Gfx::RenderState state, bool enabled); virtual bool GetRenderState(Gfx::RenderState state); - virtual void DrawPrimitive(Gfx::PrimitiveType, Vertex *vertices, int vertexCount); - virtual void DrawPrimitive(Gfx::PrimitiveType, VertexTex2 *vertices, int vertexCount); + virtual void SetDepthTestFunc(Gfx::CompFunc func); + virtual Gfx::CompFunc GetDepthTestFunc(); + + virtual void SetDepthBias(float factor); + virtual float GetDepthBias(); + + virtual void SetAlphaTestFunc(Gfx::CompFunc func, float refValue); + virtual void GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue); + + virtual void SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend); + virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend); + + virtual void SetClearColor(Gfx::Color color); + virtual Gfx::Color GetClearColor(); + + virtual void SetGlobalAmbient(Gfx::Color color); + virtual Gfx::Color GetGlobalAmbient(); + + virtual void SetFogParams(Gfx::FogMode mode, Gfx::Color color, float start, float end, float density); + virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density); + + virtual void SetCullMode(Gfx::CullMode mode); + virtual Gfx::CullMode GetCullMode(); + + virtual void SetFillMode(Gfx::FillMode mode) ; + virtual Gfx::FillMode GetFillMode(); private: + //! Private, OpenGL-specific data + GLDevicePrivate* m_private; + //! Was initialized? + bool m_wasInit; + //! Last encountered error + std::string m_error; //! Current world matrix Math::Matrix m_worldMat; //! Current view matrix Math::Matrix m_viewMat; + //! OpenGL modelview matrix = world matrix * view matrix + Math::Matrix m_modelviewMat; //! Current projection matrix Math::Matrix m_projectionMat; //! The current material @@ -115,9 +158,9 @@ private: //! Current lights enable status std::vector m_lightsEnabled; //! Current textures - std::vector m_textures; - //! Current render state - unsigned long m_renderState; + std::vector m_textures; + //! Set of all created textures + std::set m_allTextures; }; }; // namespace Gfx diff --git a/src/math/geometry.h b/src/math/geometry.h index 2f937e5..d5960b8 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -305,6 +305,24 @@ inline void LoadProjectionMatrix(Matrix &mat, float fov = 1.570795f, float aspec /* (3,4) */ mat.m[14] = -q * nearPlane; } +//! Loads an othogonal projection matrix +/** \a left,right coordinates for left and right vertical clipping planes + \a bottom,top coordinates for bottom and top horizontal clipping planes + \a zNear,zFar distance to nearer and farther depth clipping planes */ +inline void LoadOrthoProjectionMatrix(Matrix &mat, float left, float right, float bottom, float top, + float zNear = -1.0f, float zFar = 1.0f) +{ + mat.LoadIdentity(); + + /* (1,1) */ mat.m[0 ] = 2.0f / (right - left); + /* (2,2) */ mat.m[5 ] = 2.0f / (top - bottom); + /* (3,3) */ mat.m[10] = -2.0f / (zFar - zNear); + + /* (1,4) */ mat.m[12] = - (right + left) / (right - left); + /* (2,4) */ mat.m[12] = - (top + bottom) / (top - bottom); + /* (3,4) */ mat.m[14] = - (zFar + zNear) / (zFar - zNear); +} + //! Loads a translation matrix from given vector /** \a trans vector of translation*/ inline void LoadTranslationMatrix(Matrix &mat, const Vector &trans) diff --git a/src/math/matrix.h b/src/math/matrix.h index 9b29f46..0315a33 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -118,6 +118,12 @@ struct Matrix /* (4,4) */ m[15] = 1.0f; } + //! Returns the struct cast to \c float* array; use with care! + inline float* Array() + { + return (float*)this; + } + //! Transposes the matrix inline void Transpose() { diff --git a/src/math/point.h b/src/math/point.h index 84be190..d6768c8 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -67,6 +67,12 @@ struct Point x = y = 0.0f; } + //! Returns the struct cast to \c float* array; use with care! + inline float* Array() + { + return (float*)this; + } + //! Returns the distance from (0,0) to the point (x,y) inline float Length() { diff --git a/src/math/vector.h b/src/math/vector.h index 3c756f2..baba6bb 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -72,6 +72,12 @@ struct Vector x = y = z = 0.0f; } + //! Returns the struct cast to \c float* array; use with care! + inline float* Array() + { + return (float*)this; + } + //! Returns the vector length inline float Length() const { -- cgit v1.2.3-1-g7c22 From fd6147bea012093ce0f70d843eb6732c581d2a4f Mon Sep 17 00:00:00 2001 From: Zaba999 Date: Mon, 2 Jul 2012 21:41:24 +0200 Subject: translations plus small fixes --- src/CBot/CBot.cpp | 26 ++++++++++++++------------ src/CBot/CBot.h | 8 +++----- src/CBot/idees.txt | 54 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index e2b01a9..1344efd 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -13,18 +13,20 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// -// compilation des diverses instructions -// toutes les routines Compile sont statiques -// et retournent un object selon ce qui a t trouv comme instruction - -// principe de compilation: -// les routines Compile retournent un objet de la classe correspondant l'opration trouve -// il s'agit toujours d'une classe fille de CBotInstr. -// ( les objets CBotInstr ne sont jamais utiliss directement ) - -// si la routine Compile retourne NULL, c'est que l'instruction est fausse -// ou incomprise. -// L'erreur se trouve alors sur la pile CBotCStack::IsOk() est FALSE +// Compilation of various instructions +// Compile all routines are static +// And return an object according to what was found as instruction + +// Compiler principle: +// compile the routines return an object of the class corresponding to the operation found +// This is always a subclass of CBotInstr. +// (CBotInstr objects are never used directly) + + +// Compiles if the routine returns NULL is that the statement is false +// Or misunderstood. +// The error is then on the stack CBotCStack :: Isok () is FALSE + #include "CBot.h" diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index e28bbee..7e15f65 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -1346,9 +1346,7 @@ public: void SetIdent(long n); - static - CBotVarClass* - CBotVarClass::Find(long id); + static CBotVarClass* Find(long id); // CBotVar* GivMyThis(); @@ -1599,8 +1597,8 @@ private: CBotToken m_openblk; CBotToken m_closeblk; public: - CBotFunction::CBotFunction(); - CBotFunction::~CBotFunction(); + CBotFunction(); + ~CBotFunction(); static CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, BOOL bLocal = TRUE); static diff --git a/src/CBot/idees.txt b/src/CBot/idees.txt index 7153789..3966ee1 100644 --- a/src/CBot/idees.txt +++ b/src/CBot/idees.txt @@ -1,39 +1,41 @@ -pour la gestion des instances d'une classe. -l'objet cr actuellement avec CBotVar::Create(nom, pClasse) -est a conserver tel quel, en dehors des vars sur la pile +for managing instances of a class. -il faut un autre type de variable pour garder les pointeurs -CBotTypPtClass par exemple +the object being created with CBotVar :: Create (name, pClasse) + is to keep as is, outside the vars on the stack -L'instance de la classe doit avoir un compteur d'utilisation -qui est le nombre d'objet de classe CBotTypPtClass qui y rfrent. -Le compteur est dcrment lorsque le pointeur est dtruit, -l'objet supprim lorsqu'il n'y a plus de pointeurs. + we need another type of variable to keep the pointers + For example CBotTypPtClass + The instance of the class must have a usage count + which is the number of class object to which they refer CBotTypPtClass. + The counter is decremented when the pointer is destroyed, + be deleted when there is more pointers. -Dans le cas des robots, Daniel cre une instance de sa classe "Object" -et peut retourner des pointeurs cette instance par des routines genre FindRobot() -Object FindRobot(int n) { } + In the case of robots, Daniel creates an instance of class "Object" + and can return pointers to this proceeding by routines such FindRobot () -pResult dans ce cas est un pointeur CBotTypPtClass -lorsqu'il a trouv le robot concern, il lui faudra faire + Object FindRobot (int n) {} -pResult->SetPointeur(InstanceDeLaClassObject); + pResult in this case is a pointer CBotTypPtClass + when he found the robot concerned, it must make -cette opration incrmente le compteur des rfrences + pResult-> SetPointeur (InstanceDeLaClassObject); --- + this operation increments the reference -lorsque le robot est dtruit, l'instance de la classe Object correspondant -est dtruit galement. -s'il reste des pointeurs cet objet, et l'on risque la plant + - -solution 1: - garder non pas le pointeur l'objet directement, mais - un index dans une tables de pointeurs + when the robot is destroyed, the instance of the Object class corresponding + is also destroyed. + if there are pointers to that object, and we planted the risk + + Solution 1: + not keep the pointer to the object directly, but + an index into a table of pointers + + Solution 2: + not destroy the object when there imdiatement pointers + but marked as virtually destroyed -solution 2: - ne pas dtruire l'objet imdiatement lorsqu'il reste des pointeurs - mais le marqu comme virtuellement dtruit \ No newline at end of file -- cgit v1.2.3-1-g7c22 From f95df35dc58e01b99ffddfc4ad394feaa4460b09 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 4 Jul 2012 00:04:53 +0200 Subject: Multitexturing support - added CImage class for loading/saving images and a simple test for it - added libpng library to build - added Gfx::Texture struct - updated the Gfx::CDevice interface to include new features - implemented the new features in Gfx::CGLDevice --- CMakeLists.txt | 1 + src/CMakeLists.txt | 9 +- src/common/image.cpp | 221 +++++++++++++++++++ src/common/image.h | 84 ++++++++ src/common/test/CMakeLists.txt | 6 + src/common/test/image_test.cpp | 34 +++ src/graphics/common/device.cpp | 17 ++ src/graphics/common/device.h | 185 +++++++++++++++- src/graphics/common/texture.h | 9 +- src/graphics/opengl/gldevice.cpp | 449 ++++++++++++++++++++++++++++++++++----- src/graphics/opengl/gldevice.h | 33 ++- 11 files changed, 982 insertions(+), 66 deletions(-) create mode 100644 src/common/image.cpp create mode 100644 src/common/image.h create mode 100644 src/common/test/CMakeLists.txt create mode 100644 src/common/test/image_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a50bcf..8264ef7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ project(colobot C CXX) find_package(OpenGL REQUIRED) find_package(SDL REQUIRED) find_package(SDL_image REQUIRED) +find_package(PNG REQUIRED) # TODO: check for SDL version. Should be >= 1.2.10 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1be7a59..a02e7b9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,7 @@ app/app.cpp app/main.cpp app/system.cpp common/event.cpp +common/image.cpp common/iman.cpp # common/metafile.cpp # common/misc.cpp @@ -150,10 +151,16 @@ set(LIBS ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} +${PNG_LIBRARIES} #CBot -- not yet WinAPI-independent ) -include_directories(. ${CMAKE_CURRENT_BINARY_DIR}) +include_directories(. ${CMAKE_CURRENT_BINARY_DIR} +${SDL_INCLUDE_DIR} +${SDL_IMAGE_INCLUDE_DIR} +${SDLTTF_INCLUDE_DIR} +${PNG_INCLUDE_DIRS} +) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/CBot) diff --git a/src/common/image.cpp b/src/common/image.cpp new file mode 100644 index 0000000..78834b4 --- /dev/null +++ b/src/common/image.cpp @@ -0,0 +1,221 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// image.cpp + +#include "image.h" + +#include +#include +#include +#include +#include +#include + + +/* <---------------------------------------------------------------> */ + +/* The following code is from savesurf program by Angelo "Encelo" Theodorou + Source: http://encelo.netsons.org/old/sdl/ + The code was refactored and modified slightly to fit the needs. + The copyright information below is kept unchanged. */ + + +/* SaveSurf: an example on how to save a SDLSurface in PNG + Copyright (C) 2006 Angelo "Encelo" Theodorou + + This program 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 + (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + NOTE: + + This program is part of "Mars, Land of No Mercy" SDL examples, + you can find other examples on http://marsnomercy.org +*/ + +std::string PNG_ERROR = ""; + +void PNGUserError(png_structp ctx, png_const_charp str) +{ + PNG_ERROR = std::string(str); +} + +int PNGColortypeFromSurface(SDL_Surface *surface) +{ + int colortype = PNG_COLOR_MASK_COLOR; /* grayscale not supported */ + + if (surface->format->palette) + colortype |= PNG_COLOR_MASK_PALETTE; + else if (surface->format->Amask) + colortype |= PNG_COLOR_MASK_ALPHA; + + return colortype; +} + +bool PNGSaveSurface(const char *filename, SDL_Surface *surf) +{ + FILE *fp; + png_structp png_ptr; + png_infop info_ptr; + int i, colortype; + png_bytep *row_pointers; + + PNG_ERROR = ""; + + /* Opening output file */ + fp = fopen(filename, "wb"); + if (fp == NULL) + { + PNG_ERROR = std::string("Could not open file '") + std::string(filename) + std::string("' for saving"); + return false; + } + + /* Initializing png structures and callbacks */ + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, PNGUserError, NULL); + if (png_ptr == NULL) + return false; + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) + { + png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + PNG_ERROR = "png_create_info_struct() error!"; + return false; + } + + if (setjmp(png_jmpbuf(png_ptr))) { + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fp); + return false; + } + + png_init_io(png_ptr, fp); + + colortype = PNGColortypeFromSurface(surf); + png_set_IHDR(png_ptr, info_ptr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + /* Writing the image */ + png_write_info(png_ptr, info_ptr); + png_set_packing(png_ptr); + + row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surf->h); + for (i = 0; i < surf->h; i++) + row_pointers[i] = (png_bytep)(Uint8 *)surf->pixels + i*surf->pitch; + png_write_image(png_ptr, row_pointers); + png_write_end(png_ptr, info_ptr); + + /* Cleaning out... */ + free(row_pointers); + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fp); + + return true; +} + +/* <---------------------------------------------------------------> */ + + +CImage::CImage() +{ + m_data = NULL; +} + +CImage::~CImage() +{ + Free(); +} + +bool CImage::IsEmpty() +{ + return m_data == NULL; +} + +void CImage::Free() +{ + if (m_data != NULL) + { + if (m_data->surface != NULL) + { + SDL_FreeSurface(m_data->surface); + m_data->surface = NULL; + } + delete m_data; + m_data = NULL; + } +} + +ImageData* CImage::GetData() +{ + return m_data; +} + +std::string CImage::GetError() +{ + return m_error; +} + +bool CImage::Load(const std::string& fileName) +{ + if (! IsEmpty() ) + Free(); + + m_data = new ImageData(); + + m_error = ""; + + m_data->surface = IMG_Load(fileName.c_str()); + if (m_data->surface == NULL) + { + delete m_data; + m_data = NULL; + + m_error = std::string(IMG_GetError()); + return false; + } + + return true; +} + +bool CImage::SavePNG(const std::string& fileName) +{ + if (IsEmpty()) + { + m_error = "Empty image!"; + return false; + } + + m_error = ""; + + if (! PNGSaveSurface(fileName.c_str(), m_data->surface) ) + { + m_error = PNG_ERROR; + return false; + } + + return true; +} diff --git a/src/common/image.h b/src/common/image.h new file mode 100644 index 0000000..4d86d31 --- /dev/null +++ b/src/common/image.h @@ -0,0 +1,84 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// image.h + +#pragma once + + +#include +#include + + +// Forward declaration without including headers to clutter the code +struct SDL_Surface; + +//! Implementation-specific image data +/** Note that the struct has no destructor and the surface + will not be freed at destruction. */ +struct ImageData +{ + //! SDL surface with image data + SDL_Surface* surface; + + ImageData() { surface = NULL; } +}; + +/** + \class CImage + \brief Image loaded from file + + Wrapper around SDL_Image library to load images. Also contains + function for saving images to PNG. + */ +class CImage +{ +private: + //! Blocked! + CImage(const CImage &other) {} + //! Blocked! + void operator=(const CImage &other) {} + +public: + //! Constructs empty image (with NULL data) + CImage(); + //! Destroys image, calling Free() + virtual ~CImage(); + + //! Frees the allocated image data + void Free(); + + //! Returns whether the image is empty (has NULL data) + bool IsEmpty(); + + //! Returns the image data; if empty - returns NULL + ImageData* GetData(); + + //! Loads an image from the specified file + bool Load(const std::string &fileName); + + //! Saves the image to the specified file in PNG format + bool SavePNG(const std::string &fileName); + + //! Returns the last error + std::string GetError(); + +private: + //! Last encountered error + std::string m_error; + //! Image data + ImageData* m_data; +}; diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt new file mode 100644 index 0000000..680116c --- /dev/null +++ b/src/common/test/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 2.8) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") + +add_executable(image_test ../image.cpp image_test.cpp) diff --git a/src/common/test/image_test.cpp b/src/common/test/image_test.cpp new file mode 100644 index 0000000..0ad1ee2 --- /dev/null +++ b/src/common/test/image_test.cpp @@ -0,0 +1,34 @@ +#include "../image.h" + +#include +#include + +/* For now, just a simple test: loading a file from image + * and saving it to another in PNG. */ + +int main(int argc, char *argv[]) +{ + if (argc != 3) + { + printf("Usage: %s in_image out_image\n", argv[0]); + return 0; + } + + CImage image; + + if (! image.Load(argv[1])) + { + std::string err = image.GetError(); + printf("Error loading '%s': %s\n", err.c_str()); + return 1; + } + + if (! image.SavePNG(argv[2])) + { + std::string err = image.GetError(); + printf("Error saving PNG '%s': %s\n", err.c_str()); + return 2; + } + + return 0; +} diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp index fcd4318..bb51699 100644 --- a/src/graphics/common/device.cpp +++ b/src/graphics/common/device.cpp @@ -31,3 +31,20 @@ void Gfx::DeviceConfig::LoadDefault() doubleBuf = true; noFrame = false; } + +void Gfx::TextureParams::LoadDefault() +{ + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + + wrapS = Gfx::TEX_WRAP_REPEAT; + wrapT = Gfx::TEX_WRAP_REPEAT; + + colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; +} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 961fb6b..3382c9a 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -30,6 +30,9 @@ #include +class CImage; + + namespace Gfx { /** @@ -144,6 +147,15 @@ enum CullMode CULL_CCW }; +/** + \enum ShadeModel + \brief Shade model used in rendering */ +enum ShadeModel +{ + SHADE_FLAT, + SHADE_SMOOTH +}; + /** \enum FillMode \brief Polygon fill mode */ @@ -169,6 +181,147 @@ enum PrimitiveType PRIMITIVE_TRIANGLE_STRIP }; +/** + \enum TexMinFilter + \brief Minification texture filter + + Corresponds to OpenGL modes but should translate to DirectX too. */ +enum TexMinFilter +{ + TEX_MIN_FILTER_NEAREST, + TEX_MIN_FILTER_LINEAR, + TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST, + TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST, + TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR, + TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR +}; + +/** + \enum TexMagFilter + \brief Magnification texture filter */ +enum TexMagFilter +{ + TEX_MAG_FILTER_NEAREST, + TEX_MAG_FILTER_LINEAR +}; + +/** + \enum TexWrapMode + \brief Wrapping mode for texture coords */ +enum TexWrapMode +{ + TEX_WRAP_CLAMP, + TEX_WRAP_REPEAT +}; + +/** + \enum TexMixOperation + \brief Multitexture mixing operation + */ +enum TexMixOperation +{ + TEX_MIX_OPER_MODULATE, + TEX_MIX_OPER_ADD +}; + +/** + \enum TexMixArgument + \brief Multitexture mixing argument + */ +enum TexMixArgument +{ + TEX_MIX_ARG_CURRENT, + TEX_MIX_ARG_TEXTURE, + TEX_MIX_ARG_DIFFUSE, + TEX_MIX_ARG_FACTOR +}; + +/** + \enum TextureParams + \brief Parameters for texture creation + */ +struct TextureParams +{ + //! Minification filter + Gfx::TexMinFilter minFilter; + //! Magnification filter + Gfx::TexMagFilter magFilter; + //! Wrap S coord mode + Gfx::TexWrapMode wrapS; + //! Wrap T coord mode + Gfx::TexWrapMode wrapT; + //! Mixing operation done on color values + Gfx::TexMixOperation colorOperation; + //! 1st argument of color operations + Gfx::TexMixArgument colorArg1; + //! 2nd argument of color operations + Gfx::TexMixArgument colorArg2; + //! Mixing operation done on alpha values + Gfx::TexMixOperation alphaOperation; + //! 1st argument of alpha operations + Gfx::TexMixArgument alphaArg1; + //! 2nd argument of alpha operations + Gfx::TexMixArgument alphaArg2; + + //! Constructor; calls LoadDefault() + TextureParams() + { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/* + +Notes for rewriting DirectX code: + +>> SetRenderState() translates to many functions depending on param + +D3DRENDERSTATE_ALPHABLENDENABLE -> SetRenderState() with RENDER_STATE_BLENDING +D3DRENDERSTATE_ALPHAFUNC -> SetAlphaTestFunc() func +D3DRENDERSTATE_ALPHAREF -> SetAlphaTestFunc() ref +D3DRENDERSTATE_ALPHATESTENABLE -> SetRenderState() with RENDER_STATE_ALPHA_TEST +D3DRENDERSTATE_AMBIENT -> SetGlobalAmbient() +D3DRENDERSTATE_CULLMODE -> SetCullMode() +D3DRENDERSTATE_DESTBLEND -> SetBlendFunc() dest blending func +D3DRENDERSTATE_DITHERENABLE -> SetRenderState() with RENDER_STATE_DITHERING +D3DRENDERSTATE_FILLMODE -> SetFillMode() +D3DRENDERSTATE_FOGCOLOR -> SetFogParams() +D3DRENDERSTATE_FOGENABLE -> SetRenderState() with RENDER_STATE_FOG +D3DRENDERSTATE_FOGEND -> SetFogParams() +D3DRENDERSTATE_FOGSTART -> SetFogParams() +D3DRENDERSTATE_FOGVERTEXMODE -> SetFogParams() fog model +D3DRENDERSTATE_LIGHTING -> SetRenderState() with RENDER_STATE_LIGHTING +D3DRENDERSTATE_SHADEMODE -> SetShadeModel() +D3DRENDERSTATE_SPECULARENABLE -> doesn't matter (always enabled) +D3DRENDERSTATE_SRCBLEND -> SetBlendFunc() src blending func +D3DRENDERSTATE_TEXTUREFACTOR -> SetTextureFactor() +D3DRENDERSTATE_ZBIAS -> SetDepthBias() +D3DRENDERSTATE_ZENABLE -> SetRenderState() with RENDER_STATE_DEPTH_TEST +D3DRENDERSTATE_ZFUNC -> SetDepthTestFunc() +D3DRENDERSTATE_ZWRITEENABLE -> SetRenderState() with RENDER_STATE_DEPTH_WRITE + + +>> SetTextureStageState() translates to SetTextureParams() + +Params from enum in struct TextureParams + D3DTSS_ADDRESS -> Gfx::TexWrapMode wrapS, wrapT + D3DTSS_ALPHAARG1 -> Gfx::TexMixArgument alphaArg1 + D3DTSS_ALPHAARG2 -> Gfx::TexMixArgument alphaArg2 + D3DTSS_ALPHAOP -> Gfx::TexMixOperation alphaOperation + D3DTSS_COLORARG1 -> Gfx::TexMixArgument colorArg1 + D3DTSS_COLORARG2 -> Gfx::TexMixArgument colorArg2 + D3DTSS_COLOROP -> Gfx::TexMixOperation colorOperation + D3DTSS_MAGFILTER -> Gfx::TexMagFilter magFilter + D3DTSS_MINFILTER -> Gfx::TexMinFilter minFilter + D3DTSS_TEXCOORDINDEX -> doesn't matter (texture coords are set explicitly by glMultiTexCoordARB*) + +Note that D3DTSS_ALPHAOP or D3DTSS_COLOROP set to D3DTOP_DISABLE must translate to disabling the whole texture stage. +In DirectX, you shouldn't mix enabling one and disabling the other. +Also, if previous stage is disabled in DirectX, the later ones are disabled, too. In OpenGL, that is not the case. + +*/ + /** \class CDevice \brief Abstract interface of graphics device @@ -226,20 +379,33 @@ public: //! Returns the current enable state of light at given index virtual bool GetLightEnabled(int index) = 0; - // TODO: - // virtual Gfx::Texture* CreateTexture(CImage *image) = 0; - // virtual void DestroyTexture(Gfx::Texture *texture) = 0; + //! Creates a texture from image; the image can be safely removed after that + virtual Gfx::Texture* CreateTexture(CImage *image, bool alpha, bool mipMap) = 0; + //! Deletes a given texture, freeing it from video memory + virtual void DestroyTexture(Gfx::Texture *texture) = 0; + //! Deletes all textures created so far + virtual void DestroyAllTextures() = 0; - //! Returns the maximum number of multitexture units + //! Returns the maximum number of multitexture stages virtual int GetMaxTextureCount() = 0; //! Sets the (multi)texture at given index virtual void SetTexture(int index, Gfx::Texture *texture) = 0; //! Returns the (multi)texture at given index virtual Gfx::Texture* GetTexture(int index) = 0; + //! Enables/disables the given texture stage + virtual void SetTextureEnabled(int index, bool enabled) = 0; + //! Returns the current enable state of given texture stage + virtual bool GetTextureEnabled(int index) = 0; - // TODO: - // virtual void GetTextureStageState() = 0; - // virtual void SetTextureStageState() = 0; + //! Sets the current params of texture with given index + virtual void SetTextureParams(int index, const Gfx::TextureParams ¶ms) = 0; + //! Returns the current params of texture with given index + virtual Gfx::TextureParams GetTextureParams(int index) = 0; + + //! Sets the texture factor to the given color value + virtual void SetTextureFactor(Gfx::Color &color) = 0; + //! Returns the current texture factor + virtual Gfx::Color GetTextureFactor() = 0; //! Renders primitive composed of vertices with single texture virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::Vertex *vertices, int vertexCount) = 0; @@ -297,6 +463,11 @@ public: //! Returns the current cull mode virtual Gfx::CullMode GetCullMode() = 0; + //! Sets the shade model + virtual void SetShadeModel(Gfx::ShadeModel model) = 0; + //! Returns the current shade model + virtual Gfx::ShadeModel GetShadeModel() = 0; + //! Sets the current fill mode virtual void SetFillMode(Gfx::FillMode mode) = 0; //! Returns the current fill mode diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index ab894db..55d5c70 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -20,9 +20,16 @@ namespace Gfx { +/** \struct Texture*/ struct Texture { - // TODO + //! Whether the texture was loaded + bool valid; + //! Id of the texture in graphics engine + unsigned int id; + + Texture() + { valid = false; id = 0; } }; }; // namespace Gfx diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index d105a93..c6e91e1 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -16,6 +16,7 @@ // gldevice.cpp +#include "common/image.h" #include "graphics/opengl/gldevice.h" #include @@ -30,21 +31,13 @@ namespace Gfx { struct GLDevicePrivate { - void (APIENTRY* glMultiTexCoord1fARB)(GLenum target, GLfloat s); void (APIENTRY* glMultiTexCoord2fARB)(GLenum target, GLfloat s, GLfloat t); - void (APIENTRY* glMultiTexCoord3fARB)(GLenum target, GLfloat s, GLfloat t, GLfloat r); - void (APIENTRY* glMultiTexCoord4fARB)(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q); void (APIENTRY* glActiveTextureARB)(GLenum texture); - void (APIENTRY* glClientActiveTextureARB)(GLenum texture); GLDevicePrivate() { - glMultiTexCoord1fARB = NULL; glMultiTexCoord2fARB = NULL; - glMultiTexCoord3fARB = NULL; - glMultiTexCoord4fARB = NULL; glActiveTextureARB = NULL; - glClientActiveTextureARB = NULL; } }; @@ -71,6 +64,7 @@ Gfx::CGLDevice::CGLDevice() { m_private = new Gfx::GLDevicePrivate(); m_wasInit = false; + m_texturing = false; } @@ -92,18 +86,10 @@ std::string Gfx::CGLDevice::GetError() bool Gfx::CGLDevice::Create() { - m_wasInit = true; - - // TODO: move to functions? - glShadeModel(GL_SMOOTH); - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - + /* First check for extensions + These should be available in standard OpenGL 1.3 + But every distribution is different + So we're loading them dynamically through SDL_GL_GetProcAddress() */ std::string extensions = std::string( (char*) glGetString(GL_EXTENSIONS)); @@ -119,37 +105,58 @@ bool Gfx::CGLDevice::Create() return false; } + m_private->glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord2fARB"); + m_private->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB"); + + if ((m_private->glMultiTexCoord2fARB == NULL) || (m_private->glActiveTextureARB == NULL)) + { + m_error = "Could not load extension functions, even though they seem supported"; + return false; + } + + m_wasInit = true; + + // This is mostly done in all modern hardware by default + // DirectX doesn't even allow the option to turn off perspective correction anymore + // So turn it on permanently + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + // Set just to be sure + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + + m_lights = std::vector(GL_MAX_LIGHTS, Gfx::Light()); + m_lightsEnabled = std::vector (GL_MAX_LIGHTS, false); + int maxTextures = 0; glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures); - m_textures = std::vector(maxTextures, NULL); - m_lights = std::vector(GL_MAX_LIGHTS, Gfx::Light()); - m_lightsEnabled = std::vector(GL_MAX_LIGHTS, false); - - m_private->glMultiTexCoord1fARB = (PFNGLMULTITEXCOORD1FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord1fARB"); - m_private->glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord2fARB"); - m_private->glMultiTexCoord3fARB = (PFNGLMULTITEXCOORD3FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord3fARB"); - m_private->glMultiTexCoord4fARB = (PFNGLMULTITEXCOORD4FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord4fARB"); - m_private->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB"); - m_private->glClientActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glClientActiveTextureARB"); + m_textures = std::vector (maxTextures, NULL); + m_texturesEnabled = std::vector (maxTextures, false); + m_texturesParams = std::vector(maxTextures, Gfx::TextureParams()); return true; } void Gfx::CGLDevice::Destroy() { - m_private->glMultiTexCoord1fARB = NULL; m_private->glMultiTexCoord2fARB = NULL; - m_private->glMultiTexCoord3fARB = NULL; - m_private->glMultiTexCoord4fARB = NULL; m_private->glActiveTextureARB = NULL; - m_private->glClientActiveTextureARB = NULL; // Delete the remaining textures - std::set::iterator it; - for (it = m_allTextures.begin(); it != m_allTextures.end(); ++it) - delete *it; - m_allTextures.clear(); + // Should not be strictly necessary, but just in case + DestroyAllTextures(); + + m_lights.clear(); + m_lightsEnabled.clear(); + + m_textures.clear(); + m_texturesEnabled.clear(); + m_texturesParams.clear(); m_wasInit = false; } @@ -177,7 +184,7 @@ void Gfx::CGLDevice::Clear() void Gfx::CGLDevice::SetTransform(Gfx::TransformType type, const Math::Matrix &matrix) { - if (type == Gfx::TRANSFORM_WORLD) + if (type == Gfx::TRANSFORM_WORLD) { m_worldMat = matrix; m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); @@ -205,7 +212,7 @@ void Gfx::CGLDevice::SetTransform(Gfx::TransformType type, const Math::Matrix &m const Math::Matrix& Gfx::CGLDevice::GetTransform(Gfx::TransformType type) { - if (type == Gfx::TRANSFORM_WORLD) + if (type == Gfx::TRANSFORM_WORLD) return m_worldMat; else if (type == Gfx::TRANSFORM_VIEW) return m_viewMat; @@ -219,7 +226,7 @@ const Math::Matrix& Gfx::CGLDevice::GetTransform(Gfx::TransformType type) void Gfx::CGLDevice::MultiplyTransform(Gfx::TransformType type, const Math::Matrix &matrix) { - if (type == Gfx::TRANSFORM_WORLD) + if (type == Gfx::TRANSFORM_WORLD) { m_worldMat = Math::MultiplyMatrices(m_worldMat, matrix); m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); @@ -306,7 +313,7 @@ const Gfx::Light& Gfx::CGLDevice::GetLight(int index) void Gfx::CGLDevice::SetLightEnabled(int index, bool enabled) { assert(index >= 0); - assert(index < (int)m_lightsEnabled.size()); + assert(index < (int)m_lights.size()); m_lightsEnabled[index] = enabled; @@ -321,11 +328,102 @@ bool Gfx::CGLDevice::GetLightEnabled(int index) return m_lightsEnabled[index]; } +Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, bool alpha, bool mipMap) +{ + Gfx::Texture *result = new Gfx::Texture(); + + // Texturing must be enabled, so enable 1st texture stage + m_private->glActiveTextureARB(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + + glGenTextures(1, &result->id); + glBindTexture(GL_TEXTURE_2D, result->id); + + GLenum sourceFormat = 0; + if (alpha) + sourceFormat = GL_RGBA; + else + sourceFormat = GL_RGB; + + ImageData *data = image->GetData(); + if (data == NULL) + return NULL; + + if (mipMap) + { + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, data->surface->w, + data->surface->h, sourceFormat, GL_UNSIGNED_BYTE, + data->surface->pixels); + } + else + { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->surface->w, data->surface->h, + 0, sourceFormat, GL_UNSIGNED_BYTE, data->surface->pixels); + } + + // Restore previous setup of 1st texture stage + RestoreTextureStage(0); + + return result; +} + +void Gfx::CGLDevice::DestroyTexture(Gfx::Texture *texture) +{ + std::set::iterator it = m_allTextures.find(texture); + if (it != m_allTextures.end()) + m_allTextures.erase(it); + + glDeleteTextures(1, &texture->id); +} + +void Gfx::CGLDevice::DestroyAllTextures() +{ + std::set allCopy = m_allTextures; + std::set::iterator it; + for (it = allCopy.begin(); it != allCopy.end(); ++it) + { + DestroyTexture(*it); + delete *it; + } +} + int Gfx::CGLDevice::GetMaxTextureCount() { return m_textures.size(); } +/** + If \a texture is \c NULL or invalid, unbinds the given texture. + If valid, binds the texture and enables the given texture stage. + The setting is remembered, even if texturing is disabled at the moment. */ +void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) +{ + assert(index >= 0); + assert(index < (int)m_textures.size()); + + // Enable the given texture stage + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glEnable(GL_TEXTURE_2D); + + if ((texture == NULL) || (! texture->valid)) + { + glBindTexture(GL_TEXTURE_2D, 0); // unbind texture + m_textures[index] = NULL; // remember the changes + } + else + { + glBindTexture(GL_TEXTURE_2D, texture->id); // bind the texture + m_textures[index] = texture; // remember the changes + SetTextureParams(index, m_texturesParams[index]); // texture params need to be re-set for the new texture + } + + // Disable the stage if it is set so + if ( (! m_texturing) || (! m_texturesEnabled[index]) ) + glDisable(GL_TEXTURE_2D); +} + +/** + Returns the previously assigned texture or \c NULL if the given stage is not enabled. */ Gfx::Texture* Gfx::CGLDevice::GetTexture(int index) { assert(index >= 0); @@ -334,14 +432,224 @@ Gfx::Texture* Gfx::CGLDevice::GetTexture(int index) return m_textures[index]; } -void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) +void Gfx::CGLDevice::SetTextureEnabled(int index, bool enabled) { assert(index >= 0); assert(index < (int)m_textures.size()); - m_textures[index] = texture; + m_texturesEnabled[index] = enabled; - // TODO + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + if (enabled) + glEnable(GL_TEXTURE_2D); + else + glDisable(GL_TEXTURE_2D); +} + +bool Gfx::CGLDevice::GetTextureEnabled(int index) +{ + assert(index >= 0); + assert(index < (int)m_textures.size()); + + return m_texturesEnabled[index]; +} + +/** + Sets the texture parameters for the given texture stage. + If the given texture was not set (bound) yet, nothing happens. + The settings are remembered, even if texturing is disabled at the moment. */ +void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶ms) +{ + assert(index >= 0); + assert(index < (int)m_textures.size()); + + // Remember the settings + m_texturesParams[index] = params; + + // Enable the given stage + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glEnable(GL_TEXTURE_2D); + + GLint minF = 0; + if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST) minF = GL_NEAREST; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR) minF = GL_LINEAR; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST) minF = GL_NEAREST_MIPMAP_NEAREST; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST) minF = GL_LINEAR_MIPMAP_NEAREST; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR) minF = GL_NEAREST_MIPMAP_LINEAR; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR) minF = GL_LINEAR_MIPMAP_LINEAR; + else assert(false); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minF); + + GLint magF = 0; + if (params.magFilter == Gfx::TEX_MAG_FILTER_NEAREST) magF = GL_NEAREST; + else if (params.magFilter == Gfx::TEX_MAG_FILTER_LINEAR) magF = GL_LINEAR; + else assert(false); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magF); + + if (params.wrapS == Gfx::TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + else if (params.wrapS == Gfx::TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + else assert(false); + + if (params.wrapT == Gfx::TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + else if (params.wrapT == Gfx::TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + else assert(false); + + + // Selection of operation and arguments + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + // Color operation + if (params.colorOperation == Gfx::TEX_MIX_OPER_MODULATE) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); + else if (params.colorOperation == Gfx::TEX_MIX_OPER_ADD) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_ADD); + else assert(false); + + // Color arg1 + if (params.colorArg1 == Gfx::TEX_MIX_ARG_CURRENT) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS); // that's right - stupid D3D enum values + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + } + else if (params.colorArg1 == Gfx::TEX_MIX_ARG_TEXTURE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + } + else if (params.colorArg1 == Gfx::TEX_MIX_ARG_DIFFUSE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PRIMARY_COLOR); // here as well + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + } + else assert(false); + + // Color arg2 + if (params.colorArg2 == Gfx::TEX_MIX_ARG_CURRENT) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + } + else if (params.colorArg2 == Gfx::TEX_MIX_ARG_TEXTURE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + } + else if (params.colorArg2 == Gfx::TEX_MIX_ARG_DIFFUSE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + } + else assert(false); + + // Alpha operation + if (params.alphaOperation == Gfx::TEX_MIX_OPER_MODULATE) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + else if (params.alphaOperation == Gfx::TEX_MIX_OPER_ADD) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_ADD); + else assert(false); + + // Alpha arg1 + if (params.alphaArg1 == Gfx::TEX_MIX_ARG_CURRENT) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + } + else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_TEXTURE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + } + else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_DIFFUSE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + } + else assert(false); + + // Alpha arg2 + if (params.alphaArg2 == Gfx::TEX_MIX_ARG_CURRENT) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + } + else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_TEXTURE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + } + else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_DIFFUSE) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + } + else assert(false); + + // Disable the stage if it is set so + if ( (! m_texturing) || (! m_texturesEnabled[index]) ) + glDisable(GL_TEXTURE_2D); +} + +Gfx::TextureParams Gfx::CGLDevice::GetTextureParams(int index) +{ + assert(index >= 0); + assert(index < (int)m_textures.size()); + + return m_texturesParams[index]; +} + +void Gfx::CGLDevice::SetTextureFactor(Gfx::Color &color) +{ + // Needs to be set for all texture stages + for (int index = 0; index < (int)m_textures.size(); ++index) + { + // Activate stage + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glEnable(GL_TEXTURE_2D); + + glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color.Array()); + + // Disable the stage if it is set so + if ( (! m_texturing) || (! m_texturesEnabled[index]) ) + glDisable(GL_TEXTURE_2D); + } +} + +Gfx::Color Gfx::CGLDevice::GetTextureFactor() +{ + // Get from 1st stage (should be the same for all stages) + m_private->glActiveTextureARB(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + + GLfloat color[4] = { 0.0f }; + glGetTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color); + + // Disable the 1st stage if it is set so + if ( (! m_texturing) || (! m_texturesEnabled[0]) ) + glDisable(GL_TEXTURE_2D); + + return Gfx::Color(color[0], color[1], color[2], color[3]); +} + +void Gfx::CGLDevice::RestoreTextureStage(int index) +{ + // Ensure that we're working with the right stage + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glEnable(GL_TEXTURE_2D); + + if (m_textures[index] != NULL) + glBindTexture(GL_TEXTURE_2D, m_textures[index]->id); // bind to the previous texture + else + glBindTexture(GL_TEXTURE_2D, 0); // unbind + + // Disable the stage if it is set so + if ( (! m_texturing) || (! m_texturesEnabled[index]) ) + glDisable(GL_TEXTURE_2D); } void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount) @@ -374,7 +682,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vert for (int i = 0; i < vertexCount; ++i) { - // TODO: specular? + // TODO: specular through EXT_separate_specular_color? glColor4fv((GLfloat*)vertices[i].color.Array()); glTexCoord2fv((GLfloat*)vertices[i].texCoord.Array()); glVertex3fv((GLfloat*)vertices[i].coord.Array()); @@ -395,8 +703,8 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices for (int i = 0; i < vertexCount; ++i) { glNormal3fv((GLfloat*) vertices[i].normal.Array()); - // TODO glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y); - // TODO glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y); + m_private->glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y); + m_private->glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y); glVertex3fv((GLfloat*) vertices[i].coord.Array()); } @@ -412,14 +720,26 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) } else if (state == RENDER_STATE_TEXTURING) { + m_texturing = enabled; + if (enabled) { - glEnable(GL_TEXTURE_2D); - // TODO multitexture + // All enabled multitexture stages have to be enabled + for (int index = 0; index < (int)m_textures.size(); ++index) + { + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + if (m_texturesEnabled[index]) + glEnable(GL_TEXTURE_2D); + } } else { - glDisable(GL_TEXTURE_2D); + // All multitexture stages have to be disabled + for (int index = 0; index < (int)m_textures.size(); ++index) + { + m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glDisable(GL_TEXTURE_2D); + } } return; } @@ -445,12 +765,14 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) bool Gfx::CGLDevice::GetRenderState(Gfx::RenderState state) { + if (state == RENDER_STATE_TEXTURING) + return m_texturing; + GLenum flag = 0; switch (state) { case Gfx::RENDER_STATE_DEPTH_WRITE: flag = GL_DEPTH_WRITEMASK; break; - case Gfx::RENDER_STATE_TEXTURING: flag = GL_TEXTURE_2D; break; case Gfx::RENDER_STATE_LIGHTING: flag = GL_DEPTH_WRITEMASK; break; case Gfx::RENDER_STATE_BLENDING: flag = GL_BLEND; break; case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; @@ -602,7 +924,7 @@ void Gfx::CGLDevice::SetClearColor(Gfx::Color color) Gfx::Color Gfx::CGLDevice::GetClearColor() { - float color[4] = { 0.0f }; + GLfloat color[4] = { 0.0f }; glGetFloatv(GL_COLOR_CLEAR_VALUE, color); return Gfx::Color(color[0], color[1], color[2], color[3]); } @@ -614,7 +936,7 @@ void Gfx::CGLDevice::SetGlobalAmbient(Gfx::Color color) Gfx::Color Gfx::CGLDevice::GetGlobalAmbient() { - float color[4] = { 0.0f }; + GLfloat color[4] = { 0.0f }; glGetFloatv(GL_LIGHT_MODEL_AMBIENT, color); return Gfx::Color(color[0], color[1], color[2], color[3]); } @@ -662,6 +984,23 @@ Gfx::CullMode Gfx::CGLDevice::GetCullMode() return Gfx::CULL_CW; } +void Gfx::CGLDevice::SetShadeModel(Gfx::ShadeModel model) +{ + if (model == Gfx::SHADE_FLAT) glShadeModel(GL_FLAT); + else if (model == Gfx::SHADE_SMOOTH) glShadeModel(GL_SMOOTH); + else assert(false); +} + +Gfx::ShadeModel Gfx::CGLDevice::GetShadeModel() +{ + GLenum flag = 0; + glGetIntegerv(GL_SHADE_MODEL, (GLint*)&flag); + if (flag == GL_FLAT) return Gfx::SHADE_FLAT; + else if (flag == GL_SMOOTH) return Gfx::SHADE_SMOOTH; + else assert(false); + return Gfx::SHADE_FLAT; +} + void Gfx::CGLDevice::SetFillMode(Gfx::FillMode mode) { if (mode == Gfx::FILL_POINT) glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); @@ -677,6 +1016,6 @@ Gfx::FillMode Gfx::CGLDevice::GetFillMode() if (flag == GL_POINT) return Gfx::FILL_POINT; else if (flag == GL_LINE) return Gfx::FILL_LINES; else if (flag == GL_FILL) return Gfx::FILL_FILL; - else assert(false); + else assert(false); return Gfx::FILL_POINT; } diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 298eb56..aa5dd04 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -97,9 +97,21 @@ public: virtual void SetLightEnabled(int index, bool enabled); virtual bool GetLightEnabled(int index); + virtual Gfx::Texture* CreateTexture(CImage *image, bool alpha, bool mipMap); + virtual void DestroyTexture(Gfx::Texture *texture); + virtual void DestroyAllTextures(); + virtual int GetMaxTextureCount(); - virtual void SetTexture(int index, Gfx::Texture *texture); + virtual void SetTexture(int index, Gfx::Texture *texture); virtual Gfx::Texture* GetTexture(int index); + virtual void SetTextureEnabled(int index, bool enabled); + virtual bool GetTextureEnabled(int index); + + virtual void SetTextureParams(int index, const Gfx::TextureParams ¶ms); + virtual Gfx::TextureParams GetTextureParams(int index); + + virtual void SetTextureFactor(Gfx::Color &color); + virtual Gfx::Color GetTextureFactor(); virtual void DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount); virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount); @@ -133,6 +145,9 @@ public: virtual void SetCullMode(Gfx::CullMode mode); virtual Gfx::CullMode GetCullMode(); + virtual void SetShadeModel(Gfx::ShadeModel model); + virtual Gfx::ShadeModel GetShadeModel(); + virtual void SetFillMode(Gfx::FillMode mode) ; virtual Gfx::FillMode GetFillMode(); @@ -143,6 +158,7 @@ private: bool m_wasInit; //! Last encountered error std::string m_error; + //! Current world matrix Math::Matrix m_worldMat; //! Current view matrix @@ -151,16 +167,29 @@ private: Math::Matrix m_modelviewMat; //! Current projection matrix Math::Matrix m_projectionMat; + //! The current material Gfx::Material m_material; + //! Current lights std::vector m_lights; //! Current lights enable status std::vector m_lightsEnabled; - //! Current textures + + //! Whether texturing is enabled in general + bool m_texturing; + //! Current textures; \c NULL value means unassigned std::vector m_textures; + //! Current texture stages enable status + std::vector m_texturesEnabled; + //! Current texture params + std::vector m_texturesParams; + //! Set of all created textures std::set m_allTextures; + + //! Restores the state of given texture stage to the previously saved settings + void RestoreTextureStage(int index); }; }; // namespace Gfx -- cgit v1.2.3-1-g7c22 From af3057df7eb41973349b407539846f17d9094c21 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 4 Jul 2012 19:56:22 +0200 Subject: Merged changes from dev Resolved conflicts & added fixes. --- README-DEV.txt | 4 + src/CBot/CBotString.cpp | 2 +- src/CBot/CMakeLists.txt | 1 + src/CMakeLists.txt | 3 +- src/app/app.cpp | 66 +++--- src/app/app.h | 9 +- src/app/main.cpp | 17 +- src/common/logger.cpp | 136 ++++++++++++ src/common/logger.h | 111 ++++++++++ src/common/profile.cpp | 12 +- src/common/profile.h | 12 +- src/common/singleton.h | 57 +++++ src/graphics/common/particle.h | 5 +- src/object/robotmain.cpp | 453 ++++++++++++++++++++++++++++++++++++-- src/plugins/plugin.h | 33 +++ src/script/ClassFILE.cpp | 425 ------------------------------------ src/sound/sound.cpp | 0 src/sound/sound.h | 483 +++++++++++++++++++++++++++-------------- src/ui/maindialog.cpp | 162 +++++++------- 19 files changed, 1250 insertions(+), 741 deletions(-) create mode 100644 README-DEV.txt create mode 100644 src/common/logger.cpp create mode 100644 src/common/logger.h create mode 100644 src/common/singleton.h create mode 100644 src/plugins/plugin.h delete mode 100644 src/script/ClassFILE.cpp delete mode 100644 src/sound/sound.cpp diff --git a/README-DEV.txt b/README-DEV.txt new file mode 100644 index 0000000..fb4f464 --- /dev/null +++ b/README-DEV.txt @@ -0,0 +1,4 @@ +README for Developers + +Please refer to our wiki for developers for current information. Its current address is: +http://colobot.info/wiki/doku.php?id=developers diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 2a1e3bd..53b0f27 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -21,7 +21,7 @@ #include -HINSTANCE CBotString::m_hInstance = (HINSTANCE)LoadLibrary("Cbot.dll"); // comment le rcuprer autrement ?? +HINSTANCE CBotString::m_hInstance = (HINSTANCE)LoadLibrary("libCbot.dll"); // comment le rcuprer autrement ?? CBotString::CBotString() diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 409ef3b..9933e9c 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -10,6 +10,7 @@ CBotToken.cpp CBotTwoOpExpr.cpp CBotVar.cpp CBotWhile.cpp +CBot.rc ) add_library(CBot SHARED ${SOURCES}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a02e7b9..c72bcd9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,6 +31,7 @@ app/main.cpp app/system.cpp common/event.cpp common/image.cpp +common/logger.cpp common/iman.cpp # common/metafile.cpp # common/misc.cpp @@ -118,7 +119,7 @@ graphics/opengl/glengine.cpp # script/cbottoken.cpp # script/cmdtoken.cpp # script/script.cpp -sound/sound.cpp +# sound/sound.cpp # ui/button.cpp # ui/check.cpp # ui/color.cpp diff --git a/src/app/app.cpp b/src/app/app.cpp index b73adee..6a71f64 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -20,6 +20,7 @@ #include "app/app.h" #include "app/system.h" +#include "common/logger.h" #include "common/iman.h" #include "graphics/opengl/gldevice.h" @@ -153,6 +154,7 @@ bool CApplication::Create() { SystemDialog(SDT_ERROR, "COLOBOT - Error", std::string("Error in CEngine::BeforeCreateInit() :\n") + std::string(m_engine->GetError()) ); + m_exitCode = 1; return false; } @@ -173,6 +175,15 @@ bool CApplication::Create() { SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL initialization error:\n" + std::string(SDL_GetError()) ); + m_exitCode = 2; + return false; + } + + if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0) + { + SystemDialog( SDT_ERROR, "COLOBOT - Error", std::string("SDL_Image initialization error:\n") + + std::string(IMG_GetError()) ); + m_exitCode = 3; return false; } @@ -181,6 +192,7 @@ bool CApplication::Create() { SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL error while getting video info:\n " + std::string(SDL_GetError()) ); + m_exitCode = 2; return false; } @@ -219,13 +231,6 @@ bool CApplication::Create() if (m_private->deviceConfig.hardwareAccel) SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); - if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0) - { - SystemDialog( SDT_ERROR, "COLOBOT - Error", std::string("SDL_Image initialization error:\n") + - std::string(IMG_GetError()) ); - return false; - } - m_private->surface = SDL_SetVideoMode(m_private->deviceConfig.width, m_private->deviceConfig.height, m_private->deviceConfig.bpp, videoFlags); @@ -233,6 +238,7 @@ bool CApplication::Create() { SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("SDL error while setting video mode:\n") + std::string(SDL_GetError()) ); + m_exitCode = 2; return false; } @@ -255,6 +261,7 @@ bool CApplication::Create() { SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CDevice::Create() :\n") + std::string(m_device->GetError()) ); + m_exitCode = 1; return false; } @@ -263,6 +270,7 @@ bool CApplication::Create() { SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CEngine::Create() :\n") + std::string(m_engine->GetError()) ); + m_exitCode = 1; return false; } @@ -270,6 +278,7 @@ bool CApplication::Create() { SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CEngine::AfterDeviceSetInit() :\n") + std::string(m_engine->GetError()) ); + m_exitCode = 1; return false; } @@ -478,6 +487,11 @@ end: return m_exitCode; } +int CApplication::GetExitCode() +{ + return m_exitCode; +} + //! Translates SDL press state to PressState PressState TranslatePressState(unsigned char state) { @@ -499,7 +513,6 @@ Math::Point CApplication::WindowToInterfaceCoords(int x, int y) 1.0f - (float)y / (float)m_private->deviceConfig.height); } - void CApplication::ParseEvent() { Event event; @@ -567,6 +580,7 @@ void CApplication::ParseEvent() void CApplication::ProcessEvent(Event event) { + CLogger *l = GetLogger(); // Print the events in debug mode to test the code if (m_debugMode) { @@ -574,34 +588,34 @@ void CApplication::ProcessEvent(Event event) { case EVENT_KEY_DOWN: case EVENT_KEY_UP: - printf("EVENT_KEY_%s:\n", (event.type == EVENT_KEY_DOWN) ? "DOWN" : "UP"); - printf(" key = %4x\n", event.key.key); - printf(" state = %s\n", (event.key.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); - printf(" mod = %4x\n", event.key.mod); - printf(" unicode = %4x\n", event.key.unicode); + l->Info("EVENT_KEY_%s:\n", (event.type == EVENT_KEY_DOWN) ? "DOWN" : "UP"); + l->Info(" key = %4x\n", event.key.key); + l->Info(" state = %s\n", (event.key.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + l->Info(" mod = %4x\n", event.key.mod); + l->Info(" unicode = %4x\n", event.key.unicode); break; case EVENT_MOUSE_MOVE: - printf("EVENT_MOUSE_MOVE:\n"); - printf(" state = %s\n", (event.mouseMove.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); - printf(" pos = (%f, %f)\n", event.mouseMove.pos.x, event.mouseMove.pos.y); + l->Info("EVENT_MOUSE_MOVE:\n"); + l->Info(" state = %s\n", (event.mouseMove.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + l->Info(" pos = (%f, %f)\n", event.mouseMove.pos.x, event.mouseMove.pos.y); break; case EVENT_MOUSE_BUTTON_DOWN: case EVENT_MOUSE_BUTTON_UP: - printf("EVENT_MOUSE_BUTTON_%s:\n", (event.type == EVENT_MOUSE_BUTTON_DOWN) ? "DOWN" : "UP"); - printf(" button = %d\n", event.mouseButton.button); - printf(" state = %s\n", (event.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); - printf(" pos = (%f, %f)\n", event.mouseButton.pos.x, event.mouseButton.pos.y); + l->Info("EVENT_MOUSE_BUTTON_%s:\n", (event.type == EVENT_MOUSE_BUTTON_DOWN) ? "DOWN" : "UP"); + l->Info(" button = %d\n", event.mouseButton.button); + l->Info(" state = %s\n", (event.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + l->Info(" pos = (%f, %f)\n", event.mouseButton.pos.x, event.mouseButton.pos.y); break; case EVENT_JOY_AXIS: - printf("EVENT_JOY_AXIS:\n"); - printf(" axis = %d\n", event.joyAxis.axis); - printf(" value = %d\n", event.joyAxis.value); + l->Info("EVENT_JOY_AXIS:\n"); + l->Info(" axis = %d\n", event.joyAxis.axis); + l->Info(" value = %d\n", event.joyAxis.value); break; case EVENT_JOY_BUTTON_DOWN: case EVENT_JOY_BUTTON_UP: - printf("EVENT_JOY_BUTTON_%s:\n", (event.type == EVENT_JOY_BUTTON_DOWN) ? "DOWN" : "UP"); - printf(" button = %d\n", event.joyButton.button); - printf(" state = %s\n", (event.joyButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); + l->Info("EVENT_JOY_BUTTON_%s:\n", (event.type == EVENT_JOY_BUTTON_DOWN) ? "DOWN" : "UP"); + l->Info(" button = %d\n", event.joyButton.button); + l->Info(" state = %s\n", (event.joyButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); break; default: break; diff --git a/src/app/app.h b/src/app/app.h index 098f0ad..ed2bd9a 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -84,10 +84,13 @@ public: Error ParseArguments(int argc, char *argv[]); //! Initializes the application bool Create(); - //! Cleans up before exit - void Destroy(); //! Main event loop int Run(); + //! Returns the code to be returned at main() exit + int GetExitCode(); + + //! Cleans up before exit + void Destroy(); //! Enters the pause mode void Pause(bool pause); @@ -96,7 +99,7 @@ public: void StepSimulation(float rTime); //! Polls the state of joystick axes and buttons - void UpdateJoystick(); + void UpdateJoystick(); void SetShowStat(bool show); bool GetShowStat(); diff --git a/src/app/main.cpp b/src/app/main.cpp index 1e102d7..ece18d3 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -19,6 +19,7 @@ #include "app/app.h" #include "app/system.h" +#include "common/logger.h" #include "common/misc.h" #include "common/restext.h" @@ -26,6 +27,10 @@ //! Entry point to the program int main(int argc, char *argv[]) { + CLogger logger; // Create the logger + + logger.Info("Colobot starting\n"); + CApplication app; // single instance of the application Error err = app.ParseArguments(argc, argv); @@ -34,11 +39,19 @@ int main(int argc, char *argv[]) SystemDialog(SDT_ERROR, "COLOBOT", "Invalid commandline arguments!\n"); } + int code = 0; + if (! app.Create()) { app.Destroy(); // ensure a clean exit - return 1; + code = app.GetExitCode(); + logger.Info("Didn't run main loop. Exiting with code %d\n", code); + return code; } - return app.Run(); + code = app.Run(); + + logger.Info("Exiting with code %d\n", code); + return code; } + diff --git a/src/common/logger.cpp b/src/common/logger.cpp new file mode 100644 index 0000000..41d60eb --- /dev/null +++ b/src/common/logger.cpp @@ -0,0 +1,136 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// logger.cpp + +#include + +#include + + +template<> CLogger* CSingleton::mInstance = 0; + + +CLogger& CLogger::GetInstance() +{ + assert(mInstance); + return *mInstance; +} + + +CLogger* CLogger::GetInstancePointer() +{ + assert(mInstance); + return mInstance; +} + + +CLogger::CLogger() +{ + mFile = NULL; + mLogLevel = LOG_INFO; +} + + +CLogger::~CLogger() +{ + Close(); +} + + +void CLogger::Log(LogType type, const char *str, va_list args) +{ + if (type < mLogLevel) + return; + + switch (type) { + case LOG_WARN: fprintf(IsOpened() ? mFile : stderr, "[WARN]: "); break; + case LOG_INFO: fprintf(IsOpened() ? mFile : stderr, "[INFO]: "); break; + case LOG_ERROR: fprintf(IsOpened() ? mFile : stderr, "[ERROR]: "); break; + default: break; + } + + vfprintf(IsOpened() ? mFile : stderr, str, args); +} + + +void CLogger::Info(const char *str, ...) +{ + va_list args; + va_start(args, str); + Log(LOG_INFO, str, args); + va_end(args); +} + + +void CLogger::Warn(const char *str, ...) +{ + va_list args; + va_start(args, str); + Log(LOG_WARN, str, args); + va_end(args); +} + + +void CLogger::Error(const char *str, ...) +{ + va_list args; + va_start(args, str); + Log(LOG_ERROR, str, args); + va_end(args); +} + + +void CLogger::Message(const char *str, ...) +{ + va_list args; + va_start(args, str); + Log(LOG_NONE, str, args); + va_end(args); +} + + +void CLogger::SetOutputFile(std::string filename) +{ + mFilename = filename; + Open(); +} + + +void CLogger::Open() +{ + mFile = fopen(mFilename.c_str(), "w"); + if (mFile == NULL) + fprintf(stderr, "Could not create file %s\n", mFilename.c_str()); +} + + +void CLogger::Close() +{ + if (IsOpened()) + fclose(mFile); +} + + +bool CLogger::IsOpened() +{ + return mFile != NULL; +} + + +void CLogger::SetLogLevel(LogType type) { + mLogLevel = type; +} diff --git a/src/common/logger.h b/src/common/logger.h new file mode 100644 index 0000000..1b3829c --- /dev/null +++ b/src/common/logger.h @@ -0,0 +1,111 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// logger.h + + +#pragma once + +#include +#include + +#include + +/** + * @file common/logger.h + * @brief Class for loggin information to file or console + */ + + +/** + * \public + * \enum LogType common/logger.h + * \brief Enum representing log level +**/ +enum LogType +{ + LOG_INFO = 1, /*!< lowest level, information */ + LOG_WARN = 2, /*!< warning */ + LOG_ERROR = 3, /*!< error */ + LOG_NONE = 4 /*!< none level, used for custom messages */ +}; + + +/** +* @class CLogger +* +* @brief Class for loggin information to file or console +* +*/ +class CLogger : public CSingleton +{ + public: + CLogger(); + ~CLogger(); + + /** Write message to console or file + * @param const char str - message to write + * @param ... - additional arguments + */ + void Message(const char *str, ...); + + /** Write message to console or file with LOG_INFO level + * @param const char str - message to write + * @param ... - additional arguments + */ + void Info(const char *str, ...); + + /** Write message to console or file with LOG_WARN level + * @param const char str - message to write + * @param ... - additional arguments + */ + void Warn(const char *str, ...); + + /** Write message to console or file with LOG_ERROR level + * @param const char str - message to write + * @param ... - additional arguments + */ + void Error(const char *str, ...); + + /** Set output file to write logs to + * @param std::string filename - output file to write to + */ + void SetOutputFile(std::string filename); + + /** Set log level. Logs with level below will not be shown + * @param LogType level - minimum log level to write + */ + void SetLogLevel(LogType level); + + static CLogger& GetInstance(); + static CLogger* GetInstancePointer(); + + private: + std::string mFilename; + FILE *mFile; + LogType mLogLevel; + + void Open(); + void Close(); + bool IsOpened(); + void Log(LogType type, const char* str, va_list args); +}; + + +//! Global function to get Logger instance +inline CLogger* GetLogger() { + return CLogger::GetInstancePointer(); +} diff --git a/src/common/profile.cpp b/src/common/profile.cpp index 07dafae..d921d34 100644 --- a/src/common/profile.cpp +++ b/src/common/profile.cpp @@ -42,13 +42,13 @@ bool InitCurrentDirectory() } -bool SetProfileString(char* section, char* key, char* string) +bool SetLocalProfileString(char* section, char* key, char* string) { WritePrivateProfileString(section, key, string, g_filename); return true; } -bool GetProfileString(char* section, char* key, char* buffer, int max) +bool GetLocalProfileString(char* section, char* key, char* buffer, int max) { int nb; @@ -62,7 +62,7 @@ bool GetProfileString(char* section, char* key, char* buffer, int max) } -bool SetProfileInt(char* section, char* key, int value) +bool SetLocalProfileInt(char* section, char* key, int value) { char s[20]; @@ -71,7 +71,7 @@ bool SetProfileInt(char* section, char* key, int value) return true; } -bool GetProfileInt(char* section, char* key, int &value) +bool GetLocalProfileInt(char* section, char* key, int &value) { char s[20]; int nb; @@ -87,7 +87,7 @@ bool GetProfileInt(char* section, char* key, int &value) } -bool SetProfileFloat(char* section, char* key, float value) +bool SetLocalProfileFloat(char* section, char* key, float value) { char s[20]; @@ -96,7 +96,7 @@ bool SetProfileFloat(char* section, char* key, float value) return true; } -bool GetProfileFloat(char* section, char* key, float &value) +bool GetLocalProfileFloat(char* section, char* key, float &value) { char s[20]; int nb; diff --git a/src/common/profile.h b/src/common/profile.h index 1a36050..2c76a0b 100644 --- a/src/common/profile.h +++ b/src/common/profile.h @@ -20,11 +20,11 @@ extern bool InitCurrentDirectory(); -extern bool SetProfileString(char* section, char* key, char* string); -extern bool GetProfileString(char* section, char* key, char* buffer, int max); -extern bool SetProfileInt(char* section, char* key, int value); -extern bool GetProfileInt(char* section, char* key, int &value); -extern bool SetProfileFloat(char* section, char* key, float value); -extern bool GetProfileFloat(char* section, char* key, float &value); +extern bool SetLocalProfileString(char* section, char* key, char* string); +extern bool GetLocalProfileString(char* section, char* key, char* buffer, int max); +extern bool SetLocalProfileInt(char* section, char* key, int value); +extern bool GetLocalProfileInt(char* section, char* key, int &value); +extern bool SetLocalProfileFloat(char* section, char* key, float value); +extern bool GetLocalProfileFloat(char* section, char* key, float &value); diff --git a/src/common/singleton.h b/src/common/singleton.h new file mode 100644 index 0000000..dc09645 --- /dev/null +++ b/src/common/singleton.h @@ -0,0 +1,57 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// singleton.h + + +#pragma once + +#include + + +template class CSingleton +{ + protected: + static T* mInstance; + + public: + static T& GetInstance() { + aserrt(mInstance); + return *mInstance; + } + + static T& GetInstancePointer() { + aserrt(mInstance); + return mInstance; + } + + static bool IsCreated() { + return mInstance != NULL; + } + + CSingleton() { + assert(!mInstance); + mInstance = static_cast(this); + } + + ~CSingleton() { + mInstance = NULL; + } + + private: + CSingleton& operator=(const CSingleton &); + CSingleton(const CSingleton &); +}; diff --git a/src/graphics/common/particle.h b/src/graphics/common/particle.h index 62d001d..ec87648 100644 --- a/src/graphics/common/particle.h +++ b/src/graphics/common/particle.h @@ -26,6 +26,7 @@ class CInstanceManager; class CRobotMain; class CObject; +class CSound; @@ -303,7 +304,7 @@ protected: void DrawParticleWheel(int i); CObject* SearchObjectGun(Math::Vector old, Math::Vector pos, ParticleType type, CObject *father); CObject* SearchObjectRay(Math::Vector pos, Math::Vector goal, ParticleType type, CObject *father); - void Play(Snd::Sound sound, Math::Vector pos, float amplitude); + void Play(Sound sound, Math::Vector pos, float amplitude); bool TrackMove(int i, Math::Vector pos, float progress); void TrackDraw(int i, ParticleType type); @@ -314,7 +315,7 @@ protected: CRobotMain* m_main; CTerrain* m_terrain; CWater* m_water; - Snd::CSound* m_sound; + CSound* m_sound; Gfx::Particle m_particule[MAXPARTICULE*MAXPARTITYPE]; Gfx::Triangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 1929f6d..fb68152 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -96,7 +96,414 @@ float g_unit; // conversion factor -#include "script/ClassFILE.cpp" +// Static variables + +static CBotClass* m_pClassFILE; +static CBotProgram* m_pFuncFile; +static int m_CompteurFileOpen = 0; +static char* m_filesDir; + + + +// Prepares a file name. + +void PrepareFilename(CBotString &filename) +{ + int pos; + + pos = filename.ReverseFind('\\'); + if ( pos > 0 ) + { + filename = filename.Mid(pos+1); // Remove files with + } + + pos = filename.ReverseFind('/'); + if ( pos > 0 ) + { + filename = filename.Mid(pos+1); // also with / + } + + pos = filename.ReverseFind(':'); + if ( pos > 0 ) + { + filename = filename.Mid(pos+1); // also removes the drive letter C: + } + + filename = CBotString(m_filesDir) + CBotString("\\") + filename; +} + + +// constructor of the class +// get the filename as a parameter + +// execution +bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + CBotString mode; + + // accepts no parameters + if ( pVar == NULL ) return true; + + // must be a character string + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } + + CBotString filename = pVar->GivValString(); + PrepareFilename(filename); + + // there may be a second parameter + pVar = pVar->GivNext(); + if ( pVar != NULL ) + { + // recover mode + mode = pVar->GivValString(); + if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } + + // no third parameter + if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } + } + + // saves the file name + pVar = pThis->GivItem("filename"); + pVar->SetValString(filename); + + if ( ! mode.IsEmpty() ) + { + // opens the requested file + FILE* pFile = fopen( filename, mode ); + if ( pFile == NULL ) { Exception = CBotErrFileOpen; return false; } + + m_CompteurFileOpen ++; + + // save the channel file + pVar = pThis->GivItem("handle"); + pVar->SetValInt((long)pFile); + } + + return true; +} + +// compilation +CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) +{ + // accepts no parameters + if ( pVar == NULL ) return CBotTypResult( 0 ); + + // must be a character string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + + // there may be a second parameter + pVar = pVar->GivNext(); + if ( pVar != NULL ) + { + // which must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); + } + + // the result is void (constructor) + return CBotTypResult( 0 ); +} + + +// destructor of the class + +// execution +bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // retrieve the item "handle" + pVar = pThis->GivItem("handle"); + + // don't open? no problem :) + if ( pVar->GivInit() != IS_DEF) return true; + + FILE* pFile= (FILE*)pVar->GivValInt(); + fclose(pFile); + m_CompteurFileOpen --; + + pVar->SetInit(IS_NAN); + + return true; +} + + +// process FILE :: open +// get the r/w mode as a parameter + +// execution +bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // there must be a parameter + if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } + + // which must be a character string + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } + + // There may be a second parameter + if ( pVar->GivNext() != NULL ) + { + // if the first parameter is the file name + CBotString filename = pVar->GivValString(); + PrepareFilename(filename); + + // saves the file name + CBotVar* pVar2 = pThis->GivItem("filename"); + pVar2->SetValString(filename); + + // next parameter is the mode + pVar = pVar -> GivNext(); + } + + CBotString mode = pVar->GivValString(); + if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } + + // no third parameter + if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } + + // retrieve the item "handle" + pVar = pThis->GivItem("handle"); + + // which must not be initialized + if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return false; } + + // file contains the name + pVar = pThis->GivItem("filename"); + CBotString filename = pVar->GivValString(); + + PrepareFilename(filename); // if the name was h.filename attribute = "..."; + + // opens the requested file + FILE* pFile = fopen( filename, mode ); + if ( pFile == NULL ) + { + pResult->SetValInt(false); + return true; + } + + m_CompteurFileOpen ++; + + // Registered the channel file + pVar = pThis->GivItem("handle"); + pVar->SetValInt((long)pFile); + + pResult->SetValInt(true); + return true; +} + +// compilation +CBotTypResult cfopen (CBotVar* pThis, CBotVar* &pVar) +{ + // there must be a parameter + if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); + + // which must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + + // there may be a second parameter + pVar = pVar->GivNext(); + if ( pVar != NULL ) + { + // which must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); + } + + // the result is bool + return CBotTypResult(CBotTypBoolean); +} + + +// process FILE :: close + +// execeution +bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // it shouldn't be any parameters + if ( pVar != NULL ) return CBotErrOverParam; + + // retrieve the item "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + fclose(pFile); + m_CompteurFileOpen --; + + pVar->SetInit(IS_NAN); + + return true; +} + +// compilation +CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) +{ + // it shouldn't be any parameters + if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); + + // function returns a result "void" + return CBotTypResult( 0 ); +} + +// process FILE :: writeln + +// execution +bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // there must be a parameter + if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } + + // which must be a character string + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } + + CBotString param = pVar->GivValString(); + + // retrieve the item "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + + int res = fputs(param+CBotString("\n"), pFile); + + // if an error occurs generate an exception + if ( res < 0 ) { Exception = CBotErrWrite; return false; } + + return true; +} + +// compilation +CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) +{ + // there must be a parameter + if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); + + // which must be a character string + if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); + + // no other parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); + + // the function returns a void result + return CBotTypResult( 0 ); +} + +// process FILE :: readln + +// execution +bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // it shouldn't be any parameters + if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } + + // retrieve the item "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + + char chaine[2000]; + int i; + for ( i = 0 ; i < 2000 ; i++ ) chaine[i] = 0; + + fgets(chaine, 1999, pFile); + + for ( i = 0 ; i < 2000 ; i++ ) if (chaine[i] == '\n') chaine[i] = 0; + + // if an error occurs generate an exception + if ( ferror(pFile) ) { Exception = CBotErrRead; return false; } + + pResult->SetValString( chaine ); + + return true; +} + +// compilation +CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) +{ + // it should not be any parameter + if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); + + // function returns a result "string" + return CBotTypResult( CBotTypString ); +} +// process FILE :: readln + + +// execution +bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // it should not be any parameter + if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } + + // retrieve the item "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + + pResult->SetValInt( feof( pFile ) ); + + return true; +} + +// compilation +CBotTypResult cfeof (CBotVar* pThis, CBotVar* &pVar) +{ + // it shouldn't be any parameter + if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); + + // the function returns a boolean result + return CBotTypResult( CBotTypBoolean ); +} + + + + + +void InitClassFILE() +{ +// create a class for file management +// the use is as follows: +// file canal( "NomFichier.txt" ) +// canal.open( "r" ); // open for read +// s = canal.readln( ); // reads a line +// canal.close(); // close the file + + // create the class FILE + m_pClassFILE = new CBotClass("file", NULL); + // adds the component ".filename" + m_pClassFILE->AddItem("filename", CBotTypString); + // adds the component ".handle" + m_pClassFILE->AddItem("handle", CBotTypInt, PR_PRIVATE); + + // define a constructor and a destructor + m_pClassFILE->AddFunction("file", rfconstruct, cfconstruct ); + m_pClassFILE->AddFunction("~file", rfdestruct, NULL ); + + // end of the methods associated + m_pClassFILE->AddFunction("open", rfopen, cfopen ); + m_pClassFILE->AddFunction("close", rfclose, cfclose ); + m_pClassFILE->AddFunction("writeln", rfwrite, cfwrite ); + m_pClassFILE->AddFunction("readln", rfread, cfread ); + m_pClassFILE->AddFunction("eof", rfeof, cfeof ); + + m_pFuncFile = new CBotProgram( ); + CBotStringArray ListFonctions; + m_pFuncFile->Compile( "public file openfile(string name, string mode) {return new file(name, mode);}", ListFonctions); + m_pFuncFile->SetIdent(-2); // restoreState in special identifier for this function +} + + @@ -283,22 +690,22 @@ CRobotMain::CRobotMain(CInstanceManager* iMan) m_windowPos = Math::Point(0.15f, 0.17f); m_windowDim = Math::Point(0.70f, 0.66f); - if ( GetProfileFloat("Edit", "FontSize", fValue) ) m_fontSize = fValue; - if ( GetProfileFloat("Edit", "WindowPos.x", fValue) ) m_windowPos.x = fValue; - if ( GetProfileFloat("Edit", "WindowPos.y", fValue) ) m_windowPos.y = fValue; - if ( GetProfileFloat("Edit", "WindowDim.x", fValue) ) m_windowDim.x = fValue; - if ( GetProfileFloat("Edit", "WindowDim.y", fValue) ) m_windowDim.y = fValue; + if ( GetLocalProfileFloat("Edit", "FontSize", fValue) ) m_fontSize = fValue; + if ( GetLocalProfileFloat("Edit", "WindowPos.x", fValue) ) m_windowPos.x = fValue; + if ( GetLocalProfileFloat("Edit", "WindowPos.y", fValue) ) m_windowPos.y = fValue; + if ( GetLocalProfileFloat("Edit", "WindowDim.x", fValue) ) m_windowDim.x = fValue; + if ( GetLocalProfileFloat("Edit", "WindowDim.y", fValue) ) m_windowDim.y = fValue; m_IOPublic = false; m_IODim = Math::Point(320.0f/640.0f, (121.0f+18.0f*8)/480.0f); m_IOPos.x = (1.0f-m_IODim.x)/2.0f; // in the middle m_IOPos.y = (1.0f-m_IODim.y)/2.0f; - if ( GetProfileInt ("Edit", "IOPublic", iValue) ) m_IOPublic = iValue; - if ( GetProfileFloat("Edit", "IOPos.x", fValue) ) m_IOPos.x = fValue; - if ( GetProfileFloat("Edit", "IOPos.y", fValue) ) m_IOPos.y = fValue; - if ( GetProfileFloat("Edit", "IODim.x", fValue) ) m_IODim.x = fValue; - if ( GetProfileFloat("Edit", "IODim.y", fValue) ) m_IODim.y = fValue; + if ( GetLocalProfileInt ("Edit", "IOPublic", iValue) ) m_IOPublic = iValue; + if ( GetLocalProfileFloat("Edit", "IOPos.x", fValue) ) m_IOPos.x = fValue; + if ( GetLocalProfileFloat("Edit", "IOPos.y", fValue) ) m_IOPos.y = fValue; + if ( GetLocalProfileFloat("Edit", "IODim.x", fValue) ) m_IODim.x = fValue; + if ( GetLocalProfileFloat("Edit", "IODim.y", fValue) ) m_IODim.y = fValue; m_short->FlushShortcuts(); InitEye(); @@ -315,7 +722,7 @@ CRobotMain::CRobotMain(CInstanceManager* iMan) g_unit = 4.0f; m_gamerName[0] = 0; - GetProfileString("Gamer", "LastName", m_gamerName, 100); + GetLocalProfileString("Gamer", "LastName", m_gamerName, 100); SetGlobalGamerName(m_gamerName); ReadFreeParam(); m_dialog->SetupRecall(); @@ -430,7 +837,7 @@ void CRobotMain::CreateIni() int iValue; // colobot.ini don't exist? - if ( !GetProfileInt("Setup", "TotoMode", iValue) ) + if ( !GetLocalProfileInt("Setup", "TotoMode", iValue) ) { m_dialog->SetupMemorize(); } @@ -1801,7 +2208,7 @@ float CRobotMain::RetGameTime() void CRobotMain::SetFontSize(float size) { m_fontSize = size; - SetProfileFloat("Edit", "FontSize", m_fontSize); + SetLocalProfileFloat("Edit", "FontSize", m_fontSize); } float CRobotMain::RetFontSize() @@ -1814,8 +2221,8 @@ float CRobotMain::RetFontSize() void CRobotMain::SetWindowPos(Math::Point pos) { m_windowPos = pos; - SetProfileFloat("Edit", "WindowPos.x", m_windowPos.x); - SetProfileFloat("Edit", "WindowPos.y", m_windowPos.y); + SetLocalProfileFloat("Edit", "WindowPos.x", m_windowPos.x); + SetLocalProfileFloat("Edit", "WindowPos.y", m_windowPos.y); } Math::Point CRobotMain::RetWindowPos() @@ -1826,8 +2233,8 @@ Math::Point CRobotMain::RetWindowPos() void CRobotMain::SetWindowDim(Math::Point dim) { m_windowDim = dim; - SetProfileFloat("Edit", "WindowDim.x", m_windowDim.x); - SetProfileFloat("Edit", "WindowDim.y", m_windowDim.y); + SetLocalProfileFloat("Edit", "WindowDim.x", m_windowDim.x); + SetLocalProfileFloat("Edit", "WindowDim.y", m_windowDim.y); } Math::Point CRobotMain::RetWindowDim() @@ -1841,7 +2248,7 @@ Math::Point CRobotMain::RetWindowDim() void CRobotMain::SetIOPublic(bool bMode) { m_IOPublic = bMode; - SetProfileInt("Edit", "IOPublic", m_IOPublic); + SetLocalProfileInt("Edit", "IOPublic", m_IOPublic); } bool CRobotMain::RetIOPublic() @@ -1852,8 +2259,8 @@ bool CRobotMain::RetIOPublic() void CRobotMain::SetIOPos(Math::Point pos) { m_IOPos = pos; - SetProfileFloat("Edit", "IOPos.x", m_IOPos.x); - SetProfileFloat("Edit", "IOPos.y", m_IOPos.y); + SetLocalProfileFloat("Edit", "IOPos.x", m_IOPos.x); + SetLocalProfileFloat("Edit", "IOPos.y", m_IOPos.y); } Math::Point CRobotMain::RetIOPos() @@ -1864,8 +2271,8 @@ Math::Point CRobotMain::RetIOPos() void CRobotMain::SetIODim(Math::Point dim) { m_IODim = dim; - SetProfileFloat("Edit", "IODim.x", m_IODim.x); - SetProfileFloat("Edit", "IODim.y", m_IODim.y); + SetLocalProfileFloat("Edit", "IODim.x", m_IODim.x); + SetLocalProfileFloat("Edit", "IODim.y", m_IODim.y); } Math::Point CRobotMain::RetIODim() diff --git a/src/plugins/plugin.h b/src/plugins/plugin.h new file mode 100644 index 0000000..f238122 --- /dev/null +++ b/src/plugins/plugin.h @@ -0,0 +1,33 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// plugin.h + + +#pragma once + + +#define PLUGIN_INTERFACE(class_type, interface_type) \ + extern "C" interface_type* installPlugin() { return (interface_type *)new class_type(); } \ + extern "C" void uninstallPlugin(class_type *_class) { delete _class; } + + +class CPlugin { + public: + virtual char* PluginName() = 0; + virtual int PluginVersion() = 0; +}; + diff --git a/src/script/ClassFILE.cpp b/src/script/ClassFILE.cpp deleted file mode 100644 index d5f2c6b..0000000 --- a/src/script/ClassFILE.cpp +++ /dev/null @@ -1,425 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - - - -// Static variables - -static CBotClass* m_pClassFILE; -static CBotProgram* m_pFuncFile; -static int m_CompteurFileOpen = 0; -static char* m_filesDir; - - - -// Prepares a file name. - -void PrepareFilename(CBotString &filename) -{ - int pos; - - pos = filename.ReverseFind('\\'); - if ( pos > 0 ) - { - filename = filename.Mid(pos+1); // Remove files with - } - - pos = filename.ReverseFind('/'); - if ( pos > 0 ) - { - filename = filename.Mid(pos+1); // also with / - } - - pos = filename.ReverseFind(':'); - if ( pos > 0 ) - { - filename = filename.Mid(pos+1); // also removes the drive letter C: - } - - filename = CBotString(m_filesDir) + CBotString("\\") + filename; -} - - -// constructor of the class -// get the filename as a parameter - -// execution -bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - CBotString mode; - - // accepts no parameters - if ( pVar == NULL ) return true; - - // must be a character string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } - - CBotString filename = pVar->GivValString(); - PrepareFilename(filename); - - // there may be a second parameter - pVar = pVar->GivNext(); - if ( pVar != NULL ) - { - // recover mode - mode = pVar->GivValString(); - if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } - - // no third parameter - if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } - } - - // saves the file name - pVar = pThis->GivItem("filename"); - pVar->SetValString(filename); - - if ( ! mode.IsEmpty() ) - { - // opens the requested file - FILE* pFile = fopen( filename, mode ); - if ( pFile == NULL ) { Exception = CBotErrFileOpen; return false; } - - m_CompteurFileOpen ++; - - // save the channel file - pVar = pThis->GivItem("handle"); - pVar->SetValInt((long)pFile); - } - - return true; -} - -// compilation -CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) -{ - // accepts no parameters - if ( pVar == NULL ) return CBotTypResult( 0 ); - - // must be a character string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - - // there may be a second parameter - pVar = pVar->GivNext(); - if ( pVar != NULL ) - { - // which must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - // no third parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - } - - // the result is void (constructor) - return CBotTypResult( 0 ); -} - - -// destructor of the class - -// execution -bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // retrieve the item "handle" - pVar = pThis->GivItem("handle"); - - // don't open? no problem :) - if ( pVar->GivInit() != IS_DEF) return true; - - FILE* pFile= (FILE*)pVar->GivValInt(); - fclose(pFile); - m_CompteurFileOpen --; - - pVar->SetInit(IS_NAN); - - return true; -} - - -// process FILE :: open -// get the r/w mode as a parameter - -// execution -bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // there must be a parameter - if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } - - // which must be a character string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } - - // There may be a second parameter - if ( pVar->GivNext() != NULL ) - { - // if the first parameter is the file name - CBotString filename = pVar->GivValString(); - PrepareFilename(filename); - - // saves the file name - CBotVar* pVar2 = pThis->GivItem("filename"); - pVar2->SetValString(filename); - - // next parameter is the mode - pVar = pVar -> GivNext(); - } - - CBotString mode = pVar->GivValString(); - if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } - - // no third parameter - if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } - - // retrieve the item "handle" - pVar = pThis->GivItem("handle"); - - // which must not be initialized - if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return false; } - - // file contains the name - pVar = pThis->GivItem("filename"); - CBotString filename = pVar->GivValString(); - - PrepareFilename(filename); // if the name was h.filename attribute = "..."; - - // opens the requested file - FILE* pFile = fopen( filename, mode ); - if ( pFile == NULL ) - { - pResult->SetValInt(false); - return true; - } - - m_CompteurFileOpen ++; - - // Registered the channel file - pVar = pThis->GivItem("handle"); - pVar->SetValInt((long)pFile); - - pResult->SetValInt(true); - return true; -} - -// compilation -CBotTypResult cfopen (CBotVar* pThis, CBotVar* &pVar) -{ - // there must be a parameter - if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); - - // which must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - - // there may be a second parameter - pVar = pVar->GivNext(); - if ( pVar != NULL ) - { - // which must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - - // no third parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - } - - // the result is bool - return CBotTypResult(CBotTypBoolean); -} - - -// process FILE :: close - -// execeution -bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // it shouldn't be any parameters - if ( pVar != NULL ) return CBotErrOverParam; - - // retrieve the item "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - fclose(pFile); - m_CompteurFileOpen --; - - pVar->SetInit(IS_NAN); - - return true; -} - -// compilation -CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) -{ - // it shouldn't be any parameters - if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - - // function returns a result "void" - return CBotTypResult( 0 ); -} - -// process FILE :: writeln - -// execution -bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // there must be a parameter - if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } - - // which must be a character string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } - - CBotString param = pVar->GivValString(); - - // retrieve the item "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - - int res = fputs(param+CBotString("\n"), pFile); - - // if an error occurs generate an exception - if ( res < 0 ) { Exception = CBotErrWrite; return false; } - - return true; -} - -// compilation -CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) -{ - // there must be a parameter - if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); - - // which must be a character string - if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - - // no other parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - - // the function returns a void result - return CBotTypResult( 0 ); -} - -// process FILE :: readln - -// execution -bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // it shouldn't be any parameters - if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } - - // retrieve the item "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - - char chaine[2000]; - int i; - for ( i = 0 ; i < 2000 ; i++ ) chaine[i] = 0; - - fgets(chaine, 1999, pFile); - - for ( i = 0 ; i < 2000 ; i++ ) if (chaine[i] == '\n') chaine[i] = 0; - - // if an error occurs generate an exception - if ( ferror(pFile) ) { Exception = CBotErrRead; return false; } - - pResult->SetValString( chaine ); - - return true; -} - -// compilation -CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) -{ - // it should not be any parameter - if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - - // function returns a result "string" - return CBotTypResult( CBotTypString ); -} -// process FILE :: readln - - -// execution -bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // it should not be any parameter - if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } - - // retrieve the item "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - - pResult->SetValInt( feof( pFile ) ); - - return true; -} - -// compilation -CBotTypResult cfeof (CBotVar* pThis, CBotVar* &pVar) -{ - // it shouldn't be any parameter - if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - - // the function returns a boolean result - return CBotTypResult( CBotTypBoolean ); -} - - - - - -void InitClassFILE() -{ -// create a class for file management -// the use is as follows: -// file canal( "NomFichier.txt" ) -// canal.open( "r" ); // open for read -// s = canal.readln( ); // reads a line -// canal.close(); // close the file - - // create the class FILE - m_pClassFILE = new CBotClass("file", NULL); - // adds the component ".filename" - m_pClassFILE->AddItem("filename", CBotTypString); - // adds the component ".handle" - m_pClassFILE->AddItem("handle", CBotTypInt, PR_PRIVATE); - - // define a constructor and a destructor - m_pClassFILE->AddFunction("file", rfconstruct, cfconstruct ); - m_pClassFILE->AddFunction("~file", rfdestruct, NULL ); - - // end of the methods associated - m_pClassFILE->AddFunction("open", rfopen, cfopen ); - m_pClassFILE->AddFunction("close", rfclose, cfclose ); - m_pClassFILE->AddFunction("writeln", rfwrite, cfwrite ); - m_pClassFILE->AddFunction("readln", rfread, cfread ); - m_pClassFILE->AddFunction("eof", rfeof, cfeof ); - - m_pFuncFile = new CBotProgram( ); - CBotStringArray ListFonctions; - m_pFuncFile->Compile( "public file openfile(string name, string mode) {return new file(name, mode);}", ListFonctions); - m_pFuncFile->SetIdent(-2); // restoreState in special identifier for this function -} - diff --git a/src/sound/sound.cpp b/src/sound/sound.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/sound/sound.h b/src/sound/sound.h index a2bfc76..598ffe3 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -1,165 +1,318 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// sound.h - -#pragma once - - -#include "math/vector.h" - - -class CInstanceManager; - -namespace Snd { - -const int MAXFILES = 200; -const int MAXSOUND = 32; -const int MAXVOLUME = 20; -const int MAXOPER = 4; - - -enum Sound -{ - SOUND_CLICK = 0, - SOUND_BOUM = 1, - SOUND_EXPLO = 2, - SOUND_FLYh = 3, // human - SOUND_FLY = 4, - SOUND_STEPs = 5, // smooth - SOUND_MOTORw = 6, // wheel - SOUND_MOTORt = 7, // tank - SOUND_MOTORr = 8, // roller - SOUND_ERROR = 9, - SOUND_CONVERT = 10, - SOUND_ENERGY = 11, - SOUND_PLOUF = 12, - SOUND_BLUP = 13, - SOUND_WARNING = 14, - SOUND_DERRICK = 15, - SOUND_LABO = 16, - SOUND_STATION = 17, - SOUND_REPAIR = 18, - SOUND_RESEARCH = 19, - SOUND_INSECTs = 20, // spider - SOUND_BURN = 21, - SOUND_TZOING = 22, - SOUND_GGG = 23, - SOUND_MANIP = 24, - SOUND_FIRE = 25, // shooting with fireball - SOUND_HUMAN1 = 26, // breathing - SOUND_STEPw = 27, // water - SOUND_SWIM = 28, - SOUND_RADAR = 29, - SOUND_BUILD = 30, - SOUND_ALARM = 31, // energy alarm - SOUND_SLIDE = 32, - SOUND_EXPLOi = 33, // insect - SOUND_INSECTa = 34, // ant - SOUND_INSECTb = 35, // bee - SOUND_INSECTw = 36, // worm - SOUND_INSECTm = 37, // mother - SOUND_TREMBLE = 38, - SOUND_PSHHH = 39, - SOUND_NUCLEAR = 40, - SOUND_INFO = 41, - SOUND_OPEN = 42, - SOUND_CLOSE = 43, - SOUND_FACTORY = 44, - SOUND_EGG = 45, - SOUND_MOTORs = 46, // submarine - SOUND_MOTORi = 47, // insect (legs) - SOUND_SHIELD = 48, - SOUND_FIREi = 49, // shooting with orgaball (insect) - SOUND_GUNDEL = 50, - SOUND_PSHHH2 = 51, // shield - SOUND_MESSAGE = 52, - SOUND_BOUMm = 53, // metal - SOUND_BOUMv = 54, // plant - SOUND_BOUMs = 55, // smooth - SOUND_EXPLOl = 56, // little - SOUND_EXPLOlp = 57, // little power - SOUND_EXPLOp = 58, // power - SOUND_STEPh = 59, // hard - SOUND_STEPm = 60, // metal - SOUND_POWERON = 61, - SOUND_POWEROFF = 62, - SOUND_AIE = 63, - SOUND_WAYPOINT = 64, - SOUND_RECOVER = 65, - SOUND_DEADi = 66, - SOUND_JOSTLE = 67, - SOUND_GFLAT = 68, - SOUND_DEADg = 69, // shooting death - SOUND_DEADw = 70, // drowning - SOUND_FLYf = 71, // reactor fail - SOUND_ALARMt = 72, // temperature alarm - SOUND_FINDING = 73, // finds a cache object - SOUND_THUMP = 74, - SOUND_TOUCH = 75, - SOUND_BLITZ = 76, - SOUND_MUSHROOM = 77, - SOUND_FIREp = 78, // shooting with phazer - SOUND_EXPLOg1 = 79, // impact gun 1 - SOUND_EXPLOg2 = 80, // impact gun 2 - SOUND_MOTORd = 81, // engine friction -}; - -enum SoundNext -{ - SOPER_CONTINUE = 1, - SOPER_STOP = 2, - SOPER_LOOP = 3, -}; - -struct SoundOper -{ - char bUsed; - float finalAmplitude; - float finalFrequency; - float totalTime; - float currentTime; - Snd::SoundNext nextOper; -}; - -struct SoundChannel -{ - char bUsed; // buffer used? - char bMute; // silence? - Snd::Sound type; // SOUND_* - int priority; // so great -> important - Math::Vector pos; // position in space - unsigned short uniqueStamp; // unique marker -// LPDIRECTSOUNDBUFFER soundBuffer; -// LPDIRECTSOUND3DBUFFER soundBuffer3D; - float startAmplitude; - float startFrequency; - float changeFrequency; - int initFrequency; - float volume; // 2D: volume 1..0 depending on position - float pan; // 2D: pan -1..+1 depending on position - Snd::SoundOper oper[MAXOPER]; -}; - - - -class CSound -{ - // TODO -}; - -}; // namespace Sound +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// soundinterface.h + +/** + * @file sound/soundinterface.h + * @brief Sound plugin interface + */ + +#pragma once + +#include + +#include + +/*! + * Maximum possible audio volume + */ +#define MAXVOLUME 100 + + +/** + * \public + * \enum Sound sound/soundinterface.h + * \brief Sound enum representing sound file +**/ +enum Sound +{ + SOUND_CLICK = 0, + SOUND_BOUM = 1, + SOUND_EXPLO = 2, + SOUND_FLYh = 3, /*!< human */ + SOUND_FLY = 4, + SOUND_STEPs = 5, /*!< smooth */ + SOUND_MOTORw = 6, /*!< wheel */ + SOUND_MOTORt = 7, /*!< tank */ + SOUND_MOTORr = 8, /*!< roller */ + SOUND_ERROR = 9, + SOUND_CONVERT = 10, + SOUND_ENERGY = 11, + SOUND_PLOUF = 12, + SOUND_BLUP = 13, + SOUND_WARNING = 14, + SOUND_DERRICK = 15, + SOUND_LABO = 16, + SOUND_STATION = 17, + SOUND_REPAIR = 18, + SOUND_RESEARCH = 19, + SOUND_INSECTs = 20, /*!< spider */ + SOUND_BURN = 21, + SOUND_TZOING = 22, + SOUND_GGG = 23, + SOUND_MANIP = 24, + SOUND_FIRE = 25, /*!< shooting with fireball */ + SOUND_HUMAN1 = 26, /*!< breathing */ + SOUND_STEPw = 27, /*!< water */ + SOUND_SWIM = 28, + SOUND_RADAR = 29, + SOUND_BUILD = 30, + SOUND_ALARM = 31, /*!< energy alarm */ + SOUND_SLIDE = 32, + SOUND_EXPLOi = 33, /*!< insect */ + SOUND_INSECTa = 34, /*!< ant */ + SOUND_INSECTb = 35, /*!< bee */ + SOUND_INSECTw = 36, /*!< worm */ + SOUND_INSECTm = 37, /*!< mother */ + SOUND_TREMBLE = 38, + SOUND_PSHHH = 39, + SOUND_NUCLEAR = 40, + SOUND_INFO = 41, + SOUND_OPEN = 42, + SOUND_CLOSE = 43, + SOUND_FACTORY = 44, + SOUND_EGG = 45, + SOUND_MOTORs = 46, /*!< submarine */ + SOUND_MOTORi = 47, /*!< insect (legs) */ + SOUND_SHIELD = 48, + SOUND_FIREi = 49, /*!< shooting with orgaball (insect) */ + SOUND_GUNDEL = 50, + SOUND_PSHHH2 = 51, /*!< shield */ + SOUND_MESSAGE = 52, + SOUND_BOUMm = 53, /*!< metal */ + SOUND_BOUMv = 54, /*!< plant */ + SOUND_BOUMs = 55, /*!< smooth */ + SOUND_EXPLOl = 56, /*!< little */ + SOUND_EXPLOlp = 57, /*!< little power */ + SOUND_EXPLOp = 58, /*!< power */ + SOUND_STEPh = 59, /*!< hard */ + SOUND_STEPm = 60, /*!< metal */ + SOUND_POWERON = 61, + SOUND_POWEROFF = 62, + SOUND_AIE = 63, + SOUND_WAYPOINT = 64, + SOUND_RECOVER = 65, + SOUND_DEADi = 66, + SOUND_JOSTLE = 67, + SOUND_GFLAT = 68, + SOUND_DEADg = 69, /*!< shooting death */ + SOUND_DEADw = 70, /*!< drowning */ + SOUND_FLYf = 71, /*!< reactor fail */ + SOUND_ALARMt = 72, /*!< temperature alarm */ + SOUND_FINDING = 73, /*!< finds a cache object */ + SOUND_THUMP = 74, + SOUND_TOUCH = 75, + SOUND_BLITZ = 76, + SOUND_MUSHROOM = 77, + SOUND_FIREp = 78, /*!< shooting with phazer */ + SOUND_EXPLOg1 = 79, /*!< impact gun 1 */ + SOUND_EXPLOg2 = 80, /*!< impact gun 2 */ + SOUND_MOTORd = 81, /*!< engine friction */ +}; + + +/** + * \public + * \enum SoundNext sound/soundinterface.h + * \brief Enum representing operation that will be performend on a sound at given time +**/ +enum SoundNext +{ + SOPER_CONTINUE = 1, /*!< continue playing */ + SOPER_STOP = 2, /*!< stop playing */ + SOPER_LOOP = 3, /*!< start over */ +}; + + +/** +* @class CSoundInterface +* +* @brief Sound plugin interface +* +*/ +class CSoundInterface : public CPlugin +{ + public: + CSoundInterface() { + //CInstanceManager::getInstance().AddInstance(CLASS_SOUND, this); + //m_iMan->AddInstance(CLASS_SOUND, this); + }; + virtual ~CSoundInterface() = 0; + + /** Function to initialize sound device + * @param bool b3D - enable support for 3D sound + */ + virtual bool Create(bool b3D) = 0; + + /** Function called to cache all sound effect files. + * Function calls \link CSoundInterface::Cache() \endlink for each file + */ + virtual void CacheAll() = 0; + + /** Function called to cache sound effect file. + * This function is called by plugin interface for each file. + * @param Sound bSound - id of a file, will be used to identify sound files + * @param std::string bFile - file to load + * @return return true on success + */ + virtual bool Cache(Sound bSound, std::string bFile) = 0; + + /** Return if plugin is enabled + * @return return true if plugin is enabled + */ + virtual bool RetEnable() = 0; + + /** Change sound mode to 2D/3D + * @param bool bMode - true to enable 3D sound + */ + virtual void SetSound3D(bool bMode) = 0; + + /** Return if we use 3D sound + * @return true if we have 3D sound enabled + */ + virtual bool RetSound3D() = 0; + + /** Return if we have 3D sound capable card + * @return true for 3D sound support + */ + virtual bool RetSound3DCap() = 0; + + /** Change global sound volume + * @param int volume - range from 0 to MAXVOLUME + */ + virtual void SetAudioVolume(int volume) = 0; + + /** Return global sound volume + * @return global volume as int in range from 0 to MAXVOLUME + */ + virtual int RetAudioVolume() = 0; + + /** Set music volume + * @param int volume - range from 0 to MAXVOLUME + */ + virtual void SetMusicVolume(int volume) = 0; + + /** Return music volume + * @return music volume as int in range from 0 to MAXVOLUME + */ + virtual int RetMusicVolume() = 0; + + /** Set listener position + * @param Math::Vector eye - position of listener + * @param Math::Vector lookat - direction listener is looking at + */ + virtual void SetListener(Math::Vector eye, Math::Vector lookat) = 0; + + /** Update data each frame + * @param float rTime - time since last update + */ + virtual void FrameMove(float rTime) = 0; + + /** Play specific sound + * @param Sound sound - sound to play + * @param float amplitude - change amplitude of sound before playing + * @param float frequency - change sound frequency before playing (0.5 octave down, 2.0 octave up) + * @param bool bLoop - loop sound + * @return identifier of channel that sound will be played on + */ + virtual int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false) = 0; + + /** Play specific sound + * @param Sound sound - sound to play + * @param Math:Vector pos - position of sound in space + * @param float amplitude - change amplitude of sound before playing + * @param float frequency - change sound frequency before playing (0.5 octave down, 2.0 octave up) + * @param bool bLoop - loop sound + * @return identifier of channel that sound will be played on + */ + virtual int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false) = 0; + + /** Remove all operations that would be made on sound in channel. + * @param int channel - channel to work on + * @return return true on success + */ + virtual bool FlushEnvelope(int channel) = 0; + + /** Add envelope to sound. Envelope is a operatino that will be performend on sound in future like changing frequency + * @param int channel - channel to work on + * @param float amplitude - change amplitude + * @param float frequency - change frequency + * @param float time - when to change (sample time) + * @param SoundNext oper - operation to perform + * @return return true on success + */ + virtual bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) = 0; + + /** Set sound position in space + * @param int channel - channel to work on + * @param Math::Vector pos - new positino of a sound + * @return return true on success + */ + virtual bool Position(int channel, Math::Vector pos) = 0; + + /** Set sound frequency + * @param int channel - channel to work on + * @param float frequency - change sound frequency + * @return return true on success + */ + virtual bool Frequency(int channel, float frequency) = 0; + + /** Stop playing sound + * @param int channel - channel to work on + * @return return true on success + */ + virtual bool Stop(int channel) = 0; + + /** Stop playing all sounds + * @return return true on success + */ + virtual bool StopAll() = 0; + + /** Mute/unmute all sounds + * @param bool bMute + * @return return true on success + */ + virtual bool MuteAll(bool bMute) = 0; + + /** Start playing music + * @param int rank - track number + * @param bool bRepeat - repeat playing + * @return return true on success + */ + virtual bool PlayMusic(int rank, bool bRepeat) = 0; + + /** Restart music + * @return return true on success + */ + virtual bool RestartMusic() = 0; + + /** Susspend paying music + * @return return true on success + */ + virtual void SuspendMusic() = 0; + + /** Stop playing music + * @return return true on success + */ + virtual void StopMusic() = 0; + + /** Check if music if playing + * @return return true if music is playing + */ + virtual bool IsPlayingMusic() = 0; +}; diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp index 5d96043..c8759e0 100644 --- a/src/ui/maindialog.cpp +++ b/src/ui/maindialog.cpp @@ -3884,7 +3884,7 @@ void CMainDialog::NameSelect() RetGamerFace(m_main->RetGamerName()); - SetProfileString("Gamer", "LastName", m_main->RetGamerName()); + SetLocalProfileString("Gamer", "LastName", m_main->RetGamerName()); } // Creates a new player. @@ -5522,104 +5522,104 @@ void CMainDialog::SetupMemorize() char key[500]; char num[10]; - SetProfileString("Directory", "scene", m_sceneDir); - SetProfileString("Directory", "savegame", m_savegameDir); - SetProfileString("Directory", "public", m_publicDir); - SetProfileString("Directory", "user", m_userDir); - SetProfileString("Directory", "files", m_filesDir); + SetLocalProfileString("Directory", "scene", m_sceneDir); + SetLocalProfileString("Directory", "savegame", m_savegameDir); + SetLocalProfileString("Directory", "public", m_publicDir); + SetLocalProfileString("Directory", "user", m_userDir); + SetLocalProfileString("Directory", "files", m_filesDir); iValue = m_engine->RetTotoMode(); - SetProfileInt("Setup", "TotoMode", iValue); + SetLocalProfileInt("Setup", "TotoMode", iValue); iValue = m_bTooltip; - SetProfileInt("Setup", "Tooltips", iValue); + SetLocalProfileInt("Setup", "Tooltips", iValue); iValue = m_bGlint; - SetProfileInt("Setup", "InterfaceGlint", iValue); + SetLocalProfileInt("Setup", "InterfaceGlint", iValue); iValue = m_bRain; - SetProfileInt("Setup", "InterfaceGlint", iValue); + SetLocalProfileInt("Setup", "InterfaceGlint", iValue); iValue = m_engine->RetNiceMouse(); - SetProfileInt("Setup", "NiceMouse", iValue); + SetLocalProfileInt("Setup", "NiceMouse", iValue); iValue = m_bSoluce4; - SetProfileInt("Setup", "Soluce4", iValue); + SetLocalProfileInt("Setup", "Soluce4", iValue); iValue = m_bMovies; - SetProfileInt("Setup", "Movies", iValue); + SetLocalProfileInt("Setup", "Movies", iValue); iValue = m_bNiceReset; - SetProfileInt("Setup", "NiceReset", iValue); + SetLocalProfileInt("Setup", "NiceReset", iValue); iValue = m_bHimselfDamage; - SetProfileInt("Setup", "HimselfDamage", iValue); + SetLocalProfileInt("Setup", "HimselfDamage", iValue); iValue = m_bCameraScroll; - SetProfileInt("Setup", "CameraScroll", iValue); + SetLocalProfileInt("Setup", "CameraScroll", iValue); iValue = m_bCameraInvertX; - SetProfileInt("Setup", "CameraInvertX", iValue); + SetLocalProfileInt("Setup", "CameraInvertX", iValue); iValue = m_bEffect; - SetProfileInt("Setup", "InterfaceEffect", iValue); + SetLocalProfileInt("Setup", "InterfaceEffect", iValue); iValue = m_engine->RetShadow(); - SetProfileInt("Setup", "GroundShadow", iValue); + SetLocalProfileInt("Setup", "GroundShadow", iValue); iValue = m_engine->RetGroundSpot(); - SetProfileInt("Setup", "GroundSpot", iValue); + SetLocalProfileInt("Setup", "GroundSpot", iValue); iValue = m_engine->RetDirty(); - SetProfileInt("Setup", "ObjectDirty", iValue); + SetLocalProfileInt("Setup", "ObjectDirty", iValue); iValue = m_engine->RetFog(); - SetProfileInt("Setup", "FogMode", iValue); + SetLocalProfileInt("Setup", "FogMode", iValue); iValue = m_engine->RetLensMode(); - SetProfileInt("Setup", "LensMode", iValue); + SetLocalProfileInt("Setup", "LensMode", iValue); iValue = m_engine->RetSkyMode(); - SetProfileInt("Setup", "SkyMode", iValue); + SetLocalProfileInt("Setup", "SkyMode", iValue); iValue = m_engine->RetPlanetMode(); - SetProfileInt("Setup", "PlanetMode", iValue); + SetLocalProfileInt("Setup", "PlanetMode", iValue); iValue = m_engine->RetLightMode(); - SetProfileInt("Setup", "LightMode", iValue); + SetLocalProfileInt("Setup", "LightMode", iValue); iValue = m_engine->RetJoystick(); - SetProfileInt("Setup", "UseJoystick", iValue); + SetLocalProfileInt("Setup", "UseJoystick", iValue); fValue = m_engine->RetParticuleDensity(); - SetProfileFloat("Setup", "ParticuleDensity", fValue); + SetLocalProfileFloat("Setup", "ParticuleDensity", fValue); fValue = m_engine->RetClippingDistance(); - SetProfileFloat("Setup", "ClippingDistance", fValue); + SetLocalProfileFloat("Setup", "ClippingDistance", fValue); fValue = m_engine->RetObjectDetail(); - SetProfileFloat("Setup", "ObjectDetail", fValue); + SetLocalProfileFloat("Setup", "ObjectDetail", fValue); fValue = m_engine->RetGadgetQuantity(); - SetProfileFloat("Setup", "GadgetQuantity", fValue); + SetLocalProfileFloat("Setup", "GadgetQuantity", fValue); iValue = m_engine->RetTextureQuality(); - SetProfileInt("Setup", "TextureQuality", iValue); + SetLocalProfileInt("Setup", "TextureQuality", iValue); iValue = m_sound->RetAudioVolume(); - SetProfileInt("Setup", "AudioVolume", iValue); + SetLocalProfileInt("Setup", "AudioVolume", iValue); iValue = m_sound->RetMidiVolume(); - SetProfileInt("Setup", "MidiVolume", iValue); + SetLocalProfileInt("Setup", "MidiVolume", iValue); iValue = m_sound->RetSound3D(); - SetProfileInt("Setup", "Sound3D", iValue); + SetLocalProfileInt("Setup", "Sound3D", iValue); iValue = m_engine->RetEditIndentMode(); - SetProfileInt("Setup", "EditIndentMode", iValue); + SetLocalProfileInt("Setup", "EditIndentMode", iValue); iValue = m_engine->RetEditIndentValue(); - SetProfileInt("Setup", "EditIndentValue", iValue); + SetLocalProfileInt("Setup", "EditIndentValue", iValue); key[0] = 0; for ( i=0 ; i<100 ; i++ ) @@ -5633,21 +5633,21 @@ void CMainDialog::SetupMemorize() strcat(key, num); } } - SetProfileString("Setup", "KeyMap", key); + SetLocalProfileString("Setup", "KeyMap", key); #if _NET if ( m_accessEnable ) { iValue = m_accessMission; - SetProfileInt("Setup", "AccessMission", iValue); + SetLocalProfileInt("Setup", "AccessMission", iValue); iValue = m_accessUser; - SetProfileInt("Setup", "AccessUser", iValue); + SetLocalProfileInt("Setup", "AccessUser", iValue); } #endif iValue = m_bDeleteGamer; - SetProfileInt("Setup", "DeleteGamer", iValue); + SetLocalProfileInt("Setup", "DeleteGamer", iValue); m_engine->WriteProfile(); } @@ -5661,192 +5661,192 @@ void CMainDialog::SetupRecall() char key[500]; char* p; - if ( GetProfileString("Directory", "scene", key, _MAX_FNAME) ) + if ( GetLocalProfileString("Directory", "scene", key, _MAX_FNAME) ) { strcpy(m_sceneDir, key); } - if ( GetProfileString("Directory", "savegame", key, _MAX_FNAME) ) + if ( GetLocalProfileString("Directory", "savegame", key, _MAX_FNAME) ) { strcpy(m_savegameDir, key); } - if ( GetProfileString("Directory", "public", key, _MAX_FNAME) ) + if ( GetLocalProfileString("Directory", "public", key, _MAX_FNAME) ) { strcpy(m_publicDir, key); } - if ( GetProfileString("Directory", "user", key, _MAX_FNAME) ) + if ( GetLocalProfileString("Directory", "user", key, _MAX_FNAME) ) { strcpy(m_userDir, key); } - if ( GetProfileString("Directory", "files", key, _MAX_FNAME) ) + if ( GetLocalProfileString("Directory", "files", key, _MAX_FNAME) ) { strcpy(m_filesDir, key); } - if ( GetProfileInt("Setup", "TotoMode", iValue) ) + if ( GetLocalProfileInt("Setup", "TotoMode", iValue) ) { m_engine->SetTotoMode(iValue); } - if ( GetProfileInt("Setup", "Tooltips", iValue) ) + if ( GetLocalProfileInt("Setup", "Tooltips", iValue) ) { m_bTooltip = iValue; } - if ( GetProfileInt("Setup", "InterfaceGlint", iValue) ) + if ( GetLocalProfileInt("Setup", "InterfaceGlint", iValue) ) { m_bGlint = iValue; } - if ( GetProfileInt("Setup", "InterfaceGlint", iValue) ) + if ( GetLocalProfileInt("Setup", "InterfaceGlint", iValue) ) { m_bRain = iValue; } - if ( GetProfileInt("Setup", "NiceMouse", iValue) ) + if ( GetLocalProfileInt("Setup", "NiceMouse", iValue) ) { m_engine->SetNiceMouse(iValue); } - if ( GetProfileInt("Setup", "Soluce4", iValue) ) + if ( GetLocalProfileInt("Setup", "Soluce4", iValue) ) { m_bSoluce4 = iValue; } - if ( GetProfileInt("Setup", "Movies", iValue) ) + if ( GetLocalProfileInt("Setup", "Movies", iValue) ) { m_bMovies = iValue; } - if ( GetProfileInt("Setup", "NiceReset", iValue) ) + if ( GetLocalProfileInt("Setup", "NiceReset", iValue) ) { m_bNiceReset = iValue; } - if ( GetProfileInt("Setup", "HimselfDamage", iValue) ) + if ( GetLocalProfileInt("Setup", "HimselfDamage", iValue) ) { m_bHimselfDamage = iValue; } - if ( GetProfileInt("Setup", "CameraScroll", iValue) ) + if ( GetLocalProfileInt("Setup", "CameraScroll", iValue) ) { m_bCameraScroll = iValue; m_camera->SetCameraScroll(m_bCameraScroll); } - if ( GetProfileInt("Setup", "CameraInvertX", iValue) ) + if ( GetLocalProfileInt("Setup", "CameraInvertX", iValue) ) { m_bCameraInvertX = iValue; m_camera->SetCameraInvertX(m_bCameraInvertX); } - if ( GetProfileInt("Setup", "CameraInvertY", iValue) ) + if ( GetLocalProfileInt("Setup", "CameraInvertY", iValue) ) { m_bCameraInvertY = iValue; m_camera->SetCameraInvertY(m_bCameraInvertY); } - if ( GetProfileInt("Setup", "InterfaceEffect", iValue) ) + if ( GetLocalProfileInt("Setup", "InterfaceEffect", iValue) ) { m_bEffect = iValue; } - if ( GetProfileInt("Setup", "GroundShadow", iValue) ) + if ( GetLocalProfileInt("Setup", "GroundShadow", iValue) ) { m_engine->SetShadow(iValue); } - if ( GetProfileInt("Setup", "GroundSpot", iValue) ) + if ( GetLocalProfileInt("Setup", "GroundSpot", iValue) ) { m_engine->SetGroundSpot(iValue); } - if ( GetProfileInt("Setup", "ObjectDirty", iValue) ) + if ( GetLocalProfileInt("Setup", "ObjectDirty", iValue) ) { m_engine->SetDirty(iValue); } - if ( GetProfileInt("Setup", "FogMode", iValue) ) + if ( GetLocalProfileInt("Setup", "FogMode", iValue) ) { m_engine->SetFog(iValue); m_camera->SetOverBaseColor(RetColor(RetColor(0.0f))); } - if ( GetProfileInt("Setup", "LensMode", iValue) ) + if ( GetLocalProfileInt("Setup", "LensMode", iValue) ) { m_engine->SetLensMode(iValue); } - if ( GetProfileInt("Setup", "SkyMode", iValue) ) + if ( GetLocalProfileInt("Setup", "SkyMode", iValue) ) { m_engine->SetSkyMode(iValue); } - if ( GetProfileInt("Setup", "PlanetMode", iValue) ) + if ( GetLocalProfileInt("Setup", "PlanetMode", iValue) ) { m_engine->SetPlanetMode(iValue); } - if ( GetProfileInt("Setup", "LightMode", iValue) ) + if ( GetLocalProfileInt("Setup", "LightMode", iValue) ) { m_engine->SetLightMode(iValue); } - if ( GetProfileInt("Setup", "UseJoystick", iValue) ) + if ( GetLocalProfileInt("Setup", "UseJoystick", iValue) ) { m_engine->SetJoystick(iValue); } - if ( GetProfileFloat("Setup", "ParticuleDensity", fValue) ) + if ( GetLocalProfileFloat("Setup", "ParticuleDensity", fValue) ) { m_engine->SetParticuleDensity(fValue); } - if ( GetProfileFloat("Setup", "ClippingDistance", fValue) ) + if ( GetLocalProfileFloat("Setup", "ClippingDistance", fValue) ) { m_engine->SetClippingDistance(fValue); } - if ( GetProfileFloat("Setup", "ObjectDetail", fValue) ) + if ( GetLocalProfileFloat("Setup", "ObjectDetail", fValue) ) { m_engine->SetObjectDetail(fValue); } - if ( GetProfileFloat("Setup", "GadgetQuantity", fValue) ) + if ( GetLocalProfileFloat("Setup", "GadgetQuantity", fValue) ) { m_engine->SetGadgetQuantity(fValue); } - if ( GetProfileInt("Setup", "TextureQuality", iValue) ) + if ( GetLocalProfileInt("Setup", "TextureQuality", iValue) ) { m_engine->SetTextureQuality(iValue); } - if ( GetProfileInt("Setup", "AudioVolume", iValue) ) + if ( GetLocalProfileInt("Setup", "AudioVolume", iValue) ) { m_sound->SetAudioVolume(iValue); } - if ( GetProfileInt("Setup", "MidiVolume", iValue) ) + if ( GetLocalProfileInt("Setup", "MidiVolume", iValue) ) { m_sound->SetMidiVolume(iValue); } - if ( GetProfileInt("Setup", "EditIndentMode", iValue) ) + if ( GetLocalProfileInt("Setup", "EditIndentMode", iValue) ) { m_engine->SetEditIndentMode(iValue); } - if ( GetProfileInt("Setup", "EditIndentValue", iValue) ) + if ( GetLocalProfileInt("Setup", "EditIndentValue", iValue) ) { m_engine->SetEditIndentValue(iValue); } - if ( GetProfileString("Setup", "KeyMap", key, 500) ) + if ( GetLocalProfileString("Setup", "KeyMap", key, 500) ) { p = key; for ( i=0 ; i<100 ; i++ ) @@ -5866,19 +5866,19 @@ void CMainDialog::SetupRecall() #if _NET if ( m_accessEnable ) { - if ( GetProfileInt("Setup", "AccessMission", iValue) ) + if ( GetLocalProfileInt("Setup", "AccessMission", iValue) ) { m_accessMission = iValue; } - if ( GetProfileInt("Setup", "AccessUser", iValue) ) + if ( GetLocalProfileInt("Setup", "AccessUser", iValue) ) { m_accessUser = iValue; } } #endif - if ( GetProfileInt("Setup", "DeleteGamer", iValue) ) + if ( GetLocalProfileInt("Setup", "DeleteGamer", iValue) ) { m_bDeleteGamer = iValue; } -- cgit v1.2.3-1-g7c22 From dbd62c96aa351dc1c21e3392348edda2eb012e09 Mon Sep 17 00:00:00 2001 From: Zaba999 Date: Wed, 4 Jul 2012 22:14:28 +0200 Subject: Fixed code compilation without fpermissive flag. Removed Winapi, but now library don't work - work in progress. Some translations. --- CMakeLists.txt | 4 +- src/CBot/CBot.cpp | 885 +++++++++++++++++++++---------------------- src/CBot/CBot.h | 522 ++++++++++++------------- src/CBot/CBotAddExpr.cpp | 2 +- src/CBot/CBotClass.cpp | 145 ++++--- src/CBot/CBotCompExpr.cpp | 2 +- src/CBot/CBotDll.h | 472 +++++++++++------------ src/CBot/CBotFunction.cpp | 164 ++++---- src/CBot/CBotIf.cpp | 24 +- src/CBot/CBotProgram.cpp | 211 ++++++----- src/CBot/CBotStack.cpp | 288 +++++++------- src/CBot/CBotString.cpp | 104 ++--- src/CBot/CBotToken.cpp | 67 ++-- src/CBot/CBotToken.h | 4 +- src/CBot/CBotTwoOpExpr.cpp | 58 +-- src/CBot/CBotVar.cpp | 278 +++++++------- src/CBot/CBotWhile.cpp | 216 +++++------ src/CBot/ClassFILE.cpp | 14 +- src/CBot/StringFunctions.cpp | 98 ++--- src/CBot/old | 15 - src/CMakeLists.txt | 5 +- 21 files changed, 1788 insertions(+), 1790 deletions(-) delete mode 100644 src/CBot/old diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d24e0a..5ca52f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,8 @@ find_package(SDL_image REQUIRED) set(CMAKE_BUILD_TYPE debug) # Global compile flags -set(CMAKE_CXX_FLAGS_RELEASE "-O2") -set(CMAKE_CXX_FLAGS_DEBUG "-w -g -O0") +set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall") +set(CMAKE_CXX_FLAGS_DEBUG "-w -g -O0 -Wall") # Subdirectory with sources add_subdirectory(src bin) diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 1344efd..6cb2e5d 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -25,7 +25,7 @@ // Compiles if the routine returns NULL is that the statement is false // Or misunderstood. -// The error is then on the stack CBotCStack :: Isok () is FALSE +// The error is then on the stack CBotCStack :: Isok () is false @@ -85,19 +85,19 @@ void CBotInstr::DecLvl() } // controle la validit d'un break ou continu -BOOL CBotInstr::ChkLvl(const CBotString& label, int type) +bool CBotInstr::ChkLvl(const CBotString& label, int type) { int i = m_LoopLvl; while (--i>=0) { if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; - if ( label.IsEmpty() ) return TRUE; - if ( m_labelLvl[i] == label ) return TRUE; + if ( label.IsEmpty() ) return true; + if ( m_labelLvl[i] == label ) return true; } - return FALSE; + return false; } -BOOL CBotInstr::IsOfClass(CBotString n) +bool CBotInstr::IsOfClass(CBotString n) { return name == n; } @@ -113,12 +113,6 @@ void CBotInstr::SetToken(CBotToken* p) m_token = *p; } -void CBotInstr::SetToken(CBotString* name, int start, int end) -{ - SetToken( &CBotToken( *name, CBotString(), start, end)); -} - - // rend le type du token associ l'instruction int CBotInstr::GivTokenType() @@ -293,22 +287,22 @@ CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) return NULL; } -BOOL CBotInstr::Execute(CBotStack* &pj) +bool CBotInstr::Execute(CBotStack* &pj) { CBotString ClassManquante = name; ASM_TRAP(); // ne doit jamais passer par cette routine // mais utiliser les routines des classes filles - return FALSE; + return false; } -BOOL CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) +bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) { - if ( !Execute(pj) ) return FALSE; + if ( !Execute(pj) ) return false; pVar->SetVal( pj->GivVar() ); - return TRUE; + return true; } -void CBotInstr::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) { CBotString ClassManquante = name; ASM_TRAP(); // ne doit jamais passer par cette routine @@ -316,19 +310,19 @@ void CBotInstr::RestoreState(CBotStack* &pj, BOOL bMain) } -BOOL CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { ASM_TRAP(); // papa sait pas faire, voir les filles - return FALSE; + return false; } -BOOL CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep, BOOL bExtend) +bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { ASM_TRAP(); // papa sait pas faire, voir les filles - return FALSE; + return false; } -void CBotInstr::RestoreStateVar(CBotStack* &pile, BOOL bMain) +void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) { ASM_TRAP(); // papa sait pas faire, voir les filles } @@ -337,9 +331,9 @@ void CBotInstr::RestoreStateVar(CBotStack* &pile, BOOL bMain) // cela permet de faire l'appel CompCase sur toutes les instructions // pour savoir s'il s'agit d'un case pour la valeur dsire. -BOOL CBotInstr::CompCase(CBotStack* &pj, int val) +bool CBotInstr::CompCase(CBotStack* &pj, int val) { - return FALSE; + return false; } ////////////////////////////////////////////////////////////////////////////////////////// @@ -352,7 +346,7 @@ BOOL CBotInstr::CompCase(CBotStack* &pj, int val) // l'objet retourn par Compile est gnralement de type CBotListInstr -CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, BOOL bLocal) +CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) { pStack->SetStartError(p->GivStart()); @@ -374,7 +368,7 @@ CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, BOOL bLocal) return NULL; } -CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, BOOL bLocal) +CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) { // est-ce un nouveau bloc ? if ( p->GivType() == ID_OPBLK ) return CBotBlock::Compile(p, pStack); @@ -410,13 +404,13 @@ CBotListInstr::~CBotListInstr() delete m_Instr; } -CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, BOOL bLocal) +CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) { CBotCStack* pStk = pStack->TokenStack(p, bLocal); // les variables sont locales CBotListInstr* inst = new CBotListInstr(); - while (TRUE) + while (true) { if ( p == NULL ) break; @@ -446,10 +440,10 @@ CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, BOOL bLocal // excute une liste d'instructions -BOOL CBotListInstr::Execute(CBotStack* &pj) +bool CBotListInstr::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, TRUE);//indispensable pour SetState() + CBotStack* pile = pj->AddStack(this, true);//indispensable pour SetState() if ( pile->StackOver() ) return pj->Return( pile ); @@ -458,20 +452,20 @@ BOOL CBotListInstr::Execute(CBotStack* &pj) int state = pile->GivState(); while (state-->0) p = p->GivNext(); // revient sur l'opration interrompue - if ( p != NULL ) while (TRUE) + if ( p != NULL ) while (true) { // DEBUG( "CBotListInstr", pile->GivState(), pile ); - if ( !p->Execute(pile) ) return FALSE; + if ( !p->Execute(pile) ) return false; p = p->GivNext(); if ( p == NULL ) break; - if (!pile->IncState()) ;//return FALSE; // prt pour la suivante + if (!pile->IncState()) ;//return false; // prt pour la suivante } return pj->Return( pile ); // transmet en dessous } -void CBotListInstr::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -483,11 +477,11 @@ void CBotListInstr::RestoreState(CBotStack* &pj, BOOL bMain) int state = pile->GivState(); while ( p != NULL && state-- > 0) { - p->RestoreState(pile, FALSE); + p->RestoreState(pile, false); p = p->GivNext(); // revient sur l'opration interrompue } - if ( p != NULL ) p->RestoreState(pile, TRUE); + if ( p != NULL ) p->RestoreState(pile, true); } ////////////////////////////////////////////////////////////////////////////////////// @@ -523,7 +517,7 @@ CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) } // cre une variable et lui assigne le rsultat de la pile -BOOL CBotLeftExprVar::Execute(CBotStack* &pj) +bool CBotLeftExprVar::Execute(CBotStack* &pj) { CBotVar* var1; CBotVar* var2; @@ -535,10 +529,10 @@ BOOL CBotLeftExprVar::Execute(CBotStack* &pj) var2 = pj->GivVar(); // resultat sur la pile if ( var2 ) var1->SetVal(var2); // fait l'assignation - return TRUE; // opration faite + return true; // opration faite } -void CBotLeftExprVar::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) { CBotVar* var1; @@ -631,10 +625,10 @@ error: // excute la dfinition d'un tableau -BOOL CBotInstArray::Execute(CBotStack* &pj) +bool CBotInstArray::Execute(CBotStack* &pj) { CBotStack* pile1 = pj->AddStack(this); -// if ( pile1 == EOX ) return TRUE; +// if ( pile1 == EOX ) return true; CBotStack* pile = pile1; @@ -650,7 +644,7 @@ BOOL CBotInstArray::Execute(CBotStack* &pj) nb++; if ( pile->GivState() == 0 ) { - if ( !p->Execute(pile) ) return FALSE; // calcul de la taille // interrompu? + if ( !p->Execute(pile) ) return false; // calcul de la taille // interrompu? pile->IncState(); } p = p->GivNext3b(); @@ -698,20 +692,20 @@ BOOL CBotInstArray::Execute(CBotStack* &pj) { CBotVar* pVar = pj->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); - if ( !m_listass->Execute(pile1, pVar) ) return FALSE; + if ( !m_listass->Execute(pile1, pVar) ) return false; } pile1->IncState(); } - if ( pile1->IfStep() ) return FALSE; // montre ce pas ? + if ( pile1->IfStep() ) return false; // montre ce pas ? if ( m_next2b && - !m_next2b->Execute( pile1 ) ) return FALSE; + !m_next2b->Execute( pile1 ) ) return false; return pj->Return( pile1 ); // transmet en dessous } -void CBotInstArray::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile1 = pj; @@ -753,15 +747,15 @@ void CBotInstArray::RestoreState(CBotStack* &pj, BOOL bMain) } // cas particulier pour les indices vides -BOOL CBotEmpty :: Execute(CBotStack* &pj) +bool CBotEmpty :: Execute(CBotStack* &pj) { CBotVar* pVar = CBotVar::Create("", CBotTypInt); pVar->SetValInt(-1); // met la valeur -1 sur la pile pj->SetVar(pVar); - return TRUE; + return true; } -void CBotEmpty :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) { } @@ -878,10 +872,10 @@ error: // excute la dfinition d'un tableau -BOOL CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) +bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) { CBotStack* pile1 = pj->AddStack(); -// if ( pile1 == EOX ) return TRUE; +// if ( pile1 == EOX ) return true; CBotVar* pVar2; CBotInstr* p = m_expr; @@ -892,9 +886,9 @@ BOOL CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) { if ( pile1->GivState() > n ) continue; - pVar2 = pVar->GivItem(n, TRUE); + pVar2 = pVar->GivItem(n, true); - if ( !p->Execute(pile1, pVar2) ) return FALSE; // value l'expression + if ( !p->Execute(pile1, pVar2) ) return false; // value l'expression pile1->IncState(); } @@ -902,7 +896,7 @@ BOOL CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) return pj->Return( pile1 ); // transmet en dessous } -void CBotListArray::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) { @@ -940,7 +934,7 @@ CBotInt::~CBotInt() // delete m_next; // fait par le destructeur de la classe de base ~CBotInstr() } -CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, BOOL first) +CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) { if ( IsOfType(p, ID_OPBRK) ) { @@ -950,7 +944,7 @@ CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypRes return NULL; } - CBotInstr* inst = CompileArray(p, pStack, CBotTypResult( CBotTypArrayPointer, type ), FALSE); + CBotInstr* inst = CompileArray(p, pStack, CBotTypResult( CBotTypArrayPointer, type ), false); if ( inst != NULL || !pStack->IsOk() ) return inst; } @@ -962,7 +956,7 @@ CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypRes if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes { - if ( NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, FALSE) )) // compile la suivante + if ( NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false) )) // compile la suivante { return inst; } @@ -980,7 +974,7 @@ CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypRes return NULL; } -CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL noskip) +CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { CBotToken* pp = cont ? NULL : p; // pas de rptition du token "int" @@ -1025,7 +1019,7 @@ CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL n if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes { - if ( NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, TRUE, noskip) )) // compile la suivante + if ( NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante { return pStack->Return(inst2, pStk); } @@ -1058,7 +1052,7 @@ CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL n if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes { - if ( NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, TRUE, noskip) )) // compile la suivante + if ( NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante { return pStack->Return(inst, pStk); } @@ -1079,28 +1073,28 @@ error: // excute la dfinition de la variable entire -BOOL CBotInt::Execute(CBotStack* &pj) +bool CBotInt::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); //indispensable pour SetState() -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return FALSE; // valeur initiale // interrompu? + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return FALSE; + if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return FALSE; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) return pj->Return( pile ); // transmet en dessous } -void CBotInt::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotInt::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; if ( bMain ) @@ -1140,7 +1134,7 @@ CBotBoolean::~CBotBoolean() delete m_expr; } -CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL noskip) +CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { CBotToken* pp = cont ? NULL : p; @@ -1207,7 +1201,7 @@ CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BO suite: if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes { - if ( NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, TRUE, noskip) )) // compile la suivante + if ( NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip) )) // compile la suivante { return pStack->Return(inst, pStk); } @@ -1228,28 +1222,28 @@ error: // excute une dfinition de variable boolenne -BOOL CBotBoolean::Execute(CBotStack* &pj) +bool CBotBoolean::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return FALSE; // valeur initiale // interrompu? + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return FALSE; + if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return FALSE; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) return pj->Return( pile ); // transmet en dessous } -void CBotBoolean::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; if ( bMain ) @@ -1290,7 +1284,7 @@ CBotFloat::~CBotFloat() delete m_expr; } -CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL noskip) +CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { CBotToken* pp = cont ? NULL : p; @@ -1358,7 +1352,7 @@ CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL suite: if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes { - if ( NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, TRUE, noskip) )) // compile la suivante + if ( NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip) )) // compile la suivante { return pStack->Return(inst, pStk); } @@ -1379,28 +1373,28 @@ error: // excute la dfintion de la variable relle -BOOL CBotFloat::Execute(CBotStack* &pj) +bool CBotFloat::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return FALSE; // valeur initiale // interrompu? + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return FALSE; + if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return FALSE; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) return pj->Return( pile ); // transmet en dessous } -void CBotFloat::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; if ( bMain ) @@ -1441,7 +1435,7 @@ CBotIString::~CBotIString() delete m_expr; } -CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BOOL noskip) +CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { CBotToken* pp = cont ? NULL : p; @@ -1491,7 +1485,7 @@ CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont, BO if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes { - if ( NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, TRUE, noskip) )) // compile la suivante + if ( NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip) )) // compile la suivante { return pStack->Return(inst, pStk); } @@ -1512,28 +1506,28 @@ error: // excute la dfinition de la variable string -BOOL CBotIString::Execute(CBotStack* &pj) +bool CBotIString::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return FALSE; // valeur initiale // interrompu? + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return FALSE; + if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return FALSE; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) return pj->Return( pile ); // transmet en dessous } -void CBotIString::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotIString::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; @@ -1641,7 +1635,7 @@ CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) var->SetInit(2); } else - var->SetInit(TRUE); + var->SetInit(true); break; case ID_ASSADD: @@ -1682,21 +1676,21 @@ CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) // excute une expression avec assignation -BOOL CBotExpression::Execute(CBotStack* &pj) +bool CBotExpression::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; CBotToken* pToken = m_leftop->GivToken(); CBotVar* pVar = NULL; CBotStack* pile1 = pile; - BOOL IsInit = TRUE; + bool IsInit = true; CBotVar* result = NULL; // doit tre fait avant pour les indices ventuels (pile peut tre change) - if ( !m_leftop->ExecuteVar(pVar, pile, NULL, FALSE) ) return FALSE; // variable avant valuation de la valeur droite + if ( !m_leftop->ExecuteVar(pVar, pile, NULL, false) ) return false; // variable avant valuation de la valeur droite // DEBUG( "CBotExpression::Execute", -1, pj); if ( pile1->GivState()==0) @@ -1710,7 +1704,7 @@ BOOL CBotExpression::Execute(CBotStack* &pj) if ( pile2->GivState()==0) { // DEBUG( "CBotExpression::Execute", -2, pj); - if (m_rightop && !m_rightop->Execute(pile2)) return FALSE; // valeur initiale // interrompu? + if (m_rightop && !m_rightop->Execute(pile2)) return false; // valeur initiale // interrompu? pile2->IncState(); } @@ -1792,13 +1786,13 @@ BOOL CBotExpression::Execute(CBotStack* &pj) // DEBUG( "CBotExpression::Execute", -4, pj); if ( !m_leftop->Execute( pile2, pile1 ) ) - return FALSE; // cre et fait l'assigation du rsultat + return false; // cre et fait l'assigation du rsultat return pj->Return( pile2 ); // transmet en dessous } -void CBotExpression::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) { @@ -1813,11 +1807,11 @@ void CBotExpression::RestoreState(CBotStack* &pj, BOOL bMain) if ( pile1->GivState()==0) { - m_leftop->RestoreStateVar(pile, TRUE); // variable avant valuation de la valeur droite + m_leftop->RestoreStateVar(pile, true); // variable avant valuation de la valeur droite return; } - m_leftop->RestoreStateVar(pile, FALSE); // variable avant valuation de la valeur droite + m_leftop->RestoreStateVar(pile, false); // variable avant valuation de la valeur droite CBotStack* pile2 = pile->RestoreStack(); // attention pile et surtout pas pile1 if ( pile2 == NULL ) return; @@ -2086,20 +2080,20 @@ CBotPreIncExpr::~CBotPreIncExpr() delete m_Instr; } -BOOL CBotPostIncExpr::Execute(CBotStack* &pj) +bool CBotPostIncExpr::Execute(CBotStack* &pj) { CBotStack* pile1 = pj->AddStack(this); CBotStack* pile2 = pile1; CBotVar* var1 = NULL; - if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, TRUE) ) return FALSE; // rcupre la variable selon champs et index + if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index pile1->SetState(1); pile1->SetCopyVar(var1); // place le rsultat (avant incrmentation); CBotStack* pile3 = pile2->AddStack(this); - if ( pile3->IfStep() ) return FALSE; + if ( pile3->IfStep() ) return false; if ( var1->GivInit() == IS_NAN ) { @@ -2117,7 +2111,7 @@ BOOL CBotPostIncExpr::Execute(CBotStack* &pj) return pj->Return(pile1); // opration faite, rsultat sur pile2 } -void CBotPostIncExpr::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -2129,19 +2123,19 @@ void CBotPostIncExpr::RestoreState(CBotStack* &pj, BOOL bMain) if ( pile1 != NULL ) pile1->RestoreStack(this); } -BOOL CBotPreIncExpr::Execute(CBotStack* &pj) +bool CBotPreIncExpr::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotVar* var1; if ( pile->GivState() == 0 ) { CBotStack* pile2 = pile; - if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, TRUE) ) return FALSE; // rcupre la variable selon champs et index + if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index // pile2 est modifi en retour if ( var1->GivInit() == IS_NAN ) @@ -2162,12 +2156,12 @@ BOOL CBotPreIncExpr::Execute(CBotStack* &pj) pile->IncState(); } - if ( !m_Instr->Execute(pile) ) return FALSE; + if ( !m_Instr->Execute(pile) ) return false; return pj->Return(pile); // opration faite } -void CBotPreIncExpr::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -2234,19 +2228,19 @@ CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) // excute l'expresson unaire -BOOL CBotExprUnaire::Execute(CBotStack* &pj) +bool CBotExprUnaire::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState() == 0 ) { - if (!m_Expr->Execute( pile )) return FALSE; // interrompu ? + if (!m_Expr->Execute( pile )) return false; // interrompu ? pile->IncState(); } CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return FALSE; + if ( pile2->IfStep() ) return false; CBotVar* var = pile->GivVar(); // rcupre le rsultat sur la pile @@ -2266,7 +2260,7 @@ BOOL CBotExprUnaire::Execute(CBotStack* &pj) return pj->Return(pile); // transmet en dessous } -void CBotExprUnaire::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -2300,26 +2294,26 @@ CBotIndexExpr::~CBotIndexExpr() // trouve un champ partir de l'instance la compilation -BOOL CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { if ( pVar->GivType(1) != CBotTypArrayPointer ) ASM_TRAP(); - pVar = ((CBotVarArray*)pVar)->GivItem(0, FALSE); // la compilation rend l'lment [0] + pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // la compilation rend l'lment [0] if ( pVar == NULL ) { pile->SetError(TX_OUTARRAY, m_token.GivEnd()); - return FALSE; + return false; } if ( m_next3 != NULL ) return m_next3->ExecuteVar(pVar, pile); - return TRUE; + return true; } // idem l'excution // attention, modifie le pointeur la pile volontairement // place les index calculs sur la pile supplmentaire -BOOL CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep, BOOL bExtend) +bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { CBotStack* pj = pile; // DEBUG( "CBotIndexExpr::ExecuteVar", -1 , pj); @@ -2328,11 +2322,11 @@ BOOL CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev ASM_TRAP(); pile = pile->AddStack(); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState() == 0 ) { - if ( !m_expr->Execute(pile) ) return FALSE; + if ( !m_expr->Execute(pile) ) return false; pile->IncState(); } // traite les tableaux @@ -2357,25 +2351,25 @@ BOOL CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev // DEBUG( "CBotIndexExpr::ExecuteVar", -2 , pj); //if ( bUpdate ) - pVar->Maj(pile->GivPUser(), TRUE); + pVar->Maj(pile->GivPUser(), true); // DEBUG( "CBotIndexExpr::ExecuteVar", -3 , pj); if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return FALSE; + !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; // DEBUG( "CBotIndexExpr::ExecuteVar", -4 , pj); - return TRUE; // ne libre pas la pile + return true; // ne libre pas la pile // pour viter de recalculer les index deux fois le cas chant } -void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, BOOL bMain) +void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) { pile = pile->RestoreStack(); if ( pile == NULL ) return; if ( bMain && pile->GivState() == 0 ) { - m_expr->RestoreState(pile, TRUE); + m_expr->RestoreState(pile, true); return; } @@ -2408,7 +2402,7 @@ void CBotFieldExpr::SetUniqNum(int num) // trouve un champ partir de l'instance la compilation -BOOL CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { if ( pVar->GivType(1) != CBotTypPointer ) ASM_TRAP(); @@ -2418,22 +2412,22 @@ BOOL CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) if ( pVar == NULL ) { pile->SetError(TX_NOITEM, &m_token); - return FALSE; + return false; } if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile) ) return FALSE; + !m_next3->ExecuteVar(pVar, pile) ) return false; - return TRUE; + return true; } // idem l'excution -BOOL CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep, BOOL bExtend) +bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { CBotStack* pj = pile; pile = pile->AddStack(this); // modifie pile en sortie - if ( pile == EOX ) return TRUE; + if ( pile == EOX ) return true; // DEBUG( "CBotFieldExpre::ExecuteVar "+m_token.GivString(), 0, pj ); @@ -2452,7 +2446,7 @@ BOOL CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev return pj->Return( pile ); } - if ( bStep && pile->IfStep() ) return FALSE; + if ( bStep && pile->IfStep() ) return false; // pVar = pVar->GivItem(m_token.GivString()); pVar = pVar->GivItemRef(m_nIdent); @@ -2472,16 +2466,16 @@ BOOL CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev } // demande la mise jour de l'lment, s'il y a lieu - pVar->Maj(pile->GivPUser(), TRUE); + pVar->Maj(pile->GivPUser(), true); if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return FALSE; + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; - return TRUE; // ne libre pas la pile + return true; // ne libre pas la pile // pour conserver l'tat SetState() correspondant l'tape } -void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, BOOL bMain) +void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) { pj = pj->RestoreStack(this); // modifie pj en sortie if ( pj == NULL ) return; @@ -2505,8 +2499,8 @@ CBotLeftExpr::~CBotLeftExpr() { } -// compile une expression pour un left-oprande ( gauche d'une assignation) -// cela peut tre +// compiles an expression for a left-operand (left of an assignment) +// this can be // toto // toto[ 3 ] // toto.x @@ -2517,78 +2511,78 @@ CBotLeftExpr::~CBotLeftExpr() CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GivStart()); - - // est-ce un nom de variable ? - if (p->GivType() == TokenTypVar) - { - CBotLeftExpr* inst = new CBotLeftExpr(); // cre l'objet - - inst->SetToken(p); - - CBotVar* var; - - if ( NULL != (var = pStk->FindVar(p)) ) // cherche si variable connue - { - inst->m_nIdent = var->GivUniqNum(); - if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) - { - if ( var->IsPrivate(PR_READ) && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError( TX_PRIVATE, p ); - goto err; - } - // il s'agit d'un lement de la classe courante - // ajoute l'quivalent d'un this. devant - CBotToken pthis("this"); - inst->SetToken(&pthis); - inst->m_nIdent = -2; // ident pour this - - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( p ); // garde le nom du token - inst->AddNext3(i); // ajoute la suite - - var = pStk->FindVar(pthis); - var = var->GivItem(p->GivString()); - i->SetUniqNum(var->GivUniqNum()); - } - p = p->GivNext(); // token suivant - - while (TRUE) - { - if ( var->GivType() == CBotTypArrayPointer ) // s'il sagit d'un tableau - { - if ( IsOfType( p, ID_OPBRK ) ) // regarde s'il y a un index - { - CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); // compile la formule - inst->AddNext3(i); // ajoute la chaine - - var = ((CBotVarArray*)var)->GivItem(0,TRUE); // donne le composant [0] - - if ( i->m_expr == NULL ) - { - pStk->SetError( TX_BADINDEX, p->GivStart() ); - goto err; - } - - if ( !pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStk->SetError( TX_CLBRK, p->GivStart() ); - goto err; - } - continue; - } - } - - if ( var->GivType(1) == CBotTypPointer ) // pour les classes - { - if ( IsOfType(p, ID_DOT) ) - { - CBotToken* pp = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GivStart()); + + // is it a variable name? + if (p->GivType() == TokenTypVar) + { + CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object + + inst->SetToken(p); + + CBotVar* var; + + if ( NULL != (var = pStk->FindVar(p)) ) // seek if known variable + { + inst->m_nIdent = var->GivUniqNum(); + if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) + { + if ( var->IsPrivate(PR_READ) && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError( TX_PRIVATE, p ); + goto err; + } + // il s'agit d'un lement de la classe courante + // ajoute l'quivalent d'un this. devant + CBotToken pthis("this"); + inst->SetToken(&pthis); + inst->m_nIdent = -2; // ident pour this + + CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment + i->SetToken( p ); // garde le nom du token + inst->AddNext3(i); // ajoute la suite + + var = pStk->FindVar(pthis); + var = var->GivItem(p->GivString()); + i->SetUniqNum(var->GivUniqNum()); + } + p = p->GivNext(); // token suivant + + while (true) + { + if ( var->GivType() == CBotTypArrayPointer ) // s'il sagit d'un tableau + { + if ( IsOfType( p, ID_OPBRK ) ) // regarde s'il y a un index + { + CBotIndexExpr* i = new CBotIndexExpr(); + i->m_expr = CBotExpression::Compile(p, pStk); // compile la formule + inst->AddNext3(i); // ajoute la chaine + + var = ((CBotVarArray*)var)->GivItem(0,true); // donne le composant [0] + + if ( i->m_expr == NULL ) + { + pStk->SetError( TX_BADINDEX, p->GivStart() ); + goto err; + } + + if ( !pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) + { + pStk->SetError( TX_CLBRK, p->GivStart() ); + goto err; + } + continue; + } + } + + if ( var->GivType(1) == CBotTypPointer ) // pour les classes + { + if ( IsOfType(p, ID_DOT) ) + { + CBotToken* pp = p; CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment i->SetToken( pp ); // garde le nom du token @@ -2633,20 +2627,20 @@ err: // excute, trouve une variable et lui assigne le rsultat de la pile -BOOL CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) +bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) { CBotStack* pile = pj->AddStack(); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; -// if ( pile->IfStep() ) return FALSE; +// if ( pile->IfStep() ) return false; CBotVar* var1 = NULL; CBotVar* var2 = NULL; -// var1 = pile->FindVar(m_token, FALSE, TRUE); - if (!ExecuteVar( var1, array, NULL, FALSE )) return FALSE; +// var1 = pile->FindVar(m_token, false, true); + if (!ExecuteVar( var1, array, NULL, false )) return false; // retrouve la variable (et pas la copie) - if (pile->IfStep()) return FALSE; + if (pile->IfStep()) return false; if ( var1 ) { @@ -2677,20 +2671,20 @@ BOOL CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) // retrouve une variable pendant la compilation -BOOL CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { pVar = pile->FindVar(m_token); - if ( pVar == NULL ) return FALSE; + if ( pVar == NULL ) return false; if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile) ) return FALSE; + !m_next3->ExecuteVar(pVar, pile) ) return false; - return TRUE; + return true; } // retrouve une variable l'excution -BOOL CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep) +bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) { pile = pile->AddStack( this ); // dplace la pile @@ -2701,18 +2695,18 @@ BOOL CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevT ASM_TRAP(); #endif pile->SetError(2, &m_token); - return FALSE; + return false; } - if ( bStep && m_next3 == NULL && pile->IfStep() ) return FALSE; + if ( bStep && m_next3 == NULL && pile->IfStep() ) return false; if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, TRUE) ) return FALSE; + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; - return TRUE; + return true; } -void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, BOOL bMain) +void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) { pile = pile->RestoreStack( this ); // dplace la pile if ( pile == NULL ) return; @@ -2765,11 +2759,11 @@ extern float GivNumFloat( const char* p ) { double num = 0; double div = 10; - BOOL bNeg = FALSE; + bool bNeg = false; if (*p == '-') { - bNeg = TRUE; + bNeg = true; p++; } while (*p >= '0' && *p <= '9') @@ -2875,12 +2869,12 @@ CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) // excute, retourne le nombre correspondant -BOOL CBotExprNum::Execute(CBotStack* &pj) +bool CBotExprNum::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, m_numtype); @@ -2905,7 +2899,7 @@ BOOL CBotExprNum::Execute(CBotStack* &pj) return pj->Return(pile); // c'est ok } -void CBotExprNum::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) pj->RestoreStack(this); } @@ -2942,12 +2936,12 @@ CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) // excute, retourne la chane correspondante -BOOL CBotExprAlpha::Execute(CBotStack* &pj) +bool CBotExprAlpha::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); @@ -2961,7 +2955,7 @@ BOOL CBotExprAlpha::Execute(CBotStack* &pj) return pj->Return(pile); } -void CBotExprAlpha::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) pj->RestoreStack(this); } @@ -3002,12 +2996,12 @@ CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) // excute, retourne true ou false -BOOL CBotExprBool::Execute(CBotStack* &pj) +bool CBotExprBool::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); @@ -3018,7 +3012,7 @@ BOOL CBotExprBool::Execute(CBotStack* &pj) return pj->Return(pile); // transmet en dessous } -void CBotExprBool::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) pj->RestoreStack(this); } @@ -3038,20 +3032,20 @@ CBotExprNull::~CBotExprNull() // excute, retourne un pointeur vide -BOOL CBotExprNull::Execute(CBotStack* &pj) +bool CBotExprNull::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypNullPointer); - var->SetInit(TRUE); // pointeur null valide + var->SetInit(true); // pointeur null valide pile->SetVar( var ); // mis sur la pile return pj->Return(pile); // transmet en dessous } -void CBotExprNull::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) pj->RestoreStack(this); } @@ -3071,12 +3065,12 @@ CBotExprNan::~CBotExprNan() // excute, retourne un pointeur vide -BOOL CBotExprNan::Execute(CBotStack* &pj) +bool CBotExprNan::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypInt); var->SetInit(IS_NAN); // nombre nan @@ -3084,7 +3078,7 @@ BOOL CBotExprNan::Execute(CBotStack* &pj) return pj->Return(pile); // transmet en dessous } -void CBotExprNan::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) pj->RestoreStack(this); } @@ -3106,133 +3100,136 @@ CBotExprVar::~CBotExprVar() CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) { - CBotToken* pDebut = p; - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GivStart()); - - // est-ce un nom de variable ? - if (p->GivType() == TokenTypVar) - { - CBotInstr* inst = new CBotExprVar(); // cre l'objet - - inst->SetToken(p); - - CBotVar* var; - - if ( NULL != (var = pStk->FindVar(p)) ) // cherche si variable connue - { - int ident = var->GivUniqNum(); - ((CBotExprVar*)inst)->m_nIdent = ident; // l'identifie par son numro - - if (ident > 0 && ident < 9000) - { - if ( var->IsPrivate(privat) && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError( TX_PRIVATE, p ); - goto err; - } - - // il s'agit d'un lement de la classe courante - // ajoute l'quivalent d'un this. devant - inst->SetToken(&CBotToken("this")); - ((CBotExprVar*)inst)->m_nIdent = -2; // ident pour this - - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( p ); // garde le nom du token - i->SetUniqNum(ident); - inst->AddNext3(i); // ajoute la suite - } - - p = p->GivNext(); // token suivant - - while (TRUE) - { - if ( var->GivType() == CBotTypArrayPointer ) // s'il sagit d'un tableau - { - if ( IsOfType( p, ID_OPBRK ) ) // regarde s'il y a un index - { - CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); // compile la formule - inst->AddNext3(i); // ajoute la chaine - - var = ((CBotVarArray*)var)->GivItem(0,TRUE); // donne le composant [0] - - if ( i->m_expr == NULL ) - { - pStk->SetError( TX_BADINDEX, p->GivStart() ); - goto err; - } - if ( !pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStk->SetError( TX_CLBRK, p->GivStart() ); - goto err; - } - continue; - } - //// pStk->SetError( TX_OPBRK, p->GivStart() ); - } - if ( var->GivType(1) == CBotTypPointer ) // pour les classes - { - if ( IsOfType(p, ID_DOT) ) - { - CBotToken* pp = p; - - if ( p->GivType() == TokenTypVar ) // doit tre un nom - { - if ( p->GivNext()->GivType() == ID_OPENPAR )// un appel de mthode ? - { - CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); - if ( !pStk->IsOk() ) goto err; - inst->AddNext3(i); // ajoute la suite - return pStack->Return(inst, pStk); - } - else - { - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( pp ); // garde le nom du token - inst->AddNext3(i); // ajoute la suite - var = var->GivItem(p->GivString()); // rcupre l'item correpondant - if ( var != NULL ) - { - i->SetUniqNum(var->GivUniqNum()); - if ( var->IsPrivate() && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError( TX_PRIVATE, pp ); - goto err; - } - } - } - - - if ( var != NULL ) - { - p = p->GivNext(); // saute le nom - continue; - } - pStk->SetError( TX_NOITEM, p ); - goto err; - } - pStk->SetError( TX_DOT, p->GivStart() ); - goto err; - } - } - - break; - } - - pStk->SetCopyVar(var); // place une copie de la variable sur la pile (pour le type) - if ( pStk->IsOk() ) return pStack->Return(inst, pStk); - } - pStk->SetError(TX_UNDEFVAR, p); + CBotToken* pDebut = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GivStart()); + + // is it a variable? + if (p->GivType() == TokenTypVar) + { + CBotInstr* inst = new CBotExprVar(); // create the object + + inst->SetToken(p); + + CBotVar* var; + + if ( NULL != (var = pStk->FindVar(p)) ) // seek if known variable + { + int ident = var->GivUniqNum(); + ((CBotExprVar*)inst)->m_nIdent = ident; // identifies variable by its number + + if (ident > 0 && ident < 9000) + { + if ( var->IsPrivate(privat) && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError( TX_PRIVATE, p ); + goto err; + } + + // This is an element of the current class + // ads the equivalent of this. before + /// \TODO need to be fixed revised and fixed after adding unit + //tests + CBotToken token("this"); + inst->SetToken(&token); + ((CBotExprVar*)inst)->m_nIdent = -2; // identificator for this + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken( p ); // keeps the name of the token + i->SetUniqNum(ident); + inst->AddNext3(i); // added after + } + + p = p->GivNext(); // next token + + while (true) + { + if ( var->GivType() == CBotTypArrayPointer ) // s'il sagit d'un tableau + { + if ( IsOfType( p, ID_OPBRK ) ) // regarde s'il y a un index + { + CBotIndexExpr* i = new CBotIndexExpr(); + i->m_expr = CBotExpression::Compile(p, pStk); // compile la formule + inst->AddNext3(i); // ajoute la chaine + + var = ((CBotVarArray*)var)->GivItem(0,true); // donne le composant [0] + + if ( i->m_expr == NULL ) + { + pStk->SetError( TX_BADINDEX, p->GivStart() ); + goto err; + } + if ( !pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) + { + pStk->SetError( TX_CLBRK, p->GivStart() ); + goto err; + } + continue; + } + //// pStk->SetError( TX_OPBRK, p->GivStart() ); + } + if ( var->GivType(1) == CBotTypPointer ) // pour les classes + { + if ( IsOfType(p, ID_DOT) ) + { + CBotToken* pp = p; + + if ( p->GivType() == TokenTypVar ) // doit tre un nom + { + if ( p->GivNext()->GivType() == ID_OPENPAR )// un appel de mthode ? + { + CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); + if ( !pStk->IsOk() ) goto err; + inst->AddNext3(i); // ajoute la suite + return pStack->Return(inst, pStk); + } + else + { + CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment + i->SetToken( pp ); // garde le nom du token + inst->AddNext3(i); // ajoute la suite + var = var->GivItem(p->GivString()); // rcupre l'item correpondant + if ( var != NULL ) + { + i->SetUniqNum(var->GivUniqNum()); + if ( var->IsPrivate() && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError( TX_PRIVATE, pp ); + goto err; + } + } + } + + + if ( var != NULL ) + { + p = p->GivNext(); // saute le nom + continue; + } + pStk->SetError( TX_NOITEM, p ); + goto err; + } + pStk->SetError( TX_DOT, p->GivStart() ); + goto err; + } + } + + break; + } + + pStk->SetCopyVar(var); // place une copie de la variable sur la pile (pour le type) + if ( pStk->IsOk() ) return pStack->Return(inst, pStk); + } + pStk->SetError(TX_UNDEFVAR, p); err: - delete inst; - return pStack->Return(NULL, pStk); - } + delete inst; + return pStack->Return(NULL, pStk); + } - return pStack->Return(NULL, pStk); + return pStack->Return(NULL, pStk); } CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) @@ -3280,19 +3277,19 @@ CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) // excute, rend la valeur d'une variable -BOOL CBotExprVar::Execute(CBotStack* &pj) +bool CBotExprVar::Execute(CBotStack* &pj) { CBotVar* pVar = NULL; CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; -// if ( pile->IfStep() ) return FALSE; +// if ( pile->IfStep() ) return false; CBotStack* pile1 = pile; if ( pile1->GivState() == 0 ) { - if ( !ExecuteVar(pVar, pile, NULL, TRUE) ) return FALSE; // rcupre la variable selon champs et index + if ( !ExecuteVar(pVar, pile, NULL, true) ) return false; // rcupre la variable selon champs et index // DEBUG("CBotExprVar::Execute", 1 , pj); if ( pVar ) pile1->SetCopyVar(pVar); // la place une copie sur la pile @@ -3322,7 +3319,7 @@ BOOL CBotExprVar::Execute(CBotStack* &pj) return pj->Return(pile1); // opration faite } -void CBotExprVar::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -3340,25 +3337,25 @@ void CBotExprVar::RestoreState(CBotStack* &pj, BOOL bMain) // retrouve une variable l'excution -BOOL CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, BOOL bStep) +bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) { CBotStack* pile = pj; pj = pj->AddStack( this ); - if ( bStep && m_nIdent>0 && pj->IfStep() ) return FALSE; + if ( bStep && m_nIdent>0 && pj->IfStep() ) return false; - pVar = pj->FindVar(m_nIdent, TRUE); // cherche la variable avec mise jour si ncessaire + pVar = pj->FindVar(m_nIdent, true); // cherche la variable avec mise jour si ncessaire if ( pVar == NULL ) { #ifdef _DEBUG ASM_TRAP(); #endif pj->SetError(1, &m_token); - return FALSE; + return false; } if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, FALSE) ) - return FALSE; // Champs d'une instance, tableau, mthode ? + !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) + return false; // Champs d'une instance, tableau, mthode ? return pile->ReturnKeep( pj ); // ne rend pas la pile mais rcupre le rsultat si une mthode a t appele } @@ -3366,7 +3363,7 @@ BOOL CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToke // retrouve une variable l'excution -void CBotExprVar::RestoreStateVar(CBotStack* &pj, BOOL bMain) +void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) { pj = pj->RestoreStack( this ); if ( pj == NULL ) return; @@ -3381,7 +3378,7 @@ void CBotExprVar::RestoreStateVar(CBotStack* &pj, BOOL bMain) CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) { - BOOL first = TRUE; + bool first = true; CBotInstr* ret = NULL; // pour la liste retourner // pStack->SetStartError(p->GivStart()); @@ -3391,13 +3388,13 @@ CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) if ( IsOfType(p, ID_OPENPAR) ) { int start, end; - if (!IsOfType(p, ID_CLOSEPAR)) while (TRUE) + if (!IsOfType(p, ID_CLOSEPAR)) while (true) { start = p->GivStart(); pile = pile->TokenStack(); // garde les rsultats sur la pile if ( first ) pStack->SetStartError(start); - first = FALSE; + first = false; CBotInstr* param = CBotExpression::Compile(p, pile); end = p->GivStart(); @@ -3513,11 +3510,11 @@ CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* // excute l'appel de mthode -BOOL CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, BOOL bStep, BOOL bExtend) +bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) { CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, TRUE); // une place pour la copie de This -// if ( pile1 == EOX ) return TRUE; + CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This +// if ( pile1 == EOX ) return true; // DEBUG( "CBotInstrMethode::ExecuteVar", 0, pj ); @@ -3526,7 +3523,7 @@ BOOL CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* pre pj->SetError( TX_NULLPT, prevToken ); } - if ( pile1->IfStep() ) return FALSE; + if ( pile1->IfStep() ) return false; CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir @@ -3549,12 +3546,12 @@ BOOL CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* pre // value les paramtres // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { if ( pile2->GivState() == 0 ) { - if (!p->Execute(pile2)) return FALSE; // interrompu ici ? - if (!pile2->SetState(1)) return FALSE; // marque spciale pour reconnare les paramtres + if (!p->Execute(pile2)) return false; // interrompu ici ? + if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres } ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats @@ -3576,14 +3573,14 @@ BOOL CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* pre if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, pThis, ppVars, - pResult, pile2, GivToken())) return FALSE; // interrompu + pResult, pile2, GivToken())) return false; // interrompu if (pRes != pResult) delete pRes; pVar = NULL; // ne retourne pas une valeur par cela return pj->Return(pile2); // libre toute la pile } -void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, BOOL bMain) +void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) { if ( !bMain ) return; @@ -3604,11 +3601,11 @@ void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, BOOL bMain) // value les paramtres // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { if ( pile2->GivState() == 0 ) { - p->RestoreState(pile2, TRUE); // interrompu ici ! + p->RestoreState(pile2, true); // interrompu ici ! return; } ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs @@ -3630,13 +3627,13 @@ void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, BOOL bMain) } -BOOL CBotInstrMethode::Execute(CBotStack* &pj) +bool CBotInstrMethode::Execute(CBotStack* &pj) { CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, TRUE); // une place pour la copie de This -// if ( pile1 == EOX ) return TRUE; + CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This +// if ( pile1 == EOX ) return true; - if ( pile1->IfStep() ) return FALSE; + if ( pile1->IfStep() ) return false; CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir @@ -3656,12 +3653,12 @@ BOOL CBotInstrMethode::Execute(CBotStack* &pj) // value les paramtres // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { if ( pile2->GivState() == 0 ) { - if (!p->Execute(pile2)) return FALSE; // interrompu ici ? - if (!pile2->SetState(1)) return FALSE; // marque spciale pour reconnare les paramtres + if (!p->Execute(pile2)) return false; // interrompu ici ? + if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres } ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats @@ -3683,11 +3680,11 @@ BOOL CBotInstrMethode::Execute(CBotStack* &pj) if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, pThis, ppVars, - pResult, pile2, GivToken())) return FALSE; // interrompu + pResult, pile2, GivToken())) return false; // interrompu // met la nouvelle valeur de this la place de l'ancienne variable CBotVar* old = pile1->FindVar(m_token); - old->Copy(pThis, FALSE); + old->Copy(pThis, false); if (pRes != pResult) delete pRes; @@ -3758,7 +3755,7 @@ CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) // s'il n'y a pas de constructeur, et pas de paramtres non plus, c'est ok if ( typ == TX_UNDEFCALL && inst->m_Parameters == NULL ) typ = 0; - pVar->SetInit(TRUE); // marque l'instance comme init + pVar->SetInit(true); // marque l'instance comme init if (typ>20) { @@ -3784,12 +3781,12 @@ error: // excute une instruction "new" -BOOL CBotNew::Execute(CBotStack* &pj) +bool CBotNew::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); //pile principale -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; CBotStack* pile1 = pj->AddStack2(); //pile secondaire @@ -3834,12 +3831,12 @@ BOOL CBotNew::Execute(CBotStack* &pj) // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats if ( pile2->GivState() == 0 ) { - if (!p->Execute(pile2)) return FALSE; // interrompu ici ? + if (!p->Execute(pile2)) return false; // interrompu ici ? pile2->SetState(1); } ppVars[i++] = pile2->GivVar(); @@ -3853,7 +3850,7 @@ BOOL CBotNew::Execute(CBotStack* &pj) if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, - pResult, pile2, GivToken())) return FALSE; // interrompu + pResult, pile2, GivToken())) return false; // interrompu pThis->ConstructorSet(); // signale que le constructeur a t appel // pile->Return(pile2); // libre un bout de pile @@ -3864,7 +3861,7 @@ BOOL CBotNew::Execute(CBotStack* &pj) return pj->Return( pile1 ); // transmet en dessous } -void CBotNew::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotNew::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -3902,7 +3899,7 @@ void CBotNew::RestoreState(CBotStack* &pj, BOOL bMain) // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { pile2 = pile2->RestoreStack(); // de la place sur la pile pour les rsultats if ( pile2 == NULL ) return; @@ -3926,31 +3923,31 @@ void CBotNew::RestoreState(CBotStack* &pj, BOOL bMain) ///////////////////////////////////////////////////////////// // regarde si deux rsultats sont compatibles pour faire une opration -BOOL TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) +bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) { int t1 = type1.GivType(); int t2 = type2.GivType(); int max = (t1 > t2) ? t1 : t2; - if ( max == 99 ) return FALSE; // un rsultat est void ? + if ( max == 99 ) return false; // un rsultat est void ? // cas particulier pour les concatnation de chanes - if (op == ID_ADD && max >= CBotTypString) return TRUE; - if (op == ID_ASSADD && max >= CBotTypString) return TRUE; - if (op == ID_ASS && t1 == CBotTypString) return TRUE; + if (op == ID_ADD && max >= CBotTypString) return true; + if (op == ID_ASSADD && max >= CBotTypString) return true; + if (op == ID_ASS && t1 == CBotTypString) return true; if ( max >= CBotTypBoolean ) { if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return TRUE; + (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return TRUE; + (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return TRUE; + (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return TRUE; - if (t2 != t1) return FALSE; + (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; + if (t2 != t1) return false; if (t1 == CBotTypArrayPointer) return type1.Compare(type2); if (t1 == CBotTypPointer || t1 == CBotTypClass || @@ -3964,17 +3961,17 @@ BOOL TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) // l'opration sera refuse l'excution si le pointeur n'est pas compatible } - return TRUE; + return true; } type1.SetType(max); type2.SetType(max); - return TRUE; + return true; } // regarde si deux variables sont compatible pour un passage de paramtre -BOOL TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) +bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) { int t1 = type1.GivType(); int t2 = type2.GivType(); @@ -3984,11 +3981,11 @@ BOOL TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) int max = (t1 > t2) ? t1 : t2; - if ( max == 99 ) return FALSE; // un rsultat est void ? + if ( max == 99 ) return false; // un rsultat est void ? if ( max >= CBotTypBoolean ) { - if ( t2 != t1 ) return FALSE; + if ( t2 != t1 ) return false; if ( max == CBotTypArrayPointer ) return TypesCompatibles(type1.GivTypElem(), type2.GivTypElem()); @@ -3996,9 +3993,9 @@ BOOL TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) if ( max == CBotTypClass || max == CBotTypPointer ) return type1.GivClass() == type2.GivClass() ; - return TRUE ; + return true ; } - return TRUE; + return true; } @@ -4042,7 +4039,7 @@ size_t fRead(void *buffer, size_t length, FILE* filehandle) //////////////////////////////////////// -#if FALSE +#if false CBotString num(int n) { @@ -4058,7 +4055,7 @@ CBotString num(int n) extern void DEBUG( const char* text, int val, CBotStack* pile ) { - CBotProgram* p = pile->GivBotCall(TRUE); + CBotProgram* p = pile->GivBotCall(true); if ( !p->m_bDebugDD ) return; FILE* pf = fopen("CbotDebug.txt", "a"); diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 7e15f65..6f18be8 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -17,15 +17,15 @@ // dernire rvision : 03/10/2002 DD -#define EXTENDS TRUE +#define EXTENDS true #include "resource.h" #include "CBotDll.h" // dfinitions publiques #include "CBotToken.h" // gestion des tokens -#define STACKRUN TRUE // reprise de l'excution direct sur une routine suspendue -#define STACKMEM TRUE // prrserve la mmoire pour la pile d'excution +#define STACKRUN true // reprise de l'excution direct sur une routine suspendue +#define STACKMEM true // prrserve la mmoire pour la pile d'excution #define MAXSTACK 990 // taille du stack rserv #define EOX (CBotStack*)-1 // marqueur condition spciale @@ -61,12 +61,13 @@ class CBotRepeat; // repeat (nb) {...} //////////////////////////////////////////////////////////////////////// -// Gestion de la pile d'excution +// Management of the execution stack //////////////////////////////////////////////////////////////////////// -// en fait, en externe, la seule chose qu'il est possible de faire -// c'est de crer une instance d'une pile -// pour l'utiliser pour la routine CBotProgram::Execute(CBotStack) +// actually, externally, the only thing he can do +// this is to create an instance of a stack +// to use for routine CBotProgram :: Execute (CBotStack) + class CBotStack { @@ -90,9 +91,9 @@ private: CBotVar* m_var; // rsultat des oprations CBotVar* m_listVar; // les variables dclares ce niveau - BOOL m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) - BOOL m_bOver; // limites de la pile ? -// BOOL m_bDontDelete; // spcial, ne pas dtruire les variables au delete + bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) + bool m_bOver; // limites de la pile ? +// bool m_bDontDelete; // spcial, ne pas dtruire les variables au delete CBotProgram* m_prog; // les fonctions dfinies par user static @@ -105,7 +106,7 @@ private: void* m_pUser; CBotInstr* m_instr; // l'instruction correspondante - BOOL m_bFunc; // une entre d'une fonction ? + bool m_bFunc; // une entre d'une fonction ? CBotCall* m_call; // point de reprise dans un call extern friend class CBotTry; @@ -117,7 +118,7 @@ public: #endif CBotStack(CBotStack* ppapa); ~CBotStack(); - BOOL StackOver(); + bool StackOver(); int GivError(int& start, int& end); int GivError(); // rend le numro d'erreur retourn @@ -127,48 +128,48 @@ public: int GivType(int mode = 0); // donne le type de valeur sur le stack CBotTypResult GivTypResult(int mode = 0); // donne le type complet de valeur sur le stack -// void AddVar(CBotVar* p, BOOL bDontDelete=FALSE); // ajoute une variable locale +// void AddVar(CBotVar* p, bool bDontDelete=false); // ajoute une variable locale void AddVar(CBotVar* p); // ajoute une variable locale // void RestoreVar(CBotVar* pVar); - CBotVar* FindVar(CBotToken* &p, BOOL bUpdate = FALSE, - BOOL bModif = FALSE); // trouve une variable - CBotVar* FindVar(CBotToken& Token, BOOL bUpdate = FALSE, - BOOL bModif = FALSE); + CBotVar* FindVar(CBotToken* &p, bool bUpdate = false, + bool bModif = false); // trouve une variable + CBotVar* FindVar(CBotToken& Token, bool bUpdate = false, + bool bModif = false); CBotVar* FindVar(const char* name); - CBotVar* FindVar(long ident, BOOL bUpdate = FALSE, - BOOL bModif = FALSE); + CBotVar* FindVar(long ident, bool bUpdate = false, + bool bModif = false); - CBotVar* CopyVar(CBotToken& Token, BOOL bUpdate = FALSE); // trouve et rend une copie + CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); // trouve et rend une copie - CBotStack* AddStack(CBotInstr* instr = NULL, BOOL bBlock = FALSE); // tend le stack - CBotStack* AddStackEOX(CBotCall* instr = NULL, BOOL bBlock = FALSE); // tend le stack + CBotStack* AddStack(CBotInstr* instr = NULL, bool bBlock = false); // tend le stack + CBotStack* AddStackEOX(CBotCall* instr = NULL, bool bBlock = false); // tend le stack CBotStack* RestoreStack(CBotInstr* instr = NULL); CBotStack* RestoreStackEOX(CBotCall* instr = NULL); - CBotStack* AddStack2(BOOL bBlock = FALSE); // tend le stack - BOOL Return(CBotStack* pFils); // transmet le rsultat au dessus - BOOL ReturnKeep(CBotStack* pFils); // transmet le rsultat sans rduire la pile - BOOL BreakReturn(CBotStack* pfils, const char* name = NULL); + CBotStack* AddStack2(bool bBlock = false); // tend le stack + bool Return(CBotStack* pFils); // transmet le rsultat au dessus + bool ReturnKeep(CBotStack* pFils); // transmet le rsultat sans rduire la pile + bool BreakReturn(CBotStack* pfils, const char* name = NULL); // en cas de break ventuel - BOOL IfContinue(int state, const char* name); + bool IfContinue(int state, const char* name); // ou de "continue" - BOOL IsOk(); + bool IsOk(); - BOOL SetState(int n, int lim = -10); // slectionne un tat + bool SetState(int n, int lim = -10); // slectionne un tat int GivState(); // dans quel tat j're ? - BOOL IncState(int lim = -10); // passe l'tat suivant - BOOL IfStep(); // faire du pas pas ? - BOOL Execute(); + bool IncState(int lim = -10); // passe l'tat suivant + bool IfStep(); // faire du pas pas ? + bool Execute(); void SetVar( CBotVar* var ); void SetCopyVar( CBotVar* var ); CBotVar* GivVar(); CBotVar* GivCopyVar(); CBotVar* GivPtVar(); - BOOL GivRetVar(BOOL bRet); + bool GivRetVar(bool bRet); long GivVal(); void SetStartError(int pos); @@ -178,17 +179,17 @@ public: void SetBreak(int val, const char* name); void SetBotCall(CBotProgram* p); - CBotProgram* GivBotCall(BOOL bFirst = FALSE); + CBotProgram* GivBotCall(bool bFirst = false); void* GivPUser(); - BOOL GivBlock(); + bool GivBlock(); -// BOOL ExecuteCall(CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); - BOOL ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); +// bool ExecuteCall(CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); + bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); - BOOL SaveState(FILE* pf); - BOOL RestoreState(FILE* pf, CBotStack* &pStack); + bool SaveState(FILE* pf); + bool RestoreState(FILE* pf, CBotStack* &pStack); static void SetTimer(int n); @@ -201,7 +202,7 @@ public: // les routines inline doivent tre dclares dans le fichier .h -inline BOOL CBotStack::IsOk() +inline bool CBotStack::IsOk() { return (m_error == 0); } @@ -235,7 +236,7 @@ private: CBotVar* m_var; // rsultat des oprations - BOOL m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) + bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) CBotVar* m_listVar; static @@ -249,7 +250,7 @@ public: CBotCStack(CBotCStack* ppapa); ~CBotCStack(); - BOOL IsOk(); + bool IsOk(); int GivError(); int GivError(int& start, int& end); // rend le numro d'erreur retourn @@ -262,10 +263,10 @@ public: void AddVar(CBotVar* p); // ajoute une variable locale CBotVar* FindVar(CBotToken* &p); // trouve une variable CBotVar* FindVar(CBotToken& Token); - BOOL CheckVarLocal(CBotToken* &pToken); + bool CheckVarLocal(CBotToken* &pToken); CBotVar* CopyVar(CBotToken& Token); // trouve et rend une copie - CBotCStack* TokenStack(CBotToken* pToken = NULL, BOOL bBlock = FALSE); + CBotCStack* TokenStack(CBotToken* pToken = NULL, bool bBlock = false); CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); // transmet le rsultat au dessus CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); // transmet le rsultat au dessus @@ -285,17 +286,17 @@ public: void SetBotCall(CBotProgram* p); CBotProgram* GivBotCall(); CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); - BOOL CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); - BOOL NextToken(CBotToken* &p); + bool NextToken(CBotToken* &p); }; -extern BOOL SaveVar(FILE* pf, CBotVar* pVar); +extern bool SaveVar(FILE* pf, CBotVar* pVar); ///////////////////////////////////////////////////////////////////// -// classes dfinissant une instruction +// classes defining an instruction class CBotInstr { private: @@ -303,17 +304,17 @@ private: CBotStringArray m_labelLvl; protected: - CBotToken m_token; // conserve le token + CBotToken m_token; // keeps the token CBotString name; // debug - CBotInstr* m_next; // instructions chanes - CBotInstr* m_next2b; // seconde liste pour dfinition en chane - CBotInstr* m_next3; // troisime liste pour les indices et champs - CBotInstr* m_next3b; // ncessaire pour la dclaration des tableaux + CBotInstr* m_next; // linked command + CBotInstr* m_next2b; // second list definition chain + CBotInstr* m_next3; // third list for indices and fields + CBotInstr* m_next3b; // necessary for reporting tables /* - par exemple, le programme suivant + for example, the following program int x[]; x[1] = 4; int y[x[1]][10], z; - va gnr + is generated CBotInstrArray m_next3b-> CBotEmpty m_next-> @@ -338,31 +339,30 @@ public: virtual ~CBotInstr(); - DllExport//debug +// DllExport//debug static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); static - CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, BOOL first = TRUE); + CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first = true); virtual - BOOL Execute(CBotStack* &pj); + bool Execute(CBotStack* &pj); virtual - BOOL Execute(CBotStack* &pj, CBotVar* pVar); + bool Execute(CBotStack* &pj, CBotVar* pVar); virtual - void RestoreState(CBotStack* &pj, BOOL bMain); + void RestoreState(CBotStack* &pj, bool bMain); virtual - BOOL ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); virtual - BOOL ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep, BOOL bExtend); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); virtual - void RestoreStateVar(CBotStack* &pile, BOOL bMain); + void RestoreStateVar(CBotStack* &pile, bool bMain); virtual - BOOL CompCase(CBotStack* &pj, int val); + bool CompCase(CBotStack* &pj, int val); void SetToken(CBotToken* p); - void SetToken(CBotString* name, int start=0, int end=0); int GivTokenType(); CBotToken* GivToken(); @@ -380,9 +380,9 @@ public: static void DecLvl(); static - BOOL ChkLvl(const CBotString& label, int type); + bool ChkLvl(const CBotString& label, int type); - BOOL IsOfClass(CBotString name); + bool IsOfClass(CBotString name); }; class CBotWhile : public CBotInstr @@ -397,24 +397,34 @@ public: ~CBotWhile(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotRepeat : public CBotInstr { private: - CBotInstr* m_NbIter; // le nombre d'itration - CBotInstr* m_Block; // les instructions - CBotString m_label; // une tiquette s'il y a + /// Number of iterations + CBotInstr* m_NbIter; + + /// Instructions + CBotInstr* m_Block; + + /// Label + CBotString m_label; // une tiquette s'il y a public: - CBotRepeat(); - ~CBotRepeat(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + CBotRepeat(); + ~CBotRepeat(); + + /// Static method used for compilation + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /// Execute + bool Execute(CBotStack* &pj); + + /// Restore state + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotDo : public CBotInstr @@ -429,8 +439,8 @@ public: ~CBotDo(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotFor : public CBotInstr @@ -447,8 +457,8 @@ public: ~CBotFor(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotBreak : public CBotInstr @@ -461,8 +471,8 @@ public: ~CBotBreak(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotReturn : public CBotInstr @@ -475,8 +485,8 @@ public: ~CBotReturn(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -491,8 +501,8 @@ public: ~CBotSwitch(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -506,9 +516,9 @@ public: ~CBotCase(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); - BOOL CompCase(CBotStack* &pj, int val); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + bool CompCase(CBotStack* &pj, int val); }; class CBotCatch : public CBotInstr @@ -524,10 +534,10 @@ public: ~CBotCatch(); static CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL TestCatch(CBotStack* &pj, int val); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); - void RestoreCondState(CBotStack* &pj, BOOL bMain); + bool TestCatch(CBotStack* &pj, int val); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + void RestoreCondState(CBotStack* &pj, bool bMain); }; class CBotTry : public CBotInstr @@ -542,8 +552,8 @@ public: ~CBotTry(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotThrow : public CBotInstr @@ -556,8 +566,8 @@ public: ~CBotThrow(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -570,7 +580,7 @@ public: ~CBotStartDebugDD(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); + bool Execute(CBotStack* &pj); }; @@ -586,8 +596,8 @@ public: ~CBotIf(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -604,9 +614,9 @@ public: CBotInt(); ~CBotInt(); static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont = FALSE, BOOL noskip = FALSE); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un tableau @@ -625,8 +635,8 @@ public: ~CBotInstArray(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -643,15 +653,15 @@ public: ~CBotListArray(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - BOOL Execute(CBotStack* &pj, CBotVar* pVar); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj, CBotVar* pVar); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotEmpty : public CBotInstr { - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un boolen @@ -666,9 +676,9 @@ public: CBotBoolean(); ~CBotBoolean(); static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont = FALSE, BOOL noskip=FALSE); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -684,9 +694,9 @@ public: CBotFloat(); ~CBotFloat(); static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont = FALSE, BOOL noskip=FALSE); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un elment string @@ -701,9 +711,9 @@ public: CBotIString(); ~CBotIString(); static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, BOOL cont = FALSE, BOOL noskip=FALSE); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un elment dans une classe quelconque @@ -715,7 +725,7 @@ private: CBotClass* m_pClass; // rfrence la classe CBotInstr* m_Parameters; // les paramtres valuer pour le constructeur CBotInstr* m_expr; // la valeur mettre, s'il y a - BOOL m_hasParams; // il y a des paramtres ? + bool m_hasParams; // il y a des paramtres ? long m_nMethodeIdent; public: @@ -723,8 +733,8 @@ public: ~CBotClassInst(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = NULL); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotCondition : public CBotInstr @@ -751,11 +761,11 @@ public: ~CBotLeftExpr(); static CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pStack, CBotStack* array); + bool Execute(CBotStack* &pStack, CBotStack* array); - BOOL ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - BOOL ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep); - void RestoreStateVar(CBotStack* &pile, BOOL bMain); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + void RestoreStateVar(CBotStack* &pile, bool bMain); }; @@ -773,9 +783,9 @@ public: void SetUniqNum(int num); // static // CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - BOOL ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep, BOOL bExtend); - void RestoreStateVar(CBotStack* &pj, BOOL bMain); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; // gestion des index dans les tableaux @@ -792,9 +802,9 @@ public: ~CBotIndexExpr(); // static // CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - BOOL ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep, BOOL bExtend); - void RestoreStateVar(CBotStack* &pj, BOOL bMain); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; // une expression du genre @@ -812,8 +822,8 @@ public: ~CBotExpression(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotListExpression : public CBotInstr @@ -826,8 +836,8 @@ public: ~CBotListExpression(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotLogicExpr : public CBotInstr @@ -843,8 +853,8 @@ public: ~CBotLogicExpr(); // static // CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -882,8 +892,8 @@ public: ~CBotExprUnaire(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; // toutes les oprations 2 oprandes @@ -898,8 +908,8 @@ public: ~CBotTwoOpExpr(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = NULL); - BOOL Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -912,9 +922,9 @@ private: public: static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, BOOL bLocal = TRUE); + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); static - CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, BOOL bLocal = FALSE); + CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); }; @@ -928,9 +938,9 @@ public: CBotListInstr(); ~CBotListInstr(); static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, BOOL bLocal = TRUE); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -949,8 +959,8 @@ public: ~CBotInstrCall(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // un appel d'une mthode @@ -974,12 +984,12 @@ public: ~CBotInstrMethode(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); - BOOL Execute(CBotStack* &pj); - BOOL ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, BOOL bStep, BOOL bExtend); - void RestoreStateVar(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; -// expression donnant un nom de variable +// expression for the variable name class CBotExprVar : public CBotInstr { @@ -996,11 +1006,11 @@ public: static CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); - BOOL ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, BOOL bStep); - BOOL Execute2Var(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, BOOL bStep); - void RestoreStateVar(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + bool Execute2Var(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; class CBotPostIncExpr : public CBotInstr @@ -1014,8 +1024,8 @@ public: ~CBotPostIncExpr(); // static // CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotPreIncExpr : public CBotInstr @@ -1029,8 +1039,8 @@ public: ~CBotPreIncExpr(); // static // CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1047,8 +1057,8 @@ public: ~CBotLeftExprVar(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1062,8 +1072,8 @@ public: static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1075,8 +1085,8 @@ public: CBotExprNull(); ~CBotExprNull(); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotExprNan : public CBotInstr @@ -1087,8 +1097,8 @@ public: CBotExprNan(); ~CBotExprNan(); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotNew : public CBotInstr @@ -1105,8 +1115,8 @@ public: static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // expression reprsentant un nombre @@ -1123,8 +1133,8 @@ public: ~CBotExprNum(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1140,8 +1150,8 @@ public: ~CBotExprAlpha(); static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1166,7 +1176,7 @@ public: float GivValFloat(); CBotString GivValString(); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); + void Copy(CBotVar* pSrc, bool bName=true); void Add(CBotVar* left, CBotVar* right); // addition @@ -1176,12 +1186,12 @@ public: int Modulo(CBotVar* left, CBotVar* right); // reste de division void Power(CBotVar* left, CBotVar* right); // puissance - BOOL Lo(CBotVar* left, CBotVar* right); - BOOL Hi(CBotVar* left, CBotVar* right); - BOOL Ls(CBotVar* left, CBotVar* right); - BOOL Hs(CBotVar* left, CBotVar* right); - BOOL Eq(CBotVar* left, CBotVar* right); - BOOL Ne(CBotVar* left, CBotVar* right); + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); void XOr(CBotVar* left, CBotVar* right); void Or(CBotVar* left, CBotVar* right); @@ -1196,8 +1206,8 @@ public: void Inc(); void Dec(); - BOOL Save0State(FILE* pf); - BOOL Save1State(FILE* pf); + bool Save0State(FILE* pf); + bool Save1State(FILE* pf); }; @@ -1217,7 +1227,7 @@ public: float GivValFloat(); CBotString GivValString(); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); + void Copy(CBotVar* pSrc, bool bName=true); void Add(CBotVar* left, CBotVar* right); // addition @@ -1227,18 +1237,18 @@ public: int Modulo(CBotVar* left, CBotVar* right); // reste de division void Power(CBotVar* left, CBotVar* right); // puissance - BOOL Lo(CBotVar* left, CBotVar* right); - BOOL Hi(CBotVar* left, CBotVar* right); - BOOL Ls(CBotVar* left, CBotVar* right); - BOOL Hs(CBotVar* left, CBotVar* right); - BOOL Eq(CBotVar* left, CBotVar* right); - BOOL Ne(CBotVar* left, CBotVar* right); + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); void Neg(); void Inc(); void Dec(); - BOOL Save1State(FILE* pf); + bool Save1State(FILE* pf); }; @@ -1255,25 +1265,25 @@ public: void SetValString(const char* p); CBotString GivValString(); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); + void Copy(CBotVar* pSrc, bool bName=true); void Add(CBotVar* left, CBotVar* right); // addition - BOOL Lo(CBotVar* left, CBotVar* right); - BOOL Hi(CBotVar* left, CBotVar* right); - BOOL Ls(CBotVar* left, CBotVar* right); - BOOL Hs(CBotVar* left, CBotVar* right); - BOOL Eq(CBotVar* left, CBotVar* right); - BOOL Ne(CBotVar* left, CBotVar* right); + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - BOOL Save1State(FILE* pf); + bool Save1State(FILE* pf); }; // classe pour la gestion des boolean class CBotVarBoolean : public CBotVar { private: - BOOL m_val; // la valeur + bool m_val; // la valeur public: CBotVarBoolean( const CBotToken* name ); @@ -1285,16 +1295,16 @@ public: float GivValFloat(); CBotString GivValString(); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); + void Copy(CBotVar* pSrc, bool bName=true); void And(CBotVar* left, CBotVar* right); void Or(CBotVar* left, CBotVar* right); void XOr(CBotVar* left, CBotVar* right); void Not(); - BOOL Eq(CBotVar* left, CBotVar* right); - BOOL Ne(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - BOOL Save1State(FILE* pf); + bool Save1State(FILE* pf); }; @@ -1315,7 +1325,7 @@ private: friend class CBotVarPointer; // et le pointeur aussi int m_CptUse; // compteur d'utilisation long m_ItemIdent; // identificateur (unique) de l'instance - BOOL m_bConstructor; // set si un constructeur a t appel + bool m_bConstructor; // set si un constructeur a t appel public: CBotVarClass( const CBotToken* name, const CBotTypResult& type ); @@ -1323,19 +1333,19 @@ public: ~CBotVarClass(); // void InitCBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); + void Copy(CBotVar* pSrc, bool bName=true); void SetClass(CBotClass* pClass); //, int &nIdent); CBotClass* GivClass(); CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) CBotVar* GivItemRef(int nIdent); - CBotVar* GivItem(int n, BOOL bExtend); + CBotVar* GivItem(int n, bool bExtend); CBotVar* GivItemList(); CBotString GivValString(); - BOOL Save1State(FILE* pf); - void Maj(void* pUser, BOOL bContinue); + bool Save1State(FILE* pf); + void Maj(void* pUser, bool bContinue); void IncrementUse(); // une rfrence en plus void DecrementUse(); // une rfrence en moins @@ -1351,8 +1361,8 @@ public: // CBotVar* GivMyThis(); - BOOL Eq(CBotVar* left, CBotVar* right); - BOOL Ne(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); void ConstructorSet(); }; @@ -1371,7 +1381,7 @@ public: CBotVarPointer( const CBotToken* name, CBotTypResult& type ); ~CBotVarPointer(); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); + void Copy(CBotVar* pSrc, bool bName=true); void SetClass(CBotClass* pClass); CBotClass* GivClass(); CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) @@ -1387,11 +1397,11 @@ public: long GivIdent(); // donne le numro d'identification associ void ConstructorSet(); - BOOL Save1State(FILE* pf); - void Maj(void* pUser, BOOL bContinue); + bool Save1State(FILE* pf); + void Maj(void* pUser, bool bContinue); - BOOL Eq(CBotVar* left, CBotVar* right); - BOOL Ne(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); }; @@ -1415,37 +1425,37 @@ public: CBotVarClass* GivPointer(); - void Copy(CBotVar* pSrc, BOOL bName=TRUE); - CBotVar* GivItem(int n, BOOL bGrow=FALSE); // rend un lment selon son index numrique + void Copy(CBotVar* pSrc, bool bName=true); + CBotVar* GivItem(int n, bool bGrow=false); // rend un lment selon son index numrique // agrandi le tableau si ncessaire si bExtend // CBotVar* GivItem(const char* name); // rend un lment selon son index litral CBotVar* GivItemList(); // donne le premier lment de la liste CBotString GivValString(); // donne le contenu du tableau dans une chane - BOOL Save1State(FILE* pf); + bool Save1State(FILE* pf); }; extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); -extern BOOL TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); -extern BOOL TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ); +extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); +extern bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ); -extern BOOL WriteWord(FILE* pf, WORD w); -extern BOOL ReadWord(FILE* pf, WORD& w); -extern BOOL ReadLong(FILE* pf, long& w); -extern BOOL WriteFloat(FILE* pf, float w); -extern BOOL WriteLong(FILE* pf, long w); -extern BOOL ReadFloat(FILE* pf, float& w); -extern BOOL WriteString(FILE* pf, CBotString s); -extern BOOL ReadString(FILE* pf, CBotString& s); -extern BOOL WriteType(FILE* pf, CBotTypResult type); -extern BOOL ReadType(FILE* pf, CBotTypResult& type); +extern bool WriteWord(FILE* pf, unsigned short w); +extern bool ReadWord(FILE* pf, unsigned short& w); +extern bool ReadLong(FILE* pf, long& w); +extern bool WriteFloat(FILE* pf, float w); +extern bool WriteLong(FILE* pf, long w); +extern bool ReadFloat(FILE* pf, float& w); +extern bool WriteString(FILE* pf, CBotString s); +extern bool ReadString(FILE* pf, CBotString& s); +extern bool WriteType(FILE* pf, CBotTypResult type); +extern bool ReadType(FILE* pf, CBotTypResult& type); extern float GivNumFloat( const char* p ); -#if FALSE +#if false extern void DEBUG( const char* text, int val, CBotStack* pile ); #endif @@ -1463,36 +1473,36 @@ private: private: CBotString m_name; - BOOL (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; + bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; CBotTypResult (*m_rComp) (CBotVar* &pVar, void* pUser) ; CBotCall* m_next; public: CBotCall(const char* name, - BOOL rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); ~CBotCall(); static - BOOL AddFunction(const char* name, - BOOL rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); static CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); static - BOOL CheckCall(const char* name); + bool CheckCall(const char* name); // static // int DoCall(CBotToken* &p, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); static int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); #if STACKRUN - BOOL Run(CBotStack* pStack); + bool Run(CBotStack* pStack); static - BOOL RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); + bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); #endif CBotString GivName(); @@ -1508,7 +1518,7 @@ class CBotCallMethode { private: CBotString m_name; - BOOL (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception); + bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception); CBotTypResult (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); CBotCallMethode* m_next; @@ -1517,7 +1527,7 @@ private: public: CBotCallMethode(const char* name, - BOOL rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); ~CBotCallMethode(); @@ -1550,8 +1560,8 @@ public: ~CBotDefParam(); static CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); - BOOL Execute(CBotVar** ppVars, CBotStack* &pj); - void RestoreState(CBotStack* &pj, BOOL bMain); + bool Execute(CBotVar** ppVars, CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); void AddNext(CBotDefParam* p); int GivType(); @@ -1575,7 +1585,7 @@ private: friend class CBotCStack; // long m_nThisIdent; long m_nFuncIdent; - BOOL m_bSynchro; // mthode synchronise ? + bool m_bSynchro; // mthode synchronise ? private: CBotDefParam* m_Param; // liste des paramtres @@ -1584,8 +1594,8 @@ private: CBotToken m_retToken; // si retourne un CBotTypClass CBotTypResult m_retTyp; // type complet du rsultat - BOOL m_bPublic; // fonction publique - BOOL m_bExtern; // fonction extern + bool m_bPublic; // fonction publique + bool m_bExtern; // fonction extern CBotString m_MasterClass; // nom de la classe qu'on drive CBotProgram* m_pProg; friend class CBotProgram; @@ -1600,32 +1610,32 @@ public: CBotFunction(); ~CBotFunction(); static - CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, BOOL bLocal = TRUE); + CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, bool bLocal = true); static CBotFunction* Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass); - BOOL Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); + bool Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); void RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); void AddNext(CBotFunction* p); CBotTypResult CompileCall(const char* name, CBotVar** ppVars, long& nIdent); - CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, BOOL bPublic = TRUE); + CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic = true); int DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken); void RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack); int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass); void RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass); - BOOL CheckParam(CBotDefParam* pParam); + bool CheckParam(CBotDefParam* pParam); static void AddPublic(CBotFunction* pfunc); CBotString GivName(); CBotString GivParams(); - BOOL IsPublic(); - BOOL IsExtern(); + bool IsPublic(); + bool IsExtern(); CBotFunction* Next(); - BOOL GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); + bool GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); }; diff --git a/src/CBot/CBotAddExpr.cpp b/src/CBot/CBotAddExpr.cpp index ad87880..d94946e 100644 --- a/src/CBot/CBotAddExpr.cpp +++ b/src/CBot/CBotAddExpr.cpp @@ -93,7 +93,7 @@ CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack) // fait l'opration d'addition ou de soustraction -BOOL CBotAddExpr::Execute(CBotStack* &pStack) +bool CBotAddExpr::Execute(CBotStack* &pStack) { CBotStack* pStk1 = pStack->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 7c097af..c99185f 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -21,7 +21,7 @@ CBotClass* CBotClass::m_ExClass = NULL; -CBotClass::CBotClass(const char* name, CBotClass* pPapa, BOOL bIntrinsic) +CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic) { m_pParent = pPapa; m_name = name; @@ -30,7 +30,7 @@ CBotClass::CBotClass(const char* name, CBotClass* pPapa, BOOL bIntrinsic) m_pCalls = NULL; m_pMethod = NULL; m_rMaj = NULL; - m_IsDef = TRUE; + m_IsDef = true; m_bIntrinsic= bIntrinsic; m_cptLock = 0; m_cptOne = 0; @@ -86,7 +86,7 @@ void CBotClass::Purge() m_pCalls = NULL; delete m_pMethod; m_pMethod = NULL; - m_IsDef = FALSE; + m_IsDef = false; m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; @@ -94,7 +94,7 @@ void CBotClass::Purge() m_next = NULL; // n'appartient plus cette chane } -BOOL CBotClass::Lock(CBotProgram* p) +bool CBotClass::Lock(CBotProgram* p) { int i = m_cptLock++; @@ -102,13 +102,13 @@ BOOL CBotClass::Lock(CBotProgram* p) { m_cptOne = 1; m_ProgInLock[0] = p; - return TRUE; + return true; } if ( p == m_ProgInLock[0] ) { m_cptOne++; m_cptLock--; // a dj t compt - return TRUE; + return true; } for ( int j = 1 ; j <= i ; j++) @@ -116,7 +116,7 @@ BOOL CBotClass::Lock(CBotProgram* p) if ( p == m_ProgInLock[j] ) { m_cptLock--; - return FALSE; // dj en attente + return false; // dj en attente } } @@ -127,7 +127,7 @@ BOOL CBotClass::Lock(CBotProgram* p) else m_cptLock--; - return FALSE; + return false; } void CBotClass::Unlock() @@ -170,7 +170,7 @@ void CBotClass::FreeLock(CBotProgram* p) -BOOL CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) +bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) { CBotToken token(name, CBotString()); CBotClass* pClass = type.GivClass(); @@ -194,14 +194,14 @@ BOOL CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) } -BOOL CBotClass::AddItem(CBotVar* pVar) +bool CBotClass::AddItem(CBotVar* pVar) { pVar->SetUniqNum(++m_nbVar); if ( m_pVar == NULL ) m_pVar = pVar; else m_pVar->AddNext(pVar); - return TRUE; + return true; } void CBotClass::AddNext(CBotClass* pClass) @@ -223,15 +223,15 @@ CBotClass* CBotClass::GivParent() return m_pParent; } -BOOL CBotClass::IsChildOf(CBotClass* pClass) +bool CBotClass::IsChildOf(CBotClass* pClass) { CBotClass* p = this; while ( p != NULL ) { - if ( p == pClass ) return TRUE; + if ( p == pClass ) return true; p = p->m_pParent; } - return FALSE; + return false; } @@ -266,7 +266,7 @@ CBotVar* CBotClass::GivItemRef(int nIdent) return NULL; } -BOOL CBotClass::IsIntrinsic() +bool CBotClass::IsIntrinsic() { return m_bIntrinsic; } @@ -289,8 +289,8 @@ CBotClass* CBotClass::Find(const char* name) return NULL; } -BOOL CBotClass::AddFunction(const char* name, - BOOL rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), +bool CBotClass::AddFunction(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) { // mmorise les pointeurs aux deux fonctions @@ -315,13 +315,13 @@ BOOL CBotClass::AddFunction(const char* name, if (m_pCalls == NULL) m_pCalls = p; else m_pCalls->AddNext(p); // ajoute la liste - return TRUE; + return true; } -BOOL CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ) +bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ) { m_rMaj = rMaj; - return TRUE; + return true; } // compile une mthode associe une instance de classe @@ -348,7 +348,7 @@ CBotTypResult CBotClass::CompileMethode(const char* name, // excute une mthode -BOOL CBotClass::ExecuteMethode(long& nIdent, const char* name, +bool CBotClass::ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken) @@ -371,77 +371,77 @@ void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, -BOOL CBotClass::SaveStaticState(FILE* pf) +bool CBotClass::SaveStaticState(FILE* pf) { - if (!WriteWord( pf, CBOTVERSION*2)) return FALSE; + if (!WriteWord( pf, CBOTVERSION*2)) return false; // sauve l'tat des variables statiques dans les classes CBotClass* p = m_ExClass; while ( p != NULL ) { - if (!WriteWord( pf, 1)) return FALSE; + if (!WriteWord( pf, 1)) return false; // enregistre le nom de la classe - if (!WriteString( pf, p->GivName() )) return FALSE; + if (!WriteString( pf, p->GivName() )) return false; CBotVar* pv = p->GivVar(); while( pv != NULL ) { if ( pv->IsStatic() ) { - if (!WriteWord( pf, 1)) return FALSE; - if (!WriteString( pf, pv->GivName() )) return FALSE; + if (!WriteWord( pf, 1)) return false; + if (!WriteString( pf, pv->GivName() )) return false; - if ( !pv->Save0State(pf)) return FALSE; // entte commune - if ( !pv->Save1State(pf) ) return FALSE; // sauve selon la classe fille - if ( !WriteWord( pf, 0)) return FALSE; + if ( !pv->Save0State(pf)) return false; // entte commune + if ( !pv->Save1State(pf) ) return false; // sauve selon la classe fille + if ( !WriteWord( pf, 0)) return false; } pv = pv->GivNext(); } - if (!WriteWord( pf, 0)) return FALSE; + if (!WriteWord( pf, 0)) return false; p = p->m_ExNext; } - if (!WriteWord( pf, 0)) return FALSE; - return TRUE; + if (!WriteWord( pf, 0)) return false; + return true; } -BOOL CBotClass::RestoreStaticState(FILE* pf) +bool CBotClass::RestoreStaticState(FILE* pf) { CBotString ClassName, VarName; CBotClass* pClass; - WORD w; + unsigned short w; - if (!ReadWord( pf, w )) return FALSE; - if ( w != CBOTVERSION*2 ) return FALSE; + if (!ReadWord( pf, w )) return false; + if ( w != CBOTVERSION*2 ) return false; - while (TRUE) + while (true) { - if (!ReadWord( pf, w )) return FALSE; - if ( w == 0 ) return TRUE; + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) return true; - if (!ReadString( pf, ClassName )) return FALSE; + if (!ReadString( pf, ClassName )) return false; pClass = Find(ClassName); - while (TRUE) + while (true) { - if (!ReadWord( pf, w )) return FALSE; + if (!ReadWord( pf, w )) return false; if ( w == 0 ) break; CBotVar* pVar = NULL; CBotVar* pv = NULL; - if (!ReadString( pf, VarName )) return FALSE; + if (!ReadString( pf, VarName )) return false; if ( pClass != NULL ) pVar = pClass->GivItem(VarName); - if (!CBotVar::RestoreState(pf, pv)) return FALSE; // la variable temp + if (!CBotVar::RestoreState(pf, pv)) return false; // la variable temp if ( pVar != NULL ) pVar->Copy(pv); delete pv; } } - return TRUE; + return true; } @@ -453,7 +453,7 @@ CBotClassInst::CBotClassInst() m_var = NULL; m_Parameters = NULL; m_expr = NULL; - m_hasParams = FALSE; + m_hasParams = false; m_nMethodeIdent = 0; name = "CBotClassInst"; } @@ -484,18 +484,17 @@ CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* p = p->GivNext(); } - BOOL bIntrinsic = pClass->IsIntrinsic(); - CBotTypResult - type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, - pClass ); + bool bIntrinsic = pClass->IsIntrinsic(); + CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); CBotClassInst* inst = (CBotClassInst*)CompileArray(p, pStack, type); if ( inst != NULL || !pStack->IsOk() ) return inst; CBotCStack* pStk = pStack->TokenStack(); inst = new CBotClassInst(); - - inst->SetToken(&pClass->GivName(), p->GivStart(), p->GivEnd()); + /// \TODO Need to be revised and fixed after adding unit tests + CBotToken token(pClass->GivName(), CBotString(), p->GivStart(), p->GivEnd()); + inst->SetToken(&token); CBotToken* vartoken = p; if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) @@ -601,7 +600,7 @@ CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* var->SetPointer( pvar ); // var dj dclare pointe l'instance delete pvar; // supprime le second pointeur } - var->SetInit(TRUE); // marque le pointeur comme init + var->SetInit(true); // marque le pointeur comme init } else if (inst->m_hasParams) { @@ -640,17 +639,17 @@ error: // dclaration de l'instance d'une classe, par exemple: // CPoint A, B; -BOOL CBotClassInst::Execute(CBotStack* &pj) +bool CBotClassInst::Execute(CBotStack* &pj) { CBotVar* pThis = NULL; CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; CBotToken* pt = &m_token; CBotClass* pClass = CBotClass::Find(pt); - BOOL bIntrincic = pClass->IsIntrinsic(); + bool bIntrincic = pClass->IsIntrinsic(); // cre la variable de type pointeur l'objet @@ -682,7 +681,7 @@ BOOL CBotClassInst::Execute(CBotStack* &pj) if ( m_expr != NULL ) { // value l'expression pour l'assignation - if (!m_expr->Execute(pile)) return FALSE; + if (!m_expr->Execute(pile)) return false; if ( bIntrincic ) { @@ -692,7 +691,7 @@ BOOL CBotClassInst::Execute(CBotStack* &pj) pile->SetError(TX_NULLPT, &m_token); return pj->Return(pile); } - pThis->Copy(pile->GivVar(), FALSE); + pThis->Copy(pile->GivVar(), false); } else { @@ -700,7 +699,7 @@ BOOL CBotClassInst::Execute(CBotStack* &pj) pInstance = ((CBotVarPointer*)pile->GivVar())->GivPointer(); // valeur pour l'assignation pThis->SetPointer(pInstance); } - pThis->SetInit(TRUE); + pThis->SetInit(true); } else if ( m_hasParams ) @@ -732,12 +731,12 @@ BOOL CBotClassInst::Execute(CBotStack* &pj) // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats if ( pile2->GivState() == 0 ) { - if (!p->Execute(pile2)) return FALSE; // interrompu ici ? + if (!p->Execute(pile2)) return false; // interrompu ici ? pile2->SetState(1); } ppVars[i++] = pile2->GivVar(); @@ -751,9 +750,9 @@ BOOL CBotClassInst::Execute(CBotStack* &pj) if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, - pResult, pile2, GivToken())) return FALSE; // interrompu + pResult, pile2, GivToken())) return false; // interrompu - pThis->SetInit(TRUE); + pThis->SetInit(true); pThis->ConstructorSet(); // signale que le constructeur a t appel pile->Return(pile2); // libre un bout de pile @@ -766,17 +765,17 @@ BOOL CBotClassInst::Execute(CBotStack* &pj) pile->SetState(3); // fini cette partie } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; if ( m_next2b != NULL && - !m_next2b->Execute(pile)) return FALSE; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) return pj->Return( pile ); // transmet en dessous } -void CBotClassInst::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) { CBotVar* pThis = NULL; @@ -793,7 +792,7 @@ void CBotClassInst::RestoreState(CBotStack* &pj, BOOL bMain) CBotToken* pt = &m_token; CBotClass* pClass = CBotClass::Find(pt); - BOOL bIntrincic = pClass->IsIntrinsic(); + bool bIntrincic = pClass->IsIntrinsic(); if ( bMain && pile->GivState()<3) { @@ -827,7 +826,7 @@ void CBotClassInst::RestoreState(CBotStack* &pj, BOOL bMain) // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { pile2 = pile2->RestoreStack(); // de la place sur la pile pour les rsultats if ( pile2 == NULL ) return; @@ -858,11 +857,11 @@ void CBotClassInst::RestoreState(CBotStack* &pj, BOOL bMain) // test si un nom de procdure est dj dfini quelque part -BOOL CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) +bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { CBotString name = pToken->GivString(); - if ( CBotCall::CheckCall(name) ) return TRUE; + if ( CBotCall::CheckCall(name) ) return true; CBotFunction* pp = m_pMethod; while ( pp != NULL ) @@ -871,11 +870,11 @@ BOOL CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { // les paramtres sont-ils exactement les mmes ? if ( pp->CheckParam( pParam ) ) - return TRUE; + return true; } pp = pp->Next(); } - return FALSE; + return false; } diff --git a/src/CBot/CBotCompExpr.cpp b/src/CBot/CBotCompExpr.cpp index 41e7e05..0f296d5 100644 --- a/src/CBot/CBotCompExpr.cpp +++ b/src/CBot/CBotCompExpr.cpp @@ -83,7 +83,7 @@ CBotInstr* CBotCompExpr::Compile(CBotToken* &p, CBotCStack* pStack) // fait l'opration -BOOL CBotCompExpr::Execute(CBotStack* &pStack) +bool CBotCompExpr::Execute(CBotStack* &pStack) { CBotStack* pStk1 = pStack->AddStack(this); // if ( pStk1 == EOX ) return TRUE; diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 7fa9472..f0d7fef 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -19,10 +19,10 @@ //#include "stdafx.h" -#include +// #include #include -#define DllExport __declspec( dllexport ) +// #define DllExport __declspec( dllexport ) #define CBOTVERSION 104 @@ -93,62 +93,62 @@ private: public: // divers constructeurs selon les besoins - DllExport + //DllExport CBotTypResult(int type); // pour les types simples (CBotTypInt CBotTypString) - DllExport + //DllExport CBotTypResult(int type, const char* name); // pour les types pointeur et classe intrinsic - DllExport + //DllExport CBotTypResult(int type, CBotClass* pClass); // idem partir de l'instance d'une classe - DllExport + //DllExport CBotTypResult(int type, CBotTypResult elem); // pour les tableaux de variables - DllExport + //DllExport CBotTypResult(const CBotTypResult& typ); // pour les assignations - DllExport + //DllExport CBotTypResult(); // pour par dfaut - DllExport + //DllExport ~CBotTypResult(); - DllExport + //DllExport int GivType(int mode = 0) const; // rend le type CBotTyp* du rsultat void SetType(int n); // modifie le type - DllExport + //DllExport CBotClass* GivClass() const; // rend le pointeur la classe (pour les CBotTypClass, CBotTypPointer) - DllExport + //DllExport int GivLimite() const; // rend la taille limite du tableau (CBotTypArray) - DllExport + //DllExport void SetLimite(int n); // fixe une limite au tableau void SetArray(int* max ); // idem avec une liste de dimension (tableaux de tableaux) - DllExport + //DllExport CBotTypResult& GivTypElem() const; // rend le type des lments du tableau (CBotTypArray) - DllExport - BOOL Compare(const CBotTypResult& typ) const; + //DllExport + bool Compare(const CBotTypResult& typ) const; // compare si les types sont compatibles - DllExport - BOOL Eq(int type) const; + //DllExport + bool Eq(int type) const; // compare le type - DllExport + //DllExport CBotTypResult& operator=(const CBotTypResult& src); // copie un type complet dans un autre @@ -259,102 +259,102 @@ class CBotString private: char* m_ptr; // pointeur la chaine int m_lg; // longueur de la chaine - static - HINSTANCE m_hInstance; + // static + // HINSTANCE m_hInstance; public: - DllExport + //DllExport CBotString(); - DllExport + //DllExport CBotString(const char* p); - DllExport + //DllExport CBotString(const CBotString& p); - DllExport + //DllExport ~CBotString(); - DllExport + //DllExport void Empty(); - DllExport - BOOL IsEmpty() const; - DllExport + //DllExport + bool IsEmpty() const; + //DllExport int GivLength(); - DllExport + //DllExport int Find(const char c); - DllExport - int Find(LPCTSTR lpsz); - DllExport + //DllExport + int Find(const char* lpsz); + //DllExport int ReverseFind(const char c); - DllExport - int ReverseFind(LPCTSTR lpsz); - DllExport - BOOL LoadString(UINT id); - DllExport + //DllExport + int ReverseFind(const char* lpsz); + //DllExport + bool LoadString(unsigned int id); + //DllExport CBotString Mid(int nFirst, int nCount) const; - DllExport + //DllExport CBotString Mid(int nFirst) const; - DllExport + //DllExport CBotString Left(int nCount) const; - DllExport + //DllExport CBotString Right(int nCount) const; - DllExport + //DllExport const CBotString& operator=(const CBotString& stringSrc); - DllExport + //DllExport const CBotString& operator=(const char ch); - DllExport + //DllExport const CBotString& operator=(const char* pString); - DllExport + //DllExport const CBotString& operator+(const CBotString& str); - DllExport + //DllExport friend CBotString - operator+(const CBotString& string, LPCTSTR lpsz); + operator+(const CBotString& string, const char* lpsz); - DllExport + //DllExport const CBotString& operator+=(const char ch); - DllExport + //DllExport const CBotString& operator+=(const CBotString& str); - DllExport - BOOL operator==(const CBotString& str); - DllExport - BOOL operator==(const char* p); - DllExport - BOOL operator!=(const CBotString& str); - DllExport - BOOL operator!=(const char* p); - DllExport - BOOL operator>(const CBotString& str); - DllExport - BOOL operator>(const char* p); - DllExport - BOOL operator>=(const CBotString& str); - DllExport - BOOL operator>=(const char* p); - DllExport - BOOL operator<(const CBotString& str); - DllExport - BOOL operator<(const char* p); - DllExport - BOOL operator<=(const CBotString& str); - DllExport - BOOL operator<=(const char* p); - - DllExport - operator LPCTSTR() const; // as a C string - - int Compare(LPCTSTR lpsz) const; - - DllExport + //DllExport + bool operator==(const CBotString& str); + //DllExport + bool operator==(const char* p); + //DllExport + bool operator!=(const CBotString& str); + //DllExport + bool operator!=(const char* p); + //DllExport + bool operator>(const CBotString& str); + //DllExport + bool operator>(const char* p); + //DllExport + bool operator>=(const CBotString& str); + //DllExport + bool operator>=(const char* p); + //DllExport + bool operator<(const CBotString& str); + //DllExport + bool operator<(const char* p); + //DllExport + bool operator<=(const CBotString& str); + //DllExport + bool operator<=(const char* p); + + //DllExport + operator const char*() const; // as a C string + + int Compare(const char* lpsz) const; + + //DllExport CBotString Mid(int start, int lg=-1); - DllExport + //DllExport void MakeUpper(); - DllExport + //DllExport void MakeLower(); }; @@ -369,20 +369,20 @@ private: CBotString* m_pData; // ^aux donnes public: - DllExport + //DllExport CBotStringArray(); - DllExport + //DllExport ~CBotStringArray(); - DllExport + //DllExport void SetSize(int nb); - DllExport + //DllExport int GivSize(); - DllExport + //DllExport void Add(const CBotString& str); - DllExport + //DllExport CBotString& operator[](int nIndex); - DllExport + //DllExport CBotString& ElementAt(int nIndex); }; @@ -418,87 +418,87 @@ private: public: static CBotString m_DebugVarStr; // a fin de debug - BOOL m_bDebugDD; // idem dclanchable par robot + bool m_bDebugDD; // idem dclanchable par robot - BOOL m_bCompileClass; + bool m_bCompileClass; public: - DllExport + //DllExport static void Init(); // initialise le module (dfini les mots clefs pour les erreurs) // doit tre fait une fois (et une seule) au tout dbut - DllExport + //DllExport static void Free(); // libre les zones mmoires statiques - DllExport + //DllExport static int GivVersion(); // donne la version de la librairie CBOT - DllExport + //DllExport CBotProgram(); - DllExport + //DllExport CBotProgram(CBotVar* pInstance); - DllExport + //DllExport ~CBotProgram(); - DllExport - BOOL Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); + //DllExport + bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); // compile le programme donn en texte - // retourne FALSE s'il y a une erreur la compilation + // retourne false s'il y a une erreur la compilation // voir GetCompileError() pour rcuprer l'erreur // ListFonctions retourne le nom des fonctions dclares extern // pUser permet de passer un pointeur pour les routines dfinies par AddFunction - DllExport + //DllExport void SetIdent(long n); // associe un identificateur avec l'instance CBotProgram - DllExport + //DllExport long GivIdent(); // redonne l'identificateur - DllExport + //DllExport int GivError(); - DllExport - BOOL GetError(int& code, int& start, int& end); - DllExport - BOOL GetError(int& code, int& start, int& end, CBotProgram* &pProg); - // si TRUE + //DllExport + bool GetError(int& code, int& start, int& end); + //DllExport + bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); + // si true // donne l'erreur trouve la compilation // ou l'excution // start et end dlimite le bloc o se trouve l'erreur // pProg permet de savoir dans quel "module" s'est produite l'erreur d'excution - DllExport + //DllExport static CBotString GivErrorText(int code); - DllExport - BOOL Start(const char* name); + //DllExport + bool Start(const char* name); // dfinie quelle fonction doit tre excute - // retourne FALSE si la fontion name n'est pas trouve + // retourne false si la fontion name n'est pas trouve // le programme ne fait rien, il faut appeller Run() pour cela - DllExport - BOOL Run(void* pUser = NULL, int timer = -1); + //DllExport + bool Run(void* pUser = NULL, int timer = -1); // excute le programme - // retourne FALSE si le programme a t suspendu - // retourne TRUE si le programme s'est termin avec ou sans erreur + // retourne false si le programme a t suspendu + // retourne true si le programme s'est termin avec ou sans erreur // timer = 0 permet de faire une avance pas pas - DllExport - BOOL GetRunPos(const char* &FunctionName, int &start, int &end); + //DllExport + bool GetRunPos(const char* &FunctionName, int &start, int &end); // donne la position dans le programme en excution - // retourne FALSE si on n'est pas en excution (programme termin) + // retourne false si on n'est pas en excution (programme termin) // FunctionName est un pointeur rendu sur le nom de la fonction // start et end la position dans le texte du token en traitement - DllExport + //DllExport CBotVar* GivStackVars(const char* &FunctionName, int level); // permet d'obtenir le pointeur aux variables sur la pile d'excution // level est un paramtre d'entre, 0 pour le dernier niveau, -1, -2, etc pour les autres niveau @@ -507,41 +507,41 @@ public: // FunctionName donne le nom de la fonction o se trouvent ces variables // FunctionName == NULL signifiant qu'on est plus dans le programme (selon level) - DllExport + //DllExport void Stop(); // arrte l'excution du programme // quitte donc le mode "suspendu" - DllExport + //DllExport static void SetTimer(int n); // dfini le nombre de pas (parties d'instructions) faire - // dans Run() avant de rendre la main "FALSE" + // dans Run() avant de rendre la main "false" - DllExport + //DllExport static - BOOL AddFunction(const char* name, - BOOL rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); // cet appel permet d'ajouter de manire externe (**) // une nouvelle fonction utilisable par le programme CBot - DllExport + //DllExport static - BOOL DefineNum(const char* name, long val); + bool DefineNum(const char* name, long val); - DllExport - BOOL SaveState(FILE* pf); + //DllExport + bool SaveState(FILE* pf); // sauvegarde l'tat d'excution dans le fichier // le fichier doit avoir t ouvert avec l'appel fopen de cette dll // sinon le systme plante - DllExport - BOOL RestoreState(FILE* pf); + //DllExport + bool RestoreState(FILE* pf); // rtablie l'tat de l'excution depuis le fichier // le programme compil doit videmment tre identique - DllExport - BOOL GetPosition(const char* name, int& start, int& stop, + //DllExport + bool GetPosition(const char* name, int& start, int& stop, CBotGet modestart = GetPosExtern, CBotGet modestop = GetPosBloc); // donne la position d'une routine dans le texte d'origine @@ -555,13 +555,13 @@ public: /////////////////////////////////////////////////////////////////////////////// // routines pour la gestion d'un fichier (FILE*) - DllExport + //DllExport FILE* fOpen(const char* name, const char* mode); - DllExport + //DllExport int fClose(FILE* filehandle); - DllExport + //DllExport size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); - DllExport + //DllExport size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle); @@ -594,7 +594,7 @@ int cMoyenne(CBotVar* &pVar, CBotString& ClassName) } -BOOL rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) { float total = 0; int nb = 0; @@ -606,7 +606,7 @@ BOOL rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) } pResult->SetValFloat(total/nb); // retourne la valeur moyenne - return TRUE; // opration totalement termine + return true; // opration totalement termine } #endif @@ -645,7 +645,7 @@ protected: int m_binit; // pas initialise ? CBotVarClass* m_pMyThis; // ^lment this correspondant void* m_pUserPtr; // ^donnes user s'il y a lieu - BOOL m_bStatic; // lment static (dans une classe) + bool m_bStatic; // lment static (dans une classe) int m_mPrivate; // lment public, protected ou private ? CBotInstr* m_InitExpr; // expression pour le contenu initial @@ -663,17 +663,17 @@ public: virtual ~CBotVar( ); // destructeur -/* DllExport +/* //DllExport static CBotVar* Create( const char* name, int type, const char* ClassName = NULL); // cre une variable selon son type,*/ - DllExport + //DllExport static CBotVar* Create( const char* name, CBotTypResult type); // idem partir du type complet - DllExport + //DllExport static CBotVar* Create( const char* name, CBotClass* pClass); // idem pour une instance d'une classe connue @@ -690,51 +690,51 @@ virtual ~CBotVar( ); // destructeur CBotVar* Create( CBotVar* pVar ); - DllExport + //DllExport void SetUserPtr(void* pUser); // associe un pointeur utilisateur une instance - DllExport + //DllExport virtual void SetIdent(long UniqId); // associe un identificateur unique une instance // ( c'est l'utilisateur de s'assurer que l'id est unique) - DllExport + //DllExport void* GivUserPtr(); // rend le pointeur associ la variable - DllExport + //DllExport CBotString GivName(); // le nom de la variable, s'il est connu //////////////////////////////////////////////////////////////////////////////////// void SetName(const char* name); // change le nom de la variable - DllExport + //DllExport int GivType(int mode = 0); // rend le type de base (int) de la variable //////////////////////////////////////////////////////////////////////////////////////// - DllExport + //DllExport CBotTypResult GivTypResult(int mode = 0); // rend le type complet de la variable CBotToken* GivToken(); void SetType(CBotTypResult& type); - DllExport + //DllExport void SetInit(int bInit); // met la variable dans l'tat IS_UNDEF, IS_DEF, IS_NAN - DllExport + //DllExport int GivInit(); // donne l'tat de la variable - DllExport - void SetStatic(BOOL bStatic); - DllExport - BOOL IsStatic(); + //DllExport + void SetStatic(bool bStatic); + //DllExport + bool IsStatic(); - DllExport + //DllExport void SetPrivate(int mPrivate); - DllExport - BOOL IsPrivate(int mode = PR_PROTECT); - DllExport + //DllExport + bool IsPrivate(int mode = PR_PROTECT); + //DllExport int GivPrivate(); virtual @@ -742,55 +742,55 @@ virtual ~CBotVar( ); // destructeur void SetVal(CBotVar* var); // remprend une valeur - DllExport + //DllExport virtual CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) virtual CBotVar* GivItemRef(int nIdent); // idem partir du n ref - DllExport + //DllExport virtual - CBotVar* GivItem(int row, BOOL bGrow = FALSE); + CBotVar* GivItem(int row, bool bGrow = false); - DllExport + //DllExport virtual CBotVar* GivItemList(); // donne la liste des lments - DllExport + //DllExport CBotVar* GivStaticVar(); // rend le pointeur la variable si elle est statique - DllExport - BOOL IsElemOfClass(const char* name); + //DllExport + bool IsElemOfClass(const char* name); // dit si l'lment appartient la classe "name" - // rend TRUE si l'objet est d'une classe fille + // rend true si l'objet est d'une classe fille - DllExport + //DllExport CBotVar* GivNext(); // prochaine variable dans la liste (paramtres) //////////////////////////////////////////////////////////////////////////////////////////// void AddNext(CBotVar* pVar); // ajoute dans une liste virtual - void Copy(CBotVar* pSrc, BOOL bName = TRUE); // fait une copie de la variable + void Copy(CBotVar* pSrc, bool bName = true); // fait une copie de la variable - DllExport + //DllExport virtual void SetValInt(int val, const char* name = NULL); // initialise avec une valeur entire (#) ///////////////////////////////////////////////////////////////////////////////// - DllExport + //DllExport virtual void SetValFloat(float val); // initialise avec une valeur relle (#) //////////////////////////////////////////////////////////////////////////////// - DllExport + //DllExport virtual void SetValString(const char* p);// initialise avec une valeur chane (#) //////////////////////////////////////////////////////////////////////////////// - DllExport + //DllExport virtual int GivValInt(); // demande la valeur entire (#) //////////////////////////////////////////////////////////////////////// - DllExport + //DllExport virtual float GivValFloat(); // demande la valeur relle (#) /////////////////////////////////////////////////////////////////////// @@ -814,12 +814,12 @@ virtual ~CBotVar( ); // destructeur virtual int Modulo(CBotVar* left, CBotVar* right); // reste de division virtual void Power(CBotVar* left, CBotVar* right); // puissance - virtual BOOL Lo(CBotVar* left, CBotVar* right); - virtual BOOL Hi(CBotVar* left, CBotVar* right); - virtual BOOL Ls(CBotVar* left, CBotVar* right); - virtual BOOL Hs(CBotVar* left, CBotVar* right); - virtual BOOL Eq(CBotVar* left, CBotVar* right); - virtual BOOL Ne(CBotVar* left, CBotVar* right); + virtual bool Lo(CBotVar* left, CBotVar* right); + virtual bool Hi(CBotVar* left, CBotVar* right); + virtual bool Ls(CBotVar* left, CBotVar* right); + virtual bool Hs(CBotVar* left, CBotVar* right); + virtual bool Eq(CBotVar* left, CBotVar* right); + virtual bool Ne(CBotVar* left, CBotVar* right); virtual void And(CBotVar* left, CBotVar* right); virtual void Or(CBotVar* left, CBotVar* right); @@ -834,19 +834,19 @@ virtual ~CBotVar( ); // destructeur virtual void Dec(); - virtual BOOL Save0State(FILE* pf); - virtual BOOL Save1State(FILE* pf); - static BOOL RestoreState(FILE* pf, CBotVar* &pVar); + virtual bool Save0State(FILE* pf); + virtual bool Save1State(FILE* pf); + static bool RestoreState(FILE* pf, CBotVar* &pVar); - DllExport + //DllExport void debug(); // virtual // CBotVar* GivMyThis(); - DllExport + //DllExport virtual - void Maj(void* pUser = NULL, BOOL bContinue = TRUE); + void Maj(void* pUser = NULL, bool bContinue = true); void SetUniqNum(long n); long GivUniqNum(); @@ -888,7 +888,7 @@ private: CBotString m_name; // nom de cette classe-ci int m_nbVar; // nombre de variables dans la chane CBotVar* m_pVar; // contenu de la classe - BOOL m_bIntrinsic; // classe intrinsque + bool m_bIntrinsic; // classe intrinsque CBotClass* m_next; // chaine les classe CBotCallMethode* m_pCalls; // liste des mthodes dfinie en externe CBotFunction* m_pMethod; // liste des mthodes compiles @@ -899,37 +899,37 @@ private: CBotProgram* m_ProgInLock[5];// processus en attente pour synchro public: - BOOL m_IsDef; // marque si est dfinie ou pas encore + bool m_IsDef; // marque si est dfinie ou pas encore - DllExport + //DllExport CBotClass( const char* name, - CBotClass* pParent, BOOL bIntrinsic = FALSE ); // constructeur + CBotClass* pParent, bool bIntrinsic = false ); // constructeur // Ds qu'une classe est cre, elle est connue // partout dans CBot // le mode intrinsic donne une classe qui n'est pas gre par des pointeurs - DllExport + //DllExport ~CBotClass( ); // destructeur - DllExport - BOOL AddFunction(const char* name, - BOOL rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + //DllExport + bool AddFunction(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); // cet appel permet d'ajouter de manire externe (**) // une nouvelle mthode utilisable par les objets de cette classe - DllExport - BOOL AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); + //DllExport + bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); // dfini la routine qui sera appelle pour mettre jour les lements de la classe - DllExport - BOOL AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); + //DllExport + bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); // ajoute un lment la classe -// DllExport -// BOOL AddItem(CBotString name, CBotClass* pClass); +// //DllExport +// bool AddItem(CBotString name, CBotClass* pClass); // idem pour des lments appartenant pClass - DllExport - BOOL AddItem(CBotVar* pVar); + //DllExport + bool AddItem(CBotVar* pVar); // idem en passant le pointeur une instance d'une variable // l'objet est pris tel quel, il ne faut donc pas le dtruire @@ -938,20 +938,20 @@ public: // idem en donnant un lment de type CBotVar void AddNext(CBotClass* pClass); - DllExport + //DllExport CBotString GivName(); // rend le nom de la classe - DllExport + //DllExport CBotClass* GivParent(); // donne la classe pre (ou NULL) // dit si une classe est drive (Extends) d'une autre - // rend TRUE aussi si les classes sont identiques - DllExport - BOOL IsChildOf(CBotClass* pClass); + // rend true aussi si les classes sont identiques + //DllExport + bool IsChildOf(CBotClass* pClass); static CBotClass* Find(CBotToken* &pToken); // trouve une classe d'aprs son nom - DllExport + //DllExport static CBotClass* Find(const char* name); @@ -962,7 +962,7 @@ public: CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, CBotCStack* pStack, long& nIdent); - BOOL ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); + bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); // compile une classe dclare par l'utilisateur @@ -971,27 +971,27 @@ public: static CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack); - BOOL CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond); + bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond); - BOOL IsIntrinsic(); + bool IsIntrinsic(); void Purge(); static void Free(); - DllExport + //DllExport static - BOOL SaveStaticState(FILE* pf); + bool SaveStaticState(FILE* pf); - DllExport + //DllExport static - BOOL RestoreStaticState(FILE* pf); + bool RestoreStaticState(FILE* pf); - BOOL Lock(CBotProgram* p); + bool Lock(CBotProgram* p); void Unlock(); static void FreeLock(CBotProgram* p); - BOOL CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); }; @@ -1041,7 +1041,7 @@ private: static int GivKeyWords(const char* w); // est-ce un mot clef ? static - BOOL GivKeyDefNum(const char* w, CBotToken* &token); + bool GivKeyDefNum(const char* w, CBotToken* &token); static void LoadKeyWords(); // fait la liste des mots clefs @@ -1054,37 +1054,37 @@ public: // constructeur ~CBotToken(); // destructeur - DllExport + //DllExport int GivType(); // rend le type du token - DllExport + //DllExport CBotString& GivString(); // rend la chaine correspondant ce token - DllExport + //DllExport CBotString& GivSep(); // rend le sparateur suivant le token - DllExport + //DllExport int GivStart(); // position du dbut dans le texte - DllExport + //DllExport int GivEnd(); // position de fin dans le texte - DllExport + //DllExport CBotToken* GivNext(); // rend le suivant dans la liste - DllExport + //DllExport CBotToken* GivPrev(); // rend le Prcdent dans la liste - DllExport + //DllExport static CBotToken* CompileTokens(const char* p, int& error); // transforme tout le programme - DllExport + //DllExport static void Delete(CBotToken* pToken); // libre la liste // fonctions non utiles en export static - BOOL DefineNum(const char* name, long val); + bool DefineNum(const char* name, long val); void SetString(const char* name); void SetPos(int start, int end); @@ -1092,7 +1092,7 @@ public: void AddNext(CBotToken* p); // ajoute un token (une copie) static - CBotToken* NextToken(char* &program, int& error, BOOL first = FALSE); + CBotToken* NextToken(char* &program, int& error, bool first = false); // trouve le prochain token const CBotToken& operator=(const CBotToken& src); @@ -1163,7 +1163,7 @@ public: // excute le programme main // ------------------------- - while( FALSE = m_pMonRobot->Execute( "main", pStack )) + while( false = m_pMonRobot->Execute( "main", pStack )) { // programme suspendu // on pourrait passer la main un autre (en sauvegardant pStack pour ce robot-l) @@ -1174,10 +1174,10 @@ public: // routine implmentant l'instruction GOTO( CPoint pos ) -BOOL rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) +bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) { if (pVar->GivType() != CBotTypeClass || - pVar->IsElemOfClas("CPoint") ) { exception = 6522; return FALSE; ) + pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) // le paramtre n'est pas de la bonne classe ? // NB en fait ce contrle est dj fait par la routine pour la compilation @@ -1193,8 +1193,8 @@ BOOL rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); m_PosToGo.y = temp->GivValFloat(); - return (m_CurentPos == m_PosToGo); // rend TRUE si la position est atteinte - // rend FALSE s'il faut patienter encore + return (m_CurentPos == m_PosToGo); // rend true si la position est atteinte + // rend false s'il faut patienter encore } -#endif \ No newline at end of file +#endif diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 488061c..784e647 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -25,14 +25,14 @@ CBotFunction::CBotFunction() m_Param = NULL; // liste des paramtres vide m_Block = NULL; // le bloc d'instructions m_next = NULL; // les fonctions peuvent tre chanes - m_bPublic = FALSE; // fonction non publique - m_bExtern = FALSE; // fonction non externe + m_bPublic = false; // fonction non publique + m_bExtern = false; // fonction non externe m_nextpublic = NULL; m_prevpublic = NULL; m_pProg = NULL; // m_nThisIdent = 0; m_nFuncIdent = 0; - m_bSynchro = FALSE; + m_bSynchro = false; } CBotFunction* CBotFunction::m_listPublic = NULL; @@ -62,17 +62,17 @@ CBotFunction::~CBotFunction() } } -BOOL CBotFunction::IsPublic() +bool CBotFunction::IsPublic() { return m_bPublic; } -BOOL CBotFunction::IsExtern() +bool CBotFunction::IsExtern() { return m_bExtern; } -BOOL CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop) +bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop) { start = m_extern.GivStart(); stop = m_closeblk.GivEnd(); @@ -110,7 +110,7 @@ BOOL CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet stop = m_closeblk.GivEnd(); } - return TRUE; + return true; } @@ -168,7 +168,7 @@ CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) // compile une nouvelle fonction // bLocal permet de mettre la dclaration des paramtres au mme niveau // que le lments appartenant la classe pour les mthodes -CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, BOOL bLocal) +CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, bool bLocal) { CBotToken* pp; CBotFunction* func = finput; @@ -178,19 +178,19 @@ CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunct // func->m_nFuncIdent = CBotVar::NextUniqNum(); - while (TRUE) + while (true) { if ( IsOfType(p, ID_PUBLIC) ) { - func->m_bPublic = TRUE; + func->m_bPublic = true; continue; } pp = p; if ( IsOfType(p, ID_EXTERN) ) { func->m_extern = pp; // pour la position du mot "extern" - func->m_bExtern = TRUE; -// func->m_bPublic = TRUE; // donc aussi publique! + func->m_bExtern = true; +// func->m_bPublic = true; // donc aussi publique! continue; } break; @@ -260,7 +260,7 @@ CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunct // et compile le bloc d'instruction qui suit func->m_openblk = p; - func->m_Block = CBotBlock::Compile(p, pStk, FALSE); + func->m_Block = CBotBlock::Compile(p, pStk, false); func->m_closeblk = p->GivPrev(); if ( pStk->IsOk() ) { @@ -286,18 +286,18 @@ CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClas CBotFunction* func = new CBotFunction(); func->m_nFuncIdent = CBotVar::NextUniqNum(); - CBotCStack* pStk = pStack->TokenStack(p, TRUE); + CBotCStack* pStk = pStack->TokenStack(p, true); - while (TRUE) + while (true) { if ( IsOfType(p, ID_PUBLIC) ) { - // func->m_bPublic = TRUE; // sera fait en passe 2 + // func->m_bPublic = true; // sera fait en passe 2 continue; } if ( IsOfType(p, ID_EXTERN) ) { - func->m_bExtern = TRUE; + func->m_bExtern = true; continue; } break; @@ -367,16 +367,16 @@ bad: static int xx = 0; #endif -BOOL CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) +bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) { CBotStack* pile = pj->AddStack(this, 2); // un bout de pile local cette fonction -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; pile->SetBotCall(m_pProg); // bases pour les routines if ( pile->GivState() == 0 ) { - if ( !m_Param->Execute(ppVars, pile) ) return FALSE; // dfini les paramtres + if ( !m_Param->Execute(ppVars, pile) ) return false; // dfini les paramtres pile->IncState(); } @@ -403,14 +403,14 @@ BOOL CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) pile->IncState(); } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; if ( !m_Block->Execute(pile) ) { if ( pile->GivError() < 0 ) pile->SetError( 0 ); else - return FALSE; + return false; } return pj->Return(pile); @@ -433,7 +433,7 @@ void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInst pile2->Delete(); } - m_Param->RestoreState(pile2, TRUE); // les paramtres + m_Param->RestoreState(pile2, true); // les paramtres if ( !m_MasterClass.IsEmpty() ) { @@ -442,7 +442,7 @@ void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInst pThis->SetUniqNum(-2); } - m_Block->RestoreState(pile2, TRUE); + m_Block->RestoreState(pile2, true); } void CBotFunction::AddNext(CBotFunction* p) @@ -467,7 +467,7 @@ CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long // trouve une fonction selon son identificateur unique // si l'identificateur n'est pas trouv, cherche selon le nom et les paramtres -CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, BOOL bPublic) +CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic) { TypeOrError.SetType(TX_UNDEFCALL); // pas de routine de ce nom CBotFunction* pt; @@ -632,13 +632,13 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotS if ( pt != NULL ) { CBotStack* pStk1 = pStack->AddStack(pt, 2); // pour mettre "this" -// if ( pStk1 == EOX ) return TRUE; +// if ( pStk1 == EOX ) return true; pStk1->SetBotCall(pt->m_pProg); // on a peut-tre chang de module - if ( pStk1->IfStep() ) return FALSE; + if ( pStk1->IfStep() ) return false; - CBotStack* pStk3 = pStk1->AddStack(NULL, TRUE); // paramtres + CBotStack* pStk3 = pStk1->AddStack(NULL, true); // paramtres // prpare les paramtres sur la pile @@ -680,11 +680,11 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotS if ( !pStk3->IsOk() && pt->m_pProg != m_pProg ) { #ifdef _DEBUG - if ( m_pProg->GivFunctions()->GivName() == "LaCommande" ) return FALSE; + if ( m_pProg->GivFunctions()->GivName() == "LaCommande" ) return false; #endif pStk3->SetPosError(pToken); // indique l'erreur sur l'appel de procdure } - return FALSE; // interrompu ! + return false; // interrompu ! } return pStack->Return( pStk3 ); @@ -738,13 +738,13 @@ void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, if ( pStk1->GivState() == 0 ) { - pt->m_Param->RestoreState(pStk3, TRUE); + pt->m_Param->RestoreState(pStk3, true); return; } // initialise les variables selon paramtres - pt->m_Param->RestoreState(pStk3, FALSE); - pt->m_Block->RestoreState(pStk3, TRUE); + pt->m_Param->RestoreState(pStk3, false); + pt->m_Block->RestoreState(pStk3, true); } } @@ -758,17 +758,17 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar CBotTypResult type; CBotProgram* pProgCurrent = pStack->GivBotCall(); - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, FALSE); + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, false); if ( pt != NULL ) { // DEBUG( "CBotFunction::DoCall" + pt->GivName(), 0, pStack); CBotStack* pStk = pStack->AddStack(pt, 2); -// if ( pStk == EOX ) return TRUE; +// if ( pStk == EOX ) return true; pStk->SetBotCall(pt->m_pProg); // on a peut-tre chang de module - CBotStack* pStk3 = pStk->AddStack(NULL, TRUE); // pour mettre les paramtres passs + CBotStack* pStk3 = pStk->AddStack(NULL, true); // pour mettre les paramtres passs // prpare les paramtres sur la pile @@ -776,7 +776,7 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar { // met la variable "this" sur la pile CBotVar* pthis = CBotVar::Create("this", CBotTypNullPointer); - pthis->Copy(pThis, FALSE); + pthis->Copy(pThis, false); pthis->SetUniqNum(-2); // valeur spciale pStk->AddVar(pthis); @@ -785,7 +785,7 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar { // met la variable "super" sur la pile CBotVar* psuper = CBotVar::Create("super", CBotTypNullPointer); - psuper->Copy(pThis, FALSE); // en fait identique "this" + psuper->Copy(pThis, false); // en fait identique "this" psuper->SetUniqNum(-3); // valeur spciale pStk->AddVar(psuper); } @@ -798,8 +798,8 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar { if ( pt->m_bSynchro ) { - CBotProgram* pProgBase = pStk->GivBotCall(TRUE); - if ( !pClass->Lock(pProgBase) ) return FALSE; // attend de pouvoir + CBotProgram* pProgBase = pStk->GivBotCall(true); + if ( !pClass->Lock(pProgBase) ) return false; // attend de pouvoir } pStk->IncState(); } @@ -820,7 +820,7 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar pStk3->SetPosError(pToken); // indique l'erreur sur l'appel de procdure } } - return FALSE; // interrompu ! + return false; // interrompu ! } if ( pt->m_bSynchro ) @@ -850,30 +850,30 @@ void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar* pThis, C CBotStack* pStk3 = pStk->RestoreStack(NULL); // pour mettre les paramtres passs if ( pStk3 == NULL ) return; - pt->m_Param->RestoreState(pStk3, TRUE); // les paramtres + pt->m_Param->RestoreState(pStk3, true); // les paramtres if ( pStk->GivState() > 1 && // vrouillage est effectif ? pt->m_bSynchro ) { - CBotProgram* pProgBase = pStk->GivBotCall(TRUE); + CBotProgram* pProgBase = pStk->GivBotCall(true); pClass->Lock(pProgBase); // vrouille la classe } // finalement appelle la fonction trouve - pt->m_Block->RestoreState(pStk3, TRUE); // interrompu ! + pt->m_Block->RestoreState(pStk3, true); // interrompu ! } } // regarde si la "signature" des paramtres est identique -BOOL CBotFunction::CheckParam(CBotDefParam* pParam) +bool CBotFunction::CheckParam(CBotDefParam* pParam) { CBotDefParam* pp = m_Param; while ( pp != NULL && pParam != NULL ) { CBotTypResult type1 = pp->GivType(); CBotTypResult type2 = pParam->GivType(); - if ( !type1.Compare(type2) ) return FALSE; + if ( !type1.Compare(type2) ) return false; pp = pp->GivNext(); pParam = pParam->GivNext(); } @@ -1005,7 +1005,7 @@ void CBotDefParam::AddNext(CBotDefParam* p) } -BOOL CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) +bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) { int i = 0; CBotDefParam* p = this; @@ -1033,7 +1033,7 @@ BOOL CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) newvar->SetValInt(ppVars[i]->GivValInt()); break; case CBotTypIntrinsic: - ((CBotVarClass*)newvar)->Copy(ppVars[i], FALSE); + ((CBotVarClass*)newvar)->Copy(ppVars[i], false); break; case CBotTypPointer: case CBotTypArrayPointer: @@ -1051,10 +1051,10 @@ BOOL CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) i++; } - return TRUE; + return true; } -void CBotDefParam::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) { int i = 0; CBotDefParam* p = this; @@ -1146,25 +1146,25 @@ CBotInstr* CBotReturn::Compile(CBotToken* &p, CBotCStack* pStack) return NULL; // pas d'objet, l'erreur est sur la pile } -BOOL CBotReturn::Execute(CBotStack* &pj) +bool CBotReturn::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState() == 0 ) { - if ( m_Instr != NULL && !m_Instr->Execute(pile) ) return FALSE; // value le rsultat + if ( m_Instr != NULL && !m_Instr->Execute(pile) ) return false; // value le rsultat // le rsultat est sur la pile pile->IncState(); } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; pile->SetBreak(3, CBotString()); return pj->Return(pile); } -void CBotReturn::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; CBotStack* pile = pj->RestoreStack(this); @@ -1211,7 +1211,7 @@ CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) inst->SetToken(pp); // compile la liste des paramtres - if (!IsOfType(p, ID_CLOSEPAR)) while (TRUE) + if (!IsOfType(p, ID_CLOSEPAR)) while (true) { start = p->GivStart(); pile = pile->TokenStack(); // garde les rsultats sur la pile @@ -1278,7 +1278,7 @@ CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) return NULL; } -BOOL CBotInstrCall::Execute(CBotStack* &pj) +bool CBotInstrCall::Execute(CBotStack* &pj) { CBotVar* ppVars[1000]; CBotStack* pile = pj->AddStack(this); @@ -1292,12 +1292,12 @@ BOOL CBotInstrCall::Execute(CBotStack* &pj) // value les paramtres // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { pile = pile->AddStack(); // de la place sur la pile pour les rsultats if ( pile->GivState() == 0 ) { - if (!p->Execute(pile)) return FALSE; // interrompu ici ? + if (!p->Execute(pile)) return false; // interrompu ici ? pile->SetState(1); // marque spciale pour reconnare les paramtres } ppVars[i++] = pile->GivVar(); @@ -1307,14 +1307,14 @@ BOOL CBotInstrCall::Execute(CBotStack* &pj) ppVars[i] = NULL; CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return FALSE; + if ( pile2->IfStep() ) return false; - if ( !pile2->ExecuteCall(m_nFuncIdent, GivToken(), ppVars, m_typRes)) return FALSE; // interrompu + if ( !pile2->ExecuteCall(m_nFuncIdent, GivToken(), ppVars, m_typRes)) return false; // interrompu return pj->Return(pile2); // libre toute la pile } -void CBotInstrCall::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -1329,7 +1329,7 @@ void CBotInstrCall::RestoreState(CBotStack* &pj, BOOL bMain) // value les paramtres // et place les valeurs sur la pile // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( TRUE ) + if ( p != NULL) while ( true ) { pile = pile->RestoreStack(); // de la place sur la pile pour les rsultats if ( pile == NULL ) return; @@ -1394,7 +1394,7 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) #endif CBotClass* classe = (pOld == NULL) ? new CBotClass(name, pPapa) : pOld; classe->Purge(); // vide les anciennes dfinitions - classe->m_IsDef = FALSE; // dfinition en cours + classe->m_IsDef = false; // dfinition en cours if ( !IsOfType( p, ID_OPBLK) ) { @@ -1404,7 +1404,7 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) { - classe->CompileDefItem(p, pStack, FALSE); + classe->CompileDefItem(p, pStack, false); } if (pStack->IsOk()) return classe; @@ -1413,24 +1413,24 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) return NULL; } -BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) +bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) { - BOOL bStatic = FALSE; + bool bStatic = false; int mProtect = PR_PUBLIC; - BOOL bSynchro = FALSE; + bool bSynchro = false; while (IsOfType(p, ID_SEP)) ; CBotTypResult type( -1 ); - if ( IsOfType(p, ID_SYNCHO) ) bSynchro = TRUE; + if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; CBotToken* pBase = p; - if ( IsOfType(p, ID_STATIC) ) bStatic = TRUE; + if ( IsOfType(p, ID_STATIC) ) bStatic = true; if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; - if ( IsOfType(p, ID_STATIC) ) bStatic = TRUE; + if ( IsOfType(p, ID_STATIC) ) bStatic = true; // CBotClass* pClass = NULL; type = TypeParam(p, pStack); // type du rsultat @@ -1438,7 +1438,7 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) if ( type.Eq(-1) ) { pStack->SetError(TX_NOTYP, p); - return FALSE; + return false; } while (pStack->IsOk()) @@ -1463,14 +1463,14 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) { pStack->SetError(TX_CLBRK, p->GivStart()); - return FALSE; + return false; } /* CBotVar* pv = pStack->GivVar(); if ( pv->GivType()>= CBotTypBoolean ) { pStack->SetError(TX_BADTYPE, p->GivStart()); - return FALSE; + return false; }*/ if (limites == NULL) limites = i; @@ -1485,7 +1485,7 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) CBotFunction* f = CBotFunction::Compile1(p, pStack, this); - if ( f == NULL ) return FALSE; + if ( f == NULL ) return false; if (m_pMethod == NULL) m_pMethod = f; else m_pMethod->AddNext(f); @@ -1502,8 +1502,8 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) pf = pf->Next(); } - BOOL bConstructor = (pp->GivString() == GivName()); - CBotCStack* pile = pStack->TokenStack(NULL, TRUE); + bool bConstructor = (pp->GivString() == GivName()); + CBotCStack* pile = pStack->TokenStack(NULL, true); // rend "this" connu CBotToken TokenThis(CBotString("this"), CBotString()); @@ -1540,7 +1540,7 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) // compile une mthode p = pBase; CBotFunction* f = - CBotFunction::Compile(p, pile, NULL/*, FALSE*/); + CBotFunction::Compile(p, pile, NULL/*, false*/); if ( f != NULL ) { @@ -1563,7 +1563,7 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) if (type.Eq(0)) { pStack->SetError(TX_ENDOF, p); - return FALSE; + return false; } CBotInstr* i = NULL; @@ -1578,7 +1578,7 @@ BOOL CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, BOOL bSecond) // il y a une assignation calculer i = CBotTwoOpExpr::Compile(p, pStack); } - if ( !pStack->IsOk() ) return FALSE; + if ( !pStack->IsOk() ) return false; } @@ -1637,10 +1637,10 @@ CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) { - pOld->CompileDefItem(p, pStack, TRUE); + pOld->CompileDefItem(p, pStack, true); } - pOld->m_IsDef = TRUE; // dfinition termine + pOld->m_IsDef = true; // dfinition termine if (pStack->IsOk()) return pOld; } pStack->SetError(TX_ENDOF, p); diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotIf.cpp index 626ef0d..178992e 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotIf.cpp @@ -51,7 +51,7 @@ CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) { // la condition existe bel et bien - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); if ( pStk->IsOk() ) { // le bloc d'instruction est ok (peut tre vide) @@ -60,7 +60,7 @@ CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) if (IsOfType(p, ID_ELSE)) { // si oui, compile le bloc d'instruction qui suit - inst->m_BlockElse = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + inst->m_BlockElse = CBotBlock::CompileBlkOrInst( p, pStk, true ); if (!pStk->IsOk()) { // il n'y a pas de bloc correct aprs le else @@ -84,19 +84,19 @@ CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) // excution de l'instruction -BOOL CBotIf :: Execute(CBotStack* &pj) +bool CBotIf :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; // selon la reprise, on peut tre dans l'un des 2 tats if( pile->GivState() == 0 ) { // value la condition - if ( !m_Condition->Execute(pile) ) return FALSE; // interrompu ici ? + if ( !m_Condition->Execute(pile) ) return false; // interrompu ici ? // termine s'il y a une erreur if ( !pile->IsOk() ) @@ -105,21 +105,21 @@ BOOL CBotIf :: Execute(CBotStack* &pj) } // passe dans le second tat - if (!pile->SetState(1)) return FALSE; // prt pour la suite + if (!pile->SetState(1)) return false; // prt pour la suite } // second tat, value les instructions associes // le rsultat de la condition est sur la pile - if ( pile->GivVal() == TRUE ) // condition tait vraie ? + if ( pile->GivVal() == true ) // condition tait vraie ? { if ( m_Block != NULL && // bloc peut tre absent - !m_Block->Execute(pile) ) return FALSE; // interrompu ici ? + !m_Block->Execute(pile) ) return false; // interrompu ici ? } else { if ( m_BlockElse != NULL && // s'il existe un bloc alternatif - !m_BlockElse->Execute(pile) ) return FALSE; // interrompu ici + !m_BlockElse->Execute(pile) ) return false; // interrompu ici } // transmet le rsultat et libre la pile @@ -127,7 +127,7 @@ BOOL CBotIf :: Execute(CBotStack* &pj) } -void CBotIf :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotIf :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -145,7 +145,7 @@ void CBotIf :: RestoreState(CBotStack* &pj, BOOL bMain) // second tat, value les instructions associes // le rsultat de la condition est sur la pile - if ( pile->GivVal() == TRUE ) // condition tait vraie ? + if ( pile->GivVal() == true ) // condition tait vraie ? { if ( m_Block != NULL ) // bloc peut tre absent m_Block->RestoreState(pile, bMain); // interrompu ici ! diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index f157c4c..bbdc350 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -62,7 +62,7 @@ CBotProgram::~CBotProgram() } -BOOL CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser ) +bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser ) { int error = 0; Stop(); @@ -81,7 +81,7 @@ BOOL CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, // transforme le programme en Tokens CBotToken* pBaseToken = CBotToken::CompileTokens(program, error); - if ( pBaseToken == NULL ) return FALSE; + if ( pBaseToken == NULL ) return false; CBotCStack* pStack = new CBotCStack(NULL); @@ -115,7 +115,7 @@ BOOL CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, delete m_Prog; m_Prog = NULL; delete pBaseToken; - return FALSE; + return false; } // CBotFunction* temp = NULL; @@ -130,12 +130,12 @@ BOOL CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, if ( p->GivType() == ID_CLASS || ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) { - m_bCompileClass = TRUE; + m_bCompileClass = true; CBotClass::Compile(p, pStack); // complte la dfinition de la classe } else { - m_bCompileClass = FALSE; + m_bCompileClass = false; CBotFunction::Compile(p, pStack, next); if (next->IsExtern()) ListFonctions.Add(next->GivName()/* + next->GivParams()*/); next->m_pProg = this; // garde le pointeur au module @@ -160,7 +160,7 @@ BOOL CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, } -BOOL CBotProgram::Start(const char* name) +bool CBotProgram::Start(const char* name) { #if STACKMEM m_pStack->Delete(); @@ -179,7 +179,7 @@ BOOL CBotProgram::Start(const char* name) if ( m_pRun == NULL ) { m_ErrorCode = TX_NORUN; - return FALSE; + return false; } #if STACKMEM @@ -190,10 +190,10 @@ BOOL CBotProgram::Start(const char* name) m_pStack->SetBotCall(this); // bases pour les routines - return TRUE; // on est prt pour un Run() + return true; // on est prt pour un Run() } -BOOL CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop) +bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop) { CBotFunction* p = m_Prog; while (p != NULL) @@ -202,15 +202,15 @@ BOOL CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet m p = p->m_next; } - if ( p == NULL ) return FALSE; + if ( p == NULL ) return false; p->GetPosition(start, stop, modestart, modestop); - return TRUE; + return true; } -BOOL CBotProgram::Run(void* pUser, int timer) +bool CBotProgram::Run(void* pUser, int timer) { - BOOL ok; + bool ok; if (m_pStack == NULL || m_pRun == NULL) goto error; @@ -253,7 +253,7 @@ BOOL CBotProgram::Run(void* pUser, int timer) delete m_pStack; #endif m_pStack = NULL; - return TRUE; // excution termine !! + return true; // excution termine !! } if ( ok ) m_pRun = NULL; // plus de fonction en excution @@ -261,7 +261,7 @@ BOOL CBotProgram::Run(void* pUser, int timer) error: m_ErrorCode = TX_NORUN; - return TRUE; + return true; } void CBotProgram::Stop() @@ -277,14 +277,14 @@ void CBotProgram::Stop() -BOOL CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) +bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) { FunctionName = NULL; start = end = 0; - if (m_pStack == NULL) return FALSE; + if (m_pStack == NULL) return false; m_pStack->GetRunPos(FunctionName, start, end); - return TRUE; + return true; } CBotVar* CBotProgram::GivStackVars(const char* &FunctionName, int level) @@ -321,7 +321,7 @@ long CBotProgram::GivIdent() return m_Ident; } -BOOL CBotProgram::GetError(int& code, int& start, int& end) +bool CBotProgram::GetError(int& code, int& start, int& end) { code = m_ErrorCode; start = m_ErrorStart; @@ -329,7 +329,7 @@ BOOL CBotProgram::GetError(int& code, int& start, int& end) return code > 0; } -BOOL CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) +bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) { code = m_ErrorCode; start = m_ErrorStart; @@ -358,8 +358,8 @@ CBotFunction* CBotProgram::GivFunctions() return m_Prog; } -BOOL CBotProgram::AddFunction(const char* name, - BOOL rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), +bool CBotProgram::AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) { // mmorise les pointeurs aux deux fonctions @@ -367,25 +367,25 @@ BOOL CBotProgram::AddFunction(const char* name, } -BOOL WriteWord(FILE* pf, WORD w) +bool WriteWord(FILE* pf, unsigned short w) { size_t lg; - lg = fwrite(&w, sizeof( WORD ), 1, pf ); + lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); return (lg == 1); } -BOOL ReadWord(FILE* pf, WORD& w) +bool ReadWord(FILE* pf, unsigned short& w) { size_t lg; - lg = fread(&w, sizeof( WORD ), 1, pf ); + lg = fread(&w, sizeof( unsigned short ), 1, pf ); return (lg == 1); } -BOOL WriteFloat(FILE* pf, float w) +bool WriteFloat(FILE* pf, float w) { size_t lg; @@ -394,7 +394,7 @@ BOOL WriteFloat(FILE* pf, float w) return (lg == 1); } -BOOL ReadFloat(FILE* pf, float& w) +bool ReadFloat(FILE* pf, float& w) { size_t lg; @@ -403,7 +403,7 @@ BOOL ReadFloat(FILE* pf, float& w) return (lg == 1); } -BOOL WriteLong(FILE* pf, long w) +bool WriteLong(FILE* pf, long w) { size_t lg; @@ -412,7 +412,7 @@ BOOL WriteLong(FILE* pf, long w) return (lg == 1); } -BOOL ReadLong(FILE* pf, long& w) +bool ReadLong(FILE* pf, long& w) { size_t lg; @@ -421,24 +421,24 @@ BOOL ReadLong(FILE* pf, long& w) return (lg == 1); } -BOOL WriteString(FILE* pf, CBotString s) +bool WriteString(FILE* pf, CBotString s) { size_t lg1, lg2; lg1 = s.GivLength(); - if (!WriteWord(pf, lg1)) return FALSE; + if (!WriteWord(pf, lg1)) return false; lg2 = fwrite(s, 1, lg1, pf ); return (lg1 == lg2); } -BOOL ReadString(FILE* pf, CBotString& s) +bool ReadString(FILE* pf, CBotString& s) { - WORD w; + unsigned short w; char buf[1000]; size_t lg1, lg2; - if (!ReadWord(pf, w)) return FALSE; + if (!ReadWord(pf, w)) return false; lg1 = w; lg2 = fread(buf, 1, lg1, pf ); buf[lg2] = 0; @@ -447,29 +447,29 @@ BOOL ReadString(FILE* pf, CBotString& s) return (lg1 == lg2); } -BOOL WriteType(FILE* pf, CBotTypResult type) +bool WriteType(FILE* pf, CBotTypResult type) { int typ = type.GivType(); if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; - if ( !WriteWord(pf, typ) ) return FALSE; + if ( !WriteWord(pf, typ) ) return false; if ( typ == CBotTypClass ) { CBotClass* p = type.GivClass(); - if ( !WriteString(pf, p->GivName()) ) return FALSE; + if ( !WriteString(pf, p->GivName()) ) return false; } if ( type.Eq( CBotTypArrayBody ) || type.Eq( CBotTypArrayPointer ) ) { - if ( !WriteWord(pf, type.GivLimite()) ) return FALSE; - if ( !WriteType(pf, type.GivTypElem()) ) return FALSE; + if ( !WriteWord(pf, type.GivLimite()) ) return false; + if ( !WriteType(pf, type.GivTypElem()) ) return false; } - return TRUE; + return true; } -BOOL ReadType(FILE* pf, CBotTypResult& type) +bool ReadType(FILE* pf, CBotTypResult& type) { - WORD w, ww; - if ( !ReadWord(pf, w) ) return FALSE; + unsigned short w, ww; + if ( !ReadWord(pf, w) ) return false; type.SetType(w); if ( type.Eq( CBotTypIntrinsic ) ) @@ -480,7 +480,7 @@ BOOL ReadType(FILE* pf, CBotTypResult& type) if ( type.Eq( CBotTypClass ) ) { CBotString s; - if ( !ReadString(pf, s) ) return FALSE; + if ( !ReadString(pf, s) ) return false; type = CBotTypResult( w, s ); } @@ -488,54 +488,54 @@ BOOL ReadType(FILE* pf, CBotTypResult& type) type.Eq( CBotTypArrayBody ) ) { CBotTypResult r; - if ( !ReadWord(pf, ww) ) return FALSE; - if ( !ReadType(pf, r) ) return FALSE; + if ( !ReadWord(pf, ww) ) return false; + if ( !ReadType(pf, r) ) return false; type = CBotTypResult( w, r ); type.SetLimite((short)ww); } - return TRUE; + return true; } -BOOL CBotProgram::DefineNum(const char* name, long val) +bool CBotProgram::DefineNum(const char* name, long val) { return CBotToken::DefineNum(name, val); } -BOOL CBotProgram::SaveState(FILE* pf) +bool CBotProgram::SaveState(FILE* pf) { - if (!WriteWord( pf, CBOTVERSION)) return FALSE; + if (!WriteWord( pf, CBOTVERSION)) return false; if ( m_pStack != NULL ) { - if (!WriteWord( pf, 1)) return FALSE; - if (!WriteString( pf, m_pRun->GivName() )) return FALSE; - if (!m_pStack->SaveState(pf)) return FALSE; + if (!WriteWord( pf, 1)) return false; + if (!WriteString( pf, m_pRun->GivName() )) return false; + if (!m_pStack->SaveState(pf)) return false; } else { - if (!WriteWord( pf, 0)) return FALSE; + if (!WriteWord( pf, 0)) return false; } - return TRUE; + return true; } -BOOL CBotProgram::RestoreState(FILE* pf) +bool CBotProgram::RestoreState(FILE* pf) { - WORD w; - CBotString s; + unsigned short w; + CBotString s; Stop(); - if (!ReadWord( pf, w )) return FALSE; - if ( w != CBOTVERSION ) return FALSE; + if (!ReadWord( pf, w )) return false; + if ( w != CBOTVERSION ) return false; - if (!ReadWord( pf, w )) return FALSE; - if ( w == 0 ) return TRUE; + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) return true; - if (!ReadString( pf, s )) return FALSE; + if (!ReadString( pf, s )) return false; Start(s); // point de reprise #if STACKMEM @@ -547,12 +547,12 @@ BOOL CBotProgram::RestoreState(FILE* pf) // rcupre la pile depuis l'enregistrement // utilise un pointeur NULL (m_pStack) mais c'est ok comme a - if (!m_pStack->RestoreState(pf, m_pStack)) return FALSE; + if (!m_pStack->RestoreState(pf, m_pStack)) return false; m_pStack->SetBotCall(this); // bases pour les routines // rtabli certains tats dans la pile selon la structure m_pRun->RestoreState(NULL, m_pStack, m_pInstance); - return TRUE; + return true; } int CBotProgram::GivVersion() @@ -566,7 +566,7 @@ int CBotProgram::GivVersion() CBotCall* CBotCall::m_ListCalls = NULL; CBotCall::CBotCall(const char* name, - BOOL rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) { m_name = name; @@ -587,8 +587,8 @@ void CBotCall::Free() delete CBotCall::m_ListCalls; } -BOOL CBotCall::AddFunction(const char* name, - BOOL rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), +bool CBotCall::AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) { CBotCall* p = m_ListCalls; @@ -616,18 +616,18 @@ BOOL CBotCall::AddFunction(const char* name, if (p) p->m_next = pp; else m_ListCalls = pp; - return TRUE; + return true; } // transforme le tableau de pointeurs aux variables // en une liste de variables chanes -CBotVar* MakeListVars(CBotVar** ppVars, BOOL bSetVal=FALSE) +CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false) { int i = 0; CBotVar* pVar = NULL; - while( TRUE ) + while( true ) { ppVars[i]; if ( ppVars[i] == NULL ) break; @@ -686,16 +686,16 @@ void CBotCall::SetPUser(void* pUser) m_pUser = pUser; } -int CBotCall::CheckCall(const char* name) +bool CBotCall::CheckCall(const char* name) { CBotCall* p = m_ListCalls; while ( p != NULL ) { - if ( name == p->GivName() ) return TRUE; + if ( name == p->GivName() ) return true; p = p->m_next; } - return FALSE; + return false; } @@ -746,7 +746,7 @@ fund: #if !STACKRUN // fait la liste des paramtres selon le contenu de la pile (pStackVar) - CBotVar* pVar = MakeListVars(ppVar, TRUE); + CBotVar* pVar = MakeListVars(ppVar, true); CBotVar* pVarToDelete = pVar; // cre une variable pour le rsultat @@ -759,14 +759,14 @@ fund: if ( pResult != pRes ) delete pRes; // si rsultat diffrent rendu delete pVarToDelete; - if (res == FALSE) + if (res == false) { if (Exception!=0) { pStack->SetError(Exception, token); } delete pResult; - return FALSE; + return false; } pStack->SetVar(pResult); @@ -775,16 +775,16 @@ fund: pStack->SetError(TX_NORETVAL, token); } nIdent = pt->m_nFuncIdent; - return TRUE; + return true; #else CBotStack* pile = pStack->AddStackEOX(pt); - if ( pile == EOX ) return TRUE; + if ( pile == EOX ) return true; // fait la liste des paramtres selon le contenu de la pile (pStackVar) - CBotVar* pVar = MakeListVars(ppVar, TRUE); + CBotVar* pVar = MakeListVars(ppVar, true); CBotVar* pVarToDelete = pVar; // cre une variable pour le rsultat @@ -804,7 +804,7 @@ fund: #if STACKRUN -BOOL CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) +bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) { CBotCall* pt = m_ListCalls; @@ -817,22 +817,22 @@ BOOL CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBot nIdent = pt->m_nFuncIdent; CBotStack* pile = pStack->RestoreStackEOX(pt); - if ( pile == NULL ) return TRUE; + if ( pile == NULL ) return true; CBotStack* pile2 = pile->RestoreStack(); - return TRUE; + return true; } pt = pt->m_next; } } - return FALSE; + return false; } -BOOL CBotCall::Run(CBotStack* pStack) +bool CBotCall::Run(CBotStack* pStack) { CBotStack* pile = pStack->AddStackEOX(this); - if ( pile == EOX ) return TRUE; + if ( pile == EOX ) return true; CBotVar* pVar = pile->GivVar(); CBotStack* pile2 = pile->AddStack(); @@ -842,20 +842,20 @@ BOOL CBotCall::Run(CBotStack* pStack) int Exception = 0; int res = m_rExec(pVar, pResult, Exception, pStack->GivPUser()); - if (res == FALSE) + if (res == false) { if (Exception!=0) { pStack->SetError(Exception); } if ( pResult != pRes ) delete pResult; // si rsultat diffrent rendu - return FALSE; + return false; } if ( pResult != NULL ) pStack->SetCopyVar( pResult ); if ( pResult != pRes ) delete pResult; // si rsultat diffrent rendu - return TRUE; + return true; } #endif @@ -863,7 +863,7 @@ BOOL CBotCall::Run(CBotStack* pStack) /////////////////////////////////////////////////////////////////////////////////////// CBotCallMethode::CBotCallMethode(const char* name, - BOOL rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) { m_name = name; @@ -893,7 +893,7 @@ CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, { if ( pt->m_name == name ) { - CBotVar* pVar = MakeListVars(ppVar, TRUE); + CBotVar* pVar = MakeListVars(ppVar, true); CBotVar* pVar2 = pVar; CBotTypResult r = pt->m_rComp(pThis, pVar2); int ret = r.GivType(); @@ -942,7 +942,7 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot { // fait la liste des paramtres selon le contenu de la pile (pStackVar) - CBotVar* pVar = MakeListVars(ppVars, TRUE); + CBotVar* pVar = MakeListVars(ppVars, true); CBotVar* pVarToDelete = pVar; // puis appelle la routine externe au module @@ -951,7 +951,7 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot int res = pt->m_rExec(pThis, pVar, pResult, Exception); pStack->SetVar(pResult); - if (res == FALSE) + if (res == false) { if (Exception!=0) { @@ -959,10 +959,10 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot pStack->SetError(Exception, pToken); } delete pVarToDelete; - return FALSE; + return false; } delete pVarToDelete; - return TRUE; + return true; } pt = pt->m_next; } @@ -975,14 +975,14 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot { // fait la liste des paramtres selon le contenu de la pile (pStackVar) - CBotVar* pVar = MakeListVars(ppVars, TRUE); + CBotVar* pVar = MakeListVars(ppVars, true); CBotVar* pVarToDelete = pVar; int Exception = 0; int res = pt->m_rExec(pThis, pVar, pResult, Exception); pStack->SetVar(pResult); - if (res == FALSE) + if (res == false) { if (Exception!=0) { @@ -990,11 +990,11 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot pStack->SetError(Exception, pToken); } delete pVarToDelete; - return FALSE; + return false; } delete pVarToDelete; nIdent = pt->m_nFuncIdent; - return TRUE; + return true; } pt = pt->m_next; } @@ -1002,7 +1002,7 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot return -1; } -BOOL rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { if ( pVar == NULL ) return TX_LOWPARAM; @@ -1016,7 +1016,7 @@ BOOL rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) } pResult->SetValInt(i); - return TRUE; + return true; } CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) @@ -1030,11 +1030,11 @@ CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) CBotString CBotProgram::m_DebugVarStr = ""; -BOOL rCBotDebug( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rCBotDebug( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { pResult->SetValString( CBotProgram::m_DebugVarStr ); - return TRUE; + return true; } CBotTypResult cCBotDebug( CBotVar* &pVar, void* pUser ) @@ -1103,7 +1103,8 @@ void CBotProgram::Init() // une fonction juste pour les debug divers CBotProgram::AddFunction("CBOTDEBUGDD", rCBotDebug, cCBotDebug); - DeleteFile("CbotDebug.txt"); + //TODO implement this deletion + // DeleteFile("CbotDebug.txt"); } diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index b5aaba0..77ed7d7 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -16,6 +16,8 @@ // gestion de la pile (stack) #include "CBot.h" +#include +#include #define ITIMER 100 @@ -24,14 +26,14 @@ // gestion de la pile d'excution //////////////////////////////////////////////////////////////////////////// -int CBotStack::m_initimer = ITIMER; // init la variable statique -int CBotStack::m_timer = 0; // init la variable statique -CBotVar* CBotStack::m_retvar = NULL; // init la variable statique -int CBotStack::m_error = 0; // init la variable statique -int CBotStack::m_start = 0; // init la variable statique -int CBotStack::m_end = 0; // init la variable statique -CBotString CBotStack::m_labelBreak=""; // init la variable statique -void* CBotStack::m_pUser = NULL; +int CBotStack::m_initimer = ITIMER; +int CBotStack::m_timer = 0; +CBotVar* CBotStack::m_retvar = NULL; +int CBotStack::m_error = 0; +int CBotStack::m_start = 0; +int CBotStack::m_end = 0; +CBotString CBotStack::m_labelBreak=""; +void* CBotStack::m_pUser = NULL; #if STACKMEM @@ -48,7 +50,7 @@ CBotStack* CBotStack::FirstStack() // la vide totalement memset(p, 0, size); - p-> m_bBlock = TRUE; + p-> m_bBlock = true; m_timer = m_initimer; // met le timer au dbut CBotStack* pp = p; @@ -56,7 +58,7 @@ CBotStack* CBotStack::FirstStack() int i; for ( i = 0 ; i< 10 ; i++ ) { - pp->m_bOver = TRUE; + pp->m_bOver = true; pp ++; } #ifdef _DEBUG @@ -75,8 +77,8 @@ CBotStack* CBotStack::FirstStack() CBotStack::CBotStack(CBotStack* ppapa) { - // constructeur doit exister, sinon le destructeur n'est jamais appel ! - ASM_TRAP(); + // constructor must exist or the destructor is never called! + ASM_TRAP(); } CBotStack::~CBotStack() @@ -104,7 +106,7 @@ void CBotStack::Delete() delete m_listVar; CBotStack* p = m_prev; - BOOL bOver = m_bOver; + bool bOver = m_bOver; #ifdef _DEBUG int n = m_index; #endif @@ -122,7 +124,7 @@ void CBotStack::Delete() // routine optimise -CBotStack* CBotStack::AddStack(CBotInstr* instr, BOOL bBlock) +CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) { if (m_next != NULL) { @@ -150,11 +152,11 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, BOOL bBlock) p->m_prev = this; p->m_state = 0; p->m_call = NULL; - p->m_bFunc = FALSE; + p->m_bFunc = false; return p; } -CBotStack* CBotStack::AddStackEOX(CBotCall* instr, BOOL bBlock) +CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) { if (m_next != NULL) { @@ -171,7 +173,7 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, BOOL bBlock) return p; } -CBotStack* CBotStack::AddStack2(BOOL bBlock) +CBotStack* CBotStack::AddStack2(bool bBlock) { if (m_next2 != NULL) { @@ -194,14 +196,14 @@ CBotStack* CBotStack::AddStack2(BOOL bBlock) return p; } -BOOL CBotStack::GivBlock() +bool CBotStack::GivBlock() { return m_bBlock; } -BOOL CBotStack::Return(CBotStack* pfils) +bool CBotStack::Return(CBotStack* pfils) { - if ( pfils == this ) return TRUE; // spcial + if ( pfils == this ) return true; // spcial if (m_var != NULL) delete m_var; // valeur remplace ? m_var = pfils->m_var; // rsultat transmis @@ -213,9 +215,9 @@ BOOL CBotStack::Return(CBotStack* pfils) return (m_error == 0); // interrompu si erreur } -BOOL CBotStack::ReturnKeep(CBotStack* pfils) +bool CBotStack::ReturnKeep(CBotStack* pfils) { - if ( pfils == this ) return TRUE; // spcial + if ( pfils == this ) return true; // spcial if (m_var != NULL) delete m_var; // valeur remplace ? m_var = pfils->m_var; // rsultat transmis @@ -224,11 +226,11 @@ BOOL CBotStack::ReturnKeep(CBotStack* pfils) return (m_error == 0); // interrompu si erreur } -BOOL CBotStack::StackOver() +bool CBotStack::StackOver() { - if (!m_bOver) return FALSE; + if (!m_bOver) return false; m_error = TX_STACKOVER; - return TRUE; + return true; } #else @@ -239,7 +241,7 @@ CBotStack::CBotStack(CBotStack* ppapa) m_next2 = NULL; m_prev = ppapa; - m_bBlock = (ppapa == NULL) ? TRUE : FALSE; + m_bBlock = (ppapa == NULL) ? true : false; m_state = 0; m_step = 1; @@ -247,13 +249,13 @@ CBotStack::CBotStack(CBotStack* ppapa) if (ppapa == NULL) m_timer = m_initimer; // met le timer au dbut m_listVar = NULL; - m_bDontDelete = FALSE; + m_bDontDelete = false; m_var = NULL; m_prog = NULL; m_instr = NULL; m_call = NULL; - m_bFunc = FALSE; + m_bFunc = false; } // destructeur @@ -269,7 +271,7 @@ CBotStack::~CBotStack() } // routine optimiser -CBotStack* CBotStack::AddStack(CBotInstr* instr, BOOL bBlock) +CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) { if (m_next != NULL) { @@ -284,7 +286,7 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, BOOL bBlock) return p; } -CBotStack* CBotStack::AddStackEOX(CBotCall* instr, BOOL bBlock) +CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) { if (m_next != NULL) { @@ -305,7 +307,7 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, BOOL bBlock) return p; } -CBotStack* CBotStack::AddStack2(BOOL bBlock) +CBotStack* CBotStack::AddStack2(bool bBlock) { if (m_next2 != NULL) { @@ -322,9 +324,9 @@ CBotStack* CBotStack::AddStack2(BOOL bBlock) return p; } -BOOL CBotStack::Return(CBotStack* pfils) +bool CBotStack::Return(CBotStack* pfils) { - if ( pfils == this ) return TRUE; // spcial + if ( pfils == this ) return true; // spcial if (m_var != NULL) delete m_var; // valeur remplace ? m_var = pfils->m_var; // rsultat transmis @@ -336,9 +338,9 @@ BOOL CBotStack::Return(CBotStack* pfils) return (m_error == 0); // interrompu si erreur } -BOOL CBotStack::StackOver() +bool CBotStack::StackOver() { - return FALSE; // pas de test de dbordement dans cette version + return false; // pas de test de dbordement dans cette version } #endif @@ -377,38 +379,38 @@ CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) // routine pour l'excution pas pas -BOOL CBotStack::IfStep() +bool CBotStack::IfStep() { - if ( m_initimer > 0 || m_step++ > 0 ) return FALSE; - return TRUE; + if ( m_initimer > 0 || m_step++ > 0 ) return false; + return true; } -BOOL CBotStack::BreakReturn(CBotStack* pfils, const char* name) +bool CBotStack::BreakReturn(CBotStack* pfils, const char* name) { - if ( m_error>=0 ) return FALSE; // sortie normale - if ( m_error==-3 ) return FALSE; // sortie normale (return en cours) + if ( m_error>=0 ) return false; // sortie normale + if ( m_error==-3 ) return false; // sortie normale (return en cours) if (!m_labelBreak.IsEmpty() && (name[0] == 0 || m_labelBreak != name)) - return FALSE; // c'est pas pour moi + return false; // c'est pas pour moi m_error = 0; m_labelBreak.Empty(); return Return(pfils); } -BOOL CBotStack::IfContinue(int state, const char* name) +bool CBotStack::IfContinue(int state, const char* name) { - if ( m_error != -2 ) return FALSE; + if ( m_error != -2 ) return false; if (!m_labelBreak.IsEmpty() && (name == NULL || m_labelBreak != name)) - return FALSE; // c'est pas pour moi + return false; // c'est pas pour moi m_state = state; // o reprendre ? m_error = 0; m_labelBreak.Empty(); if ( m_next != EOX ) m_next->Delete(); // purge la pile au dessus - return TRUE; + return true; } void CBotStack::SetBreak(int val, const char* name) @@ -424,7 +426,7 @@ void CBotStack::SetBreak(int val, const char* name) // remet sur la pile la valeur calcule par le dernier CBotReturn -BOOL CBotStack::GivRetVar(BOOL bRet) +bool CBotStack::GivRetVar(bool bRet) { if (m_error == -3) { @@ -432,7 +434,7 @@ BOOL CBotStack::GivRetVar(BOOL bRet) m_var = m_retvar; m_retvar = NULL; m_error = 0; - return TRUE; + return true; } return bRet; // interrompu par autre chose que return } @@ -469,7 +471,7 @@ void CBotStack::SetType(CBotTypResult& type) // trouve une variable par son token // ce peut tre une variable compose avec un point -CBotVar* CBotStack::FindVar(CBotToken* &pToken, BOOL bUpdate, BOOL bModif) +CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif) { CBotStack* p = this; CBotString name = pToken->GivString(); @@ -482,7 +484,7 @@ CBotVar* CBotStack::FindVar(CBotToken* &pToken, BOOL bUpdate, BOOL bModif) if (pp->GivName() == name) { if ( bUpdate ) - pp->Maj(m_pUser, FALSE); + pp->Maj(m_pUser, false); return pp; } @@ -515,7 +517,7 @@ CBotVar* CBotStack::FindVar(const char* name) // retrouve une variable sur la pile selon son numro d'identification // ce qui va plus vite que de comparer les noms. -CBotVar* CBotStack::FindVar(long ident, BOOL bUpdate, BOOL bModif) +CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif) { CBotStack* p = this; while (p != NULL) @@ -526,7 +528,7 @@ CBotVar* CBotStack::FindVar(long ident, BOOL bUpdate, BOOL bModif) if (pp->GivUniqNum() == ident) { if ( bUpdate ) - pp->Maj(m_pUser, FALSE); + pp->Maj(m_pUser, false); return pp; } @@ -538,14 +540,14 @@ CBotVar* CBotStack::FindVar(long ident, BOOL bUpdate, BOOL bModif) } -CBotVar* CBotStack::FindVar(CBotToken& Token, BOOL bUpdate, BOOL bModif) +CBotVar* CBotStack::FindVar(CBotToken& Token, bool bUpdate, bool bModif) { CBotToken* pt = &Token; return FindVar(pt, bUpdate, bModif); } -CBotVar* CBotStack::CopyVar(CBotToken& Token, BOOL bUpdate) +CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) { CBotVar* pVar = FindVar( Token, bUpdate ); @@ -557,7 +559,7 @@ CBotVar* CBotStack::CopyVar(CBotToken& Token, BOOL bUpdate) } -BOOL CBotStack::SetState(int n, int limite) +bool CBotStack::SetState(int n, int limite) { m_state = n; @@ -565,7 +567,7 @@ BOOL CBotStack::SetState(int n, int limite) return ( m_timer > limite ); // interrompu si timer pass } -BOOL CBotStack::IncState(int limite) +bool CBotStack::IncState(int limite) { m_state++; @@ -603,7 +605,7 @@ void CBotStack::SetTimer(int n) m_initimer = n; } -BOOL CBotStack::Execute() +bool CBotStack::Execute() { CBotCall* instr = NULL; // instruction la plus leve CBotStack* pile; @@ -621,9 +623,9 @@ BOOL CBotStack::Execute() p = p->m_next; } - if ( instr == NULL ) return TRUE; // excution normale demande + if ( instr == NULL ) return true; // excution normale demande - if (!instr->Run(pile)) return FALSE; // excution partir de l + if (!instr->Run(pile)) return false; // excution partir de l #if STACKMEM pile->m_next->Delete(); @@ -632,7 +634,7 @@ BOOL CBotStack::Execute() #endif pile->m_next = EOX; // spcial pour reprise - return TRUE; + return true; } // met sur le stack le pointeur une variable @@ -711,10 +713,10 @@ void CBotStack::AddVar(CBotVar* pVar) void CBotStack::SetBotCall(CBotProgram* p) { m_prog = p; - m_bFunc = TRUE; + m_bFunc = true; } -CBotProgram* CBotStack::GivBotCall(BOOL bFirst) +CBotProgram* CBotStack::GivBotCall(bool bFirst) { if ( ! bFirst ) return m_prog; CBotStack* p = this; @@ -728,7 +730,7 @@ void* CBotStack::GivPUser() } -BOOL CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype) +bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype) { CBotTypResult res; @@ -750,7 +752,7 @@ BOOL CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo if (res.GivType() >= 0) return res.GivType(); SetError(TX_NOCALL, token); - return TRUE; + return true; } void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) @@ -762,17 +764,17 @@ void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) } -BOOL SaveVar(FILE* pf, CBotVar* pVar) +bool SaveVar(FILE* pf, CBotVar* pVar) { - while ( TRUE ) + while ( true ) { if ( pVar == NULL ) { return WriteWord(pf, 0); // met un terminateur } - if ( !pVar->Save0State(pf)) return FALSE; // entte commune - if ( !pVar->Save1State(pf) ) return FALSE; // sauve selon la classe fille + if ( !pVar->Save0State(pf)) return false; // entte commune + if ( !pVar->Save1State(pf) ) return false; // sauve selon la classe fille pVar = pVar->GivNext(); } @@ -856,7 +858,7 @@ CBotVar* CBotStack::GivStackVars(const char* &FunctionName, int level) return p->m_listVar; } -BOOL CBotStack::SaveState(FILE* pf) +bool CBotStack::SaveState(FILE* pf) { if ( this == NULL ) // fin de l'arbre ? { @@ -865,33 +867,33 @@ BOOL CBotStack::SaveState(FILE* pf) if ( m_next2 != NULL ) { - if (!WriteWord(pf, 2)) return FALSE; // une marque de poursuite - if (!m_next2->SaveState(pf)) return FALSE; + if (!WriteWord(pf, 2)) return false; // une marque de poursuite + if (!m_next2->SaveState(pf)) return false; } else { - if (!WriteWord(pf, 1)) return FALSE; // une marque de poursuite + if (!WriteWord(pf, 1)) return false; // une marque de poursuite } - if (!WriteWord(pf, m_bBlock)) return FALSE; // est-ce un bloc local - if (!WriteWord(pf, m_state)) return FALSE; // dans quel tat - if (!WriteWord(pf, 0)) return FALSE; // par compatibilit m_bDontDelete - if (!WriteWord(pf, m_step)) return FALSE; // dans quel tat + if (!WriteWord(pf, m_bBlock)) return false; // est-ce un bloc local + if (!WriteWord(pf, m_state)) return false; // dans quel tat + if (!WriteWord(pf, 0)) return false; // par compatibilit m_bDontDelete + if (!WriteWord(pf, m_step)) return false; // dans quel tat - if (!SaveVar(pf, m_var)) return FALSE; // le rsultat courant - if (!SaveVar(pf, m_listVar)) return FALSE; // les variables locales + if (!SaveVar(pf, m_var)) return false; // le rsultat courant + if (!SaveVar(pf, m_listVar)) return false; // les variables locales return m_next->SaveState(pf); // enregistre la suite } -BOOL CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) +bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) { - WORD w; + unsigned short w; pStack = NULL; - if (!ReadWord(pf, w)) return FALSE; - if ( w == 0 ) return TRUE; + if (!ReadWord(pf, w)) return false; + if ( w == 0 ) return true; #if STACKMEM if ( this == NULL ) pStack = FirstStack(); @@ -902,81 +904,81 @@ BOOL CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) if ( w == 2 ) { - if (!pStack->RestoreState(pf, pStack->m_next2)) return FALSE; + if (!pStack->RestoreState(pf, pStack->m_next2)) return false; } - if (!ReadWord(pf, w)) return FALSE; // est-ce un bloc local + if (!ReadWord(pf, w)) return false; // est-ce un bloc local pStack->m_bBlock = w; - if (!ReadWord(pf, w)) return FALSE; // dans quel tat j're ? + if (!ReadWord(pf, w)) return false; // dans quel tat j're ? pStack->SetState((short)w); // dans le bon tat - if (!ReadWord(pf, w)) return FALSE; // dont delete ? + if (!ReadWord(pf, w)) return false; // dont delete ? // plus utilis - if (!ReadWord(pf, w)) return FALSE; // pas pas + if (!ReadWord(pf, w)) return false; // pas pas pStack->m_step = w; - if (!CBotVar::RestoreState(pf, pStack->m_var)) return FALSE; // la variable temp - if (!CBotVar::RestoreState(pf, pStack->m_listVar)) return FALSE;// les variables locales + if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // la variable temp + if (!CBotVar::RestoreState(pf, pStack->m_listVar)) return false;// les variables locales return pStack->RestoreState(pf, pStack->m_next); } -BOOL CBotVar::Save0State(FILE* pf) +bool CBotVar::Save0State(FILE* pf) { - if (!WriteWord(pf, 100+m_mPrivate))return FALSE; // variable prive ? - if (!WriteWord(pf, m_bStatic))return FALSE; // variable static ? - if (!WriteWord(pf, m_type.GivType()))return FALSE; // enregiste le type (toujours non nul) - if (!WriteWord(pf, m_binit))return FALSE; // variable dfinie ? + if (!WriteWord(pf, 100+m_mPrivate))return false; // variable prive ? + if (!WriteWord(pf, m_bStatic))return false; // variable static ? + if (!WriteWord(pf, m_type.GivType()))return false; // enregiste le type (toujours non nul) + if (!WriteWord(pf, m_binit))return false; // variable dfinie ? return WriteString(pf, m_token->GivString()); // et le nom de la variable } -BOOL CBotVarInt::Save0State(FILE* pf) +bool CBotVarInt::Save0State(FILE* pf) { if ( !m_defnum.IsEmpty() ) { - if(!WriteWord(pf, 200 )) return FALSE; // marqueur spcial - if(!WriteString(pf, m_defnum)) return FALSE; // nom de la valeur + if(!WriteWord(pf, 200 )) return false; // marqueur spcial + if(!WriteString(pf, m_defnum)) return false; // nom de la valeur } return CBotVar::Save0State(pf); } -BOOL CBotVarInt::Save1State(FILE* pf) +bool CBotVarInt::Save1State(FILE* pf) { return WriteWord(pf, m_val); // la valeur de la variable } -BOOL CBotVarBoolean::Save1State(FILE* pf) +bool CBotVarBoolean::Save1State(FILE* pf) { return WriteWord(pf, m_val); // la valeur de la variable } -BOOL CBotVarFloat::Save1State(FILE* pf) +bool CBotVarFloat::Save1State(FILE* pf) { return WriteFloat(pf, m_val); // la valeur de la variable } -BOOL CBotVarString::Save1State(FILE* pf) +bool CBotVarString::Save1State(FILE* pf) { return WriteString(pf, m_val); // la valeur de la variable } -BOOL CBotVarClass::Save1State(FILE* pf) +bool CBotVarClass::Save1State(FILE* pf) { - if ( !WriteType(pf, m_type) ) return FALSE; - if ( !WriteLong(pf, m_ItemIdent) ) return FALSE; + if ( !WriteType(pf, m_type) ) return false; + if ( !WriteLong(pf, m_ItemIdent) ) return false; return SaveVar(pf, m_pVar); // contenu de l'objet } -BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) +bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) { - WORD w, wi, prv, st; + unsigned short w, wi, prv, st; float ww; CBotString name, s; @@ -986,31 +988,31 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) CBotVar* pNew = NULL; CBotVar* pPrev = NULL; - while ( TRUE ) // recupre toute une liste + while ( true ) // recupre toute une liste { - if (!ReadWord(pf, w)) return FALSE; // priv ou type ? - if ( w == 0 ) return TRUE; + if (!ReadWord(pf, w)) return false; // priv ou type ? + if ( w == 0 ) return true; CBotString defnum; if ( w == 200 ) { - if (!ReadString(pf, defnum)) return FALSE; // nombre avec un identifiant - if (!ReadWord(pf, w)) return FALSE; // type + if (!ReadString(pf, defnum)) return false; // nombre avec un identifiant + if (!ReadWord(pf, w)) return false; // type } prv = 100; st = 0; if ( w >= 100 ) { prv = w; - if (!ReadWord(pf, st)) return FALSE; // statique - if (!ReadWord(pf, w)) return FALSE; // type + if (!ReadWord(pf, st)) return false; // statique + if (!ReadWord(pf, w)) return false; // type } if ( w == CBotTypClass ) w = CBotTypIntrinsic; // forcment intrinsque - if (!ReadWord(pf, wi)) return FALSE; // init ? + if (!ReadWord(pf, wi)) return false; // init ? - if (!ReadString(pf, name)) return FALSE; // nom de la variable + if (!ReadString(pf, name)) return false; // nom de la variable CBotToken token(name, CBotString()); @@ -1019,17 +1021,17 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) case CBotTypInt: case CBotTypBoolean: pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadWord(pf, w)) return FALSE; + if (!ReadWord(pf, w)) return false; pNew->SetValInt((short)w, defnum); break; case CBotTypFloat: pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadFloat(pf, ww)) return FALSE; + if (!ReadFloat(pf, ww)) return false; pNew->SetValFloat(ww); break; case CBotTypString: pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadString(pf, s)) return FALSE; + if (!ReadString(pf, s)) return false; pNew->SetValString(s); break; @@ -1039,17 +1041,17 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) { CBotTypResult r; long id; - if (!ReadType(pf, r)) return FALSE; // type complet - if (!ReadLong(pf, id) ) return FALSE; + if (!ReadType(pf, r)) return false; // type complet + if (!ReadLong(pf, id) ) return false; -// if (!ReadString(pf, s)) return FALSE; +// if (!ReadString(pf, s)) return false; { CBotVar* p = NULL; if ( id ) p = CBotVarClass::Find(id) ; pNew = new CBotVarClass(&token, r); // cre directement une instance // attention cptuse = 0 - if ( !RestoreState(pf, ((CBotVarClass*)pNew)->m_pVar)) return FALSE; + if ( !RestoreState(pf, ((CBotVarClass*)pNew)->m_pVar)) return false; pNew->SetIdent(id); if ( p != NULL ) @@ -1063,7 +1065,7 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) case CBotTypPointer: case CBotTypNullPointer: - if (!ReadString(pf, s)) return FALSE; + if (!ReadString(pf, s)) return false; { pNew = CBotVar::Create(&token, CBotTypResult(w, s));// cre une variable CBotVarClass* p = NULL; @@ -1073,7 +1075,7 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) // restitue une copie de l'instance d'origine CBotVar* pInstance = NULL; - if ( !CBotVar::RestoreState( pf, pInstance ) ) return FALSE; + if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus // if ( p != NULL ) ((CBotVarPointer*)pNew)->SetPointer( p ); // plutt celui-ci ! @@ -1084,13 +1086,13 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) case CBotTypArrayPointer: { CBotTypResult r; - if (!ReadType(pf, r)) return FALSE; + if (!ReadType(pf, r)) return false; pNew = CBotVar::Create(&token, r); // cre une variable // restitue une copie de l'instance d'origine CBotVar* pInstance = NULL; - if ( !CBotVar::RestoreState( pf, pInstance ) ) return FALSE; + if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus } break; @@ -1106,7 +1108,7 @@ BOOL CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) pNew->SetPrivate(prv-100); pPrev = pNew; } - return TRUE; + return true; } @@ -1133,12 +1135,12 @@ CBotCStack::CBotCStack(CBotCStack* ppapa) m_error = 0; m_start = 0; m_end = 0; - m_bBlock = TRUE; + m_bBlock = true; } else { m_start = ppapa->m_start; - m_bBlock = FALSE; + m_bBlock = false; } m_listVar = NULL; @@ -1156,7 +1158,7 @@ CBotCStack::~CBotCStack() } // utilis uniquement la compilation -CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, BOOL bBlock) +CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) { if (m_next != NULL) return m_next; // reprise dans une pile existante @@ -1291,7 +1293,7 @@ CBotVar* CBotCStack::CopyVar(CBotToken& Token) return pCopy; } -BOOL CBotCStack::IsOk() +bool CBotCStack::IsOk() { return (m_error == 0); } @@ -1325,15 +1327,15 @@ void CBotCStack::ResetError(int n, int start, int end) m_end = end; } -BOOL CBotCStack::NextToken(CBotToken* &p) +bool CBotCStack::NextToken(CBotToken* &p) { CBotToken* pp = p; p = p->GivNext(); - if (p!=NULL) return TRUE; + if (p!=NULL) return true; SetError(TX_ENDOF, pp->GivEnd()); - return FALSE; + return false; } void CBotCStack::SetBotCall(CBotProgram* p) @@ -1398,7 +1400,7 @@ void CBotCStack::AddVar(CBotVar* pVar) // test si une variable est dj dfinie localement -BOOL CBotCStack::CheckVarLocal(CBotToken* &pToken) +bool CBotCStack::CheckVarLocal(CBotToken* &pToken) { CBotCStack* p = this; CBotString name = pToken->GivString(); @@ -1409,13 +1411,13 @@ BOOL CBotCStack::CheckVarLocal(CBotToken* &pToken) while ( pp != NULL) { if (name == pp->GivName()) - return TRUE; + return true; pp = pp->m_next; } - if ( p->m_bBlock ) return FALSE; + if ( p->m_bBlock ) return false; p = p->m_prev; } - return FALSE; + return false; } CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) @@ -1440,11 +1442,11 @@ CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nId // test si un nom de procdure est dj dfini quelque part -BOOL CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) +bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { CBotString name = pToken->GivString(); - if ( CBotCall::CheckCall(name) ) return TRUE; + if ( CBotCall::CheckCall(name) ) return true; CBotFunction* pp = m_prog->GivFunctions(); while ( pp != NULL ) @@ -1453,7 +1455,7 @@ BOOL CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { // les paramtres sont-ils exactement les mmes ? if ( pp->CheckParam( pParam ) ) - return TRUE; + return true; } pp = pp->Next(); } @@ -1465,11 +1467,11 @@ BOOL CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { // les paramtres sont-ils exactement les mmes ? if ( pp->CheckParam( pParam ) ) - return TRUE; + return true; } pp = pp->m_nextpublic; } - return FALSE; + return false; } diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 53b0f27..5f35337 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -19,9 +19,11 @@ #include "CBot.h" -#include - -HINSTANCE CBotString::m_hInstance = (HINSTANCE)LoadLibrary("libCbot.dll"); // comment le rcuprer autrement ?? +#include +#include +#include +/// TODO need to be implemented to be able to load library +// HINSTANCE CBotString::m_hInstance = (HINSTANCE)LoadLibrary("libCbot.dll"); // how to retrieve it otherwise ?? CBotString::CBotString() @@ -38,13 +40,13 @@ CBotString::~CBotString() CBotString::CBotString(const char* p) { - m_lg = lstrlen( p ); + m_lg = strlen( p ); m_ptr = NULL; if (m_lg>0) { m_ptr = (char*)malloc(m_lg+1); - lstrcpy(m_ptr, p); + strcpy(m_ptr, p); } } @@ -56,7 +58,7 @@ CBotString::CBotString(const CBotString& srcString) if (m_lg>0) { m_ptr = (char*)malloc(m_lg+1); - lstrcpy(m_ptr, srcString.m_ptr); + strcpy(m_ptr, srcString.m_ptr); } } @@ -66,7 +68,7 @@ CBotString::CBotString(const CBotString& srcString) int CBotString::GivLength() { if ( m_ptr == NULL ) return 0; - return lstrlen( m_ptr ); + return strlen( m_ptr ); } @@ -143,10 +145,10 @@ int CBotString::Find(const char c) return -1; } -int CBotString::Find(LPCTSTR lpsz) +int CBotString::Find(const char * lpsz) { int i, j; - int l = lstrlen(lpsz); + int l = strlen(lpsz); for (i = 0; i <= m_lg-l; i++) { @@ -170,10 +172,10 @@ int CBotString::ReverseFind(const char c) return -1; } -int CBotString::ReverseFind(LPCTSTR lpsz) +int CBotString::ReverseFind(const char * lpsz) { int i, j; - int l = lstrlen(lpsz); + int l = strlen(lpsz); for (i = m_lg-l; i >= 0; i--) { @@ -195,7 +197,7 @@ CBotString CBotString::Mid(int start, int lg) if ( lg < 0 ) lg = m_lg - start; char* p = (char*)malloc(m_lg+1); - lstrcpy(p, m_ptr+start); + strcpy(p, m_ptr+start); p[lg] = 0; res = p; @@ -229,11 +231,11 @@ void CBotString::MakeLower() #define MAXSTRING 256 -BOOL CBotString::LoadString(UINT id) +bool CBotString::LoadString(unsigned int id) { char buffer[MAXSTRING]; - - m_lg = ::LoadString( m_hInstance, id, buffer, MAXSTRING ); + /// \TODO implement loading strings from resources. Figure out how to do it + // m_lg = ::LoadString( m_hInstance, id, buffer, MAXSTRING ); if (m_ptr != NULL) free(m_ptr); @@ -241,10 +243,10 @@ BOOL CBotString::LoadString(UINT id) if (m_lg > 0) { m_ptr = (char*)malloc(m_lg+1); - lstrcpy(m_ptr, buffer); - return TRUE; + strcpy(m_ptr, buffer); + return true; } - return FALSE; + return false; } @@ -258,13 +260,13 @@ const CBotString& CBotString::operator=(const CBotString& stringSrc) if (m_lg > 0) { m_ptr = (char*)malloc(m_lg+1); - lstrcpy(m_ptr, stringSrc.m_ptr); + strcpy(m_ptr, stringSrc.m_ptr); } return *this; } -CBotString operator+(const CBotString& string, LPCTSTR lpsz) +CBotString operator+(const CBotString& string, const char * lpsz) { CBotString s ( string ); s += lpsz; @@ -275,9 +277,9 @@ const CBotString& CBotString::operator+(const CBotString& stringSrc) { char* p = (char*)malloc(m_lg+stringSrc.m_lg+1); - lstrcpy(p, m_ptr); + strcpy(p, m_ptr); char* pp = p + m_lg; - lstrcpy(pp, stringSrc.m_ptr); + strcpy(pp, stringSrc.m_ptr); if (m_ptr != NULL) free(m_ptr); m_ptr = p; @@ -306,12 +308,12 @@ const CBotString& CBotString::operator=(const char* pString) if ( pString != NULL ) { - m_lg = lstrlen(pString); + m_lg = strlen(pString); if (m_lg != 0) { m_ptr = (char*)malloc(m_lg+1); - lstrcpy(m_ptr, pString); + strcpy(m_ptr, pString); } } @@ -323,7 +325,7 @@ const CBotString& CBotString::operator+=(const char ch) { char* p = (char*)malloc(m_lg+2); - if (m_ptr!=NULL) lstrcpy(p, m_ptr); + if (m_ptr!=NULL) strcpy(p, m_ptr); p[m_lg++] = ch; p[m_lg] = 0; @@ -338,9 +340,9 @@ const CBotString& CBotString::operator+=(const CBotString& str) { char* p = (char*)malloc(m_lg+str.m_lg+1); - lstrcpy(p, m_ptr); + strcpy(p, m_ptr); char* pp = p + m_lg; - lstrcpy(pp, str.m_ptr); + strcpy(pp, str.m_ptr); m_lg = m_lg + str.m_lg; @@ -351,67 +353,67 @@ const CBotString& CBotString::operator+=(const CBotString& str) return *this; } -BOOL CBotString::operator==(const CBotString& str) +bool CBotString::operator==(const CBotString& str) { return Compare(str) == 0; } -BOOL CBotString::operator==(const char* p) +bool CBotString::operator==(const char* p) { return Compare(p) == 0; } -BOOL CBotString::operator!=(const CBotString& str) +bool CBotString::operator!=(const CBotString& str) { return Compare(str) != 0; } -BOOL CBotString::operator!=(const char* p) +bool CBotString::operator!=(const char* p) { return Compare(p) != 0; } -BOOL CBotString::operator>(const CBotString& str) +bool CBotString::operator>(const CBotString& str) { return Compare(str) > 0; } -BOOL CBotString::operator>(const char* p) +bool CBotString::operator>(const char* p) { return Compare(p) > 0; } -BOOL CBotString::operator>=(const CBotString& str) +bool CBotString::operator>=(const CBotString& str) { return Compare(str) >= 0; } -BOOL CBotString::operator>=(const char* p) +bool CBotString::operator>=(const char* p) { return Compare(p) >= 0; } -BOOL CBotString::operator<(const CBotString& str) +bool CBotString::operator<(const CBotString& str) { return Compare(str) < 0; } -BOOL CBotString::operator<(const char* p) +bool CBotString::operator<(const char* p) { return Compare(p) < 0; } -BOOL CBotString::operator<=(const CBotString& str) +bool CBotString::operator<=(const CBotString& str) { return Compare(str) <= 0; } -BOOL CBotString::operator<=(const char* p) +bool CBotString::operator<=(const char* p) { return Compare(p) <= 0; } -BOOL CBotString::IsEmpty() const +bool CBotString::IsEmpty() const { return (m_lg == 0); } @@ -423,20 +425,20 @@ void CBotString::Empty() m_lg = 0; } -static char nilstring[] = {0}; +static char emptyString[] = {0}; -CBotString::operator LPCTSTR() const +CBotString::operator const char * () const { - if (this == NULL || m_ptr == NULL) return nilstring; + if (this == NULL || m_ptr == NULL) return emptyString; return m_ptr; } -int CBotString::Compare(LPCTSTR lpsz) const +int CBotString::Compare(const char * lpsz) const { char* p = m_ptr; - if (lpsz == NULL) lpsz = nilstring; - if (m_ptr == NULL) p = nilstring; + if (lpsz == NULL) lpsz = emptyString; + if (m_ptr == NULL) p = emptyString; return strcmp(p, lpsz); // wcscmp } @@ -527,14 +529,14 @@ void CBotStringArray::SetSize(int nNewSize) // shrink to nothing DestructElements(m_pData, m_nSize); - delete[] (BYTE*)m_pData; + delete[] (unsigned char *)m_pData; m_pData = NULL; m_nSize = m_nMaxSize = 0; } else if (m_pData == NULL) { // create one with exact size - m_pData = (CBotString*) new BYTE[nNewSize * sizeof(CBotString)]; + m_pData = (CBotString*) new unsigned char[nNewSize * sizeof(CBotString)]; ConstructElements(m_pData, nNewSize); @@ -563,7 +565,7 @@ void CBotStringArray::SetSize(int nNewSize) { // heuristically determine growth when nGrowBy == 0 // (this avoids heap fragmentation in many situations) - nGrowBy = min(1024, max(4, m_nSize / 8)); + nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); } int nNewMax; if (nNewSize < m_nMaxSize + nGrowBy) @@ -571,7 +573,7 @@ void CBotStringArray::SetSize(int nNewSize) else nNewMax = nNewSize; // no slush - CBotString* pNewData = (CBotString*) new BYTE[nNewMax * sizeof(CBotString)]; + CBotString* pNewData = (CBotString*) new unsigned char[nNewMax * sizeof(CBotString)]; // copy new data from old memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); @@ -581,7 +583,7 @@ void CBotStringArray::SetSize(int nNewSize) // Ret rid of old stuff (note: no destructors called) - delete[] (BYTE*)m_pData; + delete[] (unsigned char *)m_pData; m_pData = pNewData; m_nSize = nNewSize; m_nMaxSize = nNewMax; diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index fbf6726..1b6392c 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -22,6 +22,7 @@ #include "CBot.h" +#include CBotStringArray CBotToken::m_ListKeyWords; int CBotToken::m_ListIdKeyWords[200]; @@ -188,27 +189,27 @@ void CBotToken::SetPos(int start, int end) m_end = end; } -BOOL CharInList(const char c, const char* list) +bool CharInList(const char c, const char* list) { int i = 0; - while (TRUE) + while (true) { - if (c == list[i++]) return TRUE; - if (list[i] == 0) return FALSE; + if (c == list[i++]) return true; + if (list[i] == 0) return false; } } -BOOL Char2InList(const char c, const char cc, const char* list) +bool Char2InList(const char c, const char cc, const char* list) { int i = 0; - while (TRUE) + while (true) { if (c == list[i++] && - cc == list[i++]) return TRUE; + cc == list[i++]) return true; - if (list[i] == 0) return FALSE; + if (list[i] == 0) return false; } } @@ -224,12 +225,12 @@ static char* nch = "\"\r\n\t"; // refus // cherche le prochain token dans une phrase // ne doit pas commencer par des sparateurs // qui sont pris avec le token prcdent -CBotToken* CBotToken::NextToken(char* &program, int& error, BOOL first) +CBotToken* CBotToken::NextToken(char* &program, int& error, bool first) { CBotString mot; // le mot trouv CBotString sep; // les sparateurs qui le suivent char c; - BOOL stop = first; + bool stop = first; if (*program == 0) return NULL; @@ -262,14 +263,14 @@ CBotToken* CBotToken::NextToken(char* &program, int& error, BOOL first) mot += c; // chane complte c = *(program++); // prochain caractre } - stop = TRUE; + stop = true; } // cas particulier pour les nombres if ( CharInList(mot[0], num )) { - BOOL bdot = FALSE; // trouv un point ? - BOOL bexp = FALSE; // trouv un exposant ? + bool bdot = false; // trouv un point ? + bool bexp = false; // trouv un exposant ? char* liste = num; if (mot[0] == '0' && c == 'x') // valeur hexadcimale ? @@ -286,10 +287,10 @@ cc: mot += c; } if ( liste == num ) // pas pour les exadcimaux { - if ( !bdot && c == '.' ) { bdot = TRUE; goto cc; } + if ( !bdot && c == '.' ) { bdot = true; goto cc; } if ( !bexp && ( c == 'e' || c == 'E' ) ) { - bexp = TRUE; + bexp = true; mot += c; c = *(program++); // prochain caractre if ( c == '-' || @@ -298,7 +299,7 @@ cc: mot += c; } } - stop = TRUE; + stop = true; } if (CharInList(mot[0], sep3)) // un sparateur oprationnel ? @@ -310,13 +311,13 @@ cc: mot += c; c = *(program++); // prochain caractre } - stop = TRUE; + stop = true; } } - while (TRUE) + while (true) { if (stop || c == 0 || CharInList(c, sep1)) { @@ -381,7 +382,7 @@ CBotToken* CBotToken::CompileTokens(const char* program, int& error) int pos = 0; error = 0; - prv = tokenbase = NextToken(p, error, TRUE); + prv = tokenbase = NextToken(p, error, true); if (tokenbase == NULL) return NULL; @@ -443,7 +444,7 @@ int CBotToken::GivKeyWords(const char* w) return -1; } -BOOL CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) +bool CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) { int i; int l = m_ListKeyDefine.GivSize(); @@ -454,11 +455,11 @@ BOOL CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) { token->m_IdKeyWord = m_ListKeyNums[i]; token->m_type = TokenTypDef; - return TRUE; + return true; } } - return FALSE; + return false; } // reprend la liste des mots clefs dans les ressources @@ -498,36 +499,36 @@ void CBotToken::LoadKeyWords() } } -BOOL CBotToken::DefineNum(const char* name, long val) +bool CBotToken::DefineNum(const char* name, long val) { int i; int l = m_ListKeyDefine.GivSize(); for (i = 0; i < l; i++) { - if (m_ListKeyDefine[i] == name) return FALSE; + if (m_ListKeyDefine[i] == name) return false; } - if ( i == MAXDEFNUM ) return FALSE; + if ( i == MAXDEFNUM ) return false; m_ListKeyDefine.Add( name ); m_ListKeyNums[i] = val; - return TRUE; + return true; } -BOOL IsOfType(CBotToken* &p, int type1, int type2) +bool IsOfType(CBotToken* &p, int type1, int type2) { if (p->GivType() == type1 || p->GivType() == type2 ) { p = p->GivNext(); - return TRUE; + return true; } - return FALSE; + return false; } // idem avec un nombre indfini d'arguments // il faut mettre un zro comme dernier argument -BOOL IsOfTypeList(CBotToken* &p, int type1, ...) +bool IsOfTypeList(CBotToken* &p, int type1, ...) { int i = type1; int max = 20; @@ -536,18 +537,18 @@ BOOL IsOfTypeList(CBotToken* &p, int type1, ...) va_list marker; va_start( marker, type1 ); /* Initialize variable arguments. */ - while (TRUE) + while (true) { if (type == i) { p = p->GivNext(); va_end( marker ); /* Reset variable arguments. */ - return TRUE; + return true; } if (--max == 0 || 0 == (i = va_arg( marker, int))) { va_end( marker ); /* Reset variable arguments. */ - return FALSE; + return false; } } } diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index 2d92409..62e9bf3 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -33,5 +33,5 @@ // ) -extern BOOL IsOfType(CBotToken* &p, int type1, int type2 = -1); -extern BOOL IsOfTypeList(CBotToken* &p, int type1, ...); +extern bool IsOfType(CBotToken* &p, int type1, int type2 = -1); +extern bool IsOfTypeList(CBotToken* &p, int type1, ...); diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotTwoOpExpr.cpp index d8f4ee7..e2523b5 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotTwoOpExpr.cpp @@ -95,19 +95,19 @@ static int ListOp[] = 0, }; -BOOL IsInList( int val, int* list, int& typemasque ) +bool IsInList( int val, int* list, int& typemasque ) { - while (TRUE) + while (true) { - if ( *list == 0 ) return FALSE; + if ( *list == 0 ) return false; typemasque = *list++; - if ( *list++ == val ) return TRUE; + if ( *list++ == val ) return true; } } -BOOL TypeOk( int type, int test ) +bool TypeOk( int type, int test ) { - while (TRUE) + while (true) { if ( type == 0 ) return (test & 1); type--; test /= 2; @@ -279,43 +279,43 @@ CBotInstr* CBotTwoOpExpr::Compile(CBotToken* &p, CBotCStack* pStack, int* pOpera } -BOOL IsNan(CBotVar* left, CBotVar* right, int* err = NULL) +bool IsNan(CBotVar* left, CBotVar* right, int* err = NULL) { if ( left ->GivInit() > IS_DEF || right->GivInit() > IS_DEF ) { if ( err != NULL ) *err = TX_OPNAN ; - return TRUE; + return true; } - return FALSE; + return false; } // fait l'opration sur 2 oprandes -BOOL CBotTwoOpExpr::Execute(CBotStack* &pStack) +bool CBotTwoOpExpr::Execute(CBotStack* &pStack) { CBotStack* pStk1 = pStack->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise -// if ( pStk1 == EOX ) return TRUE; +// if ( pStk1 == EOX ) return true; // selon la reprise, on peut tre dans l'un des 2 tats if ( pStk1->GivState() == 0 ) // 1er tat, value l'oprande de gauche { - if (!m_leftop->Execute(pStk1) ) return FALSE; // interrompu ici ? + if (!m_leftop->Execute(pStk1) ) return false; // interrompu ici ? // pour les OU et ET logique, n'value pas la seconde expression si pas ncessaire - if ( (GivTokenType() == ID_LOG_AND || GivTokenType() == ID_TXT_AND ) && pStk1->GivVal() == FALSE ) + if ( (GivTokenType() == ID_LOG_AND || GivTokenType() == ID_TXT_AND ) && pStk1->GivVal() == false ) { CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); - res->SetValInt(FALSE); + res->SetValInt(false); pStk1->SetVar(res); return pStack->Return(pStk1); // transmet le rsultat } - if ( (GivTokenType() == ID_LOG_OR||GivTokenType() == ID_TXT_OR) && pStk1->GivVal() == TRUE ) + if ( (GivTokenType() == ID_LOG_OR||GivTokenType() == ID_TXT_OR) && pStk1->GivVal() == true ) { CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); - res->SetValInt(TRUE); + res->SetValInt(true); pStk1->SetVar(res); return pStack->Return(pStk1); // transmet le rsultat } @@ -334,7 +334,7 @@ BOOL CBotTwoOpExpr::Execute(CBotStack* &pStack) // 2e tat, value l'oprande de droite if ( pStk2->GivState() == 0 ) { - if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrompu ici ? + if ( !m_rightop->Execute(pStk2) ) return false; // interrompu ici ? pStk2->IncState(); } @@ -342,7 +342,7 @@ BOOL CBotTwoOpExpr::Execute(CBotStack* &pStack) CBotTypResult type2 = pStk2->GivTypResult(); CBotStack* pStk3 = pStk2->AddStack(this); // ajoute un lment la pile - if ( pStk3->IfStep() ) return FALSE; // montre l'opration si step by step + if ( pStk3->IfStep() ) return false; // montre l'opration si step by step // cre une variable temporaire pour y mettre le rsultat // quel est le type du rsultat ? @@ -475,7 +475,7 @@ BOOL CBotTwoOpExpr::Execute(CBotStack* &pStack) return pStack->Return(pStk2); // transmet le rsultat } -void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, BOOL bMain) +void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, bool bMain) { if ( !bMain ) return; CBotStack* pStk1 = pStack->RestoreStack(this); // ajoute un lment la pile @@ -501,31 +501,31 @@ void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, BOOL bMain) } -BOOL CBotLogicExpr::Execute(CBotStack* &pStack) +bool CBotLogicExpr::Execute(CBotStack* &pStack) { CBotStack* pStk1 = pStack->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise -// if ( pStk1 == EOX ) return TRUE; +// if ( pStk1 == EOX ) return true; if ( pStk1->GivState() == 0 ) { - if ( !m_condition->Execute(pStk1) ) return FALSE; - if (!pStk1->SetState(1)) return FALSE; + if ( !m_condition->Execute(pStk1) ) return false; + if (!pStk1->SetState(1)) return false; } - if ( pStk1->GivVal() == TRUE ) + if ( pStk1->GivVal() == true ) { - if ( !m_op1->Execute(pStk1) ) return FALSE; + if ( !m_op1->Execute(pStk1) ) return false; } else { - if ( !m_op2->Execute(pStk1) ) return FALSE; + if ( !m_op2->Execute(pStk1) ) return false; } return pStack->Return(pStk1); // transmet le rsultat } -void CBotLogicExpr::RestoreState(CBotStack* &pStack, BOOL bMain) +void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) { if ( !bMain ) return; @@ -538,7 +538,7 @@ void CBotLogicExpr::RestoreState(CBotStack* &pStack, BOOL bMain) return; } - if ( pStk1->GivVal() == TRUE ) + if ( pStk1->GivVal() == true ) { m_op1->RestoreState(pStk1, bMain); } @@ -557,7 +557,7 @@ void t() #endif #if 01 -void t(BOOL t) +void t(bool t) { int x; x = 1 + t ? 1 : 3 + 4 * 2 ; diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 37c8fb4..79ba021 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -33,9 +33,9 @@ CBotVar::CBotVar( ) m_InitExpr = NULL; m_LimExpr = NULL; m_type = -1; - m_binit = FALSE; + m_binit = false; m_ident = 0; - m_bStatic = FALSE; + m_bStatic = false; m_mPrivate = 0; } @@ -48,8 +48,8 @@ CBotVarInt::CBotVarInt( const CBotToken* name ) m_InitExpr = NULL; m_LimExpr = NULL; m_type = CBotTypInt; - m_binit = FALSE; - m_bStatic = FALSE; + m_binit = false; + m_bStatic = false; m_mPrivate = 0; m_val = 0; @@ -64,8 +64,8 @@ CBotVarFloat::CBotVarFloat( const CBotToken* name ) m_InitExpr = NULL; m_LimExpr = NULL; m_type = CBotTypFloat; - m_binit = FALSE; - m_bStatic = FALSE; + m_binit = false; + m_bStatic = false; m_mPrivate = 0; m_val = 0; @@ -80,8 +80,8 @@ CBotVarString::CBotVarString( const CBotToken* name ) m_InitExpr = NULL; m_LimExpr = NULL; m_type = CBotTypString; - m_binit = FALSE; - m_bStatic = FALSE; + m_binit = false; + m_bStatic = false; m_mPrivate = 0; m_val.Empty(); @@ -96,8 +96,8 @@ CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) m_InitExpr = NULL; m_LimExpr = NULL; m_type = CBotTypBoolean; - m_binit = FALSE; - m_bStatic = FALSE; + m_binit = false; + m_bStatic = false; m_mPrivate = 0; m_val = 0; @@ -139,10 +139,10 @@ void CBotVarClass::InitCBotVarClass( const CBotToken* name, CBotTypResult& type m_pClass = NULL; m_pParent = NULL; - m_binit = FALSE; - m_bStatic = FALSE; + m_binit = false; + m_bStatic = false; m_mPrivate = 0; - m_bConstructor = FALSE; + m_bConstructor = false; m_CptUse = 0; m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); @@ -189,7 +189,7 @@ CBotVarClass::~CBotVarClass( ) void CBotVarClass::ConstructorSet() { - m_bConstructor = TRUE; + m_bConstructor = true; } @@ -201,9 +201,9 @@ CBotVar::~CBotVar( ) void CBotVar::debug() { - const char* p = (LPCTSTR) m_token->GivString(); - CBotString s = (LPCTSTR) GivValString(); - const char* v = (LPCTSTR) s; + const char* p = (const char*) m_token->GivString(); + CBotString s = (const char*) GivValString(); + const char* v = (const char*) s; if ( m_type.Eq(CBotTypClass) ) { @@ -260,19 +260,19 @@ void* CBotVar::GivUserPtr() return m_pUserPtr; } -BOOL CBotVar::Save1State(FILE* pf) +bool CBotVar::Save1State(FILE* pf) { // cette routine "virtual" ne doit jamais tre appelle, // il doit y avoir une routine pour chaque classe fille (CBotVarInt, CBotVarFloat, etc) // ( voir le type dans m_type ) ASM_TRAP(); - return FALSE; + return false; } -void CBotVar::Maj(void* pUser, BOOL bContinu) +void CBotVar::Maj(void* pUser, bool bContinu) { /* if (!bContinu && m_pMyThis != NULL) - m_pMyThis->Maj(pUser, TRUE);*/ + m_pMyThis->Maj(pUser, true);*/ } @@ -326,7 +326,7 @@ CBotVar* CBotVar::Create(const CBotToken* name, CBotTypResult type) while (type.Eq(CBotTypArrayBody)) { type = type.GivTypElem(); - pv = ((CBotVarArray*)pv)->GivItem(0, TRUE); // cre au moins l'lment [0] + pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] } return array; @@ -397,7 +397,7 @@ CBotVar* CBotVar::Create( const char* n, CBotTypResult type) while (type.Eq(CBotTypArrayBody)) { type = type.GivTypElem(); - pv = ((CBotVarArray*)pv)->GivItem(0, TRUE); // cre au moins l'lment [0] + pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] } return array; @@ -471,7 +471,7 @@ int CBotVar::GivInit() return m_binit; } -void CBotVar::SetInit(BOOL bInit) +void CBotVar::SetInit(int bInit) { m_binit = bInit; if ( bInit == 2 ) m_binit = IS_DEF; // cas spcial @@ -533,14 +533,14 @@ CBotVar* CBotVar::GivItemList() return NULL; } -CBotVar* CBotVar::GivItem(int row, BOOL bGrow) +CBotVar* CBotVar::GivItem(int row, bool bGrow) { ASM_TRAP(); return NULL; } // dit si une variable appartient une classe donne -BOOL CBotVar::IsElemOfClass(const char* name) +bool CBotVar::IsElemOfClass(const char* name) { CBotClass* pc = NULL; @@ -555,11 +555,11 @@ BOOL CBotVar::IsElemOfClass(const char* name) while ( pc != NULL ) { - if ( pc->GivName() == name ) return TRUE; + if ( pc->GivName() == name ) return true; pc = pc->GivParent(); } - return FALSE; + return false; } @@ -611,7 +611,7 @@ void CBotVar::SetVal(CBotVar* var) { delete ((CBotVarClass*)this)->m_pVar; ((CBotVarClass*)this)->m_pVar = NULL; - Copy(var, FALSE); + Copy(var, false); } break; default: @@ -621,7 +621,7 @@ void CBotVar::SetVal(CBotVar* var) m_binit = var->m_binit; // copie l'tat nan s'il y a } -void CBotVar::SetStatic(BOOL bStatic) +void CBotVar::SetStatic(bool bStatic) { m_bStatic = bStatic; } @@ -631,12 +631,12 @@ void CBotVar::SetPrivate(int mPrivate) m_mPrivate = mPrivate; } -BOOL CBotVar::IsStatic() +bool CBotVar::IsStatic() { return m_bStatic; } -BOOL CBotVar::IsPrivate(int mode) +bool CBotVar::IsPrivate(int mode) { return m_mPrivate >= mode; } @@ -715,40 +715,40 @@ void CBotVar::Sub(CBotVar* left, CBotVar* right) ASM_TRAP(); } -BOOL CBotVar::Lo(CBotVar* left, CBotVar* right) +bool CBotVar::Lo(CBotVar* left, CBotVar* right) { ASM_TRAP(); - return FALSE; + return false; } -BOOL CBotVar::Hi(CBotVar* left, CBotVar* right) +bool CBotVar::Hi(CBotVar* left, CBotVar* right) { ASM_TRAP(); - return FALSE; + return false; } -BOOL CBotVar::Ls(CBotVar* left, CBotVar* right) +bool CBotVar::Ls(CBotVar* left, CBotVar* right) { ASM_TRAP(); - return FALSE; + return false; } -BOOL CBotVar::Hs(CBotVar* left, CBotVar* right) +bool CBotVar::Hs(CBotVar* left, CBotVar* right) { ASM_TRAP(); - return FALSE; + return false; } -BOOL CBotVar::Eq(CBotVar* left, CBotVar* right) +bool CBotVar::Eq(CBotVar* left, CBotVar* right) { ASM_TRAP(); - return FALSE; + return false; } -BOOL CBotVar::Ne(CBotVar* left, CBotVar* right) +bool CBotVar::Ne(CBotVar* left, CBotVar* right) { ASM_TRAP(); - return FALSE; + return false; } void CBotVar::And(CBotVar* left, CBotVar* right) @@ -800,7 +800,7 @@ void CBotVar::Dec() ASM_TRAP(); } -void CBotVar::Copy(CBotVar* pSrc, BOOL bName) +void CBotVar::Copy(CBotVar* pSrc, bool bName) { ASM_TRAP(); } @@ -837,7 +837,7 @@ void CBotVar::SetIndirection(CBotVar* pVar) ////////////////////////////////////////////////////////////////////////////////////// // copie une variable dans une autre -void CBotVarInt::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarInt::Copy(CBotVar* pSrc, bool bName) { CBotVarInt* p = (CBotVarInt*)pSrc; @@ -860,7 +860,7 @@ void CBotVarInt::Copy(CBotVar* pSrc, BOOL bName) void CBotVarInt::SetValInt(int val, const char* defnum) { m_val = val; - m_binit = TRUE; + m_binit = true; m_defnum = defnum; } @@ -869,7 +869,7 @@ void CBotVarInt::SetValInt(int val, const char* defnum) void CBotVarInt::SetValFloat(float val) { m_val = (int)val; - m_binit = TRUE; + m_binit = true; } int CBotVarInt::GivValInt() @@ -910,13 +910,13 @@ CBotString CBotVarInt::GivValString() void CBotVarInt::Mul(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() * right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::Power(CBotVar* left, CBotVar* right) { m_val = (int) pow( (double) left->GivValInt() , (double) right->GivValInt() ); - m_binit = TRUE; + m_binit = true; } int CBotVarInt::Div(CBotVar* left, CBotVar* right) @@ -925,7 +925,7 @@ int CBotVarInt::Div(CBotVar* left, CBotVar* right) if ( r != 0 ) { m_val = left->GivValInt() / r; - m_binit = TRUE; + m_binit = true; } return ( r == 0 ? TX_DIVZERO : 0 ); } @@ -936,7 +936,7 @@ int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) if ( r != 0 ) { m_val = left->GivValInt() % r; - m_binit = TRUE; + m_binit = true; } return ( r == 0 ? TX_DIVZERO : 0 ); } @@ -944,43 +944,43 @@ int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) void CBotVarInt::Add(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() + right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::Sub(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() - right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::XOr(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() ^ right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::And(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() & right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::Or(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() | right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::SL(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() << right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::ASR(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() >> right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarInt::SR(CBotVar* left, CBotVar* right) @@ -989,7 +989,7 @@ void CBotVarInt::SR(CBotVar* left, CBotVar* right) int shift = right->GivValInt(); if (shift>=1) source &= 0x7fffffff; m_val = source >> shift; - m_binit = TRUE; + m_binit = true; } void CBotVarInt::Neg() @@ -1014,32 +1014,32 @@ void CBotVarInt::Dec() m_defnum.Empty(); } -BOOL CBotVarInt::Lo(CBotVar* left, CBotVar* right) +bool CBotVarInt::Lo(CBotVar* left, CBotVar* right) { return left->GivValInt() < right->GivValInt(); } -BOOL CBotVarInt::Hi(CBotVar* left, CBotVar* right) +bool CBotVarInt::Hi(CBotVar* left, CBotVar* right) { return left->GivValInt() > right->GivValInt(); } -BOOL CBotVarInt::Ls(CBotVar* left, CBotVar* right) +bool CBotVarInt::Ls(CBotVar* left, CBotVar* right) { return left->GivValInt() <= right->GivValInt(); } -BOOL CBotVarInt::Hs(CBotVar* left, CBotVar* right) +bool CBotVarInt::Hs(CBotVar* left, CBotVar* right) { return left->GivValInt() >= right->GivValInt(); } -BOOL CBotVarInt::Eq(CBotVar* left, CBotVar* right) +bool CBotVarInt::Eq(CBotVar* left, CBotVar* right) { return left->GivValInt() == right->GivValInt(); } -BOOL CBotVarInt::Ne(CBotVar* left, CBotVar* right) +bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) { return left->GivValInt() != right->GivValInt(); } @@ -1048,7 +1048,7 @@ BOOL CBotVarInt::Ne(CBotVar* left, CBotVar* right) ////////////////////////////////////////////////////////////////////////////////////// // copie une variable dans une autre -void CBotVarFloat::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) { CBotVarFloat* p = (CBotVarFloat*)pSrc; @@ -1071,13 +1071,13 @@ void CBotVarFloat::Copy(CBotVar* pSrc, BOOL bName) void CBotVarFloat::SetValInt(int val, const char* s) { m_val = (float)val; - m_binit = TRUE; + m_binit = true; } void CBotVarFloat::SetValFloat(float val) { m_val = val; - m_binit = TRUE; + m_binit = true; } int CBotVarFloat::GivValInt() @@ -1116,13 +1116,13 @@ CBotString CBotVarFloat::GivValString() void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) { m_val = left->GivValFloat() * right->GivValFloat(); - m_binit = TRUE; + m_binit = true; } void CBotVarFloat::Power(CBotVar* left, CBotVar* right) { m_val = (float)pow( left->GivValFloat() , right->GivValFloat() ); - m_binit = TRUE; + m_binit = true; } int CBotVarFloat::Div(CBotVar* left, CBotVar* right) @@ -1131,7 +1131,7 @@ int CBotVarFloat::Div(CBotVar* left, CBotVar* right) if ( r != 0 ) { m_val = left->GivValFloat() / r; - m_binit = TRUE; + m_binit = true; } return ( r == 0 ? TX_DIVZERO : 0 ); } @@ -1142,7 +1142,7 @@ int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) if ( r != 0 ) { m_val = (float)fmod( left->GivValFloat() , r ); - m_binit = TRUE; + m_binit = true; } return ( r == 0 ? TX_DIVZERO : 0 ); } @@ -1150,13 +1150,13 @@ int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) void CBotVarFloat::Add(CBotVar* left, CBotVar* right) { m_val = left->GivValFloat() + right->GivValFloat(); - m_binit = TRUE; + m_binit = true; } void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) { m_val = left->GivValFloat() - right->GivValFloat(); - m_binit = TRUE; + m_binit = true; } void CBotVarFloat::Neg() @@ -1175,32 +1175,32 @@ void CBotVarFloat::Dec() } -BOOL CBotVarFloat::Lo(CBotVar* left, CBotVar* right) +bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) { return left->GivValFloat() < right->GivValFloat(); } -BOOL CBotVarFloat::Hi(CBotVar* left, CBotVar* right) +bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) { return left->GivValFloat() > right->GivValFloat(); } -BOOL CBotVarFloat::Ls(CBotVar* left, CBotVar* right) +bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) { return left->GivValFloat() <= right->GivValFloat(); } -BOOL CBotVarFloat::Hs(CBotVar* left, CBotVar* right) +bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) { return left->GivValFloat() >= right->GivValFloat(); } -BOOL CBotVarFloat::Eq(CBotVar* left, CBotVar* right) +bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) { return left->GivValFloat() == right->GivValFloat(); } -BOOL CBotVarFloat::Ne(CBotVar* left, CBotVar* right) +bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) { return left->GivValFloat() != right->GivValFloat(); } @@ -1209,7 +1209,7 @@ BOOL CBotVarFloat::Ne(CBotVar* left, CBotVar* right) ////////////////////////////////////////////////////////////////////////////////////// // copie une variable dans une autre -void CBotVarBoolean::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) { CBotVarBoolean* p = (CBotVarBoolean*)pSrc; @@ -1231,14 +1231,14 @@ void CBotVarBoolean::Copy(CBotVar* pSrc, BOOL bName) void CBotVarBoolean::SetValInt(int val, const char* s) { - m_val = (BOOL)val; - m_binit = TRUE; + m_val = (bool)val; + m_binit = true; } void CBotVarBoolean::SetValFloat(float val) { - m_val = (BOOL)val; - m_binit = TRUE; + m_val = (bool)val; + m_binit = true; } int CBotVarBoolean::GivValInt() @@ -1275,31 +1275,31 @@ CBotString CBotVarBoolean::GivValString() void CBotVarBoolean::And(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() && right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() || right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) { m_val = left->GivValInt() ^ right->GivValInt(); - m_binit = TRUE; + m_binit = true; } void CBotVarBoolean::Not() { - m_val = m_val ? FALSE : TRUE ; + m_val = m_val ? false : true ; } -BOOL CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) +bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) { return left->GivValInt() == right->GivValInt(); } -BOOL CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) +bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) { return left->GivValInt() != right->GivValInt(); } @@ -1307,7 +1307,7 @@ BOOL CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) ////////////////////////////////////////////////////////////////////////////////////// // copie une variable dans une autre -void CBotVarString::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarString::Copy(CBotVar* pSrc, bool bName) { CBotVarString* p = (CBotVarString*)pSrc; @@ -1328,7 +1328,7 @@ void CBotVarString::Copy(CBotVar* pSrc, BOOL bName) void CBotVarString::SetValString(const char* p) { m_val = p; - m_binit = TRUE; + m_binit = true; } CBotString CBotVarString::GivValString() @@ -1353,36 +1353,36 @@ CBotString CBotVarString::GivValString() void CBotVarString::Add(CBotVar* left, CBotVar* right) { m_val = left->GivValString() + right->GivValString(); - m_binit = TRUE; + m_binit = true; } -BOOL CBotVarString::Eq(CBotVar* left, CBotVar* right) +bool CBotVarString::Eq(CBotVar* left, CBotVar* right) { return (left->GivValString() == right->GivValString()); } -BOOL CBotVarString::Ne(CBotVar* left, CBotVar* right) +bool CBotVarString::Ne(CBotVar* left, CBotVar* right) { return (left->GivValString() != right->GivValString()); } -BOOL CBotVarString::Lo(CBotVar* left, CBotVar* right) +bool CBotVarString::Lo(CBotVar* left, CBotVar* right) { return (left->GivValString() == right->GivValString()); } -BOOL CBotVarString::Hi(CBotVar* left, CBotVar* right) +bool CBotVarString::Hi(CBotVar* left, CBotVar* right) { return (left->GivValString() == right->GivValString()); } -BOOL CBotVarString::Ls(CBotVar* left, CBotVar* right) +bool CBotVarString::Ls(CBotVar* left, CBotVar* right) { return (left->GivValString() == right->GivValString()); } -BOOL CBotVarString::Hs(CBotVar* left, CBotVar* right) +bool CBotVarString::Hs(CBotVar* left, CBotVar* right) { return (left->GivValString() == right->GivValString()); } @@ -1391,7 +1391,7 @@ BOOL CBotVarString::Hs(CBotVar* left, CBotVar* right) //////////////////////////////////////////////////////////////// // copie une variable dans une autre -void CBotVarClass::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarClass::Copy(CBotVar* pSrc, bool bName) { pSrc = pSrc->GivPointer(); // si source donn par un pointeur @@ -1520,10 +1520,10 @@ CBotClass* CBotVarClass::GivClass() } -void CBotVarClass::Maj(void* pUser, BOOL bContinu) +void CBotVarClass::Maj(void* pUser, bool bContinu) { /* if (!bContinu && m_pMyThis != NULL) - m_pMyThis->Maj(pUser, TRUE);*/ + m_pMyThis->Maj(pUser, true);*/ // une routine de mise jour existe-elle ? @@ -1569,7 +1569,7 @@ CBotVar* CBotVarClass::GivItemRef(int nIdent) // pour la gestion d'un tableau // bExtend permet d'agrandir le tableau, mais pas au dela de la taille fixe par SetArray() -CBotVar* CBotVarClass::GivItem(int n, BOOL bExtend) +CBotVar* CBotVarClass::GivItem(int n, bool bExtend) { CBotVar* p = m_pVar; @@ -1728,14 +1728,14 @@ CBotVarClass* CBotVarClass::Find(long id) return NULL; } -BOOL CBotVarClass::Eq(CBotVar* left, CBotVar* right) +bool CBotVarClass::Eq(CBotVar* left, CBotVar* right) { CBotVar* l = left->GivItemList(); CBotVar* r = right->GivItemList(); while ( l != NULL && r != NULL ) { - if ( l->Ne(l, r) ) return FALSE; + if ( l->Ne(l, r) ) return false; l = l->GivNext(); r = r->GivNext(); } @@ -1744,14 +1744,14 @@ BOOL CBotVarClass::Eq(CBotVar* left, CBotVar* right) return l == r; } -BOOL CBotVarClass::Ne(CBotVar* left, CBotVar* right) +bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) { CBotVar* l = left->GivItemList(); CBotVar* r = right->GivItemList(); while ( l != NULL && r != NULL ) { - if ( l->Ne(l, r) ) return TRUE; + if ( l->Ne(l, r) ) return true; l = l->GivNext(); r = r->GivNext(); } @@ -1775,7 +1775,7 @@ CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type ) m_type = type; m_type.SetType(CBotTypArrayPointer); - m_binit = FALSE; + m_binit = false; m_pInstance = NULL; // la liste des lments du tableau } @@ -1786,7 +1786,7 @@ CBotVarArray::~CBotVarArray() } // copie une variable dans une autre -void CBotVarArray::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarArray::Copy(CBotVar* pSrc, bool bName) { if ( pSrc->GivType() != CBotTypArrayPointer ) ASM_TRAP(); @@ -1811,7 +1811,7 @@ void CBotVarArray::Copy(CBotVar* pSrc, BOOL bName) void CBotVarArray::SetPointer(CBotVar* pVarClass) { - m_binit = TRUE; // init, mme sur un pointeur null + m_binit = true; // init, mme sur un pointeur null if ( m_pInstance == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter // car le dcrment peut dtruire l'object @@ -1839,7 +1839,7 @@ CBotVarClass* CBotVarArray::GivPointer() return m_pInstance->GivPointer(); } -CBotVar* CBotVarArray::GivItem(int n, BOOL bExtend) +CBotVar* CBotVarArray::GivItem(int n, bool bExtend) { if ( m_pInstance == NULL ) { @@ -1864,9 +1864,9 @@ CBotString CBotVarArray::GivValString() return m_pInstance->GivValString(); } -BOOL CBotVarArray::Save1State(FILE* pf) +bool CBotVarArray::Save1State(FILE* pf) { - if ( !WriteType(pf, m_type) ) return FALSE; + if ( !WriteType(pf, m_type) ) return false; return SaveVar(pf, m_pInstance); // sauve l'instance qui gre le tableau } @@ -1889,7 +1889,7 @@ CBotVarPointer::CBotVarPointer(const CBotToken* name, CBotTypResult& type ) m_type = type; if ( !type.Eq(CBotTypNullPointer) ) m_type.SetType(CBotTypPointer); // quoi qu'il en soit, c'est un pointeur - m_binit = FALSE; + m_binit = false; m_pClass = NULL; m_pVarClass = NULL; // sera dfini par un SetPointer() @@ -1902,12 +1902,12 @@ CBotVarPointer::~CBotVarPointer() } -void CBotVarPointer::Maj(void* pUser, BOOL bContinu) +void CBotVarPointer::Maj(void* pUser, bool bContinu) { /* if ( !bContinu && m_pMyThis != NULL ) - m_pMyThis->Maj(pUser, FALSE);*/ + m_pMyThis->Maj(pUser, false);*/ - if ( m_pVarClass != NULL) m_pVarClass->Maj(pUser, FALSE); + if ( m_pVarClass != NULL) m_pVarClass->Maj(pUser, false); } CBotVar* CBotVarPointer::GivItem(const char* name) @@ -1950,7 +1950,7 @@ void CBotVarPointer::ConstructorSet() void CBotVarPointer::SetPointer(CBotVar* pVarClass) { - m_binit = TRUE; // init, mme sur un pointeur null + m_binit = true; // init, mme sur un pointeur null if ( m_pVarClass == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter // car le dcrment peut dtruire l'object @@ -2009,25 +2009,25 @@ CBotClass* CBotVarPointer::GivClass() } -BOOL CBotVarPointer::Save1State(FILE* pf) +bool CBotVarPointer::Save1State(FILE* pf) { if ( m_pClass ) { - if (!WriteString(pf, m_pClass->GivName())) return FALSE; // nom de la classe + if (!WriteString(pf, m_pClass->GivName())) return false; // nom de la classe } else { - if (!WriteString(pf, "")) return FALSE; + if (!WriteString(pf, "")) return false; } - if (!WriteLong(pf, GivIdent())) return FALSE; // la rfrence unique + if (!WriteLong(pf, GivIdent())) return false; // la rfrence unique // sauve aussi une copie de l'instance return SaveVar(pf, GivPointer()); } // copie une variable dans une autre -void CBotVarPointer::Copy(CBotVar* pSrc, BOOL bName) +void CBotVarPointer::Copy(CBotVar* pSrc, bool bName) { if ( pSrc->GivType() != CBotTypPointer && pSrc->GivType() != CBotTypNullPointer) @@ -2054,26 +2054,26 @@ void CBotVarPointer::Copy(CBotVar* pSrc, BOOL bName) if (m_ident == 0 ) m_ident = p->m_ident; } -BOOL CBotVarPointer::Eq(CBotVar* left, CBotVar* right) +bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right) { CBotVarClass* l = left->GivPointer(); CBotVarClass* r = right->GivPointer(); - if ( l == r ) return TRUE; - if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return TRUE; - if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return TRUE; - return FALSE; + if ( l == r ) return true; + if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return true; + if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return true; + return false; } -BOOL CBotVarPointer::Ne(CBotVar* left, CBotVar* right) +bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) { CBotVarClass* l = left->GivPointer(); CBotVarClass* r = right->GivPointer(); - if ( l == r ) return FALSE; - if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return FALSE; - if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return FALSE; - return TRUE; + if ( l == r ) return false; + if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return false; + if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return false; + return true; } @@ -2207,9 +2207,9 @@ void CBotTypResult::SetArray( int* max ) -BOOL CBotTypResult::Compare(const CBotTypResult& typ) const +bool CBotTypResult::Compare(const CBotTypResult& typ) const { - if ( m_type != typ.m_type ) return FALSE; + if ( m_type != typ.m_type ) return false; if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); @@ -2220,10 +2220,10 @@ BOOL CBotTypResult::Compare(const CBotTypResult& typ) const return m_pClass == typ.m_pClass; } - return TRUE; + return true; } -BOOL CBotTypResult::Eq(int type) const +bool CBotTypResult::Eq(int type) const { return m_type == type; } diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 124fb3d..fcb825c 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -66,7 +66,7 @@ CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) // la condition existe IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); DecLvl(); if ( pStk->IsOk() ) @@ -83,31 +83,31 @@ CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) // excute une instruction "while" -BOOL CBotWhile :: Execute(CBotStack* &pj) +bool CBotWhile :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; - while( TRUE ) switch( pile->GivState() ) // excute la boucle + while( true ) switch( pile->GivState() ) // excute la boucle { // il y a 2 tats possibles (selon reprise) case 0: // value la condition - if ( !m_Condition->Execute(pile) ) return FALSE; // interrompu ici ? + if ( !m_Condition->Execute(pile) ) return false; // interrompu ici ? // le rsultat de la condition est sur la pile // termine s'il y a une erreur ou si la condition est fausse - if ( !pile->IsOk() || pile->GivVal() != TRUE ) + if ( !pile->IsOk() || pile->GivVal() != true ) { return pj->Return(pile); // transmet le rsultat et libre la pile } // la condition est vrai, passe dans le second mode - if (!pile->SetState(1)) return FALSE; // prt pour la suite + if (!pile->SetState(1)) return false; // prt pour la suite case 1: // value le bloc d'instruction associ @@ -125,12 +125,12 @@ BOOL CBotWhile :: Execute(CBotStack* &pj) } // repasse au test pour recommencer - if (!pile->SetState(0, 0)) return FALSE; + if (!pile->SetState(0, 0)) return false; continue; } } -void CBotWhile :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile @@ -196,7 +196,7 @@ CBotInstr* CBotRepeat::Compile(CBotToken* &p, CBotCStack* pStack) { IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); DecLvl(); if ( pStk->IsOk() ) @@ -221,19 +221,19 @@ CBotInstr* CBotRepeat::Compile(CBotToken* &p, CBotCStack* pStack) // excute une instruction "repeat" -BOOL CBotRepeat :: Execute(CBotStack* &pj) +bool CBotRepeat :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; - while( TRUE ) switch( pile->GivState() ) // excute la boucle + while( true ) switch( pile->GivState() ) // excute la boucle { // il y a 2 tats possibles (selon reprise) case 0: // value le nombre d'itration - if ( !m_NbIter->Execute(pile) ) return FALSE; // interrompu ici ? + if ( !m_NbIter->Execute(pile) ) return false; // interrompu ici ? // le rsultat de la condition est sur la pile @@ -246,7 +246,7 @@ BOOL CBotRepeat :: Execute(CBotStack* &pj) // met le nombre d'itration +1 dans le "state" - if (!pile->SetState(n+1)) return FALSE; // prt pour la suite + if (!pile->SetState(n+1)) return false; // prt pour la suite continue; // passe la suite case 1: @@ -269,12 +269,12 @@ BOOL CBotRepeat :: Execute(CBotStack* &pj) } // repasse au test pour recommencer - if (!pile->SetState(pile->GivState()-1, 0)) return FALSE; + if (!pile->SetState(pile->GivState()-1, 0)) return false; continue; } } -void CBotRepeat :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile @@ -332,7 +332,7 @@ CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) // cherche un bloc d'instruction aprs le do IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); DecLvl(); if ( pStk->IsOk() ) @@ -358,15 +358,15 @@ CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) // excute une instruction "do" -BOOL CBotDo :: Execute(CBotStack* &pj) +bool CBotDo :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile // ou le retrouve en cas de reprise -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; - while( TRUE ) switch( pile->GivState() ) // excute la boucle + while( true ) switch( pile->GivState() ) // excute la boucle { // il y a 2 tats possibles (selon reprise) case 0: // value le bloc d'instruction associ @@ -383,27 +383,27 @@ BOOL CBotDo :: Execute(CBotStack* &pj) return pj->Return(pile); // transmet le rsultat et libre la pile } - if (!pile->SetState(1)) return FALSE; // prt pour la suite + if (!pile->SetState(1)) return false; // prt pour la suite case 1: // value la condition - if ( !m_Condition->Execute(pile) ) return FALSE; // interrompu ici ? + if ( !m_Condition->Execute(pile) ) return false; // interrompu ici ? // le rsultat de la condition est sur la pile // termine s'il y a une erreur ou si la condition est fausse - if ( !pile->IsOk() || pile->GivVal() != TRUE ) + if ( !pile->IsOk() || pile->GivVal() != true ) { return pj->Return(pile); // transmet le rsultat et libre la pile } // repasse au bloc d'instruction pour recommencer - if (!pile->SetState(0, 0)) return FALSE; + if (!pile->SetState(0, 0)) return false; continue; } } -void CBotDo :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -467,7 +467,7 @@ CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) return NULL; } - CBotCStack* pStk = pStack->TokenStack(pp, TRUE); // un petit bout de pile svp + CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp // compile les instructions pour initialisation inst->m_Init = CBotListExpression::Compile( p, pStk ); @@ -494,7 +494,7 @@ CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) if ( IsOfType(p, ID_CLOSEPAR)) // manque la parenthse ? { IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); DecLvl(); if ( pStk->IsOk() ) return pStack->Return(inst, pStk);; @@ -510,39 +510,39 @@ CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) // excute l'instruction "for" -BOOL CBotFor :: Execute(CBotStack* &pj) +bool CBotFor :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, TRUE); // ajoute un lment la pile (variables locales) + CBotStack* pile = pj->AddStack(this, true); // ajoute un lment la pile (variables locales) // ou le retrouve en cas de reprise -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; - while( TRUE ) switch( pile->GivState() ) // excute la boucle + while( true ) switch( pile->GivState() ) // excute la boucle { // il y a 4 tats possibles (selon reprise) case 0: // value l'initialisation if ( m_Init != NULL && - !m_Init->Execute(pile) ) return FALSE; // interrompu ici ? - if (!pile->SetState(1)) return FALSE; // prt pour la suite + !m_Init->Execute(pile) ) return false; // interrompu ici ? + if (!pile->SetState(1)) return false; // prt pour la suite case 1: // value la condition if ( m_Test != NULL ) // pas de condition ? -> vrai ! { - if (!m_Test->Execute(pile) ) return FALSE; // interrompu ici ? + if (!m_Test->Execute(pile) ) return false; // interrompu ici ? // le rsultat de la condition est sur la pile // termine s'il y a une erreur ou si la condition est fausse - if ( !pile->IsOk() || pile->GivVal() != TRUE ) + if ( !pile->IsOk() || pile->GivVal() != true ) { return pj->Return(pile); // transmet le rsultat et libre la pile } } // la condition est vrai, passe la suite - if (!pile->SetState(2)) return FALSE; // prt pour la suite + if (!pile->SetState(2)) return false; // prt pour la suite case 2: // value le bloc d'instruction associ @@ -559,20 +559,20 @@ BOOL CBotFor :: Execute(CBotStack* &pj) return pj->Return(pile); // transmet le rsultat et libre la pile } - if (!pile->SetState(3)) return FALSE; // prt pour la suite + if (!pile->SetState(3)) return false; // prt pour la suite case 3: // value l'incrmentation if ( m_Incr != NULL && - !m_Incr->Execute(pile) ) return FALSE; // interrompu ici ? + !m_Incr->Execute(pile) ) return false; // interrompu ici ? // repasse au test pour recommencer - if (!pile->SetState(1, 0)) return FALSE; // revient au test + if (!pile->SetState(1, 0)) return false; // revient au test continue; } } -void CBotFor :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -583,28 +583,28 @@ void CBotFor :: RestoreState(CBotStack* &pj, BOOL bMain) { // il y a 4 tats possibles (selon reprise) case 0: // value l'initialisation - if ( m_Init != NULL ) m_Init->RestoreState(pile, TRUE); // interrompu ici ! + if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrompu ici ! return; case 1: - if ( m_Init != NULL ) m_Init->RestoreState(pile, FALSE); // dfinitions variables + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // dfinitions variables // value la condition - if ( m_Test != NULL ) m_Test->RestoreState(pile, TRUE); // interrompu ici ! + if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrompu ici ! return; case 2: - if ( m_Init != NULL ) m_Init->RestoreState(pile, FALSE); // dfinitions variables + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // dfinitions variables // value le bloc d'instruction associ - if ( m_Block != NULL ) m_Block->RestoreState(pile, TRUE); + if ( m_Block != NULL ) m_Block->RestoreState(pile, true); return; case 3: - if ( m_Init != NULL ) m_Init->RestoreState(pile, FALSE); // dfinitions variables + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // dfinitions variables // value l'incrmentation - if ( m_Incr != NULL ) m_Incr->RestoreState(pile, TRUE); // interrompu ici ! + if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrompu ici ! return; } } @@ -629,10 +629,10 @@ CBotListExpression::~CBotListExpression() static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) { - CBotInstr* i = CBotInt::Compile( p, pStack, FALSE, TRUE ); // est-ce une dclaration d'un entier ? - if ( i== NULL ) i = CBotFloat::Compile( p, pStack, FALSE, TRUE ); // ou d'un nombre rel ? - if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, FALSE, TRUE ); // ou d'un boolen ? - if ( i== NULL ) i = CBotIString::Compile( p, pStack, FALSE, TRUE ); // ou d'une chane ? + CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // est-ce une dclaration d'un entier ? + if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // ou d'un nombre rel ? + if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // ou d'un boolen ? + if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ou d'une chane ? if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compile une expression return i; } @@ -660,7 +660,7 @@ CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) return NULL; } -BOOL CBotListExpression::Execute(CBotStack* &pj) +bool CBotListExpression::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack();// indispensable CBotInstr* p = m_Expr; // la premire expression @@ -668,17 +668,17 @@ BOOL CBotListExpression::Execute(CBotStack* &pj) int state = pile->GivState(); while (state-->0) p = p->GivNext(); // revient sur l'opration interrompue - if ( p != NULL ) while (TRUE) + if ( p != NULL ) while (true) { - if ( !p->Execute(pile) ) return FALSE; + if ( !p->Execute(pile) ) return false; p = p->GivNext(); if ( p == NULL ) break; - if (!pile->IncState()) return FALSE; // prt pour la suivante + if (!pile->IncState()) return false; // prt pour la suivante } return pj->Return(pile); } -void CBotListExpression::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; int state = 0x7000; @@ -694,7 +694,7 @@ void CBotListExpression::RestoreState(CBotStack* &pj, BOOL bMain) while (p != NULL && state-->0) { - p->RestoreState(pile, FALSE); + p->RestoreState(pile, false); p = p->GivNext(); // revient sur l'opration interrompue } @@ -770,7 +770,7 @@ CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(NULL, pStk); } - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, TRUE ); + CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); if ( !pStk->IsOk() ) { delete inst; @@ -811,21 +811,21 @@ CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) // excute une instruction "switch" -BOOL CBotSwitch :: Execute(CBotStack* &pj) +bool CBotSwitch :: Execute(CBotStack* &pj) { CBotStack* pile1 = pj->AddStack(this); // ajoute un lment la pile -// if ( pile1 == EOX ) return TRUE; +// if ( pile1 == EOX ) return true; CBotInstr* p = m_Block; // la premire expression int state = pile1->GivState(); if (state == 0) { - if ( !m_Value->Execute(pile1) ) return FALSE; + if ( !m_Value->Execute(pile1) ) return false; pile1->SetState(state = -1); } - if ( pile1->IfStep() ) return FALSE; + if ( pile1->IfStep() ) return false; if ( state == -1 ) { @@ -843,7 +843,7 @@ BOOL CBotSwitch :: Execute(CBotStack* &pj) if ( p == NULL ) return pj->Return(pile1); // termin si plus rien - if ( !pile1->SetState(state) ) return FALSE; + if ( !pile1->SetState(state) ) return false; } p = m_Block; // revient au dbut @@ -852,13 +852,13 @@ BOOL CBotSwitch :: Execute(CBotStack* &pj) while( p != NULL ) { if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); - if ( !pile1->IncState() ) return FALSE; + if ( !pile1->IncState() ) return false; p = p->GivNext(); } return pj->Return(pile1); } -void CBotSwitch :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -882,13 +882,13 @@ void CBotSwitch :: RestoreState(CBotStack* &pj, BOOL bMain) // p = m_Block; // revient au dbut while ( p != NULL && state-- > 0 ) { - p->RestoreState(pile1, FALSE); + p->RestoreState(pile1, false); p = p->GivNext(); // avance dans la liste } if( p != NULL ) { - p->RestoreState(pile1, TRUE); + p->RestoreState(pile1, true); return; } } @@ -942,21 +942,21 @@ CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) // excution de l'instruction "case" -BOOL CBotCase::Execute(CBotStack* &pj) +bool CBotCase::Execute(CBotStack* &pj) { - return TRUE; // l'instruction "case" ne fait rien ! + return true; // l'instruction "case" ne fait rien ! } -void CBotCase::RestoreState(CBotStack* &pj, BOOL bMain) +void CBotCase::RestoreState(CBotStack* &pj, bool bMain) { } // routine permettant de trouver le point d'entre "case" // correspondant la valeur cherche -BOOL CBotCase::CompCase(CBotStack* &pile, int val) +bool CBotCase::CompCase(CBotStack* &pile, int val) { - if ( m_Value == NULL ) return TRUE; // cas pour "default" + if ( m_Value == NULL ) return true; // cas pour "default" while (!m_Value->Execute(pile)); // met sur la pile la valeur correpondant (sans interruption) return (pile->GivVal() == val); // compare avec la valeur cherche @@ -1016,18 +1016,18 @@ CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) // excution l'instructino "break" ou "continu" -BOOL CBotBreak :: Execute(CBotStack* &pj) +bool CBotBreak :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); return pj->Return(pile); } -void CBotBreak :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) { if ( bMain ) pj->RestoreStack(this); } @@ -1092,14 +1092,14 @@ CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) // les arrts par suspension // et les "finaly" -BOOL CBotTry :: Execute(CBotStack* &pj) +bool CBotTry :: Execute(CBotStack* &pj) { int val; CBotStack* pile1 = pj->AddStack(this); // ajoute un lment la pile -// if ( pile1 == EOX ) return TRUE; +// if ( pile1 == EOX ) return true; - if ( pile1->IfStep() ) return FALSE; + if ( pile1->IfStep() ) return false; // ou le retrouve en cas de reprise CBotStack* pile0 = pj->AddStack2(); // ajoute un lment la pile secondaire CBotStack* pile2 = pile0->AddStack(); @@ -1114,14 +1114,14 @@ BOOL CBotTry :: Execute(CBotStack* &pj) val = pile1->GivError(); if ( val == 0 && CBotStack::m_initimer == 0 ) // en mode de step ? - return FALSE; // ne fait pas le catch + return false; // ne fait pas le catch pile1->IncState(); pile2->SetState(val); // mmorise le numro de l'erreur pile1->SetError(0); // pour l'instant il n'y a plus d'erreur ! if ( val == 0 && CBotStack::m_initimer < 0 ) // en mode de step ? - return FALSE; // ne fait pas le catch + return false; // ne fait pas le catch } // il y a eu une interruption @@ -1137,16 +1137,16 @@ BOOL CBotTry :: Execute(CBotStack* &pj) if ( --state <= 0 ) { // demande au bloc catch s'il se sent concern - if ( !pc->TestCatch(pile2, val) ) return FALSE; // suspendu ! + if ( !pc->TestCatch(pile2, val) ) return false; // suspendu ! pile1->IncState(); } if ( --state <= 0 ) { - if ( pile2->GivVal() == TRUE ) + if ( pile2->GivVal() == true ) { // pile0->SetState(1); - if ( !pc->Execute(pile2) ) return FALSE; // excute l'opration + if ( !pc->Execute(pile2) ) return false; // excute l'opration if ( m_FinalInst == NULL ) return pj->Return(pile2); // termine le try @@ -1164,7 +1164,7 @@ BOOL CBotTry :: Execute(CBotStack* &pj) { // pile0->SetState(1); - if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return FALSE; + if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; if (!pile2->IsOk()) return pj->Return(pile2); // garde cette exception pile2->SetError(pile1->GivState()==-1 ? val : 0); // remet l'erreur initiale return pj->Return(pile2); @@ -1176,11 +1176,11 @@ BOOL CBotTry :: Execute(CBotStack* &pj) return pj->Return(pile2); // termine le try sans exception aucune pile1->SetError(val); // remet l'erreur - return FALSE; // ce n'est pas pour nous + return false; // ce n'est pas pour nous } -void CBotTry :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -1217,7 +1217,7 @@ void CBotTry :: RestoreState(CBotStack* &pj, BOOL bMain) } if ( --state <= 0 ) { - if ( pile2->GivVal() == TRUE ) + if ( pile2->GivVal() == true ) { pc->RestoreState(pile2, bMain); // excute l'opration return; @@ -1285,27 +1285,27 @@ CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) // excution de "catch" -BOOL CBotCatch :: Execute(CBotStack* &pj) +bool CBotCatch :: Execute(CBotStack* &pj) { - if ( m_Block == NULL ) return TRUE; + if ( m_Block == NULL ) return true; return m_Block->Execute(pj); // excute le bloc associ } -void CBotCatch :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) { if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); } -void CBotCatch :: RestoreCondState(CBotStack* &pj, BOOL bMain) +void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) { m_Cond->RestoreState(pj, bMain); } // routine pour savoir si le catch est faire ou non -BOOL CBotCatch :: TestCatch(CBotStack* &pile, int val) +bool CBotCatch :: TestCatch(CBotStack* &pile, int val) { - if ( !m_Cond->Execute(pile) ) return FALSE; + if ( !m_Cond->Execute(pile) ) return false; if ( val > 0 || pile->GivType() != CBotTypBoolean ) { @@ -1314,7 +1314,7 @@ BOOL CBotCatch :: TestCatch(CBotStack* &pile, int val) pile->SetVar(var); // remet sur la pile } - return TRUE; + return true; } /////////////////////////////////////////////////////////////////////////// @@ -1359,18 +1359,18 @@ CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) // excute l'instruction "throw" -BOOL CBotThrow :: Execute(CBotStack* &pj) +bool CBotThrow :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return TRUE; +// if ( pile == EOX ) return true; if ( pile->GivState() == 0 ) { - if ( !m_Value->Execute(pile) ) return FALSE; + if ( !m_Value->Execute(pile) ) return false; pile->IncState(); } - if ( pile->IfStep() ) return FALSE; + if ( pile->IfStep() ) return false; int val = pile->GivVal(); if ( val < 0 ) val = TX_BADTHROW; @@ -1378,7 +1378,7 @@ BOOL CBotThrow :: Execute(CBotStack* &pj) return pj->Return( pile ); } -void CBotThrow :: RestoreState(CBotStack* &pj, BOOL bMain) +void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; @@ -1417,11 +1417,11 @@ CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) // excute l'instruction "throw" -BOOL CBotStartDebugDD :: Execute(CBotStack* &pj) +bool CBotStartDebugDD :: Execute(CBotStack* &pj) { CBotProgram* p = pj->GivBotCall(); - p->m_bDebugDD = TRUE; + p->m_bDebugDD = true; - return TRUE; + return true; } diff --git a/src/CBot/ClassFILE.cpp b/src/CBot/ClassFILE.cpp index e4dd578..330e814 100644 --- a/src/CBot/ClassFILE.cpp +++ b/src/CBot/ClassFILE.cpp @@ -58,7 +58,7 @@ void PrepareFilename(CBotString &filename) //DD! // reois le nom du fichier en paramtre // excution -BOOL rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { CBotString mode; @@ -132,7 +132,7 @@ CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) // destructeur de la classe // excution -BOOL rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // rcupre l'lment "handle" pVar = pThis->GivItem("handle"); @@ -154,7 +154,7 @@ BOOL rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception // reois le mode r/w en paramtre // excution -BOOL rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // il doit y avoir un paramtre if ( pVar == NULL ) { Exception = CBotErrLowParam; return FALSE; } @@ -243,7 +243,7 @@ CBotTypResult cfopen (CBotVar* pThis, CBotVar* &pVar) // mthode FILE :: close // excution -BOOL rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // il ne doit pas y avoir de paramtre if ( pVar != NULL ) return CBotErrOverParam; @@ -275,7 +275,7 @@ CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) // mthode FILE :: writeln // excution -BOOL rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // il doit y avoir un paramtre if ( pVar == NULL ) { Exception = CBotErrLowParam; return FALSE; } @@ -319,7 +319,7 @@ CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) // mthode FILE :: readln // excution -BOOL rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // il ne doit pas y avoir de paramtre if ( pVar != NULL ) { Exception = CBotErrOverParam; return FALSE; } @@ -360,7 +360,7 @@ CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) // excution -BOOL rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // il ne doit pas y avoir de paramtre if ( pVar != NULL ) { Exception = CBotErrOverParam; return FALSE; } diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 2c3cfc2..27332db 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -18,23 +18,23 @@ // donne la longueur d'une chane // excution -BOOL rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // pas de second paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // met la longueur sur la pile pResult->SetValInt( s.GivLength() ); - return TRUE; + return true; } // int xxx ( string ) @@ -60,36 +60,36 @@ CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) // donne la partie gauche d'une chane // excution -BOOL rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // il faut un second paramtre pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return TRUE; } + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } // rcupre ce nombre int n = pVar->GivValInt(); // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } // prend la partie intressante s = s.Left( n ); // la met sur la pile pResult->SetValString( s ); - return TRUE; + return true; } // string xxx ( string, int ) @@ -122,58 +122,58 @@ CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) // donne la partie droite d'une chane // excution -BOOL rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // il faut un second paramtre pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return TRUE; } + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } // rcupre ce nombre int n = pVar->GivValInt(); // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } // prend la partie intressante s = s.Right( n ); // la met sur la pile pResult->SetValString( s ); - return TRUE; + return true; } // donne la partie centrale d'une chane // excution -BOOL rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // il faut un second paramtre pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return TRUE; } + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } // rcupre ce nombre int n = pVar->GivValInt(); @@ -184,13 +184,13 @@ BOOL rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) pVar = pVar->GivNext(); // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return TRUE; } + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } // rcupre ce nombre int l = pVar->GivValInt(); // mais pas de 4e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } // prend la partie intressante s = s.Mid( n, l ); @@ -203,7 +203,7 @@ BOOL rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) // la met sur la pile pResult->SetValString( s ); - return TRUE; + return true; } // donne la partie centrale d'une chane @@ -247,25 +247,25 @@ CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) // donne le nombre contenu dans une chane // excution -BOOL rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // mais pas de 2e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } float val = GivNumFloat(s); // la met la valeur sur la pile pResult->SetValFloat( val ); - return TRUE; + return true; } // float xxx ( string ) @@ -291,35 +291,35 @@ CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) // trouve une chaine dans une autre // excution -BOOL rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // il faut un second paramtre pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // rcupre ce nombre CBotString s2 = pVar->GivValString(); // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } // met le rsultat sur la pile int res = s.Find(s2); pResult->SetValInt( res ); if ( res < 0 ) pResult->SetInit( IS_NAN ); - return TRUE; + return true; } // int xxx ( string, string ) @@ -352,51 +352,51 @@ CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) // donne une chaine en majuscule // excution -BOOL rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // mais pas de 2e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } s.MakeUpper(); // la met la valeur sur la pile pResult->SetValString( s ); - return TRUE; + return true; } // donne une chaine en minuscules // excution -BOOL rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return TRUE; } + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return TRUE; } + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } // recupre le contenu de la string CBotString s = pVar->GivValString(); // mais pas de 2e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return TRUE; } + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } s.MakeLower(); // la met la valeur sur la pile pResult->SetValString( s ); - return TRUE; + return true; } // string xxx ( string ) diff --git a/src/CBot/old b/src/CBot/old deleted file mode 100644 index ea18736..0000000 --- a/src/CBot/old +++ /dev/null @@ -1,15 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1be7a59..250120c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ # CBot shared library is built separately -# add_subdirectory(CBot) -- not yet WinAPI-independent +add_subdirectory(CBot) # Configure options @@ -25,6 +25,7 @@ configure_file(common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h # Source files # Commented out files are still dependent on DirectX or WinAPI + set(SOURCES app/app.cpp app/main.cpp @@ -150,7 +151,7 @@ set(LIBS ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} -#CBot -- not yet WinAPI-independent +CBot ) include_directories(. ${CMAKE_CURRENT_BINARY_DIR}) -- cgit v1.2.3-1-g7c22 From e8c9945e13fca88a6f8232838682df0654437f3e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 5 Jul 2012 23:47:29 +0200 Subject: Fixed bug with texturing - moved creation-time tex params to Gfx::TextureCreateParams - fixed bug with texture creation - added simple test for multitexturing --- src/graphics/common/device.cpp | 8 +- src/graphics/common/device.h | 31 ++- src/graphics/opengl/gldevice.cpp | 192 ++++++++-------- src/graphics/opengl/gldevice.h | 5 +- src/graphics/opengl/test/CMakeLists.txt | 31 +++ src/graphics/opengl/test/tex1.png | Bin 0 -> 151263 bytes src/graphics/opengl/test/tex2.png | Bin 0 -> 57503 bytes src/graphics/opengl/test/texture_test.cpp | 365 ++++++++++++++++++++++++++++++ 8 files changed, 526 insertions(+), 106 deletions(-) create mode 100644 src/graphics/opengl/test/CMakeLists.txt create mode 100644 src/graphics/opengl/test/tex1.png create mode 100644 src/graphics/opengl/test/tex2.png create mode 100644 src/graphics/opengl/test/texture_test.cpp diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp index bb51699..79102b2 100644 --- a/src/graphics/common/device.cpp +++ b/src/graphics/common/device.cpp @@ -32,14 +32,20 @@ void Gfx::DeviceConfig::LoadDefault() noFrame = false; } -void Gfx::TextureParams::LoadDefault() +void Gfx::TextureCreateParams::LoadDefault() { + alpha = false; + mipmap = false; + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; magFilter = Gfx::TEX_MAG_FILTER_NEAREST; wrapS = Gfx::TEX_WRAP_REPEAT; wrapT = Gfx::TEX_WRAP_REPEAT; +} +void Gfx::TextureParams::LoadDefault() +{ colorOperation = Gfx::TEX_MIX_OPER_MODULATE; colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 3382c9a..41181aa 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -237,11 +237,15 @@ enum TexMixArgument }; /** - \enum TextureParams + \struct TextureCreateParams \brief Parameters for texture creation - */ -struct TextureParams + */ +struct TextureCreateParams { + //! Whether the texture image contains alpha + bool alpha; + //! Whether to generate mipmaps + bool mipmap; //! Minification filter Gfx::TexMinFilter minFilter; //! Magnification filter @@ -250,6 +254,21 @@ struct TextureParams Gfx::TexWrapMode wrapS; //! Wrap T coord mode Gfx::TexWrapMode wrapT; + + //! Constructor; calls LoadDefault() + TextureCreateParams() + { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/** + \struct TextureParams + \brief Parameters for texture creation + */ +struct TextureParams +{ //! Mixing operation done on color values Gfx::TexMixOperation colorOperation; //! 1st argument of color operations @@ -302,9 +321,9 @@ D3DRENDERSTATE_ZFUNC -> SetDepthTestFunc() D3DRENDERSTATE_ZWRITEENABLE -> SetRenderState() with RENDER_STATE_DEPTH_WRITE ->> SetTextureStageState() translates to SetTextureParams() +>> SetTextureStageState() translates to SetTextureParams() or CreateTexture() for some params -Params from enum in struct TextureParams +Params from enum in struct TextureCreateParams or TextureParams D3DTSS_ADDRESS -> Gfx::TexWrapMode wrapS, wrapT D3DTSS_ALPHAARG1 -> Gfx::TexMixArgument alphaArg1 D3DTSS_ALPHAARG2 -> Gfx::TexMixArgument alphaArg2 @@ -380,7 +399,7 @@ public: virtual bool GetLightEnabled(int index) = 0; //! Creates a texture from image; the image can be safely removed after that - virtual Gfx::Texture* CreateTexture(CImage *image, bool alpha, bool mipMap) = 0; + virtual Gfx::Texture* CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) = 0; //! Deletes a given texture, freeing it from video memory virtual void DestroyTexture(Gfx::Texture *texture) = 0; //! Deletes all textures created so far diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index c6e91e1..3182dfc 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -105,14 +105,14 @@ bool Gfx::CGLDevice::Create() return false; } - m_private->glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord2fARB"); + /*m_private->glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord2fARB"); m_private->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB"); if ((m_private->glMultiTexCoord2fARB == NULL) || (m_private->glActiveTextureARB == NULL)) { m_error = "Could not load extension functions, even though they seem supported"; return false; - } + }*/ m_wasInit = true; @@ -144,8 +144,8 @@ bool Gfx::CGLDevice::Create() void Gfx::CGLDevice::Destroy() { - m_private->glMultiTexCoord2fARB = NULL; - m_private->glActiveTextureARB = NULL; + /*m_private->glMultiTexCoord2fARB = NULL; + m_private->glActiveTextureARB = NULL;*/ // Delete the remaining textures // Should not be strictly necessary, but just in case @@ -328,41 +328,83 @@ bool Gfx::CGLDevice::GetLightEnabled(int index) return m_lightsEnabled[index]; } -Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, bool alpha, bool mipMap) +/** If image is invalid, returns NULL. + Otherwise, returns pointer to new Gfx::Texture struct. + This struct must not be deleted in other way than through DeleteTexture() */ +Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) { + ImageData *data = image->GetData(); + if (data == NULL) + return NULL; + Gfx::Texture *result = new Gfx::Texture(); + result->valid = true; - // Texturing must be enabled, so enable 1st texture stage - m_private->glActiveTextureARB(GL_TEXTURE0_ARB); + // Use & enable 1st texture stage + glActiveTextureARB(GL_TEXTURE0_ARB); glEnable(GL_TEXTURE_2D); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glGenTextures(1, &result->id); glBindTexture(GL_TEXTURE_2D, result->id); + // Set params + + GLint minF = 0; + if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST) minF = GL_NEAREST; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR) minF = GL_LINEAR; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST) minF = GL_NEAREST_MIPMAP_NEAREST; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST) minF = GL_LINEAR_MIPMAP_NEAREST; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR) minF = GL_NEAREST_MIPMAP_LINEAR; + else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR) minF = GL_LINEAR_MIPMAP_LINEAR; + else assert(false); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minF); + + GLint magF = 0; + if (params.magFilter == Gfx::TEX_MAG_FILTER_NEAREST) magF = GL_NEAREST; + else if (params.magFilter == Gfx::TEX_MAG_FILTER_LINEAR) magF = GL_LINEAR; + else assert(false); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magF); + + if (params.wrapS == Gfx::TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + else if (params.wrapS == Gfx::TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + else assert(false); + + if (params.wrapT == Gfx::TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + else if (params.wrapT == Gfx::TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + else assert(false); + + + if (params.mipmap) + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); + else + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); + GLenum sourceFormat = 0; - if (alpha) + if (params.alpha) sourceFormat = GL_RGBA; else sourceFormat = GL_RGB; - ImageData *data = image->GetData(); - if (data == NULL) - return NULL; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->surface->w, data->surface->h, + 0, sourceFormat, GL_UNSIGNED_BYTE, data->surface->pixels); - if (mipMap) - { - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, data->surface->w, - data->surface->h, sourceFormat, GL_UNSIGNED_BYTE, - data->surface->pixels); - } + + // Restore the previous state of 1st stage + if (m_textures[0] == NULL) + glBindTexture(GL_TEXTURE_2D, 0); else - { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->surface->w, data->surface->h, - 0, sourceFormat, GL_UNSIGNED_BYTE, data->surface->pixels); - } + glBindTexture(GL_TEXTURE_2D, m_textures[0]->id); - // Restore previous setup of 1st texture stage - RestoreTextureStage(0); + if ( (! m_texturing) || (! m_texturesEnabled[0]) ) + glDisable(GL_TEXTURE_2D); return result; } @@ -373,6 +415,13 @@ void Gfx::CGLDevice::DestroyTexture(Gfx::Texture *texture) if (it != m_allTextures.end()) m_allTextures.erase(it); + // Unbind the texture if in use anywhere + for (int index = 0; index < (int)m_textures.size(); ++index) + { + if (m_textures[index] == texture) + SetTexture(index, NULL); + } + glDeleteTextures(1, &texture->id); } @@ -402,7 +451,7 @@ void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) assert(index < (int)m_textures.size()); // Enable the given texture stage - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTextureARB(GL_TEXTURE0_ARB + index); glEnable(GL_TEXTURE_2D); if ((texture == NULL) || (! texture->valid)) @@ -439,7 +488,7 @@ void Gfx::CGLDevice::SetTextureEnabled(int index, bool enabled) m_texturesEnabled[index] = enabled; - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTextureARB(GL_TEXTURE0_ARB + index); if (enabled) glEnable(GL_TEXTURE_2D); else @@ -466,41 +515,17 @@ void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶m // Remember the settings m_texturesParams[index] = params; + // Don't actually do anything if texture not set + if (m_textures[index] == NULL) + { + printf("No texture set: %d\n", index); + return; + } + // Enable the given stage - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTextureARB(GL_TEXTURE0_ARB + index); glEnable(GL_TEXTURE_2D); - GLint minF = 0; - if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST) minF = GL_NEAREST; - else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR) minF = GL_LINEAR; - else if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST) minF = GL_NEAREST_MIPMAP_NEAREST; - else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST) minF = GL_LINEAR_MIPMAP_NEAREST; - else if (params.minFilter == Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR) minF = GL_NEAREST_MIPMAP_LINEAR; - else if (params.minFilter == Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR) minF = GL_LINEAR_MIPMAP_LINEAR; - else assert(false); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minF); - - GLint magF = 0; - if (params.magFilter == Gfx::TEX_MAG_FILTER_NEAREST) magF = GL_NEAREST; - else if (params.magFilter == Gfx::TEX_MAG_FILTER_LINEAR) magF = GL_LINEAR; - else assert(false); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magF); - - if (params.wrapS == Gfx::TEX_WRAP_CLAMP) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - else if (params.wrapS == Gfx::TEX_WRAP_REPEAT) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - else assert(false); - - if (params.wrapT == Gfx::TEX_WRAP_CLAMP) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - else if (params.wrapT == Gfx::TEX_WRAP_REPEAT) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - else assert(false); - - // Selection of operation and arguments glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); @@ -609,7 +634,7 @@ void Gfx::CGLDevice::SetTextureFactor(Gfx::Color &color) for (int index = 0; index < (int)m_textures.size(); ++index) { // Activate stage - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTextureARB(GL_TEXTURE0_ARB + index); glEnable(GL_TEXTURE_2D); glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color.Array()); @@ -623,7 +648,7 @@ void Gfx::CGLDevice::SetTextureFactor(Gfx::Color &color) Gfx::Color Gfx::CGLDevice::GetTextureFactor() { // Get from 1st stage (should be the same for all stages) - m_private->glActiveTextureARB(GL_TEXTURE0_ARB); + glActiveTextureARB(GL_TEXTURE0_ARB); glEnable(GL_TEXTURE_2D); GLfloat color[4] = { 0.0f }; @@ -636,22 +661,6 @@ Gfx::Color Gfx::CGLDevice::GetTextureFactor() return Gfx::Color(color[0], color[1], color[2], color[3]); } -void Gfx::CGLDevice::RestoreTextureStage(int index) -{ - // Ensure that we're working with the right stage - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); - glEnable(GL_TEXTURE_2D); - - if (m_textures[index] != NULL) - glBindTexture(GL_TEXTURE_2D, m_textures[index]->id); // bind to the previous texture - else - glBindTexture(GL_TEXTURE_2D, 0); // unbind - - // Disable the stage if it is set so - if ( (! m_texturing) || (! m_texturesEnabled[index]) ) - glDisable(GL_TEXTURE_2D); -} - void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount) { if (type == Gfx::PRIMITIVE_LINES) @@ -661,10 +670,12 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, in else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) glBegin(GL_TRIANGLE_STRIP); + glColor3f(1.0f, 1.0f, 1.0f); // TODO: set global color? + for (int i = 0; i < vertexCount; ++i) { glNormal3fv((GLfloat*)vertices[i].normal.Array()); - glTexCoord2fv((GLfloat*)vertices[i].texCoord.Array()); + glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array()); glVertex3fv((GLfloat*)vertices[i].coord.Array()); } @@ -684,7 +695,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vert { // TODO: specular through EXT_separate_specular_color? glColor4fv((GLfloat*)vertices[i].color.Array()); - glTexCoord2fv((GLfloat*)vertices[i].texCoord.Array()); + glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array()); glVertex3fv((GLfloat*)vertices[i].coord.Array()); } @@ -703,8 +714,8 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices for (int i = 0; i < vertexCount; ++i) { glNormal3fv((GLfloat*) vertices[i].normal.Array()); - m_private->glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y); - m_private->glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y); + glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y); + glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y); glVertex3fv((GLfloat*) vertices[i].coord.Array()); } @@ -722,25 +733,16 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) { m_texturing = enabled; - if (enabled) + // Enable/disable stages with new setting + for (int index = 0; index < (int)m_textures.size(); ++index) { - // All enabled multitexture stages have to be enabled - for (int index = 0; index < (int)m_textures.size(); ++index) - { - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); - if (m_texturesEnabled[index]) - glEnable(GL_TEXTURE_2D); - } - } - else - { - // All multitexture stages have to be disabled - for (int index = 0; index < (int)m_textures.size(); ++index) - { - m_private->glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTextureARB(GL_TEXTURE0_ARB + index); + if (m_texturing && m_texturesEnabled[index]) + glEnable(GL_TEXTURE_2D); + else glDisable(GL_TEXTURE_2D); - } } + return; } diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index aa5dd04..2b1b05d 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -97,7 +97,7 @@ public: virtual void SetLightEnabled(int index, bool enabled); virtual bool GetLightEnabled(int index); - virtual Gfx::Texture* CreateTexture(CImage *image, bool alpha, bool mipMap); + virtual Gfx::Texture* CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms); virtual void DestroyTexture(Gfx::Texture *texture); virtual void DestroyAllTextures(); @@ -187,9 +187,6 @@ private: //! Set of all created textures std::set m_allTextures; - - //! Restores the state of given texture stage to the previously saved settings - void RestoreTextureStage(int index); }; }; // namespace Gfx diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt new file mode 100644 index 0000000..ce2a83f --- /dev/null +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 2.8) + +find_package(OpenGL REQUIRED) +find_package(SDL REQUIRED) +find_package(SDL_image REQUIRED) +find_package(PNG REQUIRED) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") + + +set(SOURCES +../gldevice.cpp +../../common/device.cpp +../../../common/logger.cpp +../../../common/image.cpp +texture_test.cpp +) + +include_directories(../../../) + +set(LIBS +${SDL_LIBRARY} +${SDLIMAGE_LIBRARY} +${OPENGL_LIBRARY} +${PNG_LIBRARIES} +) + +add_executable(texture_test ${SOURCES}) + +target_link_libraries(texture_test ${LIBS}) diff --git a/src/graphics/opengl/test/tex1.png b/src/graphics/opengl/test/tex1.png new file mode 100644 index 0000000..46c68a0 Binary files /dev/null and b/src/graphics/opengl/test/tex1.png differ diff --git a/src/graphics/opengl/test/tex2.png b/src/graphics/opengl/test/tex2.png new file mode 100644 index 0000000..ebdae0d Binary files /dev/null and b/src/graphics/opengl/test/tex2.png differ diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp new file mode 100644 index 0000000..022cf87 --- /dev/null +++ b/src/graphics/opengl/test/texture_test.cpp @@ -0,0 +1,365 @@ +#include "common/logger.h" +#include "common/image.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include + + +#define DEV 1 + + +void Init(Gfx::CGLDevice *device) +{ + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); + device->SetShadeModel(Gfx::SHADE_SMOOTH); + + CImage img1; + if (! img1.Load("tex1.png")) + { + std::string err = img1.GetError(); + GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); + } + CImage img2; + if (! img2.Load("tex2.png")) + { + std::string err = img2.GetError(); + GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); + } + + Gfx::TextureCreateParams tex1CreateParams; + tex1CreateParams.alpha = true; + tex1CreateParams.mipmap = true; + tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; + tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; + tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; + + Gfx::TextureCreateParams tex2CreateParams; + tex2CreateParams.alpha = true; + tex2CreateParams.mipmap = true; + tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; + tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; + + Gfx::Texture* tex1 = device->CreateTexture(&img1, tex1CreateParams); + Gfx::Texture* tex2 = device->CreateTexture(&img2, tex2CreateParams); + + device->SetTexture(0, tex1); + device->SetTexture(1, tex2); + + Gfx::TextureParams tex1Params; + tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(0, tex1Params); + + Gfx::TextureParams tex2Params; + tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(1, tex2Params); + + device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); +} + +void Render(Gfx::CGLDevice *device) +{ + device->BeginScene(); + + glFlush(); + + Math::Matrix ortho; + Math::LoadOrthoProjectionMatrix(ortho, -10, 10, -10, 10); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, ortho); + + Math::Matrix id; + id.LoadIdentity(); + + device->SetTransform(Gfx::TRANSFORM_WORLD, id); + device->SetTransform(Gfx::TRANSFORM_VIEW, id); + + static Gfx::VertexTex2 quad[] = + { + Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), + + Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), + }; + + Math::Matrix t; + Math::LoadTranslationMatrix(t, Math::Vector(-4.0f, 4.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_VIEW, t); + + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, false); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + Math::LoadTranslationMatrix(t, Math::Vector( 4.0f, 4.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_VIEW, t); + + device->SetTextureEnabled(0, false); + device->SetTextureEnabled(1, true); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + + Math::LoadTranslationMatrix(t, Math::Vector( 0.0f, -4.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_VIEW, t); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + device->EndScene(); +} + +void InitGL() +{ + CImage img1; + if (! img1.Load("tex1.png")) + { + std::string err = img1.GetError(); + GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); + } + CImage img2; + if (! img2.Load("tex2.png")) + { + std::string err = img2.GetError(); + GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); + } + + unsigned int textureHandle1 = 0; + + glActiveTexture(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + + glPixelStorei(GL_UNPACK_ALIGNMENT,1); + + glGenTextures(1, &textureHandle1); + glBindTexture(GL_TEXTURE_2D, textureHandle1); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img1.GetData()->surface->w, img1.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img1.GetData()->surface->pixels); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + + + unsigned int textureHandle2 = 0; + + glActiveTexture(GL_TEXTURE1_ARB); + glEnable(GL_TEXTURE_2D); + + glPixelStorei(GL_UNPACK_ALIGNMENT,1); + + glGenTextures(1, &textureHandle2); + glBindTexture(GL_TEXTURE_2D, textureHandle2); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img2.GetData()->surface->w, img2.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img2.GetData()->surface->pixels); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + + + glMatrixMode(GL_PROJECTION); + glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void RenderGL() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glLoadIdentity(); + + glColor3f(1.0f, 1.0f, 1.0f); + + glPushMatrix(); + glTranslatef(-4.0f, 4.0f, 0.0f); + + glActiveTextureARB(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1_ARB); + glDisable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + { + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); + glVertex2f(-2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); + glVertex2f(2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); + glVertex2f(2.0f, 2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); + glVertex2f(-2.0f, 2.0f); + } + glEnd(); + + glPopMatrix(); + glPushMatrix(); + glTranslatef( 4.0f, 4.0f, 0.0f); + + glActiveTextureARB(GL_TEXTURE0_ARB); + glDisable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1_ARB); + glEnable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + { + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); + glVertex2f(-2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); + glVertex2f(2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); + glVertex2f(2.0f, 2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); + glVertex2f(-2.0f, 2.0f); + } + glEnd(); + + glPopMatrix(); + glPushMatrix(); + glTranslatef( 0.0f, -4.0f, 0.0f); + + glActiveTextureARB(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1_ARB); + glEnable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + { + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); + glVertex2f(-2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); + glVertex2f(2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); + glVertex2f(2.0f, 2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); + glVertex2f(-2.0f, 2.0f); + } + glEnd(); + + glPopMatrix(); + + glFlush(); +} + + +int main() +{ + CLogger(); + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Texture Test", "Texture Test"); + + + #if DEV + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device); + #else + InitGL(); + #endif + + bool done = false; + while (! done) + { + #if DEV + Render(device); + #else + RenderGL(); + #endif + + SDL_GL_SwapBuffers(); + + SDL_Event event; + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) + done = true; + + usleep(50000); + } + + #if DEV + device->Destroy(); + #endif + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + return 0; +} -- cgit v1.2.3-1-g7c22 From 32043605153543bd72eb012ff310367299ad4e8f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 6 Jul 2012 19:00:22 +0200 Subject: Refactoring in math & texture modules - moved texture-related structs to texture.h & code to texture.cpp - cleaned up texture test code - added Math:: namespace qualifiers to math modules for clarity --- src/CMakeLists.txt | 1 + src/graphics/common/device.cpp | 23 ---- src/graphics/common/device.h | 109 ----------------- src/graphics/common/texture.cpp | 43 +++++++ src/graphics/common/texture.h | 109 +++++++++++++++++ src/graphics/opengl/gldevice.cpp | 2 +- src/graphics/opengl/test/texture_test.cpp | 186 +----------------------------- src/math/func.h | 28 ++--- src/math/geometry.h | 111 +++++++++--------- src/math/matrix.h | 14 +-- src/math/vector.h | 12 +- 11 files changed, 239 insertions(+), 399 deletions(-) create mode 100644 src/graphics/common/texture.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c72bcd9..fada14c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,6 +52,7 @@ graphics/common/planet.cpp graphics/common/pyro.cpp graphics/common/terrain.cpp graphics/common/text.cpp +graphics/common/texture.cpp graphics/common/water.cpp graphics/opengl/gldevice.cpp graphics/opengl/glengine.cpp diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp index 79102b2..fcd4318 100644 --- a/src/graphics/common/device.cpp +++ b/src/graphics/common/device.cpp @@ -31,26 +31,3 @@ void Gfx::DeviceConfig::LoadDefault() doubleBuf = true; noFrame = false; } - -void Gfx::TextureCreateParams::LoadDefault() -{ - alpha = false; - mipmap = false; - - minFilter = Gfx::TEX_MIN_FILTER_NEAREST; - magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - - wrapS = Gfx::TEX_WRAP_REPEAT; - wrapT = Gfx::TEX_WRAP_REPEAT; -} - -void Gfx::TextureParams::LoadDefault() -{ - colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; - colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - - alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; - alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; -} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 41181aa..6a71a8a 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -181,115 +181,6 @@ enum PrimitiveType PRIMITIVE_TRIANGLE_STRIP }; -/** - \enum TexMinFilter - \brief Minification texture filter - - Corresponds to OpenGL modes but should translate to DirectX too. */ -enum TexMinFilter -{ - TEX_MIN_FILTER_NEAREST, - TEX_MIN_FILTER_LINEAR, - TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST, - TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST, - TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR, - TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR -}; - -/** - \enum TexMagFilter - \brief Magnification texture filter */ -enum TexMagFilter -{ - TEX_MAG_FILTER_NEAREST, - TEX_MAG_FILTER_LINEAR -}; - -/** - \enum TexWrapMode - \brief Wrapping mode for texture coords */ -enum TexWrapMode -{ - TEX_WRAP_CLAMP, - TEX_WRAP_REPEAT -}; - -/** - \enum TexMixOperation - \brief Multitexture mixing operation - */ -enum TexMixOperation -{ - TEX_MIX_OPER_MODULATE, - TEX_MIX_OPER_ADD -}; - -/** - \enum TexMixArgument - \brief Multitexture mixing argument - */ -enum TexMixArgument -{ - TEX_MIX_ARG_CURRENT, - TEX_MIX_ARG_TEXTURE, - TEX_MIX_ARG_DIFFUSE, - TEX_MIX_ARG_FACTOR -}; - -/** - \struct TextureCreateParams - \brief Parameters for texture creation - */ -struct TextureCreateParams -{ - //! Whether the texture image contains alpha - bool alpha; - //! Whether to generate mipmaps - bool mipmap; - //! Minification filter - Gfx::TexMinFilter minFilter; - //! Magnification filter - Gfx::TexMagFilter magFilter; - //! Wrap S coord mode - Gfx::TexWrapMode wrapS; - //! Wrap T coord mode - Gfx::TexWrapMode wrapT; - - //! Constructor; calls LoadDefault() - TextureCreateParams() - { LoadDefault(); } - - //! Loads the default values - void LoadDefault(); -}; - -/** - \struct TextureParams - \brief Parameters for texture creation - */ -struct TextureParams -{ - //! Mixing operation done on color values - Gfx::TexMixOperation colorOperation; - //! 1st argument of color operations - Gfx::TexMixArgument colorArg1; - //! 2nd argument of color operations - Gfx::TexMixArgument colorArg2; - //! Mixing operation done on alpha values - Gfx::TexMixOperation alphaOperation; - //! 1st argument of alpha operations - Gfx::TexMixArgument alphaArg1; - //! 2nd argument of alpha operations - Gfx::TexMixArgument alphaArg2; - - //! Constructor; calls LoadDefault() - TextureParams() - { LoadDefault(); } - - //! Loads the default values - void LoadDefault(); -}; - /* Notes for rewriting DirectX code: diff --git a/src/graphics/common/texture.cpp b/src/graphics/common/texture.cpp new file mode 100644 index 0000000..50e71cd --- /dev/null +++ b/src/graphics/common/texture.cpp @@ -0,0 +1,43 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// texture.cpp + +#include "graphics/common/texture.h" + + +void Gfx::TextureCreateParams::LoadDefault() +{ + alpha = false; + mipmap = false; + + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + + wrapS = Gfx::TEX_WRAP_REPEAT; + wrapT = Gfx::TEX_WRAP_REPEAT; +} + +void Gfx::TextureParams::LoadDefault() +{ + colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; +} diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index 55d5c70..55b2fc2 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -20,6 +20,115 @@ namespace Gfx { +/** + \enum TexMinFilter + \brief Minification texture filter + + Corresponds to OpenGL modes but should translate to DirectX too. */ +enum TexMinFilter +{ + TEX_MIN_FILTER_NEAREST, + TEX_MIN_FILTER_LINEAR, + TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST, + TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST, + TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR, + TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR +}; + +/** + \enum TexMagFilter + \brief Magnification texture filter */ +enum TexMagFilter +{ + TEX_MAG_FILTER_NEAREST, + TEX_MAG_FILTER_LINEAR +}; + +/** + \enum TexWrapMode + \brief Wrapping mode for texture coords */ +enum TexWrapMode +{ + TEX_WRAP_CLAMP, + TEX_WRAP_REPEAT +}; + +/** + \enum TexMixOperation + \brief Multitexture mixing operation + */ +enum TexMixOperation +{ + TEX_MIX_OPER_MODULATE, + TEX_MIX_OPER_ADD +}; + +/** + \enum TexMixArgument + \brief Multitexture mixing argument + */ +enum TexMixArgument +{ + TEX_MIX_ARG_CURRENT, + TEX_MIX_ARG_TEXTURE, + TEX_MIX_ARG_DIFFUSE, + TEX_MIX_ARG_FACTOR +}; + +/** + \struct TextureCreateParams + \brief Parameters for texture creation + */ +struct TextureCreateParams +{ + //! Whether the texture image contains alpha + bool alpha; + //! Whether to generate mipmaps + bool mipmap; + //! Minification filter + Gfx::TexMinFilter minFilter; + //! Magnification filter + Gfx::TexMagFilter magFilter; + //! Wrap S coord mode + Gfx::TexWrapMode wrapS; + //! Wrap T coord mode + Gfx::TexWrapMode wrapT; + + //! Constructor; calls LoadDefault() + TextureCreateParams() + { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + +/** + \struct TextureParams + \brief Parameters for texture creation + */ +struct TextureParams +{ + //! Mixing operation done on color values + Gfx::TexMixOperation colorOperation; + //! 1st argument of color operations + Gfx::TexMixArgument colorArg1; + //! 2nd argument of color operations + Gfx::TexMixArgument colorArg2; + //! Mixing operation done on alpha values + Gfx::TexMixOperation alphaOperation; + //! 1st argument of alpha operations + Gfx::TexMixArgument alphaArg1; + //! 2nd argument of alpha operations + Gfx::TexMixArgument alphaArg2; + + //! Constructor; calls LoadDefault() + TextureParams() + { LoadDefault(); } + + //! Loads the default values + void LoadDefault(); +}; + /** \struct Texture*/ struct Texture { diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 3182dfc..f253568 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -135,7 +135,7 @@ bool Gfx::CGLDevice::Create() int maxTextures = 0; glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures); - m_textures = std::vector (maxTextures, NULL); + m_textures = std::vector (maxTextures, (Gfx::Texture*)(NULL)); m_texturesEnabled = std::vector (maxTextures, false); m_texturesParams = std::vector(maxTextures, Gfx::TextureParams()); diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 022cf87..764e127 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -7,11 +7,6 @@ #include #include -#include - - -#define DEV 1 - void Init(Gfx::CGLDevice *device) { @@ -68,8 +63,6 @@ void Render(Gfx::CGLDevice *device) { device->BeginScene(); - glFlush(); - Math::Matrix ortho; Math::LoadOrthoProjectionMatrix(ortho, -10, 10, -10, 10); device->SetTransform(Gfx::TRANSFORM_PROJECTION, ortho); @@ -119,172 +112,6 @@ void Render(Gfx::CGLDevice *device) device->EndScene(); } -void InitGL() -{ - CImage img1; - if (! img1.Load("tex1.png")) - { - std::string err = img1.GetError(); - GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); - } - CImage img2; - if (! img2.Load("tex2.png")) - { - std::string err = img2.GetError(); - GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); - } - - unsigned int textureHandle1 = 0; - - glActiveTexture(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - - glGenTextures(1, &textureHandle1); - glBindTexture(GL_TEXTURE_2D, textureHandle1); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img1.GetData()->surface->w, img1.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img1.GetData()->surface->pixels); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - - - unsigned int textureHandle2 = 0; - - glActiveTexture(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - - glGenTextures(1, &textureHandle2); - glBindTexture(GL_TEXTURE_2D, textureHandle2); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img2.GetData()->surface->w, img2.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img2.GetData()->surface->pixels); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - - - glMatrixMode(GL_PROJECTION); - glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - -void RenderGL() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glLoadIdentity(); - - glColor3f(1.0f, 1.0f, 1.0f); - - glPushMatrix(); - glTranslatef(-4.0f, 4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glDisable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - glPushMatrix(); - glTranslatef( 4.0f, 4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glDisable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - glPushMatrix(); - glTranslatef( 0.0f, -4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - - glFlush(); -} - - int main() { CLogger(); @@ -322,24 +149,15 @@ int main() SDL_WM_SetCaption("Texture Test", "Texture Test"); - - #if DEV Gfx::CGLDevice *device = new Gfx::CGLDevice(); device->Create(); Init(device); - #else - InitGL(); - #endif bool done = false; while (! done) { - #if DEV Render(device); - #else - RenderGL(); - #endif SDL_GL_SwapBuffers(); @@ -348,12 +166,10 @@ int main() if (event.type == SDL_QUIT) done = true; - usleep(50000); + usleep(10000); } - #if DEV device->Destroy(); - #endif SDL_FreeSurface(surface); diff --git a/src/math/func.h b/src/math/func.h index 8f0e4ab..9a417dc 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -34,15 +34,15 @@ namespace Math /* @{ */ // start of group //! Compares \a a and \a b within \a tolerance -inline bool IsEqual(float a, float b, float tolerance = TOLERANCE) +inline bool IsEqual(float a, float b, float tolerance = Math::TOLERANCE) { return fabs(a - b) < tolerance; } //! Compares \a a to zero within \a tolerance -inline bool IsZero(float a, float tolerance = TOLERANCE) +inline bool IsZero(float a, float tolerance = Math::TOLERANCE) { - return IsEqual(a, 0.0f, tolerance); + return Math::IsEqual(a, 0.0f, tolerance); } //! Minimum @@ -59,12 +59,12 @@ inline float Min(float a, float b, float c) inline float Min(float a, float b, float c, float d) { - return Min( Min(a, b), Min(c, d) ); + return Math::Min( Math::Min(a, b), Math::Min(c, d) ); } inline float Min(float a, float b, float c, float d, float e) { - return Min( Min(a, b), Min(c, d), e ); + return Math::Min( Math::Min(a, b), Math::Min(c, d), e ); } //! Maximum @@ -76,17 +76,17 @@ inline float Max(float a, float b) inline float Max(float a, float b, float c) { - return Max( Max(a, b), c ); + return Math::Max( Math::Max(a, b), c ); } inline float Max(float a, float b, float c, float d) { - return Max( Max(a, b), Max(c, d) ); + return Math::Max( Math::Max(a, b), Math::Max(c, d) ); } inline float Max(float a, float b, float c, float d, float e) { - return Max( Max(a, b), Max(c, d), e ); + return Math::Max( Math::Max(a, b), Math::Max(c, d), e ); } //! Returns the normalized value (0 .. 1) @@ -130,7 +130,7 @@ inline float Rand() //! Returns a normalized angle, that is in other words between 0 and 2 * PI inline float NormAngle(float angle) { - angle = Mod(angle, PI*2.0f); + angle = Math::Mod(angle, PI*2.0f); if ( angle < 0.0f ) return PI*2.0f + angle; @@ -140,9 +140,9 @@ inline float NormAngle(float angle) //! Test if a angle is between two terminals inline bool TestAngle(float angle, float min, float max) { - angle = NormAngle(angle); - min = NormAngle(min); - max = NormAngle(max); + angle = Math::NormAngle(angle); + min = Math::NormAngle(min); + max = Math::NormAngle(max); if ( min > max ) return ( angle <= max || angle >= min ); @@ -163,8 +163,8 @@ inline float PropAngle(int a, int b, float p) /** A positive angle is counterclockwise (CCW). */ inline float Direction(float a, float g) { - a = NormAngle(a); - g = NormAngle(g); + a = Math::NormAngle(a); + g = Math::NormAngle(g); if ( a < g ) { diff --git a/src/math/geometry.h b/src/math/geometry.h index d5960b8..e56ff10 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -40,7 +40,7 @@ namespace Math //! Returns py up on the line \a a - \a b -inline float MidPoint(const Point &a, const Point &b, float px) +inline float MidPoint(const Math::Point &a, const Math::Point &b, float px) { if (IsEqual(a.x, b.x)) { @@ -53,7 +53,7 @@ inline float MidPoint(const Point &a, const Point &b, float px) } //! Tests whether the point \a p is inside the triangle (\a a,\a b,\a c) -inline bool IsInsideTriangle(Point a, Point b, Point c, Point p) +inline bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math::Point p) { float n, m; @@ -82,13 +82,13 @@ inline bool IsInsideTriangle(Point a, Point b, Point c, Point p) /** \a center center of rotation \a angle angle is in radians (positive is counterclockwise (CCW) ) \a p the point */ -inline Point RotatePoint(const Point ¢er, float angle, const Point &p) +inline Math::Point RotatePoint(const Math::Point ¢er, float angle, const Math::Point &p) { - Point a; + Math::Point a; a.x = p.x-center.x; a.y = p.y-center.y; - Point b; + Math::Point b; b.x = a.x*cosf(angle) - a.y*sinf(angle); b.y = a.x*sinf(angle) + a.y*cosf(angle); @@ -101,23 +101,23 @@ inline Point RotatePoint(const Point ¢er, float angle, const Point &p) //! Rotates a point around the origin (0,0) /** \a angle angle in radians (positive is counterclockwise (CCW) ) \a p the point */ -inline Point RotatePoint(float angle, const Point &p) +inline Math::Point RotatePoint(float angle, const Math::Point &p) { float x = p.x*cosf(angle) - p.y*sinf(angle); float y = p.x*sinf(angle) + p.y*cosf(angle); - return Point(x, y); + return Math::Point(x, y); } //! Rotates a vector (dist, 0). /** \a angle angle is in radians (positive is counterclockwise (CCW) ) \a dist distance to origin */ -inline Point RotatePoint(float angle, float dist) +inline Math::Point RotatePoint(float angle, float dist) { float x = dist*cosf(angle); float y = dist*sinf(angle); - return Point(x, y); + return Math::Point(x, y); } //! TODO documentation @@ -140,13 +140,13 @@ inline void RotatePoint(float cx, float cy, float angle, float &px, float &py) \a angleH,angleV rotation angles in radians (positive is counterclockwise (CCW) ) ) \a p the point \returns the rotated point */ -inline void RotatePoint(const Vector ¢er, float angleH, float angleV, Vector &p) +inline void RotatePoint(const Math::Vector ¢er, float angleH, float angleV, Math::Vector &p) { p.x -= center.x; p.y -= center.y; p.z -= center.z; - Vector b; + Math::Vector b; b.x = p.x*cosf(angleH) - p.z*sinf(angleH); b.y = p.z*sinf(angleV) + p.y*cosf(angleV); b.z = p.x*sinf(angleH) + p.z*cosf(angleH); @@ -159,18 +159,18 @@ inline void RotatePoint(const Vector ¢er, float angleH, float angleV, Vector \a angleH,angleV rotation angles in radians (positive is counterclockwise (CCW) ) ) \a p the point \returns the rotated point */ -inline void RotatePoint2(const Vector center, float angleH, float angleV, Vector &p) +inline void RotatePoint2(const Math::Vector center, float angleH, float angleV, Math::Vector &p) { p.x -= center.x; p.y -= center.y; p.z -= center.z; - Vector a; + Math::Vector a; a.x = p.x*cosf(angleH) - p.z*sinf(angleH); a.y = p.y; a.z = p.x*sinf(angleH) + p.z*cosf(angleH); - Vector b; + Math::Vector b; b.x = a.x; b.y = a.z*sinf(angleV) + a.y*cosf(angleV); b.z = a.z*cosf(angleV) - a.y*sinf(angleV); @@ -196,7 +196,7 @@ inline float RotateAngle(float x, float y) /** \a center the center point \a p1,p2 the two points \returns The angle in radians (positive is counterclockwise (CCW) ) */ -inline float RotateAngle(const Point ¢er, const Point &p1, const Point &p2) +inline float RotateAngle(const Math::Point ¢er, const Math::Point &p1, const Math::Point &p2) { if (PointsEqual(p1, center)) return 0; @@ -221,11 +221,12 @@ inline float RotateAngle(const Point ¢er, const Point &p1, const Point &p2) /** \a from origin \a at view direction \a worldUp up vector */ -inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, const Vector &worldUp) +inline void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from, + const Math::Vector &at, const Math::Vector &worldUp) { // Get the z basis vector, which points straight ahead. This is the // difference from the eyepoint to the lookat point. - Vector view = at - from; + Math::Vector view = at - from; float length = view.Length(); assert(! IsZero(length) ); @@ -237,18 +238,18 @@ inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, co // vector onto the up vector. The projection is the y basis vector. float dotProduct = DotProduct(worldUp, view); - Vector up = worldUp - dotProduct * view; + Math::Vector up = worldUp - dotProduct * view; // If this vector has near-zero length because the input specified a // bogus up vector, let's try a default up vector if ( IsZero(length = up.Length()) ) { - up = Vector(0.0f, 1.0f, 0.0f) - view.y * view; + up = Math::Vector(0.0f, 1.0f, 0.0f) - view.y * view; // If we still have near-zero length, resort to a different axis. if ( IsZero(length = up.Length()) ) { - up = Vector(0.0f, 0.0f, 1.0f) - view.z * view; + up = Math::Vector(0.0f, 0.0f, 1.0f) - view.z * view; assert(! IsZero(up.Length()) ); } @@ -259,7 +260,7 @@ inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, co // The x basis vector is found simply with the cross product of the y // and z basis vectors - Vector right = CrossProduct(up, view); + Math::Vector right = CrossProduct(up, view); // Start building the matrix. The first three rows contains the basis // vectors used to rotate the view to point at the lookat point @@ -286,7 +287,7 @@ inline void LoadViewMatrix(Matrix &mat, const Vector &from, const Vector &at, co \a aspect aspect ratio (width / height) \a nearPlane distance to near cut plane \a farPlane distance to far cut plane */ -inline void LoadProjectionMatrix(Matrix &mat, float fov = 1.570795f, float aspect = 1.0f, +inline void LoadProjectionMatrix(Math::Matrix &mat, float fov = 1.570795f, float aspect = 1.0f, float nearPlane = 1.0f, float farPlane = 1000.0f) { assert(fabs(farPlane - nearPlane) >= 0.01f); @@ -309,7 +310,7 @@ inline void LoadProjectionMatrix(Matrix &mat, float fov = 1.570795f, float aspec /** \a left,right coordinates for left and right vertical clipping planes \a bottom,top coordinates for bottom and top horizontal clipping planes \a zNear,zFar distance to nearer and farther depth clipping planes */ -inline void LoadOrthoProjectionMatrix(Matrix &mat, float left, float right, float bottom, float top, +inline void LoadOrthoProjectionMatrix(Math::Matrix &mat, float left, float right, float bottom, float top, float zNear = -1.0f, float zFar = 1.0f) { mat.LoadIdentity(); @@ -325,7 +326,7 @@ inline void LoadOrthoProjectionMatrix(Matrix &mat, float left, float right, floa //! Loads a translation matrix from given vector /** \a trans vector of translation*/ -inline void LoadTranslationMatrix(Matrix &mat, const Vector &trans) +inline void LoadTranslationMatrix(Math::Matrix &mat, const Math::Vector &trans) { mat.LoadIdentity(); /* (1,4) */ mat.m[12] = trans.x; @@ -335,7 +336,7 @@ inline void LoadTranslationMatrix(Matrix &mat, const Vector &trans) //! Loads a scaling matrix fom given vector /** \a scale vector with scaling factors for X, Y, Z */ -inline void LoadScaleMatrix(Matrix &mat, const Vector &scale) +inline void LoadScaleMatrix(Math::Matrix &mat, const Math::Vector &scale) { mat.LoadIdentity(); /* (1,1) */ mat.m[0 ] = scale.x; @@ -345,7 +346,7 @@ inline void LoadScaleMatrix(Matrix &mat, const Vector &scale) //! Loads a rotation matrix along the X axis /** \a angle angle in radians */ -inline void LoadRotationXMatrix(Matrix &mat, float angle) +inline void LoadRotationXMatrix(Math::Matrix &mat, float angle) { mat.LoadIdentity(); /* (2,2) */ mat.m[5 ] = cosf(angle); @@ -356,7 +357,7 @@ inline void LoadRotationXMatrix(Matrix &mat, float angle) //! Loads a rotation matrix along the Y axis /** \a angle angle in radians */ -inline void LoadRotationYMatrix(Matrix &mat, float angle) +inline void LoadRotationYMatrix(Math::Matrix &mat, float angle) { mat.LoadIdentity(); /* (1,1) */ mat.m[0 ] = cosf(angle); @@ -367,7 +368,7 @@ inline void LoadRotationYMatrix(Matrix &mat, float angle) //! Loads a rotation matrix along the Z axis /** \a angle angle in radians */ -inline void LoadRotationZMatrix(Matrix &mat, float angle) +inline void LoadRotationZMatrix(Math::Matrix &mat, float angle) { mat.LoadIdentity(); /* (1,1) */ mat.m[0 ] = cosf(angle); @@ -379,11 +380,11 @@ inline void LoadRotationZMatrix(Matrix &mat, float angle) //! Loads a rotation matrix along the given axis /** \a dir axis of rotation \a angle angle in radians */ -inline void LoadRotationMatrix(Matrix &mat, const Vector &dir, float angle) +inline void LoadRotationMatrix(Math::Matrix &mat, const Math::Vector &dir, float angle) { float cos = cosf(angle); float sin = sinf(angle); - Vector v = Normalize(dir); + Math::Vector v = Normalize(dir); mat.LoadIdentity(); @@ -401,9 +402,9 @@ inline void LoadRotationMatrix(Matrix &mat, const Vector &dir, float angle) } //! Calculates the matrix to make three rotations in the order X, Z and Y -inline void LoadRotationXZYMatrix(Matrix &mat, const Vector &angle) +inline void LoadRotationXZYMatrix(Math::Matrix &mat, const Math::Vector &angle) { - Matrix temp; + Math::Matrix temp; LoadRotationXMatrix(temp, angle.x); LoadRotationZMatrix(mat, angle.z); @@ -414,9 +415,9 @@ inline void LoadRotationXZYMatrix(Matrix &mat, const Vector &angle) } //! Calculates the matrix to make three rotations in the order Z, X and Y -inline void LoadRotationZXYMatrix(Matrix &mat, const Vector &angle) +inline void LoadRotationZXYMatrix(Math::Matrix &mat, const Math::Vector &angle) { - Matrix temp; + Math::Matrix temp; LoadRotationZMatrix(temp, angle.z); LoadRotationXMatrix(mat, angle.x); @@ -427,7 +428,7 @@ inline void LoadRotationZXYMatrix(Matrix &mat, const Vector &angle) } //! Returns the distance between projections on XZ plane of two vectors -inline float DistanceProjected(const Vector &a, const Vector &b) +inline float DistanceProjected(const Math::Vector &a, const Math::Vector &b) { return sqrtf( (a.x-b.x)*(a.x-b.x) + (a.z-b.z)*(a.z-b.z) ); @@ -435,10 +436,10 @@ inline float DistanceProjected(const Vector &a, const Vector &b) //! Returns the normal vector to a plane /** \param p1,p2,p3 points defining the plane */ -inline Vector NormalToPlane(const Vector &p1, const Vector &p2, const Vector &p3) +inline Math::Vector NormalToPlane(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3) { - Vector u = p3 - p1; - Vector v = p2 - p1; + Math::Vector u = p3 - p1; + Math::Vector v = p2 - p1; return Normalize(CrossProduct(u, v)); } @@ -446,7 +447,7 @@ inline Vector NormalToPlane(const Vector &p1, const Vector &p2, const Vector &p3 //! Returns a point on the line \a p1 - \a p2, in \a dist distance from \a p1 /** \a p1,p2 line start and end \a dist scaling factor from \a p1, relative to distance between \a p1 and \a p2 */ -inline Vector SegmentPoint(const Vector &p1, const Vector &p2, float dist) +inline Math::Vector SegmentPoint(const Math::Vector &p1, const Math::Vector &p2, float dist) { return p1 + (p2 - p1) * dist; } @@ -454,9 +455,10 @@ inline Vector SegmentPoint(const Vector &p1, const Vector &p2, float dist) //! Returns the distance between given point and a plane /** \param p the point \param a,b,c points defining the plane */ -inline float DistanceToPlane(const Vector &a, const Vector &b, const Vector &c, const Vector &p) +inline float DistanceToPlane(const Math::Vector &a, const Math::Vector &b, + const Math::Vector &c, const Math::Vector &p) { - Vector n = NormalToPlane(a, b, c); + Math::Vector n = NormalToPlane(a, b, c); float d = -(n.x*a.x + n.y*a.y + n.z*a.z); return fabs(n.x*p.x + n.y*p.y + n.z*p.z + d); @@ -465,10 +467,10 @@ inline float DistanceToPlane(const Vector &a, const Vector &b, const Vector &c, //! Checks if two planes defined by three points are the same /** \a plane1 array of three vectors defining the first plane \a plane2 array of three vectors defining the second plane */ -inline bool IsSamePlane(const Vector (&plane1)[3], const Vector (&plane2)[3]) +inline bool IsSamePlane(const Math::Vector (&plane1)[3], const Math::Vector (&plane2)[3]) { - Vector n1 = NormalToPlane(plane1[0], plane1[1], plane1[2]); - Vector n2 = NormalToPlane(plane2[0], plane2[1], plane2[2]); + Math::Vector n1 = NormalToPlane(plane1[0], plane1[1], plane1[2]); + Math::Vector n2 = NormalToPlane(plane2[0], plane2[1], plane2[2]); if ( fabs(n1.x-n2.x) > 0.1f || fabs(n1.y-n2.y) > 0.1f || @@ -483,7 +485,8 @@ inline bool IsSamePlane(const Vector (&plane1)[3], const Vector (&plane2)[3]) } //! Calculates the intersection "i" right "of" the plane "abc". -inline bool Intersect(const Vector &a, const Vector &b, const Vector &c, const Vector &d, const Vector &e, Vector &i) +inline bool Intersect(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, + const Math::Vector &d, const Math::Vector &e, Math::Vector &i) { float d1 = (d.x-a.x)*((b.y-a.y)*(c.z-a.z)-(c.y-a.y)*(b.z-a.z)) - (d.y-a.y)*((b.x-a.x)*(c.z-a.z)-(c.x-a.x)*(b.z-a.z)) + @@ -505,7 +508,7 @@ inline bool Intersect(const Vector &a, const Vector &b, const Vector &c, const V //! Calculates the intersection of the straight line passing through p (x, z) /** Line is parallel to the y axis, with the plane abc. Returns p.y. */ -inline bool IntersectY(const Vector &a, const Vector &b, const Vector &c, Vector &p) +inline bool IntersectY(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, Math::Vector &p) { float d = (b.x-a.x)*(c.z-a.z) - (c.x-a.x)*(b.z-a.z); float d1 = (p.x-a.x)*(c.z-a.z) - (c.x-a.x)*(p.z-a.z); @@ -520,9 +523,9 @@ inline bool IntersectY(const Vector &a, const Vector &b, const Vector &c, Vector } //! Calculates the end point -inline Vector LookatPoint(const Vector &eye, float angleH, float angleV, float length) +inline Math::Vector LookatPoint(const Math::Vector &eye, float angleH, float angleV, float length) { - Vector lookat = eye; + Math::Vector lookat = eye; lookat.z += length; RotatePoint(eye, angleH, angleV, lookat); @@ -531,7 +534,7 @@ inline Vector LookatPoint(const Vector &eye, float angleH, float angleV, float l } //! TODO documentation -inline Vector Transform(const Matrix &m, const Vector &p) +inline Math::Vector Transform(const Math::Matrix &m, const Math::Vector &p) { return MatrixVectorMultiply(m, p); } @@ -539,7 +542,7 @@ inline Vector Transform(const Matrix &m, const Vector &p) //! Calculates the projection of the point \a p on a straight line \a a to \a b. /** \a p point to project \a a,b two ends of the line */ -inline Vector Projection(const Vector &a, const Vector &b, const Vector &p) +inline Math::Vector Projection(const Math::Vector &a, const Math::Vector &b, const Math::Vector &p) { float k = DotProduct(b - a, p - a); k /= DotProduct(b - a, b - a); @@ -548,15 +551,15 @@ inline Vector Projection(const Vector &a, const Vector &b, const Vector &p) } //! Calculates point of view to look at a center two angles and a distance -inline Vector RotateView(Vector center, float angleH, float angleV, float dist) +inline Math::Vector RotateView(Math::Vector center, float angleH, float angleV, float dist) { - Matrix mat1, mat2; + Math::Matrix mat1, mat2; LoadRotationZMatrix(mat1, -angleV); LoadRotationYMatrix(mat2, -angleH); - Matrix mat = MultiplyMatrices(mat2, mat1); + Math::Matrix mat = MultiplyMatrices(mat2, mat1); - Vector eye; + Math::Vector eye; eye.x = 0.0f+dist; eye.y = 0.0f; eye.z = 0.0f; diff --git a/src/math/matrix.h b/src/math/matrix.h index 0315a33..7ee40e8 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -388,9 +388,9 @@ inline bool MatricesEqual(const Matrix &m1, const Matrix &m2, } //! Convenience function for getting transposed matrix -inline Matrix Transpose(const Matrix &m) +inline Math::Matrix Transpose(const Math::Matrix &m) { - Matrix result = m; + Math::Matrix result = m; result.Transpose(); return result; } @@ -399,7 +399,7 @@ inline Matrix Transpose(const Matrix &m) /** \a left left-hand matrix \a right right-hand matrix \returns multiplied matrices */ -inline Matrix MultiplyMatrices(const Matrix &left, const Matrix &right) +inline Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right) { return left.Multiply(right); } @@ -413,25 +413,25 @@ inline Matrix MultiplyMatrices(const Matrix &left, const Matrix &right) The result, a 4x1 vector is then converted to 3x1 by dividing x,y,z coords by the fourth coord (w). */ -inline Vector MatrixVectorMultiply(const Matrix &m, const Vector &v, bool wDivide = false) +inline Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide = false) { float x = v.x * m.m[0 ] + v.y * m.m[4 ] + v.z * m.m[8 ] + m.m[12]; float y = v.x * m.m[1 ] + v.y * m.m[5 ] + v.z * m.m[9 ] + m.m[13]; float z = v.x * m.m[2 ] + v.y * m.m[6 ] + v.z * m.m[10] + m.m[14]; if (!wDivide) - return Vector(x, y, z); + return Math::Vector(x, y, z); float w = v.x * m.m[3 ] + v.y * m.m[7 ] + v.z * m.m[11] + m.m[15]; if (IsZero(w)) - return Vector(x, y, z); + return Math::Vector(x, y, z); x /= w; y /= w; z /= w; - return Vector(x, y, z); + return Math::Vector(x, y, z); } /* @} */ // end of group diff --git a/src/math/vector.h b/src/math/vector.h index baba6bb..41f11a8 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -205,7 +205,7 @@ struct Vector }; // struct Point //! Checks if two vectors are equal within given \a tolerance -inline bool VectorsEqual(const Vector &a, const Vector &b, float tolerance = TOLERANCE) +inline bool VectorsEqual(const Math::Vector &a, const Math::Vector &b, float tolerance = TOLERANCE) { return IsEqual(a.x, b.x, tolerance) && IsEqual(a.y, b.y, tolerance) @@ -213,7 +213,7 @@ inline bool VectorsEqual(const Vector &a, const Vector &b, float tolerance = TOL } //! Convenience function for getting normalized vector -inline Vector Normalize(const Vector &v) +inline Vector Normalize(const Math::Vector &v) { Vector result = v; result.Normalize(); @@ -221,25 +221,25 @@ inline Vector Normalize(const Vector &v) } //! Convenience function for calculating dot product -inline float DotProduct(const Vector &left, const Vector &right) +inline float DotProduct(const Math::Vector &left, const Math::Vector &right) { return left.DotMultiply(right); } //! Convenience function for calculating cross product -inline Vector CrossProduct(const Vector &left, const Vector &right) +inline Vector CrossProduct(const Math::Vector &left, const Math::Vector &right) { return left.CrossMultiply(right); } //! Convenience function for calculating angle (in radians) between two vectors -inline float Angle(const Vector &a, const Vector &b) +inline float Angle(const Math::Vector &a, const Math::Vector &b) { return a.Angle(b); } //! Returns the distance between the ends of two vectors -inline float Distance(const Vector &a, const Vector &b) +inline float Distance(const Math::Vector &a, const Math::Vector &b) { return sqrtf( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + -- cgit v1.2.3-1-g7c22 From 1910219518afb26edf7329e0945b9ddba8061a08 Mon Sep 17 00:00:00 2001 From: Zaba999 Date: Tue, 10 Jul 2012 22:58:52 +0200 Subject: Dependency on WINAPI completely removed. --- CMakeLists.txt | 5 +- src/CBot/CBot.cpp | 5201 +++++++++++++++++++++++------------------------ src/CBot/CBot.rc | 279 --- src/CBot/CBotDll.h | 1677 +++++++-------- src/CBot/CBotStack.cpp | 7 +- src/CBot/CBotString.cpp | 710 ++++--- src/CBot/CBotToken.cpp | 1 + src/CBot/CMakeLists.txt | 1 - src/CBot/resource.h | 204 +- 9 files changed, 3879 insertions(+), 4206 deletions(-) delete mode 100644 src/CBot/CBot.rc diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ca52f8..44c0a3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,10 @@ find_package(SDL_image REQUIRED) # Build with debugging symbols set(CMAKE_BUILD_TYPE debug) + # Global compile flags -set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall") -set(CMAKE_CXX_FLAGS_DEBUG "-w -g -O0 -Wall") +set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall -std=gnu++0x") +set(CMAKE_CXX_FLAGS_DEBUG "-w -g -O0 -Wall -std=gnu++0x") # Subdirectory with sources add_subdirectory(src bin) diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 6cb2e5d..e73eea0 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -13,318 +13,315 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// -// Compilation of various instructions -// Compile all routines are static -// And return an object according to what was found as instruction -// Compiler principle: +// compilation of various instructions +// compile all routines are static +// and return an object according to what was found as instruction + +// compiler principle: // compile the routines return an object of the class corresponding to the operation found -// This is always a subclass of CBotInstr. +// this is always a subclass of CBotInstr. // (CBotInstr objects are never used directly) -// Compiles if the routine returns NULL is that the statement is false -// Or misunderstood. -// The error is then on the stack CBotCStack :: Isok () is false +// compiles if the routine returns NULL is that the statement is false +// or misunderstood. +// the error is then on the stack CBotCStack :: Isok () is false #include "CBot.h" - - - -// les divers constructeurs / destructeurs -// pour librer tout selon l'arbre tabli CBotInstr::CBotInstr() { - name = "CBotInstr"; - m_next = NULL; - m_next2b = NULL; - m_next3 = NULL; - m_next3b = NULL; + name = "CBotInstr"; + m_next = NULL; + m_next2b = NULL; + m_next3 = NULL; + m_next3b = NULL; } CBotInstr::~CBotInstr() { - delete m_next; - delete m_next2b; - delete m_next3; - delete m_next3b; + delete m_next; + delete m_next2b; + delete m_next3; + delete m_next3b; } -// compteur de boucles imbriques, -// pour dtermniner les break et continue valides -// et liste des labels utilisables +// counter of nested loops, +// to determine the break and continue valid +// list of labels used -int CBotInstr::m_LoopLvl = 0; -CBotStringArray - CBotInstr::m_labelLvl = CBotStringArray(); -// ajoute un niveau avec un label +int CBotInstr::m_LoopLvl = 0; +CBotStringArray CBotInstr::m_labelLvl = CBotStringArray(); + +// adds a level with a label void CBotInstr::IncLvl(CBotString& label) { - m_labelLvl.SetSize(m_LoopLvl+1); - m_labelLvl[m_LoopLvl] = label; - m_LoopLvl++; + m_labelLvl.SetSize(m_LoopLvl+1); + m_labelLvl[m_LoopLvl] = label; + m_LoopLvl++; } -// ajoute un niveau (instruction switch) +// adds a level (switch statement) void CBotInstr::IncLvl() { - m_labelLvl.SetSize(m_LoopLvl+1); - m_labelLvl[m_LoopLvl] = "#SWITCH"; - m_LoopLvl++; + m_labelLvl.SetSize(m_LoopLvl+1); + m_labelLvl[m_LoopLvl] = "#SWITCH"; + m_LoopLvl++; } -// libre un niveau +// free a level void CBotInstr::DecLvl() { - m_LoopLvl--; - m_labelLvl[m_LoopLvl].Empty(); + m_LoopLvl--; + m_labelLvl[m_LoopLvl].Empty(); } -// controle la validit d'un break ou continu +// control validity of break and continue bool CBotInstr::ChkLvl(const CBotString& label, int type) { - int i = m_LoopLvl; - while (--i>=0) - { - if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; - if ( label.IsEmpty() ) return true; - if ( m_labelLvl[i] == label ) return true; - } - return false; + int i = m_LoopLvl; + while (--i>=0) + { + if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; + if ( label.IsEmpty() ) return true; + if ( m_labelLvl[i] == label ) return true; + } + return false; } bool CBotInstr::IsOfClass(CBotString n) { - return name == n; + return name == n; } //////////////////////////////////////////////////////////////////////////// -// gestion de base de la classe CBotInstr +// database management class CBotInstr -// dfinie le token correspondant l'instruction +// set the token corresponding to the instruction void CBotInstr::SetToken(CBotToken* p) { - m_token = *p; + m_token = *p; } -// rend le type du token associ l'instruction +// return the type of the token assicated with the instruction int CBotInstr::GivTokenType() { - return m_token.GivType(); + return m_token.GivType(); } -// rend le token associ +// return associated token CBotToken* CBotInstr::GivToken() { - return &m_token; + return &m_token; } -// ajoute une instruction la suite des autres +// adds the statement following the other void CBotInstr::AddNext(CBotInstr* n) { - CBotInstr* p = this; - while ( p->m_next != NULL ) p = p->m_next; - p->m_next = n; + CBotInstr* p = this; + while ( p->m_next != NULL ) p = p->m_next; + p->m_next = n; } void CBotInstr::AddNext3(CBotInstr* n) { - CBotInstr* p = this; - while ( p->m_next3 != NULL ) p = p->m_next3; - p->m_next3 = n; + CBotInstr* p = this; + while ( p->m_next3 != NULL ) p = p->m_next3; + p->m_next3 = n; } void CBotInstr::AddNext3b(CBotInstr* n) { - CBotInstr* p = this; - while ( p->m_next3b != NULL ) p = p->m_next3b; - p->m_next3b = n; + CBotInstr* p = this; + while ( p->m_next3b != NULL ) p = p->m_next3b; + p->m_next3b = n; } -// donne l'instruction suivante +// returns next statement CBotInstr* CBotInstr::GivNext() { - return m_next; + return m_next; } CBotInstr* CBotInstr::GivNext3() { - return m_next3; + return m_next3; } CBotInstr* CBotInstr::GivNext3b() { - return m_next3b; + return m_next3b; } /////////////////////////////////////////////////////////////////////////// -// compile une instruction, qui peut tre -// while, do, try, throw, if, for, switch, break, continu, return -// int, float, boolean, string, -// dclaration d'une instance d'une classe -// expression quelconque +// compile an instruction which can be +// while, do, try, throw, if, for, switch, break, continue, return +// int, float, boolean, string, +// declaration of an instance of a class +// arbitrary expression + CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; + CBotToken* pp = p; + + if ( p == NULL ) return NULL; + + int type = p->GivType(); // what is the next token + + // is it a lable? + if ( IsOfType( pp, TokenTypVar ) && + IsOfType( pp, ID_DOTS ) ) + { + type = pp->GivType(); + // these instructions accept only lable + if (!IsOfTypeList( pp, ID_WHILE, ID_FOR, ID_DO, ID_REPEAT, 0 )) + { + pStack->SetError(TX_LABEL, pp->GivStart()); + return NULL; + } + } + + // call routine corresponding to the compilation token found + switch (type) + { + case ID_WHILE: + return CBotWhile::Compile(p, pStack); + + case ID_FOR: + return CBotFor::Compile(p, pStack); + + case ID_DO: + return CBotDo::Compile(p, pStack); + + case ID_REPEAT: + return CBotRepeat::Compile(p, pStack); + + case ID_BREAK: + case ID_CONTINUE: + return CBotBreak::Compile(p, pStack); + + case ID_SWITCH: + return CBotSwitch::Compile(p, pStack); + + case ID_TRY: + return CBotTry::Compile(p, pStack); + + case ID_THROW: + return CBotThrow::Compile(p, pStack); + + case ID_DEBUGDD: + return CBotStartDebugDD::Compile(p, pStack); + + case ID_INT: + return CBotInt::Compile(p, pStack); + + case ID_FLOAT: + return CBotFloat::Compile(p, pStack); + + case ID_STRING: + return CBotIString::Compile(p, pStack); + + case ID_BOOLEAN: + case ID_BOOL: + return CBotBoolean::Compile(p, pStack); + + case ID_IF: + return CBotIf::Compile(p, pStack); + + case ID_RETURN: + return CBotReturn::Compile(p, pStack); + + case ID_ELSE: + pStack->SetStartError(p->GivStart()); + pStack->SetError(TX_ELSEWITHOUTIF, p->GivEnd()); + return NULL; + + case ID_CASE: + pStack->SetStartError(p->GivStart()); + pStack->SetError(TX_OUTCASE, p->GivEnd()); + return NULL; + } - if ( p == NULL ) return NULL; - - int type = p->GivType(); // quel est le prochaine token ? - - // y a-t-il un label ? - if ( IsOfType( pp, TokenTypVar ) && - IsOfType( pp, ID_DOTS ) ) - { - type = pp->GivType(); - // seules ces instructions acceptent un label - if (!IsOfTypeList( pp, ID_WHILE, ID_FOR, ID_DO, ID_REPEAT, 0 )) - { - pStack->SetError(TX_LABEL, pp->GivStart()); - return NULL; - } - } - - // appel la routine de compilation correspondant au token trouv - switch (type) - { - case ID_WHILE: - return CBotWhile::Compile(p, pStack); - - case ID_FOR: - return CBotFor::Compile(p, pStack); - - case ID_DO: - return CBotDo::Compile(p, pStack); - - case ID_REPEAT: - return CBotRepeat::Compile(p, pStack); - - case ID_BREAK: - case ID_CONTINUE: - return CBotBreak::Compile(p, pStack); - - case ID_SWITCH: - return CBotSwitch::Compile(p, pStack); - - case ID_TRY: - return CBotTry::Compile(p, pStack); - - case ID_THROW: - return CBotThrow::Compile(p, pStack); - - case ID_DEBUGDD: - return CBotStartDebugDD::Compile(p, pStack); - - case ID_INT: - return CBotInt::Compile(p, pStack); - - case ID_FLOAT: - return CBotFloat::Compile(p, pStack); - - case ID_STRING: - return CBotIString::Compile(p, pStack); - - case ID_BOOLEAN: - case ID_BOOL: - return CBotBoolean::Compile(p, pStack); - - case ID_IF: - return CBotIf::Compile(p, pStack); - - case ID_RETURN: - return CBotReturn::Compile(p, pStack); - - case ID_ELSE: - pStack->SetStartError(p->GivStart()); - pStack->SetError(TX_ELSEWITHOUTIF, p->GivEnd()); - return NULL; - - case ID_CASE: - pStack->SetStartError(p->GivStart()); - pStack->SetError(TX_OUTCASE, p->GivEnd()); - return NULL; - } - - pStack->SetStartError(p->GivStart()); - - // ne doit pas tre un mot rserv par DefineNum - if ( p->GivType() == TokenTypDef ) - { - pStack->SetError(TX_RESERVED, p); - return NULL; - } - - // ce peut tre une dfinition d'instance de class - CBotToken* ppp = p; - if ( IsOfType( ppp, TokenTypVar ) /* && IsOfType( ppp, TokenTypVar )*/ ) - { - if ( CBotClass::Find(p) != NULL ) - { - // oui, compile la dclaration de l'instance - return CBotClassInst::Compile(p, pStack); - } - } - - // ce peut tre une instruction arithmtique - CBotInstr* inst = CBotExpression::Compile(p, pStack); - if (IsOfType(p, ID_SEP)) - { - return inst; - } - pStack->SetError(TX_ENDOF, p->GivStart()); - delete inst; - return NULL; + pStack->SetStartError(p->GivStart()); + + // ne doit pas tre un mot rserv par DefineNum + if (p->GivType() == TokenTypDef) + { + pStack->SetError(TX_RESERVED, p); + return NULL; + } + + // this might be an instance of class definnition + CBotToken* ppp = p; + if (IsOfType( ppp, TokenTypVar )) + { + if ( CBotClass::Find(p) != NULL ) + { + // oui, compile la dclaration de l'instance + return CBotClassInst::Compile(p, pStack); + } + } + + // ce peut tre une instruction arithmtique + CBotInstr* inst = CBotExpression::Compile(p, pStack); + if (IsOfType(p, ID_SEP)) + { + return inst; + } + pStack->SetError(TX_ENDOF, p->GivStart()); + delete inst; + return NULL; } bool CBotInstr::Execute(CBotStack* &pj) { - CBotString ClassManquante = name; - ASM_TRAP(); // ne doit jamais passer par cette routine - // mais utiliser les routines des classes filles - return false; + CBotString ClassManquante = name; + ASM_TRAP(); // ne doit jamais passer par cette routine + // mais utiliser les routines des classes filles + return false; } bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) { - if ( !Execute(pj) ) return false; - pVar->SetVal( pj->GivVar() ); - return true; + if ( !Execute(pj) ) return false; + pVar->SetVal( pj->GivVar() ); + return true; } void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) { - CBotString ClassManquante = name; - ASM_TRAP(); // ne doit jamais passer par cette routine - // mais utiliser les routines des classes filles + CBotString ClassManquante = name; + ASM_TRAP(); // ne doit jamais passer par cette routine + // mais utiliser les routines des classes filles } bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - ASM_TRAP(); // papa sait pas faire, voir les filles - return false; + ASM_TRAP(); // papa sait pas faire, voir les filles + return false; } bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { - ASM_TRAP(); // papa sait pas faire, voir les filles - return false; + ASM_TRAP(); // papa sait pas faire, voir les filles + return false; } void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) { - ASM_TRAP(); // papa sait pas faire, voir les filles + ASM_TRAP(); // papa sait pas faire, voir les filles } // cette routine n'est dfinie que pour la classe fille CBotCase @@ -333,7 +330,7 @@ void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) bool CBotInstr::CompCase(CBotStack* &pj, int val) { - return false; + return false; } ////////////////////////////////////////////////////////////////////////////////////////// @@ -348,38 +345,38 @@ bool CBotInstr::CompCase(CBotStack* &pj, int val) CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) { - pStack->SetStartError(p->GivStart()); + pStack->SetStartError(p->GivStart()); - if (IsOfType(p, ID_OPBLK)) - { - CBotInstr* inst = CBotListInstr::Compile( p, pStack, bLocal ); + if (IsOfType(p, ID_OPBLK)) + { + CBotInstr* inst = CBotListInstr::Compile( p, pStack, bLocal ); - if (IsOfType(p, ID_CLBLK)) - { - return inst; - } + if (IsOfType(p, ID_CLBLK)) + { + return inst; + } - pStack->SetError(TX_CLOSEBLK, p->GivStart()); // manque la parenthse - delete inst; - return NULL; - } + pStack->SetError(TX_CLOSEBLK, p->GivStart()); // manque la parenthse + delete inst; + return NULL; + } - pStack->SetError(TX_OPENBLK, p->GivStart()); - return NULL; + pStack->SetError(TX_OPENBLK, p->GivStart()); + return NULL; } CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) { - // est-ce un nouveau bloc ? - if ( p->GivType() == ID_OPBLK ) return CBotBlock::Compile(p, pStack); + // est-ce un nouveau bloc ? + if ( p->GivType() == ID_OPBLK ) return CBotBlock::Compile(p, pStack); - // sinon, cherche une instruction unique la place + // sinon, cherche une instruction unique la place - // pour grer les cas avec dfinition local l'instructin (*) - CBotCStack* pStk = pStack->TokenStack(p, bLocal); + // pour grer les cas avec dfinition local l'instructin (*) + CBotCStack* pStk = pStack->TokenStack(p, bLocal); - return pStack->Return( CBotInstr::Compile(p, pStk), // une instruction unique - pStk); + return pStack->Return( CBotInstr::Compile(p, pStk), // une instruction unique + pStk); } // (*) c'est le cas dans l'instruction suivante @@ -395,47 +392,47 @@ CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool b CBotListInstr::CBotListInstr() { - m_Instr = NULL; - name = "CBotListInstr"; + m_Instr = NULL; + name = "CBotListInstr"; } CBotListInstr::~CBotListInstr() { - delete m_Instr; + delete m_Instr; } CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) { - CBotCStack* pStk = pStack->TokenStack(p, bLocal); // les variables sont locales + CBotCStack* pStk = pStack->TokenStack(p, bLocal); // les variables sont locales - CBotListInstr* inst = new CBotListInstr(); + CBotListInstr* inst = new CBotListInstr(); - while (true) - { - if ( p == NULL ) break; + while (true) + { + if ( p == NULL ) break; - if (IsOfType(p, ID_SEP)) continue; // instruction vide ignore - if ( p->GivType() == ID_CLBLK ) break; // dja plus d'instruction + if (IsOfType(p, ID_SEP)) continue; // instruction vide ignore + if ( p->GivType() == ID_CLBLK ) break; // dja plus d'instruction - if (IsOfType(p, 0)) - { - pStack->SetError(TX_CLOSEBLK, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } + if (IsOfType(p, 0)) + { + pStack->SetError(TX_CLOSEBLK, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk ); // compile la suivante + CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk ); // compile la suivante - if (!pStk->IsOk()) - { - delete inst; - return pStack->Return(NULL, pStk); - } + if (!pStk->IsOk()) + { + delete inst; + return pStack->Return(NULL, pStk); + } - if ( inst->m_Instr == NULL ) inst->m_Instr = i; - else inst->m_Instr->AddNext(i); // ajoute la suite - } - return pStack->Return(inst, pStk); + if ( inst->m_Instr == NULL ) inst->m_Instr = i; + else inst->m_Instr->AddNext(i); // ajoute la suite + } + return pStack->Return(inst, pStk); } // excute une liste d'instructions @@ -443,45 +440,45 @@ CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal bool CBotListInstr::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, true);//indispensable pour SetState() - if ( pile->StackOver() ) return pj->Return( pile ); + CBotStack* pile = pj->AddStack(this, true);//indispensable pour SetState() + if ( pile->StackOver() ) return pj->Return( pile ); - CBotInstr* p = m_Instr; // la premire expression + CBotInstr* p = m_Instr; // la premire expression - int state = pile->GivState(); - while (state-->0) p = p->GivNext(); // revient sur l'opration interrompue + int state = pile->GivState(); + while (state-->0) p = p->GivNext(); // revient sur l'opration interrompue - if ( p != NULL ) while (true) - { -// DEBUG( "CBotListInstr", pile->GivState(), pile ); + if ( p != NULL ) while (true) + { +// DEBUG( "CBotListInstr", pile->GivState(), pile ); - if ( !p->Execute(pile) ) return false; - p = p->GivNext(); - if ( p == NULL ) break; - if (!pile->IncState()) ;//return false; // prt pour la suivante - } + if ( !p->Execute(pile) ) return false; + p = p->GivNext(); + if ( p == NULL ) break; + if (!pile->IncState()) ;//return false; // prt pour la suivante + } - return pj->Return( pile ); // transmet en dessous + return pj->Return( pile ); // transmet en dessous } void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - CBotInstr* p = m_Instr; // la premire expression + CBotInstr* p = m_Instr; // la premire expression - int state = pile->GivState(); - while ( p != NULL && state-- > 0) - { - p->RestoreState(pile, false); - p = p->GivNext(); // revient sur l'opration interrompue - } + int state = pile->GivState(); + while ( p != NULL && state-- > 0) + { + p->RestoreState(pile, false); + p = p->GivNext(); // revient sur l'opration interrompue + } - if ( p != NULL ) p->RestoreState(pile, true); + if ( p != NULL ) p->RestoreState(pile, true); } ////////////////////////////////////////////////////////////////////////////////////// @@ -491,9 +488,9 @@ void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) CBotLeftExprVar::CBotLeftExprVar() { - name = "CBotLeftExprVar"; - m_typevar = -1; - m_nIdent = 0; + name = "CBotLeftExprVar"; + m_typevar = -1; + m_nIdent = 0; } CBotLeftExprVar::~CBotLeftExprVar() @@ -502,44 +499,44 @@ CBotLeftExprVar::~CBotLeftExprVar() CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) { - // vrifie que le token est un nom de variable - if (p->GivType() != TokenTypVar) - { - pStack->SetError( TX_NOVAR, p->GivStart()); - return NULL; - } + // vrifie que le token est un nom de variable + if (p->GivType() != TokenTypVar) + { + pStack->SetError( TX_NOVAR, p->GivStart()); + return NULL; + } - CBotLeftExprVar* inst = new CBotLeftExprVar(); - inst->SetToken(p); - p = p->GivNext(); + CBotLeftExprVar* inst = new CBotLeftExprVar(); + inst->SetToken(p); + p = p->GivNext(); - return inst; + return inst; } // cre une variable et lui assigne le rsultat de la pile bool CBotLeftExprVar::Execute(CBotStack* &pj) { - CBotVar* var1; - CBotVar* var2; + CBotVar* var1; + CBotVar* var2; - var1 = CBotVar::Create(m_token.GivString(), m_typevar); - var1->SetUniqNum(m_nIdent); // avec cet identificateur unique - pj->AddVar(var1); // la place sur la pile - - var2 = pj->GivVar(); // resultat sur la pile - if ( var2 ) var1->SetVal(var2); // fait l'assignation + var1 = CBotVar::Create(m_token.GivString(), m_typevar); + var1->SetUniqNum(m_nIdent); // avec cet identificateur unique + pj->AddVar(var1); // la place sur la pile + + var2 = pj->GivVar(); // resultat sur la pile + if ( var2 ) var1->SetVal(var2); // fait l'assignation - return true; // opration faite + return true; // opration faite } void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) { - CBotVar* var1; + CBotVar* var1; - var1 = pj->FindVar(m_token.GivString()); - if ( var1 == NULL ) ASM_TRAP(); + var1 = pj->FindVar(m_token.GivString()); + if ( var1 == NULL ) ASM_TRAP(); - var1->SetUniqNum(m_nIdent); // avec cet identificateur unique + var1->SetUniqNum(m_nIdent); // avec cet identificateur unique } ////////////////////////////////////////////////////////////////////////////////////// @@ -553,73 +550,73 @@ void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) CBotInstArray::CBotInstArray() { - m_var = NULL; - m_listass = NULL; - name = "CBotInstArray"; + m_var = NULL; + m_listass = NULL; + name = "CBotInstArray"; } CBotInstArray::~CBotInstArray() { - delete m_var; - delete m_listass; + delete m_var; + delete m_listass; } CBotInstr* CBotInstArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) { - CBotCStack* pStk = pStack->TokenStack(p); + CBotCStack* pStk = pStack->TokenStack(p); - CBotInstArray* inst = new CBotInstArray(); // cre l'objet + CBotInstArray* inst = new CBotInstArray(); // cre l'objet - CBotToken* vartoken = p; - inst->SetToken(vartoken); + CBotToken* vartoken = p; + inst->SetToken(vartoken); - // dtermine l'expression valable pour l'lment gauche - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable ? - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } + // dtermine l'expression valable pour l'lment gauche + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable ? + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } - CBotInstr* i; - while (IsOfType(p, ID_OPBRK)) // avec des indices ? - { - if ( p->GivType() != ID_CLBRK ) - i = CBotExpression::Compile( p, pStk ); // expression pour la valeur - else - i = new CBotEmpty(); // spcial si pas de formule + CBotInstr* i; + while (IsOfType(p, ID_OPBRK)) // avec des indices ? + { + if ( p->GivType() != ID_CLBRK ) + i = CBotExpression::Compile( p, pStk ); // expression pour la valeur + else + i = new CBotEmpty(); // spcial si pas de formule - inst->AddNext3b(i); // construit une liste - type = CBotTypResult(CBotTypArrayPointer, type); + inst->AddNext3b(i); // construit une liste + type = CBotTypResult(CBotTypArrayPointer, type); - if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - } + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + } - CBotVar* var = CBotVar::Create(vartoken, type); // cre avec une instance - inst->m_typevar = type; + CBotVar* var = CBotVar::Create(vartoken, type); // cre avec une instance + inst->m_typevar = type; - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // lui attribut un numro unique + pStack->AddVar(var); // la place sur la pile - if ( IsOfType(p, ID_ASS) ) // avec une assignation - { - inst->m_listass = CBotListArray::Compile( p, pStk, type.GivTypElem() ); - } + if ( IsOfType(p, ID_ASS) ) // avec une assignation + { + inst->m_listass = CBotListArray::Compile( p, pStk, type.GivTypElem() ); + } - if ( pStk->IsOk() ) return pStack->Return(inst, pStk); - } + if ( pStk->IsOk() ) return pStack->Return(inst, pStk); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } @@ -627,132 +624,132 @@ error: bool CBotInstArray::Execute(CBotStack* &pj) { - CBotStack* pile1 = pj->AddStack(this); -// if ( pile1 == EOX ) return true; - - CBotStack* pile = pile1; - - if ( pile1->GivState() == 0 ) - { - // cherche les dimensions max du tableau - CBotInstr* p = GivNext3b(); // les diffrentes formules - int nb = 0; - - while (p != NULL) - { - pile = pile->AddStack(); // petite place pour travailler - nb++; - if ( pile->GivState() == 0 ) - { - if ( !p->Execute(pile) ) return false; // calcul de la taille // interrompu? - pile->IncState(); - } - p = p->GivNext3b(); - } - - p = GivNext3b(); - pile = pile1; // revient sur la pile - int n = 0; - int max[100]; - - while (p != NULL) - { - pile = pile->AddStack(); // rcupre la mme petite place - CBotVar* v = pile->GivVar(); // rsultat - max[n] = v->GivValInt(); // valeur - if (max[n]>MAXARRAYSIZE) - { - pile->SetError(TX_OUTARRAY, &m_token); - return pj->Return ( pile ); - } - n++; - p = p->GivNext3b(); - } - while (n<100) max[n++] = 0; - - m_typevar.SetArray( max ); // mmorise les limitations - - // cre simplement un pointeur null - CBotVar* var = CBotVar::Create(m_var->GivToken(), m_typevar); - var->SetPointer(NULL); - var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); - pj->AddVar(var); // inscrit le tableau de base sur la pile - -#if STACKMEM - pile1->AddStack()->Delete(); + CBotStack* pile1 = pj->AddStack(this); +// if ( pile1 == EOX ) return true; + + CBotStack* pile = pile1; + + if ( pile1->GivState() == 0 ) + { + // cherche les dimensions max du tableau + CBotInstr* p = GivNext3b(); // les diffrentes formules + int nb = 0; + + while (p != NULL) + { + pile = pile->AddStack(); // petite place pour travailler + nb++; + if ( pile->GivState() == 0 ) + { + if ( !p->Execute(pile) ) return false; // calcul de la taille // interrompu? + pile->IncState(); + } + p = p->GivNext3b(); + } + + p = GivNext3b(); + pile = pile1; // revient sur la pile + int n = 0; + int max[100]; + + while (p != NULL) + { + pile = pile->AddStack(); // rcupre la mme petite place + CBotVar* v = pile->GivVar(); // rsultat + max[n] = v->GivValInt(); // valeur + if (max[n]>MAXARRAYSIZE) + { + pile->SetError(TX_OUTARRAY, &m_token); + return pj->Return ( pile ); + } + n++; + p = p->GivNext3b(); + } + while (n<100) max[n++] = 0; + + m_typevar.SetArray( max ); // mmorise les limitations + + // cre simplement un pointeur null + CBotVar* var = CBotVar::Create(m_var->GivToken(), m_typevar); + var->SetPointer(NULL); + var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); + pj->AddVar(var); // inscrit le tableau de base sur la pile + +#if STACKMEM + pile1->AddStack()->Delete(); #else - delete pile1->AddStack(); // plus besoin des indices + delete pile1->AddStack(); // plus besoin des indices #endif - pile1->IncState(); - } + pile1->IncState(); + } - if ( pile1->GivState() == 1 ) - { - if ( m_listass != NULL ) // il y a des assignation pour ce tableau - { - CBotVar* pVar = pj->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); + if ( pile1->GivState() == 1 ) + { + if ( m_listass != NULL ) // il y a des assignation pour ce tableau + { + CBotVar* pVar = pj->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); - if ( !m_listass->Execute(pile1, pVar) ) return false; - } - pile1->IncState(); - } + if ( !m_listass->Execute(pile1, pVar) ) return false; + } + pile1->IncState(); + } - if ( pile1->IfStep() ) return false; // montre ce pas ? + if ( pile1->IfStep() ) return false; // montre ce pas ? - if ( m_next2b && - !m_next2b->Execute( pile1 ) ) return false; + if ( m_next2b && + !m_next2b->Execute( pile1 ) ) return false; - return pj->Return( pile1 ); // transmet en dessous + return pj->Return( pile1 ); // transmet en dessous } void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile1 = pj; + CBotStack* pile1 = pj; - CBotVar* var = pj->FindVar(m_var->GivToken()->GivString()); - if ( var != NULL ) var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); + CBotVar* var = pj->FindVar(m_var->GivToken()->GivString()); + if ( var != NULL ) var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); - if ( bMain ) - { - pile1 = pj->RestoreStack(this); - CBotStack* pile = pile1; - if ( pile == NULL ) return; + if ( bMain ) + { + pile1 = pj->RestoreStack(this); + CBotStack* pile = pile1; + if ( pile == NULL ) return; - if ( pile1->GivState() == 0 ) - { - // cherche les dimensions max du tableau - CBotInstr* p = GivNext3b(); // les diffrentes formules + if ( pile1->GivState() == 0 ) + { + // cherche les dimensions max du tableau + CBotInstr* p = GivNext3b(); // les diffrentes formules - while (p != NULL) - { - pile = pile->RestoreStack(); // petite place pour travailler - if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) - { - p->RestoreState(pile, bMain); // calcul de la taille // interrompu! - return; - } - p = p->GivNext3b(); - } - } - if ( pile1->GivState() == 1 && m_listass != NULL ) - { - m_listass->RestoreState(pile1, bMain); - } + while (p != NULL) + { + pile = pile->RestoreStack(); // petite place pour travailler + if ( pile == NULL ) return; + if ( pile->GivState() == 0 ) + { + p->RestoreState(pile, bMain); // calcul de la taille // interrompu! + return; + } + p = p->GivNext3b(); + } + } + if ( pile1->GivState() == 1 && m_listass != NULL ) + { + m_listass->RestoreState(pile1, bMain); + } - } + } - if ( m_next2b ) m_next2b->RestoreState( pile1, bMain ); + if ( m_next2b ) m_next2b->RestoreState( pile1, bMain ); } // cas particulier pour les indices vides bool CBotEmpty :: Execute(CBotStack* &pj) { - CBotVar* pVar = CBotVar::Create("", CBotTypInt); - pVar->SetValInt(-1); // met la valeur -1 sur la pile - pj->SetVar(pVar); - return true; + CBotVar* pVar = CBotVar::Create("", CBotTypInt); + pVar->SetValInt(-1); // met la valeur -1 sur la pile + pj->SetVar(pVar); + return true; } void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) @@ -766,107 +763,107 @@ void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) CBotListArray::CBotListArray() { - m_expr = NULL; - name = "CBotListArray"; + m_expr = NULL; + name = "CBotListArray"; } CBotListArray::~CBotListArray() { - delete m_expr; + delete m_expr; } CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) { - CBotCStack* pStk = pStack->TokenStack(p); - - CBotToken* pp = p; - - if ( IsOfType( p, ID_NULL ) ) - { - CBotInstr* inst = new CBotExprNull (); - inst->SetToken( pp ); -// CBotVar* var = CBotVar::Create("", CBotTypNullPointer); -// pStk->SetVar(var); - return pStack->Return(inst, pStk); // ok avec lment vide - } - - CBotListArray* inst = new CBotListArray(); // cre l'objet - - if ( IsOfType( p, ID_OPENPAR ) ) - { - // prend chaque lment l'un aprs l'autre - if ( type.Eq( CBotTypArrayPointer ) ) - { - type = type.GivTypElem(); - - pStk->SetStartError(p->GivStart()); - if ( NULL == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) ) ) - { - goto error; - } - - while ( IsOfType( p, ID_COMMA ) ) // d'autres lments ? - { - pStk->SetStartError(p->GivStart()); - - CBotInstr* i = CBotListArray::Compile( p, pStk, type ); - if ( NULL == i ) - { - goto error; - } - - inst->m_expr->AddNext3(i); - } - } - else - { - pStk->SetStartError(p->GivStart()); - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - CBotVar* pv = pStk->GivVar(); // le rsultat de l'expression - - if ( pv == NULL || !TypesCompatibles( type, pv->GivTypResult() )) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - - while ( IsOfType( p, ID_COMMA ) ) // d'autres lments ? - { - pStk->SetStartError(p->GivStart()); - - CBotInstr* i = CBotTwoOpExpr::Compile( p, pStk ) ; - if ( NULL == i ) - { - goto error; - } - - CBotVar* pv = pStk->GivVar(); // le rsultat de l'expression - - if ( pv == NULL || !TypesCompatibles( type, pv->GivTypResult() )) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - inst->m_expr->AddNext3(i); - } - } - - if (!IsOfType(p, ID_CLOSEPAR) ) - { - pStk->SetError(TX_CLOSEPAR, p->GivStart()); - goto error; - } - - return pStack->Return(inst, pStk); - } + CBotCStack* pStk = pStack->TokenStack(p); + + CBotToken* pp = p; + + if ( IsOfType( p, ID_NULL ) ) + { + CBotInstr* inst = new CBotExprNull (); + inst->SetToken( pp ); +// CBotVar* var = CBotVar::Create("", CBotTypNullPointer); +// pStk->SetVar(var); + return pStack->Return(inst, pStk); // ok avec lment vide + } + + CBotListArray* inst = new CBotListArray(); // cre l'objet + + if ( IsOfType( p, ID_OPENPAR ) ) + { + // prend chaque lment l'un aprs l'autre + if ( type.Eq( CBotTypArrayPointer ) ) + { + type = type.GivTypElem(); + + pStk->SetStartError(p->GivStart()); + if ( NULL == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) ) ) + { + goto error; + } + + while ( IsOfType( p, ID_COMMA ) ) // d'autres lments ? + { + pStk->SetStartError(p->GivStart()); + + CBotInstr* i = CBotListArray::Compile( p, pStk, type ); + if ( NULL == i ) + { + goto error; + } + + inst->m_expr->AddNext3(i); + } + } + else + { + pStk->SetStartError(p->GivStart()); + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + CBotVar* pv = pStk->GivVar(); // le rsultat de l'expression + + if ( pv == NULL || !TypesCompatibles( type, pv->GivTypResult() )) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + + while ( IsOfType( p, ID_COMMA ) ) // d'autres lments ? + { + pStk->SetStartError(p->GivStart()); + + CBotInstr* i = CBotTwoOpExpr::Compile( p, pStk ) ; + if ( NULL == i ) + { + goto error; + } + + CBotVar* pv = pStk->GivVar(); // le rsultat de l'expression + + if ( pv == NULL || !TypesCompatibles( type, pv->GivTypResult() )) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + inst->m_expr->AddNext3(i); + } + } + + if (!IsOfType(p, ID_CLOSEPAR) ) + { + pStk->SetError(TX_CLOSEPAR, p->GivStart()); + goto error; + } + + return pStack->Return(inst, pStk); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } @@ -874,43 +871,43 @@ error: bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) { - CBotStack* pile1 = pj->AddStack(); -// if ( pile1 == EOX ) return true; - CBotVar* pVar2; + CBotStack* pile1 = pj->AddStack(); +// if ( pile1 == EOX ) return true; + CBotVar* pVar2; - CBotInstr* p = m_expr; + CBotInstr* p = m_expr; - int n = 0; + int n = 0; - for ( ; p != NULL ; n++, p = p->GivNext3() ) - { - if ( pile1->GivState() > n ) continue; + for ( ; p != NULL ; n++, p = p->GivNext3() ) + { + if ( pile1->GivState() > n ) continue; - pVar2 = pVar->GivItem(n, true); + pVar2 = pVar->GivItem(n, true); - if ( !p->Execute(pile1, pVar2) ) return false; // value l'expression + if ( !p->Execute(pile1, pVar2) ) return false; // value l'expression - pile1->IncState(); - } + pile1->IncState(); + } - return pj->Return( pile1 ); // transmet en dessous + return pj->Return( pile1 ); // transmet en dessous } void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) - { - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if ( bMain ) + { + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - CBotInstr* p = m_expr; + CBotInstr* p = m_expr; - int state = pile->GivState(); + int state = pile->GivState(); - while( state-- > 0 ) p = p->GivNext3() ; + while( state-- > 0 ) p = p->GivNext3() ; - p->RestoreState(pile, bMain); // calcul de la taille // interrompu! - } + p->RestoreState(pile, bMain); // calcul de la taille // interrompu! + } } ////////////////////////////////////////////////////////////////////////////////////// @@ -921,197 +918,197 @@ void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) CBotInt::CBotInt() { - m_next = NULL; // pour les dfinitions multiples - m_var = - m_expr = NULL; - name = "CBotInt"; + m_next = NULL; // pour les dfinitions multiples + m_var = + m_expr = NULL; + name = "CBotInt"; } CBotInt::~CBotInt() { - delete m_var; - delete m_expr; -// delete m_next; // fait par le destructeur de la classe de base ~CBotInstr() + delete m_var; + delete m_expr; +// delete m_next; // fait par le destructeur de la classe de base ~CBotInstr() } CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) { - if ( IsOfType(p, ID_OPBRK) ) - { - if ( !IsOfType(p, ID_CLBRK) ) - { - pStack->SetError(TX_CLBRK, p->GivStart()); - return NULL; - } + if ( IsOfType(p, ID_OPBRK) ) + { + if ( !IsOfType(p, ID_CLBRK) ) + { + pStack->SetError(TX_CLBRK, p->GivStart()); + return NULL; + } - CBotInstr* inst = CompileArray(p, pStack, CBotTypResult( CBotTypArrayPointer, type ), false); - if ( inst != NULL || !pStack->IsOk() ) return inst; - } + CBotInstr* inst = CompileArray(p, pStack, CBotTypResult( CBotTypArrayPointer, type ), false); + if ( inst != NULL || !pStack->IsOk() ) return inst; + } - // compile une dclaration de tableau - if (first) return NULL ; + // compile une dclaration de tableau + if (first) return NULL ; - CBotInstr* inst = CBotInstArray::Compile( p, pStack, type ); - if ( inst == NULL ) return NULL; + CBotInstr* inst = CBotInstArray::Compile( p, pStack, type ); + if ( inst == NULL ) return NULL; - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false) )) // compile la suivante - { - return inst; - } - delete inst; - return NULL; - } + if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + { + if ( NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false) )) // compile la suivante + { + return inst; + } + delete inst; + return NULL; + } - if (IsOfType(p, ID_SEP)) // instruction termine - { - return inst; - } + if (IsOfType(p, ID_SEP)) // instruction termine + { + return inst; + } - delete inst; - pStack->SetError(TX_ENDOF, p->GivStart()); - return NULL; + delete inst; + pStack->SetError(TX_ENDOF, p->GivStart()); + return NULL; } CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { - CBotToken* pp = cont ? NULL : p; // pas de rptition du token "int" - - if (!cont && !IsOfType(p, ID_INT)) return NULL; - - CBotInt* inst = (CBotInt*)CompileArray(p, pStack, CBotTypInt); - if ( inst != NULL || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotInt(); // cre l'objet - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - inst->SetToken( vartoken ); - - // dtermine l'expression valable pour l'lment gauche - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypInt; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) // avec des indices ? - { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable - - // compile une dclaration de tableau - - CBotInstr* inst2 = CBotInstArray::Compile( p, pStk, CBotTypInt ); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante - { - return pStack->Return(inst2, pStk); - } - } - inst = (CBotInt*)inst2; - goto suite; // pas d'assignation, variable dj cre - } - - if (IsOfType(p, ID_ASS)) // avec une assignation ? - { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - if ( pStk->GivType() >= CBotTypBoolean ) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - } - - { - CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile - } - - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante - { - return pStack->Return(inst, pStk); - } - } + CBotToken* pp = cont ? NULL : p; // pas de rptition du token "int" + + if (!cont && !IsOfType(p, ID_INT)) return NULL; + + CBotInt* inst = (CBotInt*)CompileArray(p, pStack, CBotTypInt); + if ( inst != NULL || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotInt(); // cre l'objet + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + inst->SetToken( vartoken ); + + // dtermine l'expression valable pour l'lment gauche + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypInt; + if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) // avec des indices ? + { + delete inst; // n'est pas de type CBotInt + p = vartoken; // revient sur le nom de la variable + + // compile une dclaration de tableau + + CBotInstr* inst2 = CBotInstArray::Compile( p, pStk, CBotTypInt ); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + + if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + { + if ( NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante + { + return pStack->Return(inst2, pStk); + } + } + inst = (CBotInt*)inst2; + goto suite; // pas d'assignation, variable dj cre + } + + if (IsOfType(p, ID_ASS)) // avec une assignation ? + { + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + if ( pStk->GivType() >= CBotTypBoolean ) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + } + + { + CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// cre la variable (aprs l'assignation value) + var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // lui attribut un numro unique + pStack->AddVar(var); // la place sur la pile + } + + if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + { + if ( NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante + { + return pStack->Return(inst, pStk); + } + } suite: - if (noskip || IsOfType(p, ID_SEP)) // instruction termine - { - return pStack->Return(inst, pStk); - } + if (noskip || IsOfType(p, ID_SEP)) // instruction termine + { + return pStack->Return(inst, pStk); + } - pStk->SetError(TX_ENDOF, p->GivStart()); - } + pStk->SetError(TX_ENDOF, p->GivStart()); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } // excute la dfinition de la variable entire bool CBotInt::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); //indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); //indispensable pour SetState() +// if ( pile == EOX ) return true; - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? + m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return false; - } + if (!pile->SetState(1)) return false; + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + if ( m_next2b && + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) - return pj->Return( pile ); // transmet en dessous + return pj->Return( pile ); // transmet en dessous } void CBotInt::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile = pj; - if ( bMain ) - { - pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj; + if ( bMain ) + { + pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale // interrompu! - return; - } - } + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale // interrompu! + return; + } + } - m_var->RestoreState(pile, bMain); + m_var->RestoreState(pile, bMain); - if ( m_next2b ) m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if ( m_next2b ) m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1123,145 +1120,145 @@ void CBotInt::RestoreState(CBotStack* &pj, bool bMain) CBotBoolean::CBotBoolean() { - m_var = - m_expr = NULL; - name = "CBotBoolean"; + m_var = + m_expr = NULL; + name = "CBotBoolean"; } CBotBoolean::~CBotBoolean() { - delete m_var; - delete m_expr; + delete m_var; + delete m_expr; } CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { - CBotToken* pp = cont ? NULL : p; - - if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return NULL; - - CBotBoolean* inst = (CBotBoolean*)CompileArray(p, pStack, CBotTypBoolean); - if ( inst != NULL || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotBoolean(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - inst->SetToken( vartoken ); - CBotVar* var = NULL; - - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypBoolean; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) // avec des indices ? - { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable - - // compile une dclaration de tableau - - inst = (CBotBoolean*)CBotInstArray::Compile( p, pStk, CBotTypBoolean ); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - goto suite; // pas d'assignation, variable dj cre - } - - if (IsOfType(p, ID_ASS)) // avec une assignation ? - { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - if ( !pStk->GivTypResult().Eq(CBotTypBoolean) ) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - } - - var = CBotVar::Create(vartoken, CBotTypBoolean);// cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + CBotToken* pp = cont ? NULL : p; + + if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return NULL; + + CBotBoolean* inst = (CBotBoolean*)CompileArray(p, pStack, CBotTypBoolean); + if ( inst != NULL || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotBoolean(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + inst->SetToken( vartoken ); + CBotVar* var = NULL; + + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypBoolean; + if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) // avec des indices ? + { + delete inst; // n'est pas de type CBotInt + p = vartoken; // revient sur le nom de la variable + + // compile une dclaration de tableau + + inst = (CBotBoolean*)CBotInstArray::Compile( p, pStk, CBotTypBoolean ); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + goto suite; // pas d'assignation, variable dj cre + } + + if (IsOfType(p, ID_ASS)) // avec une assignation ? + { + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + if ( !pStk->GivTypResult().Eq(CBotTypBoolean) ) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + } + + var = CBotVar::Create(vartoken, CBotTypBoolean);// cre la variable (aprs l'assignation value) + var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // lui attribut un numro unique + pStack->AddVar(var); // la place sur la pile suite: - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip) )) // compile la suivante - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) // instruction termine - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } + if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + { + if ( NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip) )) // compile la suivante + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) // instruction termine + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } // excute une dfinition de variable boolenne bool CBotBoolean::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() +// if ( pile == EOX ) return true; - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? + m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return false; - } + if (!pile->SetState(1)) return false; + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + if ( m_next2b && + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) - return pj->Return( pile ); // transmet en dessous + return pj->Return( pile ); // transmet en dessous } void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile = pj; - if ( bMain ) - { - pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj; + if ( bMain ) + { + pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? - return; - } - } + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? + return; + } + } - m_var->RestoreState( pile, bMain ); // + m_var->RestoreState( pile, bMain ); // - if ( m_next2b ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if ( m_next2b ) + m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1273,146 +1270,146 @@ void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) CBotFloat::CBotFloat() { - m_var = - m_expr = NULL; - name = "CBotFloat"; + m_var = + m_expr = NULL; + name = "CBotFloat"; } CBotFloat::~CBotFloat() { - delete m_var; - delete m_expr; + delete m_var; + delete m_expr; } CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { - CBotToken* pp = cont ? NULL : p; - - if (!cont && !IsOfType(p, ID_FLOAT)) return NULL; - - CBotFloat* inst = (CBotFloat*)CompileArray(p, pStack, CBotTypFloat); - if ( inst != NULL || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotFloat(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - CBotVar* var = NULL; - inst->SetToken(vartoken); - - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypFloat; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable - { - pStk->SetStartError(vartoken->GivStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) // avec des indices ? - { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable - - // compile une dclaration de tableau - - inst = (CBotFloat*)CBotInstArray::Compile( p, pStk, CBotTypFloat ); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - goto suite; // pas d'assignation, variable dj cre - } - - if (IsOfType(p, ID_ASS)) // avec une assignation ? - { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - if ( pStk->GivType() >= CBotTypBoolean ) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - } - - var = CBotVar::Create(vartoken, CBotTypFloat); // cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + CBotToken* pp = cont ? NULL : p; + + if (!cont && !IsOfType(p, ID_FLOAT)) return NULL; + + CBotFloat* inst = (CBotFloat*)CompileArray(p, pStack, CBotTypFloat); + if ( inst != NULL || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotFloat(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + CBotVar* var = NULL; + inst->SetToken(vartoken); + + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypFloat; + if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + { + pStk->SetStartError(vartoken->GivStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) // avec des indices ? + { + delete inst; // n'est pas de type CBotInt + p = vartoken; // revient sur le nom de la variable + + // compile une dclaration de tableau + + inst = (CBotFloat*)CBotInstArray::Compile( p, pStk, CBotTypFloat ); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + goto suite; // pas d'assignation, variable dj cre + } + + if (IsOfType(p, ID_ASS)) // avec une assignation ? + { + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + if ( pStk->GivType() >= CBotTypBoolean ) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + } + + var = CBotVar::Create(vartoken, CBotTypFloat); // cre la variable (aprs l'assignation value) + var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // lui attribut un numro unique + pStack->AddVar(var); // la place sur la pile suite: - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip) )) // compile la suivante - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) // instruction termine - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } + if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + { + if ( NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip) )) // compile la suivante + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) // instruction termine + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } // excute la dfintion de la variable relle bool CBotFloat::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() +// if ( pile == EOX ) return true; - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? + m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return false; - } + if (!pile->SetState(1)) return false; + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + if ( m_next2b && + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) - return pj->Return( pile ); // transmet en dessous + return pj->Return( pile ); // transmet en dessous } void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile = pj; - if ( bMain ) - { - pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj; + if ( bMain ) + { + pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? - return; - } - } + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? + return; + } + } - m_var->RestoreState( pile, bMain ); // + m_var->RestoreState( pile, bMain ); // - if ( m_next2b ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if ( m_next2b ) + m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1424,129 +1421,129 @@ void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) CBotIString::CBotIString() { - m_var = - m_expr = NULL; - name = "CBotIString"; + m_var = + m_expr = NULL; + name = "CBotIString"; } CBotIString::~CBotIString() { - delete m_var; - delete m_expr; + delete m_var; + delete m_expr; } CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { - CBotToken* pp = cont ? NULL : p; - - if (!cont && !IsOfType(p, ID_STRING)) return NULL; - - CBotIString* inst = (CBotIString*)CompileArray(p, pStack, CBotTypString); - if ( inst != NULL || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotIString(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - inst->SetToken( vartoken ); - - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypString; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable - { - pStk->SetStartError(vartoken->GivStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); - goto error; - } - - if (IsOfType(p, ID_ASS)) // avec une assignation ? - { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } -/* if ( !pStk->GivTypResult().Eq(CBotTypString) ) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - }*/ - } - - CBotVar* var = CBotVar::Create(vartoken, CBotTypString); // cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile - - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip) )) // compile la suivante - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) // instruction termine - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } + CBotToken* pp = cont ? NULL : p; + + if (!cont && !IsOfType(p, ID_STRING)) return NULL; + + CBotIString* inst = (CBotIString*)CompileArray(p, pStack, CBotTypString); + if ( inst != NULL || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotIString(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + inst->SetToken( vartoken ); + + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypString; + if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + { + pStk->SetStartError(vartoken->GivStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); + goto error; + } + + if (IsOfType(p, ID_ASS)) // avec une assignation ? + { + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } +/* if ( !pStk->GivTypResult().Eq(CBotTypString) ) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + }*/ + } + + CBotVar* var = CBotVar::Create(vartoken, CBotTypString); // cre la variable (aprs l'assignation value) + var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // lui attribut un numro unique + pStack->AddVar(var); // la place sur la pile + + if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + { + if ( NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip) )) // compile la suivante + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) // instruction termine + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } // excute la dfinition de la variable string bool CBotIString::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() +// if ( pile == EOX ) return true; - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? + m_var->Execute( pile ); // cre et fait l'assigation du rsultat - if (!pile->SetState(1)) return false; - } + if (!pile->SetState(1)) return false; + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + if ( m_next2b && + !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) - return pj->Return( pile ); // transmet en dessous + return pj->Return( pile ); // transmet en dessous } void CBotIString::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile = pj; - - if ( bMain ) - { - pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj; + + if ( bMain ) + { + pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? - return; - } - } + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? + return; + } + } - m_var->RestoreState( pile, bMain ); // + m_var->RestoreState( pile, bMain ); // - if ( m_next2b ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if ( m_next2b ) + m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1559,269 +1556,269 @@ void CBotIString::RestoreState(CBotStack* &pj, bool bMain) CBotExpression::CBotExpression() { - m_leftop = NULL; - m_rightop = NULL; - name = "CBotExpression"; + m_leftop = NULL; + m_rightop = NULL; + name = "CBotExpression"; } CBotExpression::~CBotExpression() { - delete m_leftop; - delete m_rightop; + delete m_leftop; + delete m_rightop; } CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; - - CBotExpression* inst = new CBotExpression(); - - inst->m_leftop = CBotLeftExpr::Compile(p, pStack); - - inst->SetToken(p); - int OpType = p->GivType(); - - if ( pStack->IsOk() && - IsOfTypeList(p, ID_ASS, ID_ASSADD, ID_ASSSUB, ID_ASSMUL, ID_ASSDIV, ID_ASSMODULO, - ID_ASSAND, ID_ASSXOR, ID_ASSOR, - ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) - { - if ( inst->m_leftop == NULL ) - { - pStack->SetError(TX_BADLEFT, p->GivEnd()); - delete inst; - return NULL; - } - - inst->m_rightop = CBotExpression::Compile(p, pStack); - if (inst->m_rightop == NULL) - { - delete inst; - return NULL; - } - - CBotTypResult type1 = pStack->GivTypResult(); - - // rcupre la variable pour la marquer assigne - CBotVar* var = NULL; - inst->m_leftop->ExecuteVar(var, pStack); - if ( var == NULL ) - { - delete inst; - return NULL; - } - - if (OpType != ID_ASS && var->GivInit() != IS_DEF) - { - pStack->SetError(TX_NOTINIT, pp); - delete inst; - return NULL; - } - - CBotTypResult type2 = var->GivTypResult(); - - // quels sont les types acceptables ? - switch (OpType) - { - case ID_ASS: - // if (type2 == CBotTypClass) type2 = -1; // pas de classe - if ( (type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer) ) || - (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) - { -/* CBotClass* c1 = type1.GivClass(); - CBotClass* c2 = type2.GivClass(); - if ( !c1->IsChildOf(c2) ) type2.SetType(-1); // pas la mme classe -//- if ( !type1.Eq(CBotTypClass) ) var->SetPointer(pStack->GivVar()->GivPointer());*/ - var->SetInit(2); - } - else - var->SetInit(true); - - break; - case ID_ASSADD: - if (type2.Eq(CBotTypBoolean) || - type2.Eq(CBotTypPointer) ) type2 = -1; // nombres et chaines - break; - case ID_ASSSUB: - case ID_ASSMUL: - case ID_ASSDIV: - case ID_ASSMODULO: - if (type2.GivType() >= CBotTypBoolean) type2 = -1; // nombres uniquement - break; - } - - if (!TypeCompatible( type1, type2, OpType )) - { - pStack->SetError(TX_BADTYPE, &inst->m_token); - delete inst; - return NULL; - } - - return inst; // types compatibles ? - } - - delete inst; -// p = p->GivNext(); - int start, end, error = pStack->GivError(start, end); - - p = pp; // revient au dbut - pStack->SetError(0,0); // oublie l'erreur - -// return CBotTwoOpExpr::Compile(p, pStack); // essaie sans assignation - CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // essaie sans assignation - if ( i != NULL && error == TX_PRIVATE && p->GivType() == ID_ASS ) - pStack->ResetError( error, start, end ); - return i; + CBotToken* pp = p; + + CBotExpression* inst = new CBotExpression(); + + inst->m_leftop = CBotLeftExpr::Compile(p, pStack); + + inst->SetToken(p); + int OpType = p->GivType(); + + if ( pStack->IsOk() && + IsOfTypeList(p, ID_ASS, ID_ASSADD, ID_ASSSUB, ID_ASSMUL, ID_ASSDIV, ID_ASSMODULO, + ID_ASSAND, ID_ASSXOR, ID_ASSOR, + ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) + { + if ( inst->m_leftop == NULL ) + { + pStack->SetError(TX_BADLEFT, p->GivEnd()); + delete inst; + return NULL; + } + + inst->m_rightop = CBotExpression::Compile(p, pStack); + if (inst->m_rightop == NULL) + { + delete inst; + return NULL; + } + + CBotTypResult type1 = pStack->GivTypResult(); + + // rcupre la variable pour la marquer assigne + CBotVar* var = NULL; + inst->m_leftop->ExecuteVar(var, pStack); + if ( var == NULL ) + { + delete inst; + return NULL; + } + + if (OpType != ID_ASS && var->GivInit() != IS_DEF) + { + pStack->SetError(TX_NOTINIT, pp); + delete inst; + return NULL; + } + + CBotTypResult type2 = var->GivTypResult(); + + // quels sont les types acceptables ? + switch (OpType) + { + case ID_ASS: + // if (type2 == CBotTypClass) type2 = -1; // pas de classe + if ( (type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer) ) || + (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) + { +/* CBotClass* c1 = type1.GivClass(); + CBotClass* c2 = type2.GivClass(); + if ( !c1->IsChildOf(c2) ) type2.SetType(-1); // pas la mme classe +//- if ( !type1.Eq(CBotTypClass) ) var->SetPointer(pStack->GivVar()->GivPointer());*/ + var->SetInit(2); + } + else + var->SetInit(true); + + break; + case ID_ASSADD: + if (type2.Eq(CBotTypBoolean) || + type2.Eq(CBotTypPointer) ) type2 = -1; // nombres et chaines + break; + case ID_ASSSUB: + case ID_ASSMUL: + case ID_ASSDIV: + case ID_ASSMODULO: + if (type2.GivType() >= CBotTypBoolean) type2 = -1; // nombres uniquement + break; + } + + if (!TypeCompatible( type1, type2, OpType )) + { + pStack->SetError(TX_BADTYPE, &inst->m_token); + delete inst; + return NULL; + } + + return inst; // types compatibles ? + } + + delete inst; +// p = p->GivNext(); + int start, end, error = pStack->GivError(start, end); + + p = pp; // revient au dbut + pStack->SetError(0,0); // oublie l'erreur + +// return CBotTwoOpExpr::Compile(p, pStack); // essaie sans assignation + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // essaie sans assignation + if ( i != NULL && error == TX_PRIVATE && p->GivType() == ID_ASS ) + pStack->ResetError( error, start, end ); + return i; } // excute une expression avec assignation bool CBotExpression::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - CBotToken* pToken = m_leftop->GivToken(); - CBotVar* pVar = NULL; - - CBotStack* pile1 = pile; - - bool IsInit = true; - CBotVar* result = NULL; - - // doit tre fait avant pour les indices ventuels (pile peut tre change) - if ( !m_leftop->ExecuteVar(pVar, pile, NULL, false) ) return false; // variable avant valuation de la valeur droite - -// DEBUG( "CBotExpression::Execute", -1, pj); - if ( pile1->GivState()==0) - { - pile1->SetCopyVar(pVar); // garde une copie sur la pile (si interrompu) - pile1->IncState(); - } - - CBotStack* pile2 = pile->AddStack(); // attention pile et surtout pas pile1 - - if ( pile2->GivState()==0) - { -// DEBUG( "CBotExpression::Execute", -2, pj); - if (m_rightop && !m_rightop->Execute(pile2)) return false; // valeur initiale // interrompu? - pile2->IncState(); - } - - if ( pile1->GivState() == 1 ) - { -// DEBUG( "CBotExpression::Execute", -3, pj); - if ( m_token.GivType() != ID_ASS ) - { - pVar = pile1->GivVar(); // rcupre si interrompu - IsInit = pVar->GivInit(); - if ( IsInit == IS_NAN ) - { - pile2->SetError(TX_OPNAN, m_leftop->GivToken()); - return pj->Return(pile2); - } - result = CBotVar::Create("", pVar->GivTypResult(2)); - } - - switch ( m_token.GivType() ) - { - case ID_ASS: - break; - case ID_ASSADD: - result->Add(pile1->GivVar(), pile2->GivVar()); // additionne - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSSUB: - result->Sub(pile1->GivVar(), pile2->GivVar()); // soustrait - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSMUL: - result->Mul(pile1->GivVar(), pile2->GivVar()); // multiplie - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSDIV: - if (IsInit && - result->Div(pile1->GivVar(), pile2->GivVar())) // divise - pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSMODULO: - if (IsInit && - result->Modulo(pile1->GivVar(), pile2->GivVar())) // reste de la division - pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSAND: - result->And(pile1->GivVar(), pile2->GivVar()); // multiplie - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSXOR: - result->XOr(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSOR: - result->Or(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSSL: - result->SL(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSSR: - result->SR(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat - break; - case ID_ASSASR: - result->ASR(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat - break; - default: - ASM_TRAP(); - } - if (!IsInit) - pile2->SetError(TX_NOTINIT, m_leftop->GivToken()); - - pile1->IncState(); - } - -// DEBUG( "CBotExpression::Execute", -4, pj); - if ( !m_leftop->Execute( pile2, pile1 ) ) - return false; // cre et fait l'assigation du rsultat - - return pj->Return( pile2 ); // transmet en dessous + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + CBotToken* pToken = m_leftop->GivToken(); + CBotVar* pVar = NULL; + + CBotStack* pile1 = pile; + + bool IsInit = true; + CBotVar* result = NULL; + + // doit tre fait avant pour les indices ventuels (pile peut tre change) + if ( !m_leftop->ExecuteVar(pVar, pile, NULL, false) ) return false; // variable avant valuation de la valeur droite + +// DEBUG( "CBotExpression::Execute", -1, pj); + if ( pile1->GivState()==0) + { + pile1->SetCopyVar(pVar); // garde une copie sur la pile (si interrompu) + pile1->IncState(); + } + + CBotStack* pile2 = pile->AddStack(); // attention pile et surtout pas pile1 + + if ( pile2->GivState()==0) + { +// DEBUG( "CBotExpression::Execute", -2, pj); + if (m_rightop && !m_rightop->Execute(pile2)) return false; // valeur initiale // interrompu? + pile2->IncState(); + } + + if ( pile1->GivState() == 1 ) + { +// DEBUG( "CBotExpression::Execute", -3, pj); + if ( m_token.GivType() != ID_ASS ) + { + pVar = pile1->GivVar(); // rcupre si interrompu + IsInit = pVar->GivInit(); + if ( IsInit == IS_NAN ) + { + pile2->SetError(TX_OPNAN, m_leftop->GivToken()); + return pj->Return(pile2); + } + result = CBotVar::Create("", pVar->GivTypResult(2)); + } + + switch ( m_token.GivType() ) + { + case ID_ASS: + break; + case ID_ASSADD: + result->Add(pile1->GivVar(), pile2->GivVar()); // additionne + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSSUB: + result->Sub(pile1->GivVar(), pile2->GivVar()); // soustrait + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSMUL: + result->Mul(pile1->GivVar(), pile2->GivVar()); // multiplie + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSDIV: + if (IsInit && + result->Div(pile1->GivVar(), pile2->GivVar())) // divise + pile2->SetError(TX_DIVZERO, &m_token); + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSMODULO: + if (IsInit && + result->Modulo(pile1->GivVar(), pile2->GivVar())) // reste de la division + pile2->SetError(TX_DIVZERO, &m_token); + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSAND: + result->And(pile1->GivVar(), pile2->GivVar()); // multiplie + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSXOR: + result->XOr(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSOR: + result->Or(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSSL: + result->SL(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSSR: + result->SR(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); // re-place le rsultat + break; + case ID_ASSASR: + result->ASR(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); // re-place le rsultat + break; + default: + ASM_TRAP(); + } + if (!IsInit) + pile2->SetError(TX_NOTINIT, m_leftop->GivToken()); + + pile1->IncState(); + } + +// DEBUG( "CBotExpression::Execute", -4, pj); + if ( !m_leftop->Execute( pile2, pile1 ) ) + return false; // cre et fait l'assigation du rsultat + + return pj->Return( pile2 ); // transmet en dessous } void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) - { - CBotToken* pToken = m_leftop->GivToken(); - CBotVar* pVar = NULL; + if ( bMain ) + { + CBotToken* pToken = m_leftop->GivToken(); + CBotVar* pVar = NULL; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - CBotStack* pile1 = pile; - + CBotStack* pile1 = pile; + - if ( pile1->GivState()==0) - { - m_leftop->RestoreStateVar(pile, true); // variable avant valuation de la valeur droite - return; - } + if ( pile1->GivState()==0) + { + m_leftop->RestoreStateVar(pile, true); // variable avant valuation de la valeur droite + return; + } - m_leftop->RestoreStateVar(pile, false); // variable avant valuation de la valeur droite + m_leftop->RestoreStateVar(pile, false); // variable avant valuation de la valeur droite - CBotStack* pile2 = pile->RestoreStack(); // attention pile et surtout pas pile1 - if ( pile2 == NULL ) return; + CBotStack* pile2 = pile->RestoreStack(); // attention pile et surtout pas pile1 + if ( pile2 == NULL ) return; - if ( pile2->GivState()==0) - { - if (m_rightop) m_rightop->RestoreState(pile2, bMain); // valeur initiale // interrompu? - return; - } - } + if ( pile2->GivState()==0) + { + if (m_rightop) m_rightop->RestoreState(pile2, bMain); // valeur initiale // interrompu? + return; + } + } } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1836,24 +1833,24 @@ void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) { - pStack->SetStartError(p->GivStart()); - if ( IsOfType(p, ID_OPENPAR )) - { - CBotInstr* inst = CBotBoolExpr::Compile( p, pStack ); - if ( NULL != inst ) - { - if ( IsOfType(p, ID_CLOSEPAR )) - { - return inst; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); // manque la parenthse - } - delete inst; - } + pStack->SetStartError(p->GivStart()); + if ( IsOfType(p, ID_OPENPAR )) + { + CBotInstr* inst = CBotBoolExpr::Compile( p, pStack ); + if ( NULL != inst ) + { + if ( IsOfType(p, ID_CLOSEPAR )) + { + return inst; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); // manque la parenthse + } + delete inst; + } - pStack->SetError(TX_OPENPAR, p->GivStart()); // manque la parenthse + pStack->SetError(TX_OPENPAR, p->GivStart()); // manque la parenthse - return NULL; + return NULL; } @@ -1866,21 +1863,21 @@ CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) { - pStack->SetStartError(p->GivStart()); + pStack->SetStartError(p->GivStart()); - CBotInstr* inst = CBotTwoOpExpr::Compile( p, pStack ); + CBotInstr* inst = CBotTwoOpExpr::Compile( p, pStack ); - if ( NULL != inst ) - { - if ( pStack->GivTypResult().Eq(CBotTypBoolean) ) - { - return inst; - } - pStack->SetError(TX_NOTBOOL, p->GivStart()); // n'est pas un boolan - } + if ( NULL != inst ) + { + if ( pStack->GivTypResult().Eq(CBotTypBoolean) ) + { + return inst; + } + pStack->SetError(TX_NOTBOOL, p->GivStart()); // n'est pas un boolan + } - delete inst; - return NULL; + delete inst; + return NULL; } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1888,166 +1885,166 @@ CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) ////////////////////////////////////////////////////////////////////////////////////// // compile soit : -// une instruction entre parenthses (...) -// une expression unaire (ngatif, not) -// nom de variable -// les variables pr et post incrmentes ou dcrmentes -// un nombre donn par DefineNum -// une constante -// un appel de procdure -// l'instruction new +// une instruction entre parenthses (...) +// une expression unaire (ngatif, not) +// nom de variable +// les variables pr et post incrmentes ou dcrmentes +// un nombre donn par DefineNum +// une constante +// un appel de procdure +// l'instruction new // cette classe n'a pas de constructeur, car il n'y a jamais d'instance de cette classe // l'objet retourn par Compile est de la classe correspondant l'instruction CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GivStart()); - - // est-ce une expression entre parenthse ? - if (IsOfType(p, ID_OPENPAR)) - { - CBotInstr* inst = CBotExpression::Compile( p, pStk ); - - if ( NULL != inst ) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - return pStack->Return(inst, pStk); - } - pStk->SetError(TX_CLOSEPAR, p->GivStart()); - } - delete inst; - return pStack->Return(NULL, pStk); - } - - // est-ce une opration unaire ? - CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); - if (inst != NULL || !pStk->IsOk()) - return pStack->Return(inst, pStk); - - // est-ce un nom de variable ? - if (p->GivType() == TokenTypVar) - { - // c'est peut-tre un appel de mthode sans le "this." devant - inst = CBotExprVar::CompileMethode(p, pStk); - if ( inst != NULL ) return pStack->Return(inst, pStk); - - - // est-ce un appel de procdure ? - inst = CBotInstrCall::Compile(p, pStk); - if ( inst != NULL || !pStk->IsOk() ) - return pStack->Return(inst, pStk); - - - CBotToken* pvar = p; - // non, c'est une variable "ordinaire" - inst = CBotExprVar::Compile(p, pStk); - - CBotToken* pp = p; - // post incrment ou dcrment ? - if (IsOfType(p, ID_INC, ID_DEC)) - { - if ( pStk->GivType() >= CBotTypBoolean ) - { - pStk->SetError(TX_BADTYPE, pp); - delete inst; - return pStack->Return(NULL, pStk); - } - - // recompile la variable pour read-only - delete inst; - p = pvar; - inst = CBotExprVar::Compile(p, pStk, PR_READ); - p = p->GivNext(); - - CBotPostIncExpr* i = new CBotPostIncExpr(); - i->SetToken(pp); - i->m_Instr = inst; // instruction associe - return pStack->Return(i, pStk); - } - return pStack->Return(inst, pStk); - } - - // est-ce une variable princrmente ou prdcrmente ? - CBotToken* pp = p; - if (IsOfType(p, ID_INC, ID_DEC)) - { - CBotPreIncExpr* i = new CBotPreIncExpr(); - i->SetToken(pp); - - if (p->GivType() == TokenTypVar) - { - if (NULL != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) - { - if ( pStk->GivType() >= CBotTypBoolean ) - { - pStk->SetError(TX_BADTYPE, pp); - delete inst; - return pStack->Return(NULL, pStk); - } - return pStack->Return(i, pStk); - } - delete i; - return pStack->Return(NULL, pStk); - } - } - - // est-ce un nombre ou un DefineNum ? - if (p->GivType() == TokenTypNum || - p->GivType() == TokenTypDef ) - { - CBotInstr* inst = CBotExprNum::Compile( p, pStk ); - return pStack->Return(inst, pStk); - } - - // est-ce une chaine ? - if (p->GivType() == TokenTypString) - { - CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // est un lment "true" ou "false" - if (p->GivType() == ID_TRUE || - p->GivType() == ID_FALSE ) - { - CBotInstr* inst = CBotExprBool::Compile( p, pStk ); - return pStack->Return(inst, pStk); - } - - // est un objet crer avec new - if (p->GivType() == ID_NEW) - { - CBotInstr* inst = CBotNew::Compile( p, pStk ); - return pStack->Return(inst, pStk); - } - - // est un pointeur nul - if (IsOfType( p, ID_NULL )) - { - CBotInstr* inst = new CBotExprNull (); - inst->SetToken( pp ); - CBotVar* var = CBotVar::Create("", CBotTypNullPointer); - pStk->SetVar(var); - return pStack->Return(inst, pStk); - } - - // est un nombre nan - if (IsOfType( p, ID_NAN )) - { - CBotInstr* inst = new CBotExprNan (); - inst->SetToken( pp ); - CBotVar* var = CBotVar::Create("", CBotTypInt); - var->SetInit(IS_NAN); - pStk->SetVar(var); - return pStack->Return(inst, pStk); - } - - - return pStack->Return(NULL, pStk); + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GivStart()); + + // est-ce une expression entre parenthse ? + if (IsOfType(p, ID_OPENPAR)) + { + CBotInstr* inst = CBotExpression::Compile( p, pStk ); + + if ( NULL != inst ) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + return pStack->Return(inst, pStk); + } + pStk->SetError(TX_CLOSEPAR, p->GivStart()); + } + delete inst; + return pStack->Return(NULL, pStk); + } + + // est-ce une opration unaire ? + CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); + if (inst != NULL || !pStk->IsOk()) + return pStack->Return(inst, pStk); + + // est-ce un nom de variable ? + if (p->GivType() == TokenTypVar) + { + // c'est peut-tre un appel de mthode sans le "this." devant + inst = CBotExprVar::CompileMethode(p, pStk); + if ( inst != NULL ) return pStack->Return(inst, pStk); + + + // est-ce un appel de procdure ? + inst = CBotInstrCall::Compile(p, pStk); + if ( inst != NULL || !pStk->IsOk() ) + return pStack->Return(inst, pStk); + + + CBotToken* pvar = p; + // non, c'est une variable "ordinaire" + inst = CBotExprVar::Compile(p, pStk); + + CBotToken* pp = p; + // post incrment ou dcrment ? + if (IsOfType(p, ID_INC, ID_DEC)) + { + if ( pStk->GivType() >= CBotTypBoolean ) + { + pStk->SetError(TX_BADTYPE, pp); + delete inst; + return pStack->Return(NULL, pStk); + } + + // recompile la variable pour read-only + delete inst; + p = pvar; + inst = CBotExprVar::Compile(p, pStk, PR_READ); + p = p->GivNext(); + + CBotPostIncExpr* i = new CBotPostIncExpr(); + i->SetToken(pp); + i->m_Instr = inst; // instruction associe + return pStack->Return(i, pStk); + } + return pStack->Return(inst, pStk); + } + + // est-ce une variable princrmente ou prdcrmente ? + CBotToken* pp = p; + if (IsOfType(p, ID_INC, ID_DEC)) + { + CBotPreIncExpr* i = new CBotPreIncExpr(); + i->SetToken(pp); + + if (p->GivType() == TokenTypVar) + { + if (NULL != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) + { + if ( pStk->GivType() >= CBotTypBoolean ) + { + pStk->SetError(TX_BADTYPE, pp); + delete inst; + return pStack->Return(NULL, pStk); + } + return pStack->Return(i, pStk); + } + delete i; + return pStack->Return(NULL, pStk); + } + } + + // est-ce un nombre ou un DefineNum ? + if (p->GivType() == TokenTypNum || + p->GivType() == TokenTypDef ) + { + CBotInstr* inst = CBotExprNum::Compile( p, pStk ); + return pStack->Return(inst, pStk); + } + + // est-ce une chaine ? + if (p->GivType() == TokenTypString) + { + CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // est un lment "true" ou "false" + if (p->GivType() == ID_TRUE || + p->GivType() == ID_FALSE ) + { + CBotInstr* inst = CBotExprBool::Compile( p, pStk ); + return pStack->Return(inst, pStk); + } + + // est un objet crer avec new + if (p->GivType() == ID_NEW) + { + CBotInstr* inst = CBotNew::Compile( p, pStk ); + return pStack->Return(inst, pStk); + } + + // est un pointeur nul + if (IsOfType( p, ID_NULL )) + { + CBotInstr* inst = new CBotExprNull (); + inst->SetToken( pp ); + CBotVar* var = CBotVar::Create("", CBotTypNullPointer); + pStk->SetVar(var); + return pStack->Return(inst, pStk); + } + + // est un nombre nan + if (IsOfType( p, ID_NAN )) + { + CBotInstr* inst = new CBotExprNan (); + inst->SetToken( pp ); + CBotVar* var = CBotVar::Create("", CBotTypInt); + var->SetInit(IS_NAN); + pStk->SetVar(var); + return pStack->Return(inst, pStk); + } + + + return pStack->Return(NULL, pStk); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2060,218 +2057,218 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) CBotPostIncExpr::CBotPostIncExpr() { - m_Instr = NULL; - name = "CBotPostIncExpr"; + m_Instr = NULL; + name = "CBotPostIncExpr"; } CBotPostIncExpr::~CBotPostIncExpr() { - delete m_Instr; + delete m_Instr; } CBotPreIncExpr::CBotPreIncExpr() { - m_Instr = NULL; - name = "CBotPreIncExpr"; + m_Instr = NULL; + name = "CBotPreIncExpr"; } CBotPreIncExpr::~CBotPreIncExpr() { - delete m_Instr; + delete m_Instr; } bool CBotPostIncExpr::Execute(CBotStack* &pj) { - CBotStack* pile1 = pj->AddStack(this); - CBotStack* pile2 = pile1; + CBotStack* pile1 = pj->AddStack(this); + CBotStack* pile2 = pile1; - CBotVar* var1 = NULL; + CBotVar* var1 = NULL; - if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index + if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index - pile1->SetState(1); - pile1->SetCopyVar(var1); // place le rsultat (avant incrmentation); + pile1->SetState(1); + pile1->SetCopyVar(var1); // place le rsultat (avant incrmentation); - CBotStack* pile3 = pile2->AddStack(this); - if ( pile3->IfStep() ) return false; + CBotStack* pile3 = pile2->AddStack(this); + if ( pile3->IfStep() ) return false; - if ( var1->GivInit() == IS_NAN ) - { - pile1->SetError( TX_OPNAN, &m_token ); - } + if ( var1->GivInit() == IS_NAN ) + { + pile1->SetError( TX_OPNAN, &m_token ); + } - if ( var1->GivInit() != IS_DEF ) - { - pile1->SetError( TX_NOTINIT, &m_token ); - } + if ( var1->GivInit() != IS_DEF ) + { + pile1->SetError( TX_NOTINIT, &m_token ); + } - if (GivTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); + if (GivTokenType() == ID_INC) var1->Inc(); + else var1->Dec(); - return pj->Return(pile1); // opration faite, rsultat sur pile2 + return pj->Return(pile1); // opration faite, rsultat sur pile2 } void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile1 = pj->RestoreStack(this); - if ( pile1 == NULL ) return; + CBotStack* pile1 = pj->RestoreStack(this); + if ( pile1 == NULL ) return; - ((CBotExprVar*)m_Instr)->RestoreStateVar(pile1, bMain); + ((CBotExprVar*)m_Instr)->RestoreStateVar(pile1, bMain); - if ( pile1 != NULL ) pile1->RestoreStack(this); + if ( pile1 != NULL ) pile1->RestoreStack(this); } bool CBotPreIncExpr::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - CBotVar* var1; + CBotVar* var1; - if ( pile->GivState() == 0 ) - { - CBotStack* pile2 = pile; - if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index - // pile2 est modifi en retour + if ( pile->GivState() == 0 ) + { + CBotStack* pile2 = pile; + if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index + // pile2 est modifi en retour - if ( var1->GivInit() == IS_NAN ) - { - pile->SetError( TX_OPNAN, &m_token ); - return pj->Return(pile); // opration faite - } + if ( var1->GivInit() == IS_NAN ) + { + pile->SetError( TX_OPNAN, &m_token ); + return pj->Return(pile); // opration faite + } - if ( var1->GivInit() != IS_DEF ) - { - pile->SetError( TX_NOTINIT, &m_token ); - return pj->Return(pile); // opration faite - } + if ( var1->GivInit() != IS_DEF ) + { + pile->SetError( TX_NOTINIT, &m_token ); + return pj->Return(pile); // opration faite + } - if (GivTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); // ((CBotVarInt*)var1)->m_val + if (GivTokenType() == ID_INC) var1->Inc(); + else var1->Dec(); // ((CBotVarInt*)var1)->m_val - pile->IncState(); - } + pile->IncState(); + } - if ( !m_Instr->Execute(pile) ) return false; - return pj->Return(pile); // opration faite + if ( !m_Instr->Execute(pile) ) return false; + return pj->Return(pile); // opration faite } void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) - { - return; - } + if ( pile->GivState() == 0 ) + { + return; + } - m_Instr->RestoreState(pile, bMain); + m_Instr->RestoreState(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////// // compile une expression unaire -// + -// - -// not -// ! -// ~ +// + +// - +// not +// ! +// ~ CBotExprUnaire::CBotExprUnaire() { - m_Expr = NULL; - name = "CBotExprUnaire"; + m_Expr = NULL; + name = "CBotExprUnaire"; } CBotExprUnaire::~CBotExprUnaire() { - delete m_Expr; + delete m_Expr; } CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) { - int op = p->GivType(); - CBotToken* pp = p; - if ( !IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 ) ) return NULL; + int op = p->GivType(); + CBotToken* pp = p; + if ( !IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 ) ) return NULL; - CBotCStack* pStk = pStack->TokenStack(pp); + CBotCStack* pStk = pStack->TokenStack(pp); - CBotExprUnaire* inst = new CBotExprUnaire(); - inst->SetToken(pp); + CBotExprUnaire* inst = new CBotExprUnaire(); + inst->SetToken(pp); - if ( NULL != (inst->m_Expr = CBotParExpr::Compile( p, pStk )) ) - { - if ( op == ID_ADD && pStk->GivType() < CBotTypBoolean ) // seulement avec des nombre - return pStack->Return(inst, pStk); - if ( op == ID_SUB && pStk->GivType() < CBotTypBoolean ) // seulement avec des nombre - return pStack->Return(inst, pStk); - if ( op == ID_NOT && pStk->GivType() < CBotTypFloat ) // seulement avec des entiers - return pStack->Return(inst, pStk); - if ( op == ID_LOG_NOT && pStk->GivTypResult().Eq(CBotTypBoolean) )// seulement avec des boolens - return pStack->Return(inst, pStk); - if ( op == ID_TXT_NOT && pStk->GivTypResult().Eq(CBotTypBoolean) )// seulement avec des boolens - return pStack->Return(inst, pStk); - - pStk->SetError(TX_BADTYPE, &inst->m_token); - } - delete inst; - return pStack->Return(NULL, pStk); + if ( NULL != (inst->m_Expr = CBotParExpr::Compile( p, pStk )) ) + { + if ( op == ID_ADD && pStk->GivType() < CBotTypBoolean ) // seulement avec des nombre + return pStack->Return(inst, pStk); + if ( op == ID_SUB && pStk->GivType() < CBotTypBoolean ) // seulement avec des nombre + return pStack->Return(inst, pStk); + if ( op == ID_NOT && pStk->GivType() < CBotTypFloat ) // seulement avec des entiers + return pStack->Return(inst, pStk); + if ( op == ID_LOG_NOT && pStk->GivTypResult().Eq(CBotTypBoolean) )// seulement avec des boolens + return pStack->Return(inst, pStk); + if ( op == ID_TXT_NOT && pStk->GivTypResult().Eq(CBotTypBoolean) )// seulement avec des boolens + return pStack->Return(inst, pStk); + + pStk->SetError(TX_BADTYPE, &inst->m_token); + } + delete inst; + return pStack->Return(NULL, pStk); } // excute l'expresson unaire bool CBotExprUnaire::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) - { - if (!m_Expr->Execute( pile )) return false; // interrompu ? - pile->IncState(); - } + if ( pile->GivState() == 0 ) + { + if (!m_Expr->Execute( pile )) return false; // interrompu ? + pile->IncState(); + } - CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return false; + CBotStack* pile2 = pile->AddStack(); + if ( pile2->IfStep() ) return false; - CBotVar* var = pile->GivVar(); // rcupre le rsultat sur la pile + CBotVar* var = pile->GivVar(); // rcupre le rsultat sur la pile - switch (GivTokenType()) - { - case ID_ADD: - break; // ne fait donc rien - case ID_SUB: - var->Neg(); // change le signe - break; - case ID_NOT: - case ID_LOG_NOT: - case ID_TXT_NOT: - var->Not(); - break; - } - return pj->Return(pile); // transmet en dessous + switch (GivTokenType()) + { + case ID_ADD: + break; // ne fait donc rien + case ID_SUB: + var->Neg(); // change le signe + break; + case ID_NOT: + case ID_LOG_NOT: + case ID_TXT_NOT: + var->Not(); + break; + } + return pj->Return(pile); // transmet en dessous } void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL) return; - if ( pile->GivState() == 0 ) - { - m_Expr->RestoreState( pile, bMain ); // interrompu ici ! - return; - } + if ( pile->GivState() == 0 ) + { + m_Expr->RestoreState( pile, bMain ); // interrompu ici ! + return; + } } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2283,30 +2280,30 @@ void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) CBotIndexExpr::CBotIndexExpr() { - m_expr = NULL; - name = "CBotIndexExpr"; + m_expr = NULL; + name = "CBotIndexExpr"; } CBotIndexExpr::~CBotIndexExpr() { - delete m_expr; + delete m_expr; } // trouve un champ partir de l'instance la compilation bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - if ( pVar->GivType(1) != CBotTypArrayPointer ) - ASM_TRAP(); + if ( pVar->GivType(1) != CBotTypArrayPointer ) + ASM_TRAP(); - pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // la compilation rend l'lment [0] - if ( pVar == NULL ) - { - pile->SetError(TX_OUTARRAY, m_token.GivEnd()); - return false; - } - if ( m_next3 != NULL ) return m_next3->ExecuteVar(pVar, pile); - return true; + pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // la compilation rend l'lment [0] + if ( pVar == NULL ) + { + pile->SetError(TX_OUTARRAY, m_token.GivEnd()); + return false; + } + if ( m_next3 != NULL ) return m_next3->ExecuteVar(pVar, pile); + return true; } // idem l'excution @@ -2315,66 +2312,66 @@ bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { - CBotStack* pj = pile; -// DEBUG( "CBotIndexExpr::ExecuteVar", -1 , pj); + CBotStack* pj = pile; +// DEBUG( "CBotIndexExpr::ExecuteVar", -1 , pj); - if ( pVar->GivType(1) != CBotTypArrayPointer ) - ASM_TRAP(); + if ( pVar->GivType(1) != CBotTypArrayPointer ) + ASM_TRAP(); - pile = pile->AddStack(); -// if ( pile == EOX ) return true; + pile = pile->AddStack(); +// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) - { - if ( !m_expr->Execute(pile) ) return false; - pile->IncState(); - } - // traite les tableaux + if ( pile->GivState() == 0 ) + { + if ( !m_expr->Execute(pile) ) return false; + pile->IncState(); + } + // traite les tableaux - CBotVar* p = pile->GivVar(); // rsultat sur la pile + CBotVar* p = pile->GivVar(); // rsultat sur la pile - if ( p == NULL || p->GivType() > CBotTypDouble ) - { - pile->SetError(TX_BADINDEX, prevToken); - return pj->Return(pile); - } + if ( p == NULL || p->GivType() > CBotTypDouble ) + { + pile->SetError(TX_BADINDEX, prevToken); + return pj->Return(pile); + } - int n = p->GivValInt(); // position dans le tableau -// DEBUG( "CBotIndexExpr::ExecuteVar", n , pj); + int n = p->GivValInt(); // position dans le tableau +// DEBUG( "CBotIndexExpr::ExecuteVar", n , pj); - pVar = ((CBotVarArray*)pVar)->GivItem(n, bExtend); - if ( pVar == NULL ) - { - pile->SetError(TX_OUTARRAY, prevToken); - return pj->Return(pile); - } + pVar = ((CBotVarArray*)pVar)->GivItem(n, bExtend); + if ( pVar == NULL ) + { + pile->SetError(TX_OUTARRAY, prevToken); + return pj->Return(pile); + } -// DEBUG( "CBotIndexExpr::ExecuteVar", -2 , pj); - //if ( bUpdate ) - pVar->Maj(pile->GivPUser(), true); +// DEBUG( "CBotIndexExpr::ExecuteVar", -2 , pj); + //if ( bUpdate ) + pVar->Maj(pile->GivPUser(), true); -// DEBUG( "CBotIndexExpr::ExecuteVar", -3 , pj); - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; +// DEBUG( "CBotIndexExpr::ExecuteVar", -3 , pj); + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; -// DEBUG( "CBotIndexExpr::ExecuteVar", -4 , pj); - return true; // ne libre pas la pile - // pour viter de recalculer les index deux fois le cas chant +// DEBUG( "CBotIndexExpr::ExecuteVar", -4 , pj); + return true; // ne libre pas la pile + // pour viter de recalculer les index deux fois le cas chant } void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) { - pile = pile->RestoreStack(); - if ( pile == NULL ) return; + pile = pile->RestoreStack(); + if ( pile == NULL ) return; - if ( bMain && pile->GivState() == 0 ) - { - m_expr->RestoreState(pile, true); - return; - } + if ( bMain && pile->GivState() == 0 ) + { + m_expr->RestoreState(pile, true); + return; + } - if ( m_next3 ) - m_next3->RestoreStateVar(pile, bMain); + if ( m_next3 ) + m_next3->RestoreStateVar(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2386,8 +2383,8 @@ void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) CBotFieldExpr::CBotFieldExpr() { - name = "CBotFieldExpr"; - m_nIdent = 0; + name = "CBotFieldExpr"; + m_nIdent = 0; } CBotFieldExpr::~CBotFieldExpr() @@ -2396,7 +2393,7 @@ CBotFieldExpr::~CBotFieldExpr() void CBotFieldExpr::SetUniqNum(int num) { - m_nIdent = num; + m_nIdent = num; } @@ -2404,84 +2401,84 @@ void CBotFieldExpr::SetUniqNum(int num) bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - if ( pVar->GivType(1) != CBotTypPointer ) - ASM_TRAP(); + if ( pVar->GivType(1) != CBotTypPointer ) + ASM_TRAP(); -// pVar = pVar->GivItem(m_token.GivString()); - pVar = pVar->GivItemRef(m_nIdent); - if ( pVar == NULL ) - { - pile->SetError(TX_NOITEM, &m_token); - return false; - } +// pVar = pVar->GivItem(m_token.GivString()); + pVar = pVar->GivItemRef(m_nIdent); + if ( pVar == NULL ) + { + pile->SetError(TX_NOITEM, &m_token); + return false; + } - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile) ) return false; + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile) ) return false; - return true; + return true; } // idem l'excution bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { - CBotStack* pj = pile; - pile = pile->AddStack(this); // modifie pile en sortie - if ( pile == EOX ) return true; + CBotStack* pj = pile; + pile = pile->AddStack(this); // modifie pile en sortie + if ( pile == EOX ) return true; -// DEBUG( "CBotFieldExpre::ExecuteVar "+m_token.GivString(), 0, pj ); +// DEBUG( "CBotFieldExpre::ExecuteVar "+m_token.GivString(), 0, pj ); - if ( pVar->GivType(1) != CBotTypPointer ) - ASM_TRAP(); + if ( pVar->GivType(1) != CBotTypPointer ) + ASM_TRAP(); - CBotVarClass* pItem = pVar->GivPointer(); - if ( pItem == NULL ) - { - pile->SetError(TX_NULLPT, prevToken); - return pj->Return( pile ); - } - if ( pItem->GivUserPtr() == OBJECTDELETED ) - { - pile->SetError(TX_DELETEDPT, prevToken); - return pj->Return( pile ); - } + CBotVarClass* pItem = pVar->GivPointer(); + if ( pItem == NULL ) + { + pile->SetError(TX_NULLPT, prevToken); + return pj->Return( pile ); + } + if ( pItem->GivUserPtr() == OBJECTDELETED ) + { + pile->SetError(TX_DELETEDPT, prevToken); + return pj->Return( pile ); + } - if ( bStep && pile->IfStep() ) return false; + if ( bStep && pile->IfStep() ) return false; -// pVar = pVar->GivItem(m_token.GivString()); - pVar = pVar->GivItemRef(m_nIdent); - if ( pVar == NULL ) - { - pile->SetError(TX_NOITEM, &m_token); - return pj->Return( pile ); - } +// pVar = pVar->GivItem(m_token.GivString()); + pVar = pVar->GivItemRef(m_nIdent); + if ( pVar == NULL ) + { + pile->SetError(TX_NOITEM, &m_token); + return pj->Return( pile ); + } - if ( pVar->IsStatic() ) - { -// DEBUG( "IsStatic", 0, pj) ; - // pour une variable statique, la prend dans la classe elle-mme - CBotClass* pClass = pItem->GivClass(); - pVar = pClass->GivItem(m_token.GivString()); -// DEBUG( "done "+pVar->GivName(), 0, pj) ; - } + if ( pVar->IsStatic() ) + { +// DEBUG( "IsStatic", 0, pj) ; + // pour une variable statique, la prend dans la classe elle-mme + CBotClass* pClass = pItem->GivClass(); + pVar = pClass->GivItem(m_token.GivString()); +// DEBUG( "done "+pVar->GivName(), 0, pj) ; + } - // demande la mise jour de l'lment, s'il y a lieu - pVar->Maj(pile->GivPUser(), true); + // demande la mise jour de l'lment, s'il y a lieu + pVar->Maj(pile->GivPUser(), true); - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; - return true; // ne libre pas la pile - // pour conserver l'tat SetState() correspondant l'tape + return true; // ne libre pas la pile + // pour conserver l'tat SetState() correspondant l'tape } void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) { - pj = pj->RestoreStack(this); // modifie pj en sortie - if ( pj == NULL ) return; + pj = pj->RestoreStack(this); // modifie pj en sortie + if ( pj == NULL ) return; - if ( m_next3 != NULL ) - m_next3->RestoreStateVar(pj, bMain); + if ( m_next3 != NULL ) + m_next3->RestoreStateVar(pj, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2491,8 +2488,8 @@ void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) CBotLeftExpr::CBotLeftExpr() { - name = "CBotLeftExpr"; - m_nIdent = 0; + name = "CBotLeftExpr"; + m_nIdent = 0; } CBotLeftExpr::~CBotLeftExpr() @@ -2584,135 +2581,135 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) { CBotToken* pp = p; - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( pp ); // garde le nom du token - inst->AddNext3(i); // ajoute la suite - - if ( p->GivType() == TokenTypVar ) // doit tre un nom - { - var = var->GivItem(p->GivString()); // rcupre l'item correpondant - if ( var != NULL ) - { - if ( var->IsPrivate(PR_READ) && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError( TX_PRIVATE, pp ); - goto err; - } - - i->SetUniqNum(var->GivUniqNum()); - p = p->GivNext(); // saute le nom - continue; - } - pStk->SetError( TX_NOITEM, p ); - } - pStk->SetError( TX_DOT, p->GivStart() ); - goto err; - } - } - break; - } - - - if ( pStk->IsOk() ) return (CBotLeftExpr*) pStack->Return(inst, pStk); - } - pStk->SetError(TX_UNDEFVAR, p); + CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment + i->SetToken( pp ); // garde le nom du token + inst->AddNext3(i); // ajoute la suite + + if ( p->GivType() == TokenTypVar ) // doit tre un nom + { + var = var->GivItem(p->GivString()); // rcupre l'item correpondant + if ( var != NULL ) + { + if ( var->IsPrivate(PR_READ) && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError( TX_PRIVATE, pp ); + goto err; + } + + i->SetUniqNum(var->GivUniqNum()); + p = p->GivNext(); // saute le nom + continue; + } + pStk->SetError( TX_NOITEM, p ); + } + pStk->SetError( TX_DOT, p->GivStart() ); + goto err; + } + } + break; + } + + + if ( pStk->IsOk() ) return (CBotLeftExpr*) pStack->Return(inst, pStk); + } + pStk->SetError(TX_UNDEFVAR, p); err: - delete inst; - return (CBotLeftExpr*) pStack->Return(NULL, pStk); - } + delete inst; + return (CBotLeftExpr*) pStack->Return(NULL, pStk); + } - return (CBotLeftExpr*) pStack->Return(NULL, pStk); + return (CBotLeftExpr*) pStack->Return(NULL, pStk); } // excute, trouve une variable et lui assigne le rsultat de la pile bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) { - CBotStack* pile = pj->AddStack(); -// if ( pile == EOX ) return true; - -// if ( pile->IfStep() ) return false; - - CBotVar* var1 = NULL; - CBotVar* var2 = NULL; - -// var1 = pile->FindVar(m_token, false, true); - if (!ExecuteVar( var1, array, NULL, false )) return false; - // retrouve la variable (et pas la copie) - if (pile->IfStep()) return false; - - if ( var1 ) - { - var2 = pj->GivVar(); // resultat sur la pile d'entre - if ( var2 ) - { - CBotTypResult t1 = var1->GivTypResult(); - CBotTypResult t2 = var2->GivTypResult(); - if ( t2.Eq(CBotTypPointer) ) - { - CBotClass* c1 = t1.GivClass(); - CBotClass* c2 = t2.GivClass(); - if ( !c2->IsChildOf(c1)) - { - CBotToken* pt = &m_token; - pile->SetError(TX_BADTYPE, pt); - return pj->Return(pile); // opration faite - } - } - var1->SetVal(var2); // fait l'assignation - } - pile->SetCopyVar( var1 ); // remplace sur la pile par une copie de la variable elle-mme - // (pour avoir le nom) - } - - return pj->Return(pile); // opration faite + CBotStack* pile = pj->AddStack(); +// if ( pile == EOX ) return true; + +// if ( pile->IfStep() ) return false; + + CBotVar* var1 = NULL; + CBotVar* var2 = NULL; + +// var1 = pile->FindVar(m_token, false, true); + if (!ExecuteVar( var1, array, NULL, false )) return false; + // retrouve la variable (et pas la copie) + if (pile->IfStep()) return false; + + if ( var1 ) + { + var2 = pj->GivVar(); // resultat sur la pile d'entre + if ( var2 ) + { + CBotTypResult t1 = var1->GivTypResult(); + CBotTypResult t2 = var2->GivTypResult(); + if ( t2.Eq(CBotTypPointer) ) + { + CBotClass* c1 = t1.GivClass(); + CBotClass* c2 = t2.GivClass(); + if ( !c2->IsChildOf(c1)) + { + CBotToken* pt = &m_token; + pile->SetError(TX_BADTYPE, pt); + return pj->Return(pile); // opration faite + } + } + var1->SetVal(var2); // fait l'assignation + } + pile->SetCopyVar( var1 ); // remplace sur la pile par une copie de la variable elle-mme + // (pour avoir le nom) + } + + return pj->Return(pile); // opration faite } // retrouve une variable pendant la compilation bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - pVar = pile->FindVar(m_token); - if ( pVar == NULL ) return false; + pVar = pile->FindVar(m_token); + if ( pVar == NULL ) return false; - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile) ) return false; + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile) ) return false; - return true; + return true; } // retrouve une variable l'excution bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) { - pile = pile->AddStack( this ); // dplace la pile + pile = pile->AddStack( this ); // dplace la pile - pVar = pile->FindVar(m_nIdent); - if ( pVar == NULL ) - { -#ifdef _DEBUG - ASM_TRAP(); + pVar = pile->FindVar(m_nIdent); + if ( pVar == NULL ) + { +#ifdef _DEBUG + ASM_TRAP(); #endif - pile->SetError(2, &m_token); - return false; - } + pile->SetError(2, &m_token); + return false; + } - if ( bStep && m_next3 == NULL && pile->IfStep() ) return false; + if ( bStep && m_next3 == NULL && pile->IfStep() ) return false; - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; - return true; + return true; } void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) { - pile = pile->RestoreStack( this ); // dplace la pile - if ( pile == NULL ) return; + pile = pile->RestoreStack( this ); // dplace la pile + if ( pile == NULL ) return; - if ( m_next3 != NULL ) - m_next3->RestoreStateVar(pile, bMain); + if ( m_next3 != NULL ) + m_next3->RestoreStateVar(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2722,96 +2719,96 @@ void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) long GivNumInt( const char* p ) { - long num = 0; - while (*p >= '0' && *p <= '9') - { - num = num * 10 + *p - '0'; - p++; - } - if ( *p == 'x' || *p == 'X' ) - { - while (*++p != 0) - { - if ( *p >= '0' && *p <= '9' ) - { - num = num * 16 + *p - '0'; - continue; - } - if ( *p >= 'A' && *p <= 'F' ) - { - num = num * 16 + *p - 'A' + 10; - continue; - } - if ( *p >= 'a' && *p <= 'f' ) - { - num = num * 16 + *p - 'a' + 10; - continue; - } - break; - } - } - return num; + long num = 0; + while (*p >= '0' && *p <= '9') + { + num = num * 10 + *p - '0'; + p++; + } + if ( *p == 'x' || *p == 'X' ) + { + while (*++p != 0) + { + if ( *p >= '0' && *p <= '9' ) + { + num = num * 16 + *p - '0'; + continue; + } + if ( *p >= 'A' && *p <= 'F' ) + { + num = num * 16 + *p - 'A' + 10; + continue; + } + if ( *p >= 'a' && *p <= 'f' ) + { + num = num * 16 + *p - 'a' + 10; + continue; + } + break; + } + } + return num; } // transforme une chane en un nombre rel extern float GivNumFloat( const char* p ) { - double num = 0; - double div = 10; - bool bNeg = false; - - if (*p == '-') - { - bNeg = true; - p++; - } - while (*p >= '0' && *p <= '9') - { - num = num * 10. + (*p - '0'); - p++; - } - - if ( *p == '.' ) - { - p++; - while (*p >= '0' && *p <= '9') - { - num = num + (*p - '0') / div; - div = div * 10; - p++; - } - } - - int exp = 0; - if ( *p == 'e' || *p == 'E' ) - { - char neg = 0; - p++; - if ( *p == '-' || *p == '+' ) neg = *p++; - - while (*p >= '0' && *p <= '9') - { - exp = exp * 10 + (*p - '0'); - p++; - } - if ( neg == '-' ) exp = -exp; - } - - while ( exp > 0 ) - { - num *= 10.0; - exp--; - } - - while ( exp < 0 ) - { - num /= 10.0; - exp++; - } - - if ( bNeg ) num = -num; - return (float)num; + double num = 0; + double div = 10; + bool bNeg = false; + + if (*p == '-') + { + bNeg = true; + p++; + } + while (*p >= '0' && *p <= '9') + { + num = num * 10. + (*p - '0'); + p++; + } + + if ( *p == '.' ) + { + p++; + while (*p >= '0' && *p <= '9') + { + num = num + (*p - '0') / div; + div = div * 10; + p++; + } + } + + int exp = 0; + if ( *p == 'e' || *p == 'E' ) + { + char neg = 0; + p++; + if ( *p == '-' || *p == '+' ) neg = *p++; + + while (*p >= '0' && *p <= '9') + { + exp = exp * 10 + (*p - '0'); + p++; + } + if ( neg == '-' ) exp = -exp; + } + + while ( exp > 0 ) + { + num *= 10.0; + exp--; + } + + while ( exp < 0 ) + { + num /= 10.0; + exp++; + } + + if ( bNeg ) num = -num; + return (float)num; } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2822,7 +2819,7 @@ extern float GivNumFloat( const char* p ) CBotExprNum::CBotExprNum() { - name = "CBotExprNum"; + name = "CBotExprNum"; } CBotExprNum::~CBotExprNum() @@ -2831,77 +2828,77 @@ CBotExprNum::~CBotExprNum() CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCStack* pStk = pStack->TokenStack(); - - CBotExprNum* inst = new CBotExprNum(); - - inst->SetToken(p); - CBotString s = p->GivString(); - - inst->m_numtype = CBotTypInt; - if ( p->GivType() == TokenTypDef ) - { - inst->m_valint = p->GivIdKey(); - } - else - { - if ( s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) ) ) - { - inst->m_numtype = CBotTypFloat; - inst->m_valfloat = GivNumFloat(s); - } - else - { - inst->m_valint = GivNumInt(s); - } - } - - if (pStk->NextToken(p)) - { - CBotVar* var = CBotVar::Create((CBotToken*)NULL, inst->m_numtype); - pStk->SetVar(var); - - return pStack->Return(inst, pStk); - } - delete inst; - return pStack->Return(NULL, pStk); + CBotCStack* pStk = pStack->TokenStack(); + + CBotExprNum* inst = new CBotExprNum(); + + inst->SetToken(p); + CBotString s = p->GivString(); + + inst->m_numtype = CBotTypInt; + if ( p->GivType() == TokenTypDef ) + { + inst->m_valint = p->GivIdKey(); + } + else + { + if ( s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) ) ) + { + inst->m_numtype = CBotTypFloat; + inst->m_valfloat = GivNumFloat(s); + } + else + { + inst->m_valint = GivNumInt(s); + } + } + + if (pStk->NextToken(p)) + { + CBotVar* var = CBotVar::Create((CBotToken*)NULL, inst->m_numtype); + pStk->SetVar(var); + + return pStack->Return(inst, pStk); + } + delete inst; + return pStack->Return(NULL, pStk); } // excute, retourne le nombre correspondant bool CBotExprNum::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, m_numtype); + CBotVar* var = CBotVar::Create((CBotToken*)NULL, m_numtype); - CBotString nombre ; - if ( m_token.GivType() == TokenTypDef ) - { - nombre = m_token.GivString(); - } + CBotString nombre ; + if ( m_token.GivType() == TokenTypDef ) + { + nombre = m_token.GivString(); + } - switch (m_numtype) - { - case CBotTypShort: - case CBotTypInt: - var->SetValInt( m_valint, nombre ); // valeur du nombre - break; - case CBotTypFloat: - var->SetValFloat( m_valfloat ); // valeur du nombre - break; - } - pile->SetVar( var ); // mis sur la pile + switch (m_numtype) + { + case CBotTypShort: + case CBotTypInt: + var->SetValInt( m_valint, nombre ); // valeur du nombre + break; + case CBotTypFloat: + var->SetValFloat( m_valfloat ); // valeur du nombre + break; + } + pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // c'est ok + return pj->Return(pile); // c'est ok } void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2912,7 +2909,7 @@ void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) CBotExprAlpha::CBotExprAlpha() { - name = "CBotExprAlpha"; + name = "CBotExprAlpha"; } CBotExprAlpha::~CBotExprAlpha() @@ -2921,43 +2918,43 @@ CBotExprAlpha::~CBotExprAlpha() CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCStack* pStk = pStack->TokenStack(); + CBotCStack* pStk = pStack->TokenStack(); - CBotExprAlpha* inst = new CBotExprAlpha(); + CBotExprAlpha* inst = new CBotExprAlpha(); - inst->SetToken(p); - p = p->GivNext(); + inst->SetToken(p); + p = p->GivNext(); - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); - pStk->SetVar(var); + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); + pStk->SetVar(var); - return pStack->Return(inst, pStk); + return pStack->Return(inst, pStk); } // excute, retourne la chane correspondante bool CBotExprAlpha::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); - CBotString chaine = m_token.GivString(); - chaine = chaine.Mid(1, chaine.GivLength()-2); // enlve les guillemets + CBotString chaine = m_token.GivString(); + chaine = chaine.Mid(1, chaine.GivLength()-2); // enlve les guillemets - var->SetValString( chaine ); // valeur du nombre + var->SetValString( chaine ); // valeur du nombre - pile->SetVar( var ); // mis sur la pile + pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); + return pj->Return(pile); } void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -2968,7 +2965,7 @@ void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) CBotExprBool::CBotExprBool() { - name = "CBotExprBool"; + name = "CBotExprBool"; } CBotExprBool::~CBotExprBool() @@ -2977,44 +2974,44 @@ CBotExprBool::~CBotExprBool() CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCStack* pStk = pStack->TokenStack(); - CBotExprBool* inst = NULL; + CBotCStack* pStk = pStack->TokenStack(); + CBotExprBool* inst = NULL; - if ( p->GivType() == ID_TRUE || - p->GivType() == ID_FALSE ) - { - inst = new CBotExprBool(); - inst->SetToken(p); // mmorise l'opration false ou true - p = p->GivNext(); + if ( p->GivType() == ID_TRUE || + p->GivType() == ID_FALSE ) + { + inst = new CBotExprBool(); + inst->SetToken(p); // mmorise l'opration false ou true + p = p->GivNext(); - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - pStk->SetVar(var); - } + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + pStk->SetVar(var); + } - return pStack->Return(inst, pStk); + return pStack->Return(inst, pStk); } // excute, retourne true ou false bool CBotExprBool::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - if (GivTokenType() == ID_TRUE) var->SetValInt(1); - else var->SetValInt(0); + if (GivTokenType() == ID_TRUE) var->SetValInt(1); + else var->SetValInt(0); - pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // transmet en dessous + pile->SetVar( var ); // mis sur la pile + return pj->Return(pile); // transmet en dessous } void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -3023,7 +3020,7 @@ void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) CBotExprNull::CBotExprNull() { - name = "CBotExprNull"; + name = "CBotExprNull"; } CBotExprNull::~CBotExprNull() @@ -3034,20 +3031,20 @@ CBotExprNull::~CBotExprNull() bool CBotExprNull::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypNullPointer); + if ( pile->IfStep() ) return false; + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypNullPointer); - var->SetInit(true); // pointeur null valide - pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // transmet en dessous + var->SetInit(true); // pointeur null valide + pile->SetVar( var ); // mis sur la pile + return pj->Return(pile); // transmet en dessous } void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -3056,7 +3053,7 @@ void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) CBotExprNan::CBotExprNan() { - name = "CBotExprNan"; + name = "CBotExprNan"; } CBotExprNan::~CBotExprNan() @@ -3067,20 +3064,20 @@ CBotExprNan::~CBotExprNan() bool CBotExprNan::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypInt); + if ( pile->IfStep() ) return false; + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypInt); - var->SetInit(IS_NAN); // nombre nan - pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // transmet en dessous + var->SetInit(IS_NAN); // nombre nan + pile->SetVar( var ); // mis sur la pile + return pj->Return(pile); // transmet en dessous } void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////// @@ -3090,8 +3087,8 @@ void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) CBotExprVar::CBotExprVar() { - name = "CBotExprVar"; - m_nIdent = 0; + name = "CBotExprVar"; + m_nIdent = 0; } CBotExprVar::~CBotExprVar() @@ -3131,7 +3128,7 @@ CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) // This is an element of the current class // ads the equivalent of this. before /// \TODO need to be fixed revised and fixed after adding unit - //tests + ///tests CBotToken token("this"); inst->SetToken(&token); ((CBotExprVar*)inst)->m_nIdent = -2; // identificator for this @@ -3234,44 +3231,44 @@ err: CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(pp->GivStart()); - - // est-ce un nom de variable ? - if (pp->GivType() == TokenTypVar) - { - CBotToken pthis("this"); - CBotVar* var = pStk->FindVar(pthis); - if ( var == 0 ) return pStack->Return(NULL, pStk); - - CBotInstr* inst = new CBotExprVar(); // cre l'objet - - // il s'agit d'un lement de la classe courante - // ajoute l'quivalent d'un this. devant - inst->SetToken(&pthis); - ((CBotExprVar*)inst)->m_nIdent = -2; // ident pour this - - CBotToken* pp = p; - - if ( pp->GivType() == TokenTypVar ) // doit tre un nom - { - if ( pp->GivNext()->GivType() == ID_OPENPAR ) // un appel de mthode ? - { - CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); - if ( pStk->IsOk() ) - { - inst->AddNext3(i); // ajoute la suite - p = pp; // instructions passes - return pStack->Return(inst, pStk); - } - pStk->SetError(0,0); // l'erreur n'est pas traite ici - } - } - delete inst; - } - return pStack->Return(NULL, pStk); + CBotToken* pp = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(pp->GivStart()); + + // est-ce un nom de variable ? + if (pp->GivType() == TokenTypVar) + { + CBotToken pthis("this"); + CBotVar* var = pStk->FindVar(pthis); + if ( var == 0 ) return pStack->Return(NULL, pStk); + + CBotInstr* inst = new CBotExprVar(); // cre l'objet + + // il s'agit d'un lement de la classe courante + // ajoute l'quivalent d'un this. devant + inst->SetToken(&pthis); + ((CBotExprVar*)inst)->m_nIdent = -2; // ident pour this + + CBotToken* pp = p; + + if ( pp->GivType() == TokenTypVar ) // doit tre un nom + { + if ( pp->GivNext()->GivType() == ID_OPENPAR ) // un appel de mthode ? + { + CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); + if ( pStk->IsOk() ) + { + inst->AddNext3(i); // ajoute la suite + p = pp; // instructions passes + return pStack->Return(inst, pStk); + } + pStk->SetError(0,0); // l'erreur n'est pas traite ici + } + } + delete inst; + } + return pStack->Return(NULL, pStk); } @@ -3279,85 +3276,85 @@ CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) bool CBotExprVar::Execute(CBotStack* &pj) { - CBotVar* pVar = NULL; - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotVar* pVar = NULL; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; -// if ( pile->IfStep() ) return false; +// if ( pile->IfStep() ) return false; - CBotStack* pile1 = pile; + CBotStack* pile1 = pile; - if ( pile1->GivState() == 0 ) - { - if ( !ExecuteVar(pVar, pile, NULL, true) ) return false; // rcupre la variable selon champs et index -// DEBUG("CBotExprVar::Execute", 1 , pj); + if ( pile1->GivState() == 0 ) + { + if ( !ExecuteVar(pVar, pile, NULL, true) ) return false; // rcupre la variable selon champs et index +// DEBUG("CBotExprVar::Execute", 1 , pj); - if ( pVar ) pile1->SetCopyVar(pVar); // la place une copie sur la pile - else - { -//-- pile1->SetVar(NULL); // la pile contient dj le resultat (mthode) - return pj->Return(pile1); - } - pile1->IncState(); - } + if ( pVar ) pile1->SetCopyVar(pVar); // la place une copie sur la pile + else + { +//-- pile1->SetVar(NULL); // la pile contient dj le resultat (mthode) + return pj->Return(pile1); + } + pile1->IncState(); + } - pVar = pile1->GivVar(); // rcupre si interrompu + pVar = pile1->GivVar(); // rcupre si interrompu - if ( pVar == NULL ) - { -// pile1->SetError(TX_NULLPT, &m_token); - return pj->Return(pile1); - } + if ( pVar == NULL ) + { +// pile1->SetError(TX_NULLPT, &m_token); + return pj->Return(pile1); + } - if ( pVar->GivInit() == IS_UNDEF ) - { - CBotToken* pt = &m_token; - while ( pt->GivNext() != NULL ) pt = pt->GivNext(); - pile1->SetError(TX_NOTINIT, pt); - return pj->Return(pile1); - } - return pj->Return(pile1); // opration faite + if ( pVar->GivInit() == IS_UNDEF ) + { + CBotToken* pt = &m_token; + while ( pt->GivNext() != NULL ) pt = pt->GivNext(); + pile1->SetError(TX_NOTINIT, pt); + return pj->Return(pile1); + } + return pj->Return(pile1); // opration faite } void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - CBotStack* pile1 = pile; + CBotStack* pile1 = pile; - if ( pile1->GivState() == 0 ) - { - RestoreStateVar(pile, bMain); // rcupre la variable selon champs et index - return; - } + if ( pile1->GivState() == 0 ) + { + RestoreStateVar(pile, bMain); // rcupre la variable selon champs et index + return; + } } // retrouve une variable l'excution bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) { - CBotStack* pile = pj; - pj = pj->AddStack( this ); + CBotStack* pile = pj; + pj = pj->AddStack( this ); - if ( bStep && m_nIdent>0 && pj->IfStep() ) return false; + if ( bStep && m_nIdent>0 && pj->IfStep() ) return false; - pVar = pj->FindVar(m_nIdent, true); // cherche la variable avec mise jour si ncessaire - if ( pVar == NULL ) - { -#ifdef _DEBUG - ASM_TRAP(); + pVar = pj->FindVar(m_nIdent, true); // cherche la variable avec mise jour si ncessaire + if ( pVar == NULL ) + { +#ifdef _DEBUG + ASM_TRAP(); #endif - pj->SetError(1, &m_token); - return false; - } - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) - return false; // Champs d'une instance, tableau, mthode ? + pj->SetError(1, &m_token); + return false; + } + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) + return false; // Champs d'une instance, tableau, mthode ? - return pile->ReturnKeep( pj ); // ne rend pas la pile mais rcupre le rsultat si une mthode a t appele + return pile->ReturnKeep( pj ); // ne rend pas la pile mais rcupre le rsultat si une mthode a t appele } @@ -3365,11 +3362,11 @@ bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToke void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) { - pj = pj->RestoreStack( this ); - if ( pj == NULL ) return; + pj = pj->RestoreStack( this ); + if ( pj == NULL ) return; - if ( m_next3 != NULL ) - m_next3->RestoreStateVar(pj, bMain); + if ( m_next3 != NULL ) + m_next3->RestoreStateVar(pj, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -3378,58 +3375,58 @@ void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) { - bool first = true; - CBotInstr* ret = NULL; // pour la liste retourner - -// pStack->SetStartError(p->GivStart()); - CBotCStack* pile = pStack; - int i = 0; - - if ( IsOfType(p, ID_OPENPAR) ) - { - int start, end; - if (!IsOfType(p, ID_CLOSEPAR)) while (true) - { - start = p->GivStart(); - pile = pile->TokenStack(); // garde les rsultats sur la pile - - if ( first ) pStack->SetStartError(start); - first = false; - - CBotInstr* param = CBotExpression::Compile(p, pile); - end = p->GivStart(); - - if ( !pile->IsOk() ) - { - return pStack->Return(NULL, pile); - } - - if ( ret == NULL ) ret = param; - else ret->AddNext(param); // construit la liste - - if ( param != NULL ) - { - if ( pile->GivTypResult().Eq(99) ) - { - delete pStack->TokenStack(); - pStack->SetError(TX_VOID, p->GivStart()); - return NULL; - } - ppVars[i] = pile->GivVar(); - ppVars[i]->GivToken()->SetPos(start, end); - i++; - - if (IsOfType(p, ID_COMMA)) continue; // saute la virgule - if (IsOfType(p, ID_CLOSEPAR)) break; - } - - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - delete pStack->TokenStack(); - return NULL; - } - } - ppVars[i] = NULL; - return ret; + bool first = true; + CBotInstr* ret = NULL; // pour la liste retourner + +// pStack->SetStartError(p->GivStart()); + CBotCStack* pile = pStack; + int i = 0; + + if ( IsOfType(p, ID_OPENPAR) ) + { + int start, end; + if (!IsOfType(p, ID_CLOSEPAR)) while (true) + { + start = p->GivStart(); + pile = pile->TokenStack(); // garde les rsultats sur la pile + + if ( first ) pStack->SetStartError(start); + first = false; + + CBotInstr* param = CBotExpression::Compile(p, pile); + end = p->GivStart(); + + if ( !pile->IsOk() ) + { + return pStack->Return(NULL, pile); + } + + if ( ret == NULL ) ret = param; + else ret->AddNext(param); // construit la liste + + if ( param != NULL ) + { + if ( pile->GivTypResult().Eq(99) ) + { + delete pStack->TokenStack(); + pStack->SetError(TX_VOID, p->GivStart()); + return NULL; + } + ppVars[i] = pile->GivVar(); + ppVars[i]->GivToken()->SetPos(start, end); + i++; + + if (IsOfType(p, ID_COMMA)) continue; // saute la virgule + if (IsOfType(p, ID_CLOSEPAR)) break; + } + + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + delete pStack->TokenStack(); + return NULL; + } + } + ppVars[i] = NULL; + return ret; } ////////////////////////////////////////////////////////////////////////////////////////// @@ -3440,255 +3437,255 @@ CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) CBotInstrMethode::CBotInstrMethode() { - m_Parameters = NULL; - m_MethodeIdent = 0; -// m_nThisIdent = 0; - name = "CBotInstrMethode"; + m_Parameters = NULL; + m_MethodeIdent = 0; +// m_nThisIdent = 0; + name = "CBotInstrMethode"; } CBotInstrMethode::~CBotInstrMethode() { - delete m_Parameters; + delete m_Parameters; } CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* var) { - CBotInstrMethode* inst = new CBotInstrMethode(); - inst->SetToken(p); // token correspondant - -// inst->m_nThisIdent = CBotVar::NextUniqNum(); - - if ( NULL != var ) - { - CBotToken* pp = p; - p = p->GivNext(); - - if ( p->GivType() == ID_OPENPAR ) - { - inst->m_NomMethod = pp->GivString(); - - // compile la liste des paramtres - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStack, ppVars); - - if ( pStack->IsOk() ) - { - CBotClass* pClass = var->GivClass(); // pointeur la classe - inst->m_ClassName = pClass->GivName(); // le nom de la classe - CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, - pStack, inst->m_MethodeIdent); - delete pStack->TokenStack(); // libres les paramtres encore sur la pile - inst->m_typRes = r; - - if (inst->m_typRes.GivType() > 20) - { - pStack->SetError(inst->m_typRes.GivType(), pp); - delete inst; - return NULL; - } - // met un rsultat sur la pile pour avoir quelque chose - if (inst->m_typRes.GivType() > 0) - { - CBotVar* pResult = CBotVar::Create("", inst->m_typRes); - if (inst->m_typRes.Eq(CBotTypClass)) - { -// CBotClass* pClass = CBotClass::Find(inst->m_RetClassName); - pResult->SetClass(inst->m_typRes.GivClass()); - } - pStack->SetVar(pResult); - } - return inst; - } - delete inst; - return NULL; - } - } - pStack->SetError( 1234, p ); - delete inst; - return NULL; + CBotInstrMethode* inst = new CBotInstrMethode(); + inst->SetToken(p); // token correspondant + +// inst->m_nThisIdent = CBotVar::NextUniqNum(); + + if ( NULL != var ) + { + CBotToken* pp = p; + p = p->GivNext(); + + if ( p->GivType() == ID_OPENPAR ) + { + inst->m_NomMethod = pp->GivString(); + + // compile la liste des paramtres + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStack, ppVars); + + if ( pStack->IsOk() ) + { + CBotClass* pClass = var->GivClass(); // pointeur la classe + inst->m_ClassName = pClass->GivName(); // le nom de la classe + CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, + pStack, inst->m_MethodeIdent); + delete pStack->TokenStack(); // libres les paramtres encore sur la pile + inst->m_typRes = r; + + if (inst->m_typRes.GivType() > 20) + { + pStack->SetError(inst->m_typRes.GivType(), pp); + delete inst; + return NULL; + } + // met un rsultat sur la pile pour avoir quelque chose + if (inst->m_typRes.GivType() > 0) + { + CBotVar* pResult = CBotVar::Create("", inst->m_typRes); + if (inst->m_typRes.Eq(CBotTypClass)) + { +// CBotClass* pClass = CBotClass::Find(inst->m_RetClassName); + pResult->SetClass(inst->m_typRes.GivClass()); + } + pStack->SetVar(pResult); + } + return inst; + } + delete inst; + return NULL; + } + } + pStack->SetError( 1234, p ); + delete inst; + return NULL; } // excute l'appel de mthode bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) { - CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This -// if ( pile1 == EOX ) return true; - -// DEBUG( "CBotInstrMethode::ExecuteVar", 0, pj ); - - if ( pVar->GivPointer() == NULL ) - { - pj->SetError( TX_NULLPT, prevToken ); - } - - if ( pile1->IfStep() ) return false; - - CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir - - if ( pile1->GivState() == 0) - { - CBotVar* pThis = CBotVar::Create(pVar); - pThis->Copy(pVar); - // la valeur de This doit tre prise avant l'valuation des paramtres - // Test.Action( Test = Autre ); - // Action doit agir sur la valeur avant Test = Autre !! - pThis->SetName("this"); -// pThis->SetUniqNum(m_nThisIdent); - pThis->SetUniqNum(-2); - pile1->AddVar(pThis); - pile1->IncState(); - } - int i = 0; - - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - if ( pile2->GivState() == 0 ) - { - if (!p->Execute(pile2)) return false; // interrompu ici ? - if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres - } - ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pThis = pile1->FindVar(-2); - CBotVar* pResult = NULL; - if (m_typRes.GivType() > 0) pResult = CBotVar::Create("", m_typRes); - if (m_typRes.Eq(CBotTypClass)) - { -// CBotClass* pClass = CBotClass::Find(m_RetClassName); - pResult->SetClass(m_typRes.GivClass()); - } - CBotVar* pRes = pResult; - - if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu - if (pRes != pResult) delete pRes; - - pVar = NULL; // ne retourne pas une valeur par cela - return pj->Return(pile2); // libre toute la pile + CBotVar* ppVars[1000]; + CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This +// if ( pile1 == EOX ) return true; + +// DEBUG( "CBotInstrMethode::ExecuteVar", 0, pj ); + + if ( pVar->GivPointer() == NULL ) + { + pj->SetError( TX_NULLPT, prevToken ); + } + + if ( pile1->IfStep() ) return false; + + CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir + + if ( pile1->GivState() == 0) + { + CBotVar* pThis = CBotVar::Create(pVar); + pThis->Copy(pVar); + // la valeur de This doit tre prise avant l'valuation des paramtres + // Test.Action( Test = Autre ); + // Action doit agir sur la valeur avant Test = Autre !! + pThis->SetName("this"); +// pThis->SetUniqNum(m_nThisIdent); + pThis->SetUniqNum(-2); + pile1->AddVar(pThis); + pile1->IncState(); + } + int i = 0; + + CBotInstr* p = m_Parameters; + // value les paramtres + // et place les valeurs sur la pile + // pour pouvoir tre interrompu n'importe quand + if ( p != NULL) while ( true ) + { + if ( pile2->GivState() == 0 ) + { + if (!p->Execute(pile2)) return false; // interrompu ici ? + if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres + } + ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs + pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pThis = pile1->FindVar(-2); + CBotVar* pResult = NULL; + if (m_typRes.GivType() > 0) pResult = CBotVar::Create("", m_typRes); + if (m_typRes.Eq(CBotTypClass)) + { +// CBotClass* pClass = CBotClass::Find(m_RetClassName); + pResult->SetClass(m_typRes.GivClass()); + } + CBotVar* pRes = pResult; + + if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interrompu + if (pRes != pResult) delete pRes; + + pVar = NULL; // ne retourne pas une valeur par cela + return pj->Return(pile2); // libre toute la pile } void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotVar* ppVars[1000]; - CBotStack* pile1 = pile->RestoreStack(this); // une place pour la copie de This - if ( pile1 == NULL ) return; + CBotVar* ppVars[1000]; + CBotStack* pile1 = pile->RestoreStack(this); // une place pour la copie de This + if ( pile1 == NULL ) return; - CBotStack* pile2 = pile1->RestoreStack(); // et pour les paramtres venir - if ( pile2 == NULL ) return; + CBotStack* pile2 = pile1->RestoreStack(); // et pour les paramtres venir + if ( pile2 == NULL ) return; - CBotVar* pThis = pile1->FindVar("this"); -// pThis->SetUniqNum(m_nThisIdent); - pThis->SetUniqNum(-2); + CBotVar* pThis = pile1->FindVar("this"); +// pThis->SetUniqNum(m_nThisIdent); + pThis->SetUniqNum(-2); - int i = 0; + int i = 0; - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - if ( pile2->GivState() == 0 ) - { - p->RestoreState(pile2, true); // interrompu ici ! - return; - } - ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs - pile2 = pile2->RestoreStack(); - if ( pile2 == NULL ) return; + CBotInstr* p = m_Parameters; + // value les paramtres + // et place les valeurs sur la pile + // pour pouvoir tre interrompu n'importe quand + if ( p != NULL) while ( true ) + { + if ( pile2->GivState() == 0 ) + { + p->RestoreState(pile2, true); // interrompu ici ! + return; + } + ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs + pile2 = pile2->RestoreStack(); + if ( pile2 == NULL ) return; - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pResult = NULL; + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pResult = NULL; - CBotVar* pRes = pResult; + CBotVar* pRes = pResult; - pClass->RestoreMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, pile2); + pClass->RestoreMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, pile2); } bool CBotInstrMethode::Execute(CBotStack* &pj) { - CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This -// if ( pile1 == EOX ) return true; - - if ( pile1->IfStep() ) return false; - - CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir - - if ( pile1->GivState() == 0) - { - CBotVar* pThis = pile1->CopyVar(m_token); - // la valeur de This doit tre prise avant l'valuation des paramtres - // Test.Action( Test = Autre ); - // Action doit agir sur la valeur avant Test = Autre !! - pThis->SetName("this"); - pile1->AddVar(pThis); - pile1->IncState(); - } - int i = 0; - - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - if ( pile2->GivState() == 0 ) - { - if (!p->Execute(pile2)) return false; // interrompu ici ? - if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres - } - ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pThis = pile1->FindVar("this"); - CBotVar* pResult = NULL; - if (m_typRes.GivType()>0) pResult = CBotVar::Create("", m_typRes); - if (m_typRes.Eq(CBotTypClass)) - { -// CBotClass* pClass = CBotClass::Find(m_RetClassName); - pResult->SetClass(m_typRes.GivClass()); - } - CBotVar* pRes = pResult; - - if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu - - // met la nouvelle valeur de this la place de l'ancienne variable - CBotVar* old = pile1->FindVar(m_token); - old->Copy(pThis, false); - - if (pRes != pResult) delete pRes; - - return pj->Return(pile2); // libre toute la pile + CBotVar* ppVars[1000]; + CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This +// if ( pile1 == EOX ) return true; + + if ( pile1->IfStep() ) return false; + + CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir + + if ( pile1->GivState() == 0) + { + CBotVar* pThis = pile1->CopyVar(m_token); + // la valeur de This doit tre prise avant l'valuation des paramtres + // Test.Action( Test = Autre ); + // Action doit agir sur la valeur avant Test = Autre !! + pThis->SetName("this"); + pile1->AddVar(pThis); + pile1->IncState(); + } + int i = 0; + + CBotInstr* p = m_Parameters; + // value les paramtres + // et place les valeurs sur la pile + // pour pouvoir tre interrompu n'importe quand + if ( p != NULL) while ( true ) + { + if ( pile2->GivState() == 0 ) + { + if (!p->Execute(pile2)) return false; // interrompu ici ? + if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres + } + ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs + pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pThis = pile1->FindVar("this"); + CBotVar* pResult = NULL; + if (m_typRes.GivType()>0) pResult = CBotVar::Create("", m_typRes); + if (m_typRes.Eq(CBotTypClass)) + { +// CBotClass* pClass = CBotClass::Find(m_RetClassName); + pResult->SetClass(m_typRes.GivClass()); + } + CBotVar* pRes = pResult; + + if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interrompu + + // met la nouvelle valeur de this la place de l'ancienne variable + CBotVar* old = pile1->FindVar(m_token); + old->Copy(pThis, false); + + if (pRes != pResult) delete pRes; + + return pj->Return(pile2); // libre toute la pile } /////////////////////////////////////////////////////////////////////////// @@ -3698,10 +3695,10 @@ bool CBotInstrMethode::Execute(CBotStack* &pj) CBotNew::CBotNew() { - name = "CBotNew"; - m_Parameters = NULL; - m_nMethodeIdent = 0; -// m_nThisIdent = 0; + name = "CBotNew"; + m_Parameters = NULL; + m_nMethodeIdent = 0; +// m_nThisIdent = 0; } CBotNew::~CBotNew() @@ -3710,214 +3707,214 @@ CBotNew::~CBotNew() CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; - if ( !IsOfType(p, ID_NEW) ) return NULL; - - // vrifie que le token est un nom de classe - if (p->GivType() != TokenTypVar) return NULL; - - CBotClass* pClass = CBotClass::Find(p); - if (pClass == NULL) - { - pStack->SetError(TX_BADNEW, p); - return NULL; - } -/* if ( !pClass->m_IsDef ) - { - pStack->SetError(TX_BADNEW, p); - return NULL; - }*/ - - CBotNew* inst = new CBotNew(); - inst->SetToken(pp); - - inst->m_vartoken = p; - p = p->GivNext(); - - // cre l'objet sur le "tas" - // avec un pointeur sur cet objet - CBotVar* pVar = CBotVar::Create("", pClass); -// inst->m_nThisIdent = CBotVar::NextUniqNum(); - - // fait l'appel du crateur - CBotCStack* pStk = pStack->TokenStack(); - { - // regarde s'il y a des paramtres - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStk, ppVars); - if ( !pStk->IsOk() ) goto error; - - // le constructeur existe-il ? -// CBotString noname; - CBotTypResult r = pClass->CompileMethode(pClass->GivName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // libre le supplment de pile - int typ = r.GivType(); - - // s'il n'y a pas de constructeur, et pas de paramtres non plus, c'est ok - if ( typ == TX_UNDEFCALL && inst->m_Parameters == NULL ) typ = 0; - pVar->SetInit(true); // marque l'instance comme init - - if (typ>20) - { - pStk->SetError(typ, inst->m_vartoken.GivEnd()); - goto error; - } - - // si le constructeur n'existe pas, mais qu'il y a des paramtres - if (typ<0 && inst->m_Parameters != NULL) - { - pStk->SetError(TX_NOCONST, &inst->m_vartoken); - goto error; - } - - // rend le pointeur l'objet sur la pile - pStk->SetVar(pVar); - return pStack->Return(inst, pStk); - } + CBotToken* pp = p; + if ( !IsOfType(p, ID_NEW) ) return NULL; + + // vrifie que le token est un nom de classe + if (p->GivType() != TokenTypVar) return NULL; + + CBotClass* pClass = CBotClass::Find(p); + if (pClass == NULL) + { + pStack->SetError(TX_BADNEW, p); + return NULL; + } +/* if ( !pClass->m_IsDef ) + { + pStack->SetError(TX_BADNEW, p); + return NULL; + }*/ + + CBotNew* inst = new CBotNew(); + inst->SetToken(pp); + + inst->m_vartoken = p; + p = p->GivNext(); + + // cre l'objet sur le "tas" + // avec un pointeur sur cet objet + CBotVar* pVar = CBotVar::Create("", pClass); +// inst->m_nThisIdent = CBotVar::NextUniqNum(); + + // fait l'appel du crateur + CBotCStack* pStk = pStack->TokenStack(); + { + // regarde s'il y a des paramtres + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStk, ppVars); + if ( !pStk->IsOk() ) goto error; + + // le constructeur existe-il ? +// CBotString noname; + CBotTypResult r = pClass->CompileMethode(pClass->GivName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); + delete pStk->TokenStack(); // libre le supplment de pile + int typ = r.GivType(); + + // s'il n'y a pas de constructeur, et pas de paramtres non plus, c'est ok + if ( typ == TX_UNDEFCALL && inst->m_Parameters == NULL ) typ = 0; + pVar->SetInit(true); // marque l'instance comme init + + if (typ>20) + { + pStk->SetError(typ, inst->m_vartoken.GivEnd()); + goto error; + } + + // si le constructeur n'existe pas, mais qu'il y a des paramtres + if (typ<0 && inst->m_Parameters != NULL) + { + pStk->SetError(TX_NOCONST, &inst->m_vartoken); + goto error; + } + + // rend le pointeur l'objet sur la pile + pStk->SetVar(pVar); + return pStack->Return(inst, pStk); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } // excute une instruction "new" bool CBotNew::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); //pile principale -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); //pile principale +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - CBotStack* pile1 = pj->AddStack2(); //pile secondaire + CBotStack* pile1 = pj->AddStack2(); //pile secondaire - CBotVar* pThis = NULL; + CBotVar* pThis = NULL; - CBotToken* pt = &m_vartoken; - CBotClass* pClass = CBotClass::Find(pt); + CBotToken* pt = &m_vartoken; + CBotClass* pClass = CBotClass::Find(pt); - // cre la variable "this" de type pointeur l'objet + // cre la variable "this" de type pointeur l'objet - if ( pile->GivState()==0) - { - // cre une instance de la classe demande - // et initialise le pointeur cet objet + if ( pile->GivState()==0) + { + // cre une instance de la classe demande + // et initialise le pointeur cet objet - pThis = CBotVar::Create("this", pClass); -// pThis->SetUniqNum( m_nThisIdent ) ; - pThis->SetUniqNum( -2 ) ; + pThis = CBotVar::Create("this", pClass); +// pThis->SetUniqNum( m_nThisIdent ) ; + pThis->SetUniqNum( -2 ) ; - pile1->SetVar(pThis); // la place sur la pile1 - pile->IncState(); - } + pile1->SetVar(pThis); // la place sur la pile1 + pile->IncState(); + } - // retrouve le pointeur this si on a t interrompu - if ( pThis == NULL) - { - pThis = pile1->GivVar(); // retrouve le pointeur - } + // retrouve le pointeur this si on a t interrompu + if ( pThis == NULL) + { + pThis = pile1->GivVar(); // retrouve le pointeur + } - // y a-t-il une assignation ou des paramtres (constructeur) - if ( pile->GivState()==1) - { - // value le constructeur de l'instance + // y a-t-il une assignation ou des paramtres (constructeur) + if ( pile->GivState()==1) + { + // value le constructeur de l'instance - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; - int i = 0; + int i = 0; - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand + CBotInstr* p = m_Parameters; + // value les paramtres + // et place les valeurs sur la pile + // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats - if ( pile2->GivState() == 0 ) - { - if (!p->Execute(pile2)) return false; // interrompu ici ? - pile2->SetState(1); - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; + if ( p != NULL) while ( true ) + { + pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats + if ( pile2->GivState() == 0 ) + { + if (!p->Execute(pile2)) return false; // interrompu ici ? + pile2->SetState(1); + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; - // cre une variable pour le rsultat - CBotVar* pResult = NULL; // constructeurs toujours void + // cre une variable pour le rsultat + CBotVar* pResult = NULL; // constructeurs toujours void - if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu + if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interrompu - pThis->ConstructorSet(); // signale que le constructeur a t appel -// pile->Return(pile2); // libre un bout de pile + pThis->ConstructorSet(); // signale que le constructeur a t appel +// pile->Return(pile2); // libre un bout de pile -// pile->IncState(); - } +// pile->IncState(); + } - return pj->Return( pile1 ); // transmet en dessous + return pj->Return( pile1 ); // transmet en dessous } void CBotNew::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); //pile principale - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); //pile principale + if ( pile == NULL ) return; - CBotStack* pile1 = pj->AddStack2(); //pile secondaire + CBotStack* pile1 = pj->AddStack2(); //pile secondaire - CBotToken* pt = &m_vartoken; - CBotClass* pClass = CBotClass::Find(pt); + CBotToken* pt = &m_vartoken; + CBotClass* pClass = CBotClass::Find(pt); - // cre la variable "this" de type pointeur l'objet + // cre la variable "this" de type pointeur l'objet - if ( pile->GivState()==0) - { - return; - } + if ( pile->GivState()==0) + { + return; + } - CBotVar* pThis = pile1->GivVar(); // retrouve le pointeur -// pThis->SetUniqNum( m_nThisIdent ); - pThis->SetUniqNum( -2 ); + CBotVar* pThis = pile1->GivVar(); // retrouve le pointeur +// pThis->SetUniqNum( m_nThisIdent ); + pThis->SetUniqNum( -2 ); - // y a-t-il une assignation ou des paramtres (constructeur) - if ( pile->GivState()==1) - { - // value le constructeur de l'instance + // y a-t-il une assignation ou des paramtres (constructeur) + if ( pile->GivState()==1) + { + // value le constructeur de l'instance - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; - int i = 0; + int i = 0; - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand + CBotInstr* p = m_Parameters; + // value les paramtres + // et place les valeurs sur la pile + // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - pile2 = pile2->RestoreStack(); // de la place sur la pile pour les rsultats - if ( pile2 == NULL ) return; + if ( p != NULL) while ( true ) + { + pile2 = pile2->RestoreStack(); // de la place sur la pile pour les rsultats + if ( pile2 == NULL ) return; - if ( pile2->GivState() == 0 ) - { - p->RestoreState(pile2, bMain); // interrompu ici ! - return; - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; + if ( pile2->GivState() == 0 ) + { + p->RestoreState(pile2, bMain); // interrompu ici ! + return; + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; - pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GivString(), pThis, - ppVars, pile2) ; // interrompu ici ! - } + pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GivString(), pThis, + ppVars, pile2) ; // interrompu ici ! + } } ///////////////////////////////////////////////////////////// @@ -3925,77 +3922,77 @@ void CBotNew::RestoreState(CBotStack* &pj, bool bMain) bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) { - int t1 = type1.GivType(); - int t2 = type2.GivType(); - - int max = (t1 > t2) ? t1 : t2; - - if ( max == 99 ) return false; // un rsultat est void ? - - // cas particulier pour les concatnation de chanes - if (op == ID_ADD && max >= CBotTypString) return true; - if (op == ID_ASSADD && max >= CBotTypString) return true; - if (op == ID_ASS && t1 == CBotTypString) return true; - - if ( max >= CBotTypBoolean ) - { - if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; - if (t2 != t1) return false; - if (t1 == CBotTypArrayPointer) return type1.Compare(type2); - if (t1 == CBotTypPointer || - t1 == CBotTypClass || - t1 == CBotTypIntrinsic ) - { - CBotClass* c1 = type1.GivClass(); - CBotClass* c2 = type2.GivClass(); - - return c1->IsChildOf(c2) || c2->IsChildOf(c1); - // accepte le caste l'envers, - // l'opration sera refuse l'excution si le pointeur n'est pas compatible - } - - return true; - } - - type1.SetType(max); - type2.SetType(max); - return true; + int t1 = type1.GivType(); + int t2 = type2.GivType(); + + int max = (t1 > t2) ? t1 : t2; + + if ( max == 99 ) return false; // un rsultat est void ? + + // cas particulier pour les concatnation de chanes + if (op == ID_ADD && max >= CBotTypString) return true; + if (op == ID_ASSADD && max >= CBotTypString) return true; + if (op == ID_ASS && t1 == CBotTypString) return true; + + if ( max >= CBotTypBoolean ) + { + if ( (op == ID_EQ || op == ID_NE) && + (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && + (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE) && + (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && + (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; + if (t2 != t1) return false; + if (t1 == CBotTypArrayPointer) return type1.Compare(type2); + if (t1 == CBotTypPointer || + t1 == CBotTypClass || + t1 == CBotTypIntrinsic ) + { + CBotClass* c1 = type1.GivClass(); + CBotClass* c2 = type2.GivClass(); + + return c1->IsChildOf(c2) || c2->IsChildOf(c1); + // accepte le caste l'envers, + // l'opration sera refuse l'excution si le pointeur n'est pas compatible + } + + return true; + } + + type1.SetType(max); + type2.SetType(max); + return true; } // regarde si deux variables sont compatible pour un passage de paramtre bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) { - int t1 = type1.GivType(); - int t2 = type2.GivType(); + int t1 = type1.GivType(); + int t2 = type2.GivType(); - if ( t1 == CBotTypIntrinsic ) t1 = CBotTypClass; - if ( t2 == CBotTypIntrinsic ) t2 = CBotTypClass; + if ( t1 == CBotTypIntrinsic ) t1 = CBotTypClass; + if ( t2 == CBotTypIntrinsic ) t2 = CBotTypClass; - int max = (t1 > t2) ? t1 : t2; + int max = (t1 > t2) ? t1 : t2; - if ( max == 99 ) return false; // un rsultat est void ? + if ( max == 99 ) return false; // un rsultat est void ? - if ( max >= CBotTypBoolean ) - { - if ( t2 != t1 ) return false; + if ( max >= CBotTypBoolean ) + { + if ( t2 != t1 ) return false; - if ( max == CBotTypArrayPointer ) - return TypesCompatibles(type1.GivTypElem(), type2.GivTypElem()); + if ( max == CBotTypArrayPointer ) + return TypesCompatibles(type1.GivTypElem(), type2.GivTypElem()); - if ( max == CBotTypClass || max == CBotTypPointer ) - return type1.GivClass() == type2.GivClass() ; + if ( max == CBotTypClass || max == CBotTypPointer ) + return type1.GivClass() == type2.GivClass() ; - return true ; - } - return true; + return true ; + } + return true; } @@ -4007,65 +4004,65 @@ bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) FILE* fOpen(const char* name, const char* mode) { - return fopen(name, mode); + return fopen(name, mode); } int fClose(FILE* filehandle) { - return fclose(filehandle); + return fclose(filehandle); } size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle) { - return fwrite(buffer, elemsize, length, filehandle); + return fwrite(buffer, elemsize, length, filehandle); } size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle) { - return fread(buffer, elemsize, length, filehandle); + return fread(buffer, elemsize, length, filehandle); } size_t fWrite(const void *buffer, size_t length, FILE* filehandle) { - return fwrite(buffer, 1, length, filehandle); + return fwrite(buffer, 1, length, filehandle); } size_t fRead(void *buffer, size_t length, FILE* filehandle) { - return fread(buffer, 1, length, filehandle); + return fread(buffer, 1, length, filehandle); } //////////////////////////////////////// -#if false +#if false CBotString num(int n) { - CBotString s; - if ( n<0 ) {n = -n; s += "-";} - if ( n > 9 ) - { - s += num(n/10); - } - s += '0' + n%10; - return s; + CBotString s; + if ( n<0 ) {n = -n; s += "-";} + if ( n > 9 ) + { + s += num(n/10); + } + s += '0' + n%10; + return s; } extern void DEBUG( const char* text, int val, CBotStack* pile ) { - CBotProgram* p = pile->GivBotCall(true); - if ( !p->m_bDebugDD ) return; + CBotProgram* p = pile->GivBotCall(true); + if ( !p->m_bDebugDD ) return; - FILE* pf = fopen("CbotDebug.txt", "a"); + FILE* pf = fopen("CbotDebug.txt", "a"); - fputs( text, pf ); + fputs( text, pf ); - CBotString v = " " + num(val) + "\n"; - fputs( v, pf ); + CBotString v = " " + num(val) + "\n"; + fputs( v, pf ); - fclose( pf); + fclose( pf); } #endif diff --git a/src/CBot/CBot.rc b/src/CBot/CBot.rc deleted file mode 100644 index d8b5b74..0000000 --- a/src/CBot/CBot.rc +++ /dev/null @@ -1,279 +0,0 @@ -//Microsoft Developer Studio generated resource script. -// -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "afxres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// French (France) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) -#ifdef _WIN32 -LANGUAGE LANG_FRENCH, SUBLANG_FRENCH -#pragma code_page(1252) -#endif //_WIN32 - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE DISCARDABLE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE DISCARDABLE -BEGIN - "#include ""afxres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE DISCARDABLE -BEGIN - "\r\n" - "\0" -END - -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// String Table -// - -STRINGTABLE DISCARDABLE -BEGIN - ID_IF "if" - ID_ELSE "else" - ID_WHILE "while" - ID_DO "do" - ID_FOR "for" - ID_BREAK "break" - ID_CONTINUE "continue" - ID_SWITCH "switch" - ID_CASE "case" - ID_DEFAULT "default" - ID_TRY "try" - ID_THROW "throw" - ID_CATCH "catch" - ID_FINALLY "finally" - ID_TXT_AND "and" - ID_TXT_OR "or" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_DEBUGDD "STARTDEBUGDD" - ID_INT "int" - ID_FLOAT "float" - ID_BOOLEAN "boolean" - ID_STRING "string" - ID_VOID "void" - ID_BOOL "bool" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_TXT_NOT "not" - ID_RETURN "return" - ID_CLASS "class" - ID_EXTENDS "extends" - ID_SYNCHO "synchronized" - ID_NEW "new" - ID_PUBLIC "public" - ID_EXTERN "extern" - ID_FINAL "final" - ID_STATIC "static" - ID_PROTECTED "protected" - ID_PRIVATE "private" - ID_REPEAT "repeat" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_OPENPAR "Il manque une parenthse ouvrante." - TX_CLOSEPAR "Il manque une parenthse fermante." - TX_NOTBOOL "L'expression doit tre un boolean." - TX_UNDEFVAR "Variable non dclare." - TX_BADLEFT "Assignation impossible." - TX_ENDOF "Terminateur point-virgule non trouv." - TX_OUTCASE "Instruction ""case"" hors d'un bloc ""switch""." - TX_NOTERM "Instructions aprs la fin." -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_CLOSEBLK "Il manque la fin du bloc." - TX_ELSEWITHOUTIF "Instruction ""else"" sans ""if"" correspondant." - TX_OPENBLK "Dbut d'un bloc attendu." - TX_BADTYPE "Mauvais type de rsultat pour l'assignation." - TX_REDEFVAR "Redfinition d'une variable." - TX_BAD2TYPE "Les deux oprandes ne sont pas de types compatibles." - TX_UNDEFCALL "Routine inconnue." - TX_MISDOTS "Sparateur "" : "" attendu." - TX_WHILE "Manque le mot ""while""." - TX_BREAK "Instruction ""break"" en dehors d'une boucle." - TX_LABEL "Un label ne peut se placer que devant un ""for"", un ""while"" ou un ""do""." - TX_NOLABEL "Cette tiquette n'existe pas" - TX_NOCASE "Manque une instruction ""case""." - TX_BADNUM "Un nombre est attendu." - TX_VOID "Paramtre void." - TX_NOTYP "Dclaration de type attendu." -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_DIVZERO "Division par zro." - TX_NOTINIT "Variable non initialise." - TX_BADTHROW "Valeur ngative refuse pour ""throw""." - TX_NORETVAL "La fonction n'a pas retourn de rsultat" - TX_NORUN "Pas de fonction en excution" - TX_NOCALL "Appel d'une fonction inexistante" - TX_NOCLASS "Cette classe n'existe pas" - TX_NULLPT "Pointeur nul." - TX_OPNAN "Opration sur un ""nan""" - TX_OUTARRAY "Accs hors du tableau" - TX_STACKOVER "Dpassement de la pile" - TX_DELETEDPT "Pointeur un objet dtruit" - TX_FILEOPEN "Ouverture du fichier impossible" - TX_NOTOPEN "Fichier pas ouvert" - TX_ERRREAD "Erreur de lecture" - TX_ERRWRITE "Erreur d'criture" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_NOVAR "Nom d'une variable attendu." - TX_NOFONC "Nom de la fonction attendu." - TX_OVERPARAM "Trop de paramtres." - TX_REDEF "Cette fonction existe dj." - TX_LOWPARAM "Pas assez de paramtres." - TX_BADPARAM "Aucune fonction de ce nom n'accepte ce(s) type(s) de paramtre(s)." - TX_NUMPARAM "Aucune fonction de ce nom n'accepte ce nombre de paramtres." - TX_NOITEM "Cet lment n'existe pas dans cette classe." - TX_DOT "L'objet n'est pas une instance d'une classe." - TX_NOCONST "Il n'y a pas de constructeur appropri." - TX_REDEFCLASS "Cette classe existe dj." - TX_CLBRK """ ] "" attendu." - TX_RESERVED "Ce mot est rserv." - TX_BADNEW "Mauvais argument pour ""new""." - TX_OPBRK """ [ "" attendu." - TX_BADSTRING "Une chane de caractre est attendue." -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_BADINDEX "Mauvais type d'index" - TX_PRIVATE "Membre priv de la classe" - TX_NOPUBLIC """public"" manque" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_OPENPAR "(" - ID_CLOSEPAR ")" - ID_OPBLK "{" - ID_CLBLK "}" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_SEP ";" - ID_COMMA "," - ID_DOTS ":" - ID_DOT "." - ID_OPBRK "[" - ID_CLBRK "]" - ID_DBLDOTS "::" - ID_LOGIC "?" - ID_ADD "+" - ID_SUB "-" - ID_MUL "*" - ID_DIV "/" - ID_ASS "=" - ID_ASSADD "+=" - ID_ASSSUB "-=" - ID_ASSMUL "*=" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_TRUE "true" - ID_FALSE "false" - ID_NULL "null" - ID_NAN "nan" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_ASSDIV "/=" - ID_ASSOR "|=" - ID_ASSAND "&=" - ID_ASSXOR "^=" - ID_ASSSL "<<=" - ID_ASSSR ">>>=" - ID_ASSASR ">>=" - ID_SL "<<" - ID_SR ">>>" - ID_ASR ">>" - ID_INC "++" - ID_DEC "--" - ID_LO "<" - ID_HI ">" - ID_LS "<=" - ID_HS ">=" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_EQ "==" - ID_NE "!=" - ID_AND "&" - ID_XOR "^" - ID_OR "|" - ID_LOG_AND "&&" - ID_LOG_OR "||" - ID_LOG_NOT "!" - ID_NOT "~" - ID_MODULO "%" - ID_POWER "**" - ID_ASSMODULO "%=" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_UNDEF "undefined" - TX_NAN "not a number" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_SUPER "super" -END - -#endif // French (France) resources -///////////////////////////////////////////////////////////////////////////// - - - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// - - -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED - diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index f0d7fef..514146f 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -13,66 +13,68 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/.//////////////////////////////////////////////////////////////////////// -// Librairie pour l'interprtation du language CBOT -// pour le jeu COLOBOT -// - -//#include "stdafx.h" +#ifndef _CBOTDLL_H_ +#define _CBOTDLL_H_ +/** + * \file CBotDll.h + * \brief Library for interpretation of CBOT language + */ -// #include #include +#include "resource.h" +#include +#include -// #define DllExport __declspec( dllexport ) -#define CBOTVERSION 104 +#define CBOTVERSION 104 //////////////////////////////////////////////////////////////////////// -// quelques classes dfinies par ailleurs - -class CBotToken; // programme transform en "jetons" -class CBotStack; // pile pour l'excution -class CBotClass; // classe d'object -class CBotInstr; // instruction excuter -class CBotFunction; // les fonctions user -class CBotVar; // les variables -class CBotVarClass; // une instance de classe -class CBotVarPointer; // pointeur une instance de classe -class CBotCall; // les fonctions -class CBotCallMethode; // les mthodes -class CBotDefParam; // liste de paramtres -class CBotCStack; +// forward declaration of needed classes + +class CBotToken; // program turned into "tokens +class CBotStack; // for the execution stack +class CBotClass; // class of object +class CBotInstr; // instruction to be executed +class CBotFunction; // user functions +class CBotVar; // variables +class CBotVarClass; // instance of class +class CBotVarPointer; // pointer to an instance of class +class CBotCall; // fonctions +class CBotCallMethode; // methods +class CBotDefParam; // parameter list +class CBotCStack; // stack //////////////////////////////////////////////////////////////////////// -// Gestion des variables +// Variables management //////////////////////////////////////////////////////////////////////// // ces types sont calqus sur les types Java // ne pas changer l'ordre de ces types +/** \brief CBotType Defines known types. This types are modeled on Java types. Do not change the order of elements */ enum CBotType { - CBotTypVoid = 0, // fonction retournant void - CBotTypByte = 1, //n // nombre entier ( 8 bits) - CBotTypShort = 2, //n // nombre entier (16 bits) - CBotTypChar = 3, //n // caractre "unicode" (16 bits) - CBotTypInt = 4, // nombre entier (32 bits) - CBotTypLong = 5, //n // nombre entier (64 bits) - CBotTypFloat = 6, // nombre dcimal (32 bits) - CBotTypDouble = 7, //n // nombre dcimal (64 bits) - CBotTypBoolean = 8, // true ou false exclusivement - CBotTypString = 9, // chaine de caractre - - CBotTypArrayPointer = 10, // un tableau de variables - CBotTypArrayBody = 11, // idem mais cre l'instance - - CBotTypPointer = 12, // pointeur une instance - CBotTypNullPointer = 13, // pointeur null est spcial - - CBotTypClass = 15, // instance d'une classe - CBotTypIntrinsic = 16 // instance d'une classe intrinsque + CBotTypVoid = 0, + CBotTypByte = 1, + CBotTypShort = 2, + CBotTypChar = 3, + CBotTypInt = 4, + CBotTypLong = 5, + CBotTypFloat = 6, + CBotTypDouble = 7, + CBotTypBoolean = 8, + CBotTypString = 9, + + CBotTypArrayPointer = 10, // array of variables + CBotTypArrayBody = 11, // same but creates an instance + + CBotTypPointer = 12, // pointer to an instance + CBotTypNullPointer = 13, // null pointer is special + CBotTypClass = 15, + CBotTypIntrinsic = 16 // instance of a class intrinsic }; - //n = non encore implment +//n = non encore implment // pour SetUserPtr lors de la suppression d'un objet #define OBJECTDELETED ((void*)-1) @@ -84,90 +86,75 @@ enum CBotType class CBotTypResult { private: - int m_type; - CBotTypResult* m_pNext; // pour les types de types - CBotClass* m_pClass; // pour les drivs de classe - int m_limite; // limitation des tableaux - friend class CBotVarClass; - friend class CBotVarPointer; + int m_type; + CBotTypResult* m_pNext; // pour les types de types + CBotClass* m_pClass; // pour les drivs de classe + int m_limite; // limitation des tableaux + friend class CBotVarClass; + friend class CBotVarPointer; public: - // divers constructeurs selon les besoins - //DllExport - CBotTypResult(int type); - // pour les types simples (CBotTypInt CBotTypString) - //DllExport - CBotTypResult(int type, const char* name); - // pour les types pointeur et classe intrinsic - //DllExport - CBotTypResult(int type, CBotClass* pClass); - // idem partir de l'instance d'une classe - //DllExport - CBotTypResult(int type, CBotTypResult elem); - // pour les tableaux de variables - - //DllExport - CBotTypResult(const CBotTypResult& typ); - // pour les assignations - //DllExport - CBotTypResult(); - // pour par dfaut - //DllExport - ~CBotTypResult(); - - //DllExport - int GivType(int mode = 0) const; - // rend le type CBotTyp* du rsultat - - void SetType(int n); - // modifie le type - - //DllExport - CBotClass* GivClass() const; - // rend le pointeur la classe (pour les CBotTypClass, CBotTypPointer) - - //DllExport - int GivLimite() const; - // rend la taille limite du tableau (CBotTypArray) - - //DllExport - void SetLimite(int n); - // fixe une limite au tableau - - void SetArray(int* max ); - // idem avec une liste de dimension (tableaux de tableaux) - - //DllExport - CBotTypResult& GivTypElem() const; - // rend le type des lments du tableau (CBotTypArray) - - //DllExport - bool Compare(const CBotTypResult& typ) const; - // compare si les types sont compatibles - //DllExport - bool Eq(int type) const; - // compare le type - - //DllExport - CBotTypResult& - operator=(const CBotTypResult& src); - // copie un type complet dans un autre + // divers constructeurs selon les besoins + CBotTypResult(int type); + // pour les types simples (CBotTypInt CBotTypString) + CBotTypResult(int type, const char* name); + // pour les types pointeur et classe intrinsic + CBotTypResult(int type, CBotClass* pClass); + // idem partir de l'instance d'une classe + CBotTypResult(int type, CBotTypResult elem); + // pour les tableaux de variables + + CBotTypResult(const CBotTypResult& typ); + // pour les assignations + CBotTypResult(); + // pour par dfaut + ~CBotTypResult(); + + int GivType(int mode = 0) const; + // rend le type CBotTyp* du rsultat + + void SetType(int n); + // modifie le type + + CBotClass* GivClass() const; + // rend le pointeur la classe (pour les CBotTypClass, CBotTypPointer) + + int GivLimite() const; + // rend la taille limite du tableau (CBotTypArray) + + void SetLimite(int n); + // fixe une limite au tableau + + void SetArray(int* max ); + // idem avec une liste de dimension (tableaux de tableaux) + + CBotTypResult& GivTypElem() const; + // rend le type des lments du tableau (CBotTypArray) + + bool Compare(const CBotTypResult& typ) const; + // compare si les types sont compatibles + bool Eq(int type) const; + // compare le type + + CBotTypResult& + operator=(const CBotTypResult& src); + // copie un type complet dans un autre }; /* // pour dfinir un rsultat en sortie, utiliser par exemple - // pour rendre un simple Float - return CBotTypResult( CBotTypFloat ); + // pour rendre un simple Float + return CBotTypResult( CBotTypFloat ); - // pour rendre un tableau de string - return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); + // pour rendre un tableau de string + return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); - // pour rendre un tableau de tableau de "point" - CBotTypResult typPoint( CBotTypIntrinsic, "point" ); - CBotTypResult arrPoint( CBotTypArray, typPoint ); - return CBotTypResult( CBotTypArray, arrPoint ); + // pour rendre un tableau de tableau de "point" + CBotTypResult typPoint( CBotTypIntrinsic, "point" ); + CBotTypResult arrPoint( CBotTypArray, typPoint ); + return CBotTypResult( CBotTypArray, arrPoint ); */ @@ -178,70 +165,70 @@ public: // voici la liste des erreurs pouvant tre retournes par le module // pour la compilation -#define CBotErrOpenPar 5000 // manque la parenthse ouvrante -#define CBotErrClosePar 5001 // manque la parenthse fermante -#define CBotErrNotBoolean 5002 // l'expression doit tre un boolean -#define CBotErrUndefVar 5003 // variable non dclare -#define CBotErrBadLeft 5004 // assignation impossible ( 5 = ... ) -#define CBotErrNoTerminator 5005 // point-virgule attendu -#define CBotErrCaseOut 5006 // case en dehors d'un switch -// CBotErrNoTerm 5007, plus utile -#define CBotErrCloseBlock 5008 // manque " } " -#define CBotErrElseWhitoutIf 5009 // else sans if correspondant -#define CBotErrOpenBlock 5010 // manque " { " -#define CBotErrBadType1 5011 // mauvais type pour l'assignation -#define CBotErrRedefVar 5012 // redfinition de la variable -#define CBotErrBadType2 5013 // 2 oprandes de type incompatibles -#define CBotErrUndefCall 5014 // routine inconnue -#define CBotErrNoDoubleDots 5015 // " : " attendu -// CBotErrWhile 5016, plus utile -#define CBotErrBreakOutside 5017 // break en dehors d'une boucle -#define CBotErrUndefLabel 5019 // label inconnu -#define CBotErrLabel 5018 // label ne peut se mettre ici -#define CBotErrNoCase 5020 // manque " case " -#define CBotErrBadNum 5021 // nombre attendu -#define CBotErrVoid 5022 // " void " pas possible ici -#define CBotErrNoType 5023 // dclaration de type attendue -#define CBotErrNoVar 5024 // nom de variable attendu -#define CBotErrNoFunc 5025 // nom de fonction attendu -#define CBotErrOverParam 5026 // trop de paramtres -#define CBotErrRedefFunc 5027 // cette fonction existe dj -#define CBotErrLowParam 5028 // pas assez de paramtres -#define CBotErrBadParam 5029 // mauvais types de paramtres -#define CBotErrNbParam 5030 // mauvais nombre de paramtres -#define CBotErrUndefItem 5031 // lment n'existe pas dans la classe -#define CBotErrUndefClass 5032 // variable n'est pas une classe -#define CBotErrNoConstruct 5033 // pas de constructeur appropri -#define CBotErrRedefClass 5034 // classe existe dj -#define CBotErrCloseIndex 5035 // " ] " attendu -#define CBotErrReserved 5036 // mot rserv (par un DefineNum) -#define CBotErrBadNew 5037 // mauvais paramtre pour new -#define CBotErrOpenIndex 5038 // " [ " attendu -#define CBotErrBadString 5039 // chane de caractre attendue -#define CBotErrBadIndex 5040 // mauvais type d'index "[ false ]" -#define CBotErrPrivate 5041 // lment protg -#define CBotErrNoPublic 5042 // manque le mot "public" +#define CBotErrOpenPar 5000 // manque la parenthse ouvrante +#define CBotErrClosePar 5001 // manque la parenthse fermante +#define CBotErrNotBoolean 5002 // l'expression doit tre un boolean +#define CBotErrUndefVar 5003 // variable non dclare +#define CBotErrBadLeft 5004 // assignation impossible ( 5 = ... ) +#define CBotErrNoTerminator 5005 // point-virgule attendu +#define CBotErrCaseOut 5006 // case en dehors d'un switch +// CBotErrNoTerm 5007, plus utile +#define CBotErrCloseBlock 5008 // manque " } " +#define CBotErrElseWhitoutIf 5009 // else sans if correspondant +#define CBotErrOpenBlock 5010 // manque " { " +#define CBotErrBadType1 5011 // mauvais type pour l'assignation +#define CBotErrRedefVar 5012 // redfinition de la variable +#define CBotErrBadType2 5013 // 2 oprandes de type incompatibles +#define CBotErrUndefCall 5014 // routine inconnue +#define CBotErrNoDoubleDots 5015 // " : " attendu +// CBotErrWhile 5016, plus utile +#define CBotErrBreakOutside 5017 // break en dehors d'une boucle +#define CBotErrUndefLabel 5019 // label inconnu +#define CBotErrLabel 5018 // label ne peut se mettre ici +#define CBotErrNoCase 5020 // manque " case " +#define CBotErrBadNum 5021 // nombre attendu +#define CBotErrVoid 5022 // " void " pas possible ici +#define CBotErrNoType 5023 // dclaration de type attendue +#define CBotErrNoVar 5024 // nom de variable attendu +#define CBotErrNoFunc 5025 // nom de fonction attendu +#define CBotErrOverParam 5026 // trop de paramtres +#define CBotErrRedefFunc 5027 // cette fonction existe dj +#define CBotErrLowParam 5028 // pas assez de paramtres +#define CBotErrBadParam 5029 // mauvais types de paramtres +#define CBotErrNbParam 5030 // mauvais nombre de paramtres +#define CBotErrUndefItem 5031 // lment n'existe pas dans la classe +#define CBotErrUndefClass 5032 // variable n'est pas une classe +#define CBotErrNoConstruct 5033 // pas de constructeur appropri +#define CBotErrRedefClass 5034 // classe existe dj +#define CBotErrCloseIndex 5035 // " ] " attendu +#define CBotErrReserved 5036 // mot rserv (par un DefineNum) +#define CBotErrBadNew 5037 // mauvais paramtre pour new +#define CBotErrOpenIndex 5038 // " [ " attendu +#define CBotErrBadString 5039 // chane de caractre attendue +#define CBotErrBadIndex 5040 // mauvais type d'index "[ false ]" +#define CBotErrPrivate 5041 // lment protg +#define CBotErrNoPublic 5042 // manque le mot "public" // voici la liste des erreurs pouvant tre retournes par le module // pour l'excution -#define CBotErrZeroDiv 6000 // division par zro -#define CBotErrNotInit 6001 // variable non initialise -#define CBotErrBadThrow 6002 // throw d'une valeur ngative -#define CBotErrNoRetVal 6003 // fonction n'a pas retourn de rsultat -#define CBotErrNoRun 6004 // Run() sans fonction active -#define CBotErrUndefFunc 6005 // appel d'une fonction qui n'existe plus -#define CBotErrNotClass 6006 // cette classe n'existe pas -#define CBotErrNull 6007 // pointeur null -#define CBotErrNan 6008 // calcul avec un NAN -#define CBotErrOutArray 6009 // index hors du tableau -#define CBotErrStackOver 6010 // dpassement de la pile -#define CBotErrDeletedPtr 6011 // pointeur un objet dtruit - -#define CBotErrFileOpen 6012 // ouverture du fichier impossible -#define CBotErrNotOpen 6013 // canal pas ouvert -#define CBotErrRead 6014 // erreur la lecture -#define CBotErrWrite 6015 // erreur l'criture +#define CBotErrZeroDiv 6000 // division par zro +#define CBotErrNotInit 6001 // variable non initialise +#define CBotErrBadThrow 6002 // throw d'une valeur ngative +#define CBotErrNoRetVal 6003 // fonction n'a pas retourn de rsultat +#define CBotErrNoRun 6004 // Run() sans fonction active +#define CBotErrUndefFunc 6005 // appel d'une fonction qui n'existe plus +#define CBotErrNotClass 6006 // cette classe n'existe pas +#define CBotErrNull 6007 // pointeur null +#define CBotErrNan 6008 // calcul avec un NAN +#define CBotErrOutArray 6009 // index hors du tableau +#define CBotErrStackOver 6010 // dpassement de la pile +#define CBotErrDeletedPtr 6011 // pointeur un objet dtruit + +#define CBotErrFileOpen 6012 // ouverture du fichier impossible +#define CBotErrNotOpen 6013 // canal pas ouvert +#define CBotErrRead 6014 // erreur la lecture +#define CBotErrWrite 6015 // erreur l'criture // d'autres valeurs peuvent tre rendues // par exemple les exceptions rendues par les routines externes @@ -256,106 +243,74 @@ public: class CBotString { +public: + CBotString(); + CBotString(const char* p); + CBotString(const CBotString& p); + ~CBotString(); + + void Empty(); + bool IsEmpty() const; + int GivLength(); + int Find(const char c); + int Find(const char* lpsz); + int ReverseFind(const char c); + int ReverseFind(const char* lpsz); + bool LoadString(unsigned int id); + CBotString Mid(int nFirst, int nCount) const; + CBotString Mid(int nFirst) const; + CBotString Mid(int start, int lg=-1); + CBotString Left(int nCount) const; + CBotString Right(int nCount) const; + int Compare(const char* lpsz) const; + void MakeUpper(); + void MakeLower(); + + + /** + * \brief Overloaded oprators to work on CBotString classes + */ + const CBotString& operator=(const CBotString& stringSrc); + const CBotString& operator=(const char ch); + const CBotString& operator=(const char* pString); + const CBotString& operator+(const CBotString& str); + friend CBotString operator+(const CBotString& string, const char* lpsz); + + const CBotString& operator+=(const char ch); + const CBotString& operator+=(const CBotString& str); + bool operator==(const CBotString& str); + bool operator==(const char* p); + bool operator!=(const CBotString& str); + bool operator!=(const char* p); + bool operator>(const CBotString& str); + bool operator>(const char* p); + bool operator>=(const CBotString& str); + bool operator>=(const char* p); + bool operator<(const CBotString& str); + bool operator<(const char* p); + bool operator<=(const CBotString& str); + bool operator<=(const char* p); + + operator const char*() const; // as a C string + + private: - char* m_ptr; // pointeur la chaine - int m_lg; // longueur de la chaine - // static - // HINSTANCE m_hInstance; -public: - //DllExport - CBotString(); - //DllExport - CBotString(const char* p); - //DllExport - CBotString(const CBotString& p); - //DllExport - ~CBotString(); - - //DllExport - void Empty(); - //DllExport - bool IsEmpty() const; - //DllExport - int GivLength(); - //DllExport - int Find(const char c); - //DllExport - int Find(const char* lpsz); - //DllExport - int ReverseFind(const char c); - //DllExport - int ReverseFind(const char* lpsz); - //DllExport - bool LoadString(unsigned int id); - //DllExport - CBotString Mid(int nFirst, int nCount) const; - //DllExport - CBotString Mid(int nFirst) const; - //DllExport - CBotString Left(int nCount) const; - //DllExport - CBotString Right(int nCount) const; - - //DllExport - const CBotString& - operator=(const CBotString& stringSrc); - //DllExport - const CBotString& - operator=(const char ch); - //DllExport - const CBotString& - operator=(const char* pString); - //DllExport - const CBotString& - operator+(const CBotString& str); - //DllExport - friend CBotString - operator+(const CBotString& string, const char* lpsz); - - //DllExport - const CBotString& - operator+=(const char ch); - //DllExport - const CBotString& - operator+=(const CBotString& str); - //DllExport - bool operator==(const CBotString& str); - //DllExport - bool operator==(const char* p); - //DllExport - bool operator!=(const CBotString& str); - //DllExport - bool operator!=(const char* p); - //DllExport - bool operator>(const CBotString& str); - //DllExport - bool operator>(const char* p); - //DllExport - bool operator>=(const CBotString& str); - //DllExport - bool operator>=(const char* p); - //DllExport - bool operator<(const CBotString& str); - //DllExport - bool operator<(const char* p); - //DllExport - bool operator<=(const CBotString& str); - //DllExport - bool operator<=(const char* p); - - //DllExport - operator const char*() const; // as a C string - - int Compare(const char* lpsz) const; - - //DllExport - CBotString Mid(int start, int lg=-1); - - //DllExport - void MakeUpper(); - //DllExport - void MakeLower(); + /** \brief Pointer to string */ + char* m_ptr; + + /** \brief Length of the string */ + int m_lg; + + /** \brief Keeps the string corresponding to keyword ID */ + static const std::map s_keywordString; + + /** + * \brief MapIdToString maps given ID to its string equivalent + * \param id Provided identifier + * \return string if found, else NullString + */ + static const char * MapIdToString(EID id); }; @@ -364,35 +319,28 @@ public: class CBotStringArray : public CBotString { private: - int m_nSize; // nombre d'lments - int m_nMaxSize; // taille rserve - CBotString* m_pData; // ^aux donnes + int m_nSize; // nombre d'lments + int m_nMaxSize; // taille rserve + CBotString* m_pData; // ^aux donnes public: - //DllExport - CBotStringArray(); - //DllExport - ~CBotStringArray(); - //DllExport - void SetSize(int nb); - //DllExport - int GivSize(); - //DllExport - void Add(const CBotString& str); - //DllExport - CBotString& operator[](int nIndex); - - //DllExport - CBotString& ElementAt(int nIndex); + CBotStringArray(); + ~CBotStringArray(); + void SetSize(int nb); + int GivSize(); + void Add(const CBotString& str); + CBotString& operator[](int nIndex); + + CBotString& ElementAt(int nIndex); }; // diffrents mode pour GetPosition enum CBotGet { - GetPosExtern = 1, - GetPosNom = 2, - GetPosParam = 3, - GetPosBloc = 4 + GetPosExtern = 1, + GetPosNom = 2, + GetPosParam = 3, + GetPosBloc = 4 }; //////////////////////////////////////////////////////////////////// @@ -402,211 +350,183 @@ enum CBotGet class CBotProgram { private: - CBotFunction* m_Prog; // les fonctions dfinies par l'utilisateur - CBotFunction* m_pRun; // la fonction de base pour l'excution - CBotClass* m_pClass; // les classes dfinies dans cette partie - CBotStack* m_pStack; // la pile d'excution - CBotVar* m_pInstance; // instance de la classe parent - friend class CBotFunction; + CBotFunction* m_Prog; // les fonctions dfinies par l'utilisateur + CBotFunction* m_pRun; // la fonction de base pour l'excution + CBotClass* m_pClass; // les classes dfinies dans cette partie + CBotStack* m_pStack; // la pile d'excution + CBotVar* m_pInstance; // instance de la classe parent + friend class CBotFunction; - int m_ErrorCode; - int m_ErrorStart; - int m_ErrorEnd; + int m_ErrorCode; + int m_ErrorStart; + int m_ErrorEnd; - long m_Ident; // identificateur associ + long m_Ident; // identificateur associ public: - static - CBotString m_DebugVarStr; // a fin de debug - bool m_bDebugDD; // idem dclanchable par robot + static + CBotString m_DebugVarStr; // a fin de debug + bool m_bDebugDD; // idem dclanchable par robot - bool m_bCompileClass; + bool m_bCompileClass; public: - //DllExport - static - void Init(); - // initialise le module (dfini les mots clefs pour les erreurs) - // doit tre fait une fois (et une seule) au tout dbut - //DllExport - static - void Free(); - // libre les zones mmoires statiques - - //DllExport - static - int GivVersion(); - // donne la version de la librairie CBOT - - - //DllExport - CBotProgram(); - //DllExport - CBotProgram(CBotVar* pInstance); - //DllExport - ~CBotProgram(); - - //DllExport - bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); - // compile le programme donn en texte - // retourne false s'il y a une erreur la compilation - // voir GetCompileError() pour rcuprer l'erreur - // ListFonctions retourne le nom des fonctions dclares extern - // pUser permet de passer un pointeur pour les routines dfinies par AddFunction - - //DllExport - void SetIdent(long n); - // associe un identificateur avec l'instance CBotProgram - - //DllExport - long GivIdent(); - // redonne l'identificateur - - //DllExport - int GivError(); - //DllExport - bool GetError(int& code, int& start, int& end); - //DllExport - bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); - // si true - // donne l'erreur trouve la compilation - // ou l'excution - // start et end dlimite le bloc o se trouve l'erreur - // pProg permet de savoir dans quel "module" s'est produite l'erreur d'excution - //DllExport - static - CBotString GivErrorText(int code); - - - //DllExport - bool Start(const char* name); - // dfinie quelle fonction doit tre excute - // retourne false si la fontion name n'est pas trouve - // le programme ne fait rien, il faut appeller Run() pour cela - - //DllExport - bool Run(void* pUser = NULL, int timer = -1); - // excute le programme - // retourne false si le programme a t suspendu - // retourne true si le programme s'est termin avec ou sans erreur - // timer = 0 permet de faire une avance pas pas - - //DllExport - bool GetRunPos(const char* &FunctionName, int &start, int &end); - // donne la position dans le programme en excution - // retourne false si on n'est pas en excution (programme termin) - // FunctionName est un pointeur rendu sur le nom de la fonction - // start et end la position dans le texte du token en traitement - - //DllExport - CBotVar* GivStackVars(const char* &FunctionName, int level); - // permet d'obtenir le pointeur aux variables sur la pile d'excution - // level est un paramtre d'entre, 0 pour le dernier niveau, -1, -2, etc pour les autres niveau - // la valeur retourne (CBotVar*) est une liste de variable (ou NULL) - // qui peut tre trait que la liste des paramtres reu par une routine - // FunctionName donne le nom de la fonction o se trouvent ces variables - // FunctionName == NULL signifiant qu'on est plus dans le programme (selon level) - - //DllExport - void Stop(); - // arrte l'excution du programme - // quitte donc le mode "suspendu" - - //DllExport - static - void SetTimer(int n); - // dfini le nombre de pas (parties d'instructions) faire - // dans Run() avant de rendre la main "false" - - //DllExport - static - bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - // cet appel permet d'ajouter de manire externe (**) - // une nouvelle fonction utilisable par le programme CBot - - //DllExport - static - bool DefineNum(const char* name, long val); - - //DllExport - bool SaveState(FILE* pf); - // sauvegarde l'tat d'excution dans le fichier - // le fichier doit avoir t ouvert avec l'appel fopen de cette dll - // sinon le systme plante - //DllExport - bool RestoreState(FILE* pf); - // rtablie l'tat de l'excution depuis le fichier - // le programme compil doit videmment tre identique - - //DllExport - bool GetPosition(const char* name, int& start, int& stop, - CBotGet modestart = GetPosExtern, - CBotGet modestop = GetPosBloc); - // donne la position d'une routine dans le texte d'origine - // le mode permet de choisir l'lment trouver pour le dbut et la fin - // voir les modes ci-dessus dans CBotGet - - - CBotFunction* GivFunctions(); + static + void Init(); + // initialise le module (dfini les mots clefs pour les erreurs) + // doit tre fait une fois (et une seule) au tout dbut + static + void Free(); + // libre les zones mmoires statiques + + static + int GivVersion(); + // donne la version de la librairie CBOT + + + CBotProgram(); + CBotProgram(CBotVar* pInstance); + ~CBotProgram(); + + bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); + // compile le programme donn en texte + // retourne false s'il y a une erreur la compilation + // voir GetCompileError() pour rcuprer l'erreur + // ListFonctions retourne le nom des fonctions dclares extern + // pUser permet de passer un pointeur pour les routines dfinies par AddFunction + + void SetIdent(long n); + // associe un identificateur avec l'instance CBotProgram + + long GivIdent(); + // redonne l'identificateur + + int GivError(); + bool GetError(int& code, int& start, int& end); + bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); + // si true + // donne l'erreur trouve la compilation + // ou l'excution + // start et end dlimite le bloc o se trouve l'erreur + // pProg permet de savoir dans quel "module" s'est produite l'erreur d'excution + static + CBotString GivErrorText(int code); + + + bool Start(const char* name); + // dfinie quelle fonction doit tre excute + // retourne false si la fontion name n'est pas trouve + // le programme ne fait rien, il faut appeller Run() pour cela + + bool Run(void* pUser = NULL, int timer = -1); + // excute le programme + // retourne false si le programme a t suspendu + // retourne true si le programme s'est termin avec ou sans erreur + // timer = 0 permet de faire une avance pas pas + + bool GetRunPos(const char* &FunctionName, int &start, int &end); + // donne la position dans le programme en excution + // retourne false si on n'est pas en excution (programme termin) + // FunctionName est un pointeur rendu sur le nom de la fonction + // start et end la position dans le texte du token en traitement + + CBotVar* GivStackVars(const char* &FunctionName, int level); + // permet d'obtenir le pointeur aux variables sur la pile d'excution + // level est un paramtre d'entre, 0 pour le dernier niveau, -1, -2, etc pour les autres niveau + // la valeur retourne (CBotVar*) est une liste de variable (ou NULL) + // qui peut tre trait que la liste des paramtres reu par une routine + // FunctionName donne le nom de la fonction o se trouvent ces variables + // FunctionName == NULL signifiant qu'on est plus dans le programme (selon level) + + void Stop(); + // arrte l'excution du programme + // quitte donc le mode "suspendu" + + static + void SetTimer(int n); + // dfini le nombre de pas (parties d'instructions) faire + // dans Run() avant de rendre la main "false" + + static + bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + // cet appel permet d'ajouter de manire externe (**) + // une nouvelle fonction utilisable par le programme CBot + + static + bool DefineNum(const char* name, long val); + + bool SaveState(FILE* pf); + // sauvegarde l'tat d'excution dans le fichier + // le fichier doit avoir t ouvert avec l'appel fopen de cette dll + // sinon le systme plante + bool RestoreState(FILE* pf); + // rtablie l'tat de l'excution depuis le fichier + // le programme compil doit videmment tre identique + + bool GetPosition(const char* name, int& start, int& stop, + CBotGet modestart = GetPosExtern, + CBotGet modestop = GetPosBloc); + // donne la position d'une routine dans le texte d'origine + // le mode permet de choisir l'lment trouver pour le dbut et la fin + // voir les modes ci-dessus dans CBotGet + + + CBotFunction* GivFunctions(); }; /////////////////////////////////////////////////////////////////////////////// // routines pour la gestion d'un fichier (FILE*) - //DllExport - FILE* fOpen(const char* name, const char* mode); - //DllExport - int fClose(FILE* filehandle); - //DllExport - size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); - //DllExport - size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle); + FILE* fOpen(const char* name, const char* mode); + int fClose(FILE* filehandle); + size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); + size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle); #if 0 /* (**) Note: - Pour dfinir une fonction externe, il faut procder ainsi: + Pour dfinir une fonction externe, il faut procder ainsi: - a) dfinir une routine pour la compilation - cette routine reois la liste des paramtres (sans valeurs) - et retourne soit un type de rsultat (CBotTyp... ou 0 = void) - soit un numro d'erreur - b) dfinir une routine pour l'excution - cette rourine reCoit la liste des paramtres (avec valeurs), - une variable pour stocker le rsultat (selon le type donn la compilation) + a) dfinir une routine pour la compilation + cette routine reois la liste des paramtres (sans valeurs) + et retourne soit un type de rsultat (CBotTyp... ou 0 = void) + soit un numro d'erreur + b) dfinir une routine pour l'excution + cette rourine reCoit la liste des paramtres (avec valeurs), + une variable pour stocker le rsultat (selon le type donn la compilation) - Par exemple, une routine qui calcule la moyenne d'une liste de paramtres */ + Par exemple, une routine qui calcule la moyenne d'une liste de paramtres */ -int cMoyenne(CBotVar* &pVar, CBotString& ClassName) +int cMoyenne(CBotVar* &pVar, CBotString& ClassName) { - if ( pVar == NULL ) return 6001; // il n'y a aucun paramtre ! + if ( pVar == NULL ) return 6001; // il n'y a aucun paramtre ! - while ( pVar != NULL ) - { - if ( pVar->GivType() > CBotTypDouble ) return 6002; // ce n'est pas un nombre - pVar = pVar -> GivNext(); - } + while ( pVar != NULL ) + { + if ( pVar->GivType() > CBotTypDouble ) return 6002; // ce n'est pas un nombre + pVar = pVar -> GivNext(); + } - return CBotTypFloat; // le type du rsultat pourrait dpendre des paramtres ! + return CBotTypFloat; // le type du rsultat pourrait dpendre des paramtres ! } bool rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) { - float total = 0; - int nb = 0; - while (pVar != NULL) - { - total += pVar->GivValFloat(); - pVar = pVar->GivNext(); - nb++; - } - pResult->SetValFloat(total/nb); // retourne la valeur moyenne - - return true; // opration totalement termine + float total = 0; + int nb = 0; + while (pVar != NULL) + { + total += pVar->GivValFloat(); + pVar = pVar->GivNext(); + nb++; + } + pResult->SetValFloat(total/nb); // retourne la valeur moyenne + + return true; // opration totalement termine } #endif @@ -614,256 +534,221 @@ bool rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) ///////////////////////////////////////////////////////////////////////////////// // Classe pour la gestion des variables -// les mthodes marques DllExport // peuvent tre utile l'exterieur du module // ( il n'est pour l'instant pas prvu de pouvoir crer ces objets en externe ) // rsultats pour GivInit() -#define IS_UNDEF 0 // variable indfinie -#define IS_DEF 1 // variable dfinie -#define IS_NAN 999 // variable dfinie comme tant not a number +#define IS_UNDEF 0 // variable indfinie +#define IS_DEF 1 // variable dfinie +#define IS_NAN 999 // variable dfinie comme tant not a number // type de variable SetPrivate / IsPrivate -#define PR_PUBLIC 0 // variable publique -#define PR_READ 1 // read only -#define PR_PROTECT 2 // protected (hritage) -#define PR_PRIVATE 3 // strictement prive +#define PR_PUBLIC 0 // variable publique +#define PR_READ 1 // read only +#define PR_PROTECT 2 // protected (hritage) +#define PR_PRIVATE 3 // strictement prive class CBotVar { protected: - CBotToken* m_token; // le token correspondant + CBotToken* m_token; // le token correspondant - CBotVar* m_next; // liste de variables - friend class CBotStack; - friend class CBotCStack; - friend class CBotInstrCall; - friend class CBotProgram; + CBotVar* m_next; // liste de variables + friend class CBotStack; + friend class CBotCStack; + friend class CBotInstrCall; + friend class CBotProgram; - CBotTypResult m_type; // type de valeur + CBotTypResult m_type; // type de valeur - int m_binit; // pas initialise ? - CBotVarClass* m_pMyThis; // ^lment this correspondant - void* m_pUserPtr; // ^donnes user s'il y a lieu - bool m_bStatic; // lment static (dans une classe) - int m_mPrivate; // lment public, protected ou private ? + int m_binit; // pas initialise ? + CBotVarClass* m_pMyThis; // ^lment this correspondant + void* m_pUserPtr; // ^donnes user s'il y a lieu + bool m_bStatic; // lment static (dans une classe) + int m_mPrivate; // lment public, protected ou private ? - CBotInstr* m_InitExpr; // expression pour le contenu initial - CBotInstr* m_LimExpr; // liste des limites pour un tableau - friend class CBotClass; - friend class CBotVarClass; - friend class CBotVarPointer; - friend class CBotVarArray; + CBotInstr* m_InitExpr; // expression pour le contenu initial + CBotInstr* m_LimExpr; // liste des limites pour un tableau + friend class CBotClass; + friend class CBotVarClass; + friend class CBotVarPointer; + friend class CBotVarArray; - long m_ident; // identificateur unique - static long m_identcpt; // compteur + long m_ident; // identificateur unique + static long m_identcpt; // compteur public: - CBotVar(); -virtual ~CBotVar( ); // destructeur + CBotVar(); +virtual ~CBotVar( ); // destructeur + + static + CBotVar* Create( const char* name, CBotTypResult type); + // idem partir du type complet + + static + CBotVar* Create( const char* name, CBotClass* pClass); + // idem pour une instance d'une classe connue + + static + CBotVar* Create( const CBotToken* name, int type ); + static + CBotVar* Create( const CBotToken* name, CBotTypResult type ); + + static + CBotVar* Create( const char* name, int type, CBotClass* pClass); + static + CBotVar* Create( CBotVar* pVar ); -/* //DllExport - static - CBotVar* Create( const char* name, int type, const char* ClassName = NULL); - // cre une variable selon son type,*/ - //DllExport - static - CBotVar* Create( const char* name, CBotTypResult type); - // idem partir du type complet + void SetUserPtr(void* pUser); + // associe un pointeur utilisateur une instance - //DllExport - static - CBotVar* Create( const char* name, CBotClass* pClass); - // idem pour une instance d'une classe connue + virtual void SetIdent(long UniqId); + // associe un identificateur unique une instance + // ( c'est l'utilisateur de s'assurer que l'id est unique) - static - CBotVar* Create( const CBotToken* name, int type ); - static - CBotVar* Create( const CBotToken* name, CBotTypResult type ); + void* GivUserPtr(); + // rend le pointeur associ la variable - static - CBotVar* Create( const char* name, int type, CBotClass* pClass); + CBotString GivName(); // le nom de la variable, s'il est connu + //////////////////////////////////////////////////////////////////////////////////// + void SetName(const char* name); // change le nom de la variable - static - CBotVar* Create( CBotVar* pVar ); + int GivType(int mode = 0); // rend le type de base (int) de la variable + //////////////////////////////////////////////////////////////////////////////////////// + CBotTypResult GivTypResult(int mode = 0); // rend le type complet de la variable - //DllExport - void SetUserPtr(void* pUser); - // associe un pointeur utilisateur une instance - //DllExport - virtual void SetIdent(long UniqId); - // associe un identificateur unique une instance - // ( c'est l'utilisateur de s'assurer que l'id est unique) + CBotToken* GivToken(); + void SetType(CBotTypResult& type); - //DllExport - void* GivUserPtr(); - // rend le pointeur associ la variable + void SetInit(int bInit); // met la variable dans l'tat IS_UNDEF, IS_DEF, IS_NAN - //DllExport - CBotString GivName(); // le nom de la variable, s'il est connu - //////////////////////////////////////////////////////////////////////////////////// - void SetName(const char* name); // change le nom de la variable + int GivInit(); // donne l'tat de la variable - //DllExport - int GivType(int mode = 0); // rend le type de base (int) de la variable - //////////////////////////////////////////////////////////////////////////////////////// + void SetStatic(bool bStatic); + bool IsStatic(); - //DllExport - CBotTypResult GivTypResult(int mode = 0); // rend le type complet de la variable + void SetPrivate(int mPrivate); + bool IsPrivate(int mode = PR_PROTECT); + int GivPrivate(); + virtual + void ConstructorSet(); - CBotToken* GivToken(); - void SetType(CBotTypResult& type); + void SetVal(CBotVar* var); // remprend une valeur - //DllExport - void SetInit(int bInit); // met la variable dans l'tat IS_UNDEF, IS_DEF, IS_NAN + virtual + CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) + virtual + CBotVar* GivItemRef(int nIdent); // idem partir du n ref - //DllExport - int GivInit(); // donne l'tat de la variable + virtual + CBotVar* GivItem(int row, bool bGrow = false); - //DllExport - void SetStatic(bool bStatic); - //DllExport - bool IsStatic(); + virtual + CBotVar* GivItemList(); // donne la liste des lments - //DllExport - void SetPrivate(int mPrivate); - //DllExport - bool IsPrivate(int mode = PR_PROTECT); - //DllExport - int GivPrivate(); + CBotVar* GivStaticVar(); // rend le pointeur la variable si elle est statique - virtual - void ConstructorSet(); + bool IsElemOfClass(const char* name); + // dit si l'lment appartient la classe "name" + // rend true si l'objet est d'une classe fille - void SetVal(CBotVar* var); // remprend une valeur + CBotVar* GivNext(); // prochaine variable dans la liste (paramtres) + //////////////////////////////////////////////////////////////////////////////////////////// - //DllExport - virtual - CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) - virtual - CBotVar* GivItemRef(int nIdent); // idem partir du n ref + void AddNext(CBotVar* pVar); // ajoute dans une liste - //DllExport - virtual - CBotVar* GivItem(int row, bool bGrow = false); + virtual + void Copy(CBotVar* pSrc, bool bName = true); // fait une copie de la variable - //DllExport - virtual - CBotVar* GivItemList(); // donne la liste des lments + virtual void SetValInt(int val, const char* name = NULL); + // initialise avec une valeur entire (#) + ///////////////////////////////////////////////////////////////////////////////// - //DllExport - CBotVar* GivStaticVar(); // rend le pointeur la variable si elle est statique + virtual void SetValFloat(float val); // initialise avec une valeur relle (#) + //////////////////////////////////////////////////////////////////////////////// - //DllExport - bool IsElemOfClass(const char* name); - // dit si l'lment appartient la classe "name" - // rend true si l'objet est d'une classe fille - - //DllExport - CBotVar* GivNext(); // prochaine variable dans la liste (paramtres) - //////////////////////////////////////////////////////////////////////////////////////////// + virtual void SetValString(const char* p);// initialise avec une valeur chane (#) + //////////////////////////////////////////////////////////////////////////////// - void AddNext(CBotVar* pVar); // ajoute dans une liste + virtual int GivValInt(); // demande la valeur entire (#) + //////////////////////////////////////////////////////////////////////// - virtual - void Copy(CBotVar* pSrc, bool bName = true); // fait une copie de la variable + virtual float GivValFloat(); // demande la valeur relle (#) + /////////////////////////////////////////////////////////////////////// - //DllExport - virtual void SetValInt(int val, const char* name = NULL); - // initialise avec une valeur entire (#) - ///////////////////////////////////////////////////////////////////////////////// + virtual + CBotString GivValString(); // demande la valeur chane (#) + /////////////////////////////////////////////////////////////////////// - //DllExport - virtual void SetValFloat(float val); // initialise avec une valeur relle (#) - //////////////////////////////////////////////////////////////////////////////// + virtual void SetClass(CBotClass* pClass); + virtual + CBotClass* GivClass(); - //DllExport - virtual void SetValString(const char* p);// initialise avec une valeur chane (#) - //////////////////////////////////////////////////////////////////////////////// + virtual void SetPointer(CBotVar* p); + virtual + CBotVarClass* GivPointer(); +// virtual void SetIndirection(CBotVar* pVar); - //DllExport - virtual int GivValInt(); // demande la valeur entire (#) - //////////////////////////////////////////////////////////////////////// + virtual void Add(CBotVar* left, CBotVar* right); // addition + virtual void Sub(CBotVar* left, CBotVar* right); // soustraction + virtual void Mul(CBotVar* left, CBotVar* right); // multiplication + virtual int Div(CBotVar* left, CBotVar* right); // division + virtual int Modulo(CBotVar* left, CBotVar* right); // reste de division + virtual void Power(CBotVar* left, CBotVar* right); // puissance - //DllExport - virtual float GivValFloat(); // demande la valeur relle (#) - /////////////////////////////////////////////////////////////////////// + virtual bool Lo(CBotVar* left, CBotVar* right); + virtual bool Hi(CBotVar* left, CBotVar* right); + virtual bool Ls(CBotVar* left, CBotVar* right); + virtual bool Hs(CBotVar* left, CBotVar* right); + virtual bool Eq(CBotVar* left, CBotVar* right); + virtual bool Ne(CBotVar* left, CBotVar* right); - virtual - CBotString GivValString(); // demande la valeur chane (#) - /////////////////////////////////////////////////////////////////////// + virtual void And(CBotVar* left, CBotVar* right); + virtual void Or(CBotVar* left, CBotVar* right); + virtual void XOr(CBotVar* left, CBotVar* right); + virtual void ASR(CBotVar* left, CBotVar* right); + virtual void SR(CBotVar* left, CBotVar* right); + virtual void SL(CBotVar* left, CBotVar* right); - virtual void SetClass(CBotClass* pClass); - virtual - CBotClass* GivClass(); + virtual void Neg(); + virtual void Not(); + virtual void Inc(); + virtual void Dec(); - virtual void SetPointer(CBotVar* p); - virtual - CBotVarClass* GivPointer(); -// virtual void SetIndirection(CBotVar* pVar); - virtual void Add(CBotVar* left, CBotVar* right); // addition - virtual void Sub(CBotVar* left, CBotVar* right); // soustraction - virtual void Mul(CBotVar* left, CBotVar* right); // multiplication - virtual int Div(CBotVar* left, CBotVar* right); // division - virtual int Modulo(CBotVar* left, CBotVar* right); // reste de division - virtual void Power(CBotVar* left, CBotVar* right); // puissance + virtual bool Save0State(FILE* pf); + virtual bool Save1State(FILE* pf); + static bool RestoreState(FILE* pf, CBotVar* &pVar); - virtual bool Lo(CBotVar* left, CBotVar* right); - virtual bool Hi(CBotVar* left, CBotVar* right); - virtual bool Ls(CBotVar* left, CBotVar* right); - virtual bool Hs(CBotVar* left, CBotVar* right); - virtual bool Eq(CBotVar* left, CBotVar* right); - virtual bool Ne(CBotVar* left, CBotVar* right); - - virtual void And(CBotVar* left, CBotVar* right); - virtual void Or(CBotVar* left, CBotVar* right); - virtual void XOr(CBotVar* left, CBotVar* right); - virtual void ASR(CBotVar* left, CBotVar* right); - virtual void SR(CBotVar* left, CBotVar* right); - virtual void SL(CBotVar* left, CBotVar* right); - - virtual void Neg(); - virtual void Not(); - virtual void Inc(); - virtual void Dec(); - - - virtual bool Save0State(FILE* pf); - virtual bool Save1State(FILE* pf); - static bool RestoreState(FILE* pf, CBotVar* &pVar); - - //DllExport - void debug(); - -// virtual -// CBotVar* GivMyThis(); - - //DllExport - virtual - void Maj(void* pUser = NULL, bool bContinue = true); - - void SetUniqNum(long n); - long GivUniqNum(); - static long NextUniqNum(); + void debug(); + +// virtual +// CBotVar* GivMyThis(); + + virtual + void Maj(void* pUser = NULL, bool bContinue = true); + + void SetUniqNum(long n); + long GivUniqNum(); + static long NextUniqNum(); }; /* NOTE (#) - les mthodes SetValInt() SetValFloat() et SetValString() - ne peuvent tes appelles qu'avec des objets respectivement entier, relle ou chane - toujours s'assurer du type de la variable avant d'appeller ces mthodes + les mthodes SetValInt() SetValFloat() et SetValString() + ne peuvent tes appelles qu'avec des objets respectivement entier, relle ou chane + toujours s'assurer du type de la variable avant d'appeller ces mthodes - if ( pVar->GivType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! + if ( pVar->GivType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! - les mthodes GivValInt(), GivValFloat() et GivValString() - font des conversions de valeur, - GivValString() fonctionne sur des nombres (rend la chane correspondante) - par contre il ne faut pas faire de GivValInt() avec une variable de type chane ! + les mthodes GivValInt(), GivValFloat() et GivValString() + font des conversions de valeur, + GivValString() fonctionne sur des nombres (rend la chane correspondante) + par contre il ne faut pas faire de GivValInt() avec une variable de type chane ! */ @@ -878,227 +763,205 @@ virtual ~CBotVar( ); // destructeur class CBotClass { private: - static - CBotClass* m_ExClass; // liste des classes existante un moment donn - CBotClass* m_ExNext; // pour cette liste gnrale - CBotClass* m_ExPrev; // pour cette liste gnrale + static + CBotClass* m_ExClass; // liste des classes existante un moment donn + CBotClass* m_ExNext; // pour cette liste gnrale + CBotClass* m_ExPrev; // pour cette liste gnrale private: - CBotClass* m_pParent; // classe parent - CBotString m_name; // nom de cette classe-ci - int m_nbVar; // nombre de variables dans la chane - CBotVar* m_pVar; // contenu de la classe - bool m_bIntrinsic; // classe intrinsque - CBotClass* m_next; // chaine les classe - CBotCallMethode* m_pCalls; // liste des mthodes dfinie en externe - CBotFunction* m_pMethod; // liste des mthodes compiles - void (*m_rMaj) ( CBotVar* pThis, void* pUser ); - friend class CBotVarClass; - int m_cptLock; // pour Lock / UnLock - int m_cptOne; // pour rentrance Lock - CBotProgram* m_ProgInLock[5];// processus en attente pour synchro + CBotClass* m_pParent; // classe parent + CBotString m_name; // nom de cette classe-ci + int m_nbVar; // nombre de variables dans la chane + CBotVar* m_pVar; // contenu de la classe + bool m_bIntrinsic; // classe intrinsque + CBotClass* m_next; // chaine les classe + CBotCallMethode* m_pCalls; // liste des mthodes dfinie en externe + CBotFunction* m_pMethod; // liste des mthodes compiles + void (*m_rMaj) ( CBotVar* pThis, void* pUser ); + friend class CBotVarClass; + int m_cptLock; // pour Lock / UnLock + int m_cptOne; // pour rentrance Lock + CBotProgram* m_ProgInLock[5];// processus en attente pour synchro public: - bool m_IsDef; // marque si est dfinie ou pas encore - - //DllExport - CBotClass( const char* name, - CBotClass* pParent, bool bIntrinsic = false ); // constructeur - // Ds qu'une classe est cre, elle est connue - // partout dans CBot - // le mode intrinsic donne une classe qui n'est pas gre par des pointeurs - - //DllExport - ~CBotClass( ); // destructeur - - //DllExport - bool AddFunction(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - // cet appel permet d'ajouter de manire externe (**) - // une nouvelle mthode utilisable par les objets de cette classe - - //DllExport - bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); - // dfini la routine qui sera appelle pour mettre jour les lements de la classe - - //DllExport - bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); - // ajoute un lment la classe -// //DllExport -// bool AddItem(CBotString name, CBotClass* pClass); - // idem pour des lments appartenant pClass - //DllExport - bool AddItem(CBotVar* pVar); - // idem en passant le pointeur une instance d'une variable - // l'objet est pris tel quel, il ne faut donc pas le dtruire - - - - // idem en donnant un lment de type CBotVar - void AddNext(CBotClass* pClass); - - //DllExport - CBotString GivName(); // rend le nom de la classe - //DllExport - CBotClass* GivParent(); // donne la classe pre (ou NULL) - - // dit si une classe est drive (Extends) d'une autre - // rend true aussi si les classes sont identiques - //DllExport - bool IsChildOf(CBotClass* pClass); - - static - CBotClass* Find(CBotToken* &pToken); // trouve une classe d'aprs son nom - - //DllExport - static - CBotClass* Find(const char* name); - - CBotVar* GivVar(); // rend la liste des variables - CBotVar* GivItem(const char* name); // l'une des variables selon son nom - CBotVar* GivItemRef(int nIdent); - - CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, - CBotCStack* pStack, long& nIdent); - - bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); - void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); - - // compile une classe dclare par l'utilisateur - static - CBotClass* Compile(CBotToken* &p, CBotCStack* pStack); - static - CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack); - - bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond); - - bool IsIntrinsic(); - void Purge(); - static - void Free(); - - //DllExport - static - bool SaveStaticState(FILE* pf); - - //DllExport - static - bool RestoreStaticState(FILE* pf); - - bool Lock(CBotProgram* p); - void Unlock(); - static - void FreeLock(CBotProgram* p); - - bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + bool m_IsDef; // marque si est dfinie ou pas encore + + CBotClass( const char* name, + CBotClass* pParent, bool bIntrinsic = false ); // constructeur + // Ds qu'une classe est cre, elle est connue + // partout dans CBot + // le mode intrinsic donne une classe qui n'est pas gre par des pointeurs + + ~CBotClass( ); // destructeur + + bool AddFunction(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); + // cet appel permet d'ajouter de manire externe (**) + // une nouvelle mthode utilisable par les objets de cette classe + + bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); + // dfini la routine qui sera appelle pour mettre jour les lements de la classe + + bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); + // ajoute un lment la classe +// bool AddItem(CBotString name, CBotClass* pClass); + // idem pour des lments appartenant pClass + bool AddItem(CBotVar* pVar); + // idem en passant le pointeur une instance d'une variable + // l'objet est pris tel quel, il ne faut donc pas le dtruire + + + + // idem en donnant un lment de type CBotVar + void AddNext(CBotClass* pClass); + + CBotString GivName(); // rend le nom de la classe + CBotClass* GivParent(); // donne la classe pre (ou NULL) + + // dit si une classe est drive (Extends) d'une autre + // rend true aussi si les classes sont identiques + bool IsChildOf(CBotClass* pClass); + + static + CBotClass* Find(CBotToken* &pToken); // trouve une classe d'aprs son nom + + static + CBotClass* Find(const char* name); + + CBotVar* GivVar(); // rend la liste des variables + CBotVar* GivItem(const char* name); // l'une des variables selon son nom + CBotVar* GivItemRef(int nIdent); + + CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, + CBotCStack* pStack, long& nIdent); + + bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); + void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); + + // compile une classe dclare par l'utilisateur + static + CBotClass* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack); + + bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond); + + bool IsIntrinsic(); + void Purge(); + static + void Free(); + + static + bool SaveStaticState(FILE* pf); + + static + bool RestoreStaticState(FILE* pf); + + bool Lock(CBotProgram* p); + void Unlock(); + static + void FreeLock(CBotProgram* p); + + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); }; -#define MAXDEFNUM 1000 // nombre limite des DefineNum +#define MAXDEFNUM 1000 // nombre limite des DefineNum ///////////////////////////////////////////////////////////////////////////////////// // gestion des jetons (tokens) -#define TokenTypKeyWord 1 // un mot clef du language (voir TokenKeyWord) -#define TokenTypNum 2 // un nombre -#define TokenTypString 3 // une chaine -#define TokenTypVar 4 // un nom de variable -#define TokenTypDef 5 // une valeur selon DefineNum +#define TokenTypKeyWord 1 // un mot clef du language (voir TokenKeyWord) +#define TokenTypNum 2 // un nombre +#define TokenTypString 3 // une chaine +#define TokenTypVar 4 // un nom de variable +#define TokenTypDef 5 // une valeur selon DefineNum -#define TokenKeyWord 2000 // les mots clefs du langage -#define TokenKeyDeclare 2100 // mots clefs pour dclarations (int, float,..) -#define TokenKeyVal 2200 // les mots reprsentant une "valeur" (true, false, null, nan) -#define TokenKeyOp 2300 // les oprateurs +#define TokenKeyWord 2000 // les mots clefs du langage +#define TokenKeyDeclare 2100 // mots clefs pour dclarations (int, float,..) +#define TokenKeyVal 2200 // les mots reprsentant une "valeur" (true, false, null, nan) +#define TokenKeyOp 2300 // les oprateurs class CBotToken { private: - static - CBotStringArray m_ListKeyWords; // liste des mots clefs du language - static - int m_ListIdKeyWords[200]; // les codes correspondants + static + CBotStringArray m_ListKeyWords; // liste des mots clefs du language + static + int m_ListIdKeyWords[200]; // les codes correspondants - static - CBotStringArray m_ListKeyDefine; // les noms dfinis par un DefineNum - static - long m_ListKeyNums[MAXDEFNUM]; // les valeurs associes + static + CBotStringArray m_ListKeyDefine; // les noms dfinis par un DefineNum + static + long m_ListKeyNums[MAXDEFNUM]; // les valeurs associes private: - CBotToken* m_next; // suivant dans la liste - CBotToken* m_prev; - int m_type; // type de Token - long m_IdKeyWord; // numro du mot clef si c'en est un - // ou valeur du "define" + CBotToken* m_next; // suivant dans la liste + CBotToken* m_prev; + int m_type; // type de Token + long m_IdKeyWord; // numro du mot clef si c'en est un + // ou valeur du "define" - CBotString m_Text; // mot trouv comme token - CBotString m_Sep; // sparateurs qui suivent + CBotString m_Text; // mot trouv comme token + CBotString m_Sep; // sparateurs qui suivent - int m_start; // position dans le texte d'origine (programme) - int m_end; // itou pour la fin du token + int m_start; // position dans le texte d'origine (programme) + int m_end; // itou pour la fin du token - static - int GivKeyWords(const char* w); // est-ce un mot clef ? - static - bool GivKeyDefNum(const char* w, CBotToken* &token); + static + int GivKeyWords(const char* w); // est-ce un mot clef ? + static + bool GivKeyDefNum(const char* w, CBotToken* &token); - static - void LoadKeyWords(); // fait la liste des mots clefs + static + void LoadKeyWords(); // fait la liste des mots clefs public: - CBotToken(); - CBotToken(const CBotToken* pSrc); - CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0); - CBotToken(const char* mot, const char* sep = NULL); - // constructeur - ~CBotToken(); // destructeur - - //DllExport - int GivType(); // rend le type du token - - //DllExport - CBotString& GivString(); // rend la chaine correspondant ce token - - //DllExport - CBotString& GivSep(); // rend le sparateur suivant le token - - //DllExport - int GivStart(); // position du dbut dans le texte - //DllExport - int GivEnd(); // position de fin dans le texte - - //DllExport - CBotToken* GivNext(); // rend le suivant dans la liste - //DllExport - CBotToken* GivPrev(); // rend le Prcdent dans la liste - - //DllExport - static - CBotToken* CompileTokens(const char* p, int& error); - // transforme tout le programme - //DllExport - static - void Delete(CBotToken* pToken); // libre la liste - - - // fonctions non utiles en export - static - bool DefineNum(const char* name, long val); - void SetString(const char* name); - - void SetPos(int start, int end); - long GivIdKey(); - void AddNext(CBotToken* p); // ajoute un token (une copie) - - static - CBotToken* NextToken(char* &program, int& error, bool first = false); - // trouve le prochain token - const CBotToken& - operator=(const CBotToken& src); - - static - void Free(); + CBotToken(); + CBotToken(const CBotToken* pSrc); + CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0); + CBotToken(const char* mot, const char* sep = NULL); + // constructeur + ~CBotToken(); // destructeur + + int GivType(); // rend le type du token + + CBotString& GivString(); // rend la chaine correspondant ce token + + CBotString& GivSep(); // rend le sparateur suivant le token + + int GivStart(); // position du dbut dans le texte + int GivEnd(); // position de fin dans le texte + + CBotToken* GivNext(); // rend le suivant dans la liste + CBotToken* GivPrev(); // rend le Prcdent dans la liste + + static + CBotToken* CompileTokens(const char* p, int& error); + // transforme tout le programme + static + void Delete(CBotToken* pToken); // libre la liste + + + // fonctions non utiles en export + static + bool DefineNum(const char* name, long val); + void SetString(const char* name); + + void SetPos(int start, int end); + long GivIdKey(); + void AddNext(CBotToken* p); // ajoute un token (une copie) + + static + CBotToken* NextToken(char* &program, int& error, bool first = false); + // trouve le prochain token + const CBotToken& + operator=(const CBotToken& src); + + static + void Free(); }; @@ -1111,64 +974,64 @@ public: // dfinie la classe globale CPoint // -------------------------------- - m_pClassPoint = new CBotClass("CPoint", NULL); - // ajoute le composant ".x" - m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); - // ajoute le composant ".y" - m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); - // le joueur peut alors utiliser les instructions - // CPoint position; position.x = 12; position.y = -13.6 + m_pClassPoint = new CBotClass("CPoint", NULL); + // ajoute le composant ".x" + m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); + // ajoute le composant ".y" + m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); + // le joueur peut alors utiliser les instructions + // CPoint position; position.x = 12; position.y = -13.6 // dfinie la classe CColobotObject // -------------------------------- // cette classe gre tous les objets dans le monde de COLOBOT // le programme utilisateur "main" appartient cette classe - m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); - // ajoute le composant ".position" - m_pClassObject->AddItem("position", m_pClassPoint); - // ajoute le composant ".type" - m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); - // ajoute une dfinition de constante - m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT quivalent la valeur 1 - // ajoute la routine FIND - m_pClassObject->AddFunction( rCompFind, rDoFind ); - // le joueur peut maintenant utiliser les instructions - // CColobotObject chose; chose = FIND( ROBOT ) + m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); + // ajoute le composant ".position" + m_pClassObject->AddItem("position", m_pClassPoint); + // ajoute le composant ".type" + m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); + // ajoute une dfinition de constante + m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT quivalent la valeur 1 + // ajoute la routine FIND + m_pClassObject->AddFunction( rCompFind, rDoFind ); + // le joueur peut maintenant utiliser les instructions + // CColobotObject chose; chose = FIND( ROBOT ) // dfinie la classe CColobotRobot drive de CColobotObject // --------------------------------------------------------- // les programmes "main" associs aux robots font partie de cette classe - m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); - // ajoute la routine GOTO - m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); - // le joueur peut maintenant faire - // GOTO( FIND ( ROBOT ) ); + m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); + // ajoute la routine GOTO + m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); + // le joueur peut maintenant faire + // GOTO( FIND ( ROBOT ) ); // cre une instance de la classe Robot // ------------------------------------ // par exemple un nouveau robot qui vient d'tre fabriqu - CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); + CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); // compile le programme main pour ce robot-l // ------------------------------------------ - CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); - if ( !m_pMonRobot->Compile( LeProgramme ) ) {gestion d'erreur...}; + CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); + if ( !m_pMonRobot->Compile( LeProgramme ) ) {gestion d'erreur...}; // construit une pile pour l'interprteur // -------------------------------------- - CBotStack* pStack = new CBotStack(NULL); + CBotStack* pStack = new CBotStack(NULL); // excute le programme main // ------------------------- - while( false = m_pMonRobot->Execute( "main", pStack )) - { - // programme suspendu - // on pourrait passer la main un autre (en sauvegardant pStack pour ce robot-l) - }; - // programme "main" termin ! + while( false = m_pMonRobot->Execute( "main", pStack )) + { + // programme suspendu + // on pourrait passer la main un autre (en sauvegardant pStack pour ce robot-l) + }; + // programme "main" termin ! @@ -1176,25 +1039,27 @@ public: // routine implmentant l'instruction GOTO( CPoint pos ) bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) { - if (pVar->GivType() != CBotTypeClass || - pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) - // le paramtre n'est pas de la bonne classe ? - // NB en fait ce contrle est dj fait par la routine pour la compilation + if (pVar->GivType() != CBotTypeClass || + pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) + // le paramtre n'est pas de la bonne classe ? + // NB en fait ce contrle est dj fait par la routine pour la compilation - m_PosToGo.Copy( pVar ); // garde la position atteindre (object type CBotVar) + m_PosToGo.Copy( pVar ); // garde la position atteindre (object type CBotVar) - // ou alors - CBotVar* temp; - temp = pVar->GivItem("x"); // trouve forcment pour un object de type "CPoint" - ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); - m_PosToGo.x = temp->GivValFloat(); + // ou alors + CBotVar* temp; + temp = pVar->GivItem("x"); // trouve forcment pour un object de type "CPoint" + ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); + m_PosToGo.x = temp->GivValFloat(); - temp = pVar->GivItem("y"); // trouve forcment pour un object de type "CPoint" - ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); - m_PosToGo.y = temp->GivValFloat(); + temp = pVar->GivItem("y"); // trouve forcment pour un object de type "CPoint" + ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); + m_PosToGo.y = temp->GivValFloat(); - return (m_CurentPos == m_PosToGo); // rend true si la position est atteinte - // rend false s'il faut patienter encore + return (m_CurentPos == m_PosToGo); // rend true si la position est atteinte + // rend false s'il faut patienter encore } #endif +#endif //_CBOTDLL_H_ + diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 77ed7d7..291a1e5 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -13,7 +13,12 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/.////////////////////////////////////////////////////////////////////// -// gestion de la pile (stack) + +/** + * \file CBotStack.cpp + * \brief Management of the stack + */ + #include "CBot.h" #include diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 5f35337..33e1d04 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -13,53 +13,145 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/.///////////////////////////////////////////////////// -// gestion de chaine -// bas sur le CString de MFC -// mais moins complet + +//strings management #include "CBot.h" #include #include #include -/// TODO need to be implemented to be able to load library -// HINSTANCE CBotString::m_hInstance = (HINSTANCE)LoadLibrary("libCbot.dll"); // how to retrieve it otherwise ?? +//Map is filled with id-string pars that are needed for CBot language parsing +const std::map CBotString::s_keywordString = +{ + {ID_IF, "if"}, + {ID_ELSE, "else"}, + {ID_WHILE, "while"}, + {ID_DO, "do"}, + {ID_FOR, "for"}, + {ID_BREAK, "break"}, + {ID_CONTINUE, "continue"}, + {ID_SWITCH, "switch"}, + {ID_CASE, "case"}, + {ID_DEFAULT, "default"}, + {ID_TRY, "try"}, + {ID_THROW, "throw"}, + {ID_CATCH, "catch"}, + {ID_FINALLY, "finally"}, + {ID_TXT_AND, "and"}, + {ID_TXT_OR, "or"}, + {ID_TXT_NOT, "not"}, + {ID_RETURN, "return"}, + {ID_CLASS, "class"}, + {ID_EXTENDS, "extends"}, + {ID_SYNCHO, "synchronized"}, + {ID_NEW, "new"}, + {ID_PUBLIC, "public"}, + {ID_EXTERN, "extern"}, + {ID_FINAL, "final"}, + {ID_STATIC, "static"}, + {ID_PROTECTED, "protected"}, + {ID_PRIVATE, "private"}, + {ID_REPEAT, "repeat"}, + {ID_DEBUGDD, "STARTDEBUGDD"}, + {ID_INT, "int"}, + {ID_FLOAT, "float"}, + {ID_BOOLEAN, "boolean"}, + {ID_STRING, "string"}, + {ID_VOID, "void"}, + {ID_BOOL, "bool"}, + {ID_TRUE, "true"}, + {ID_FALSE, "false"}, + {ID_NULL, "null"}, + {ID_NAN, "nan"}, + {ID_OPENPAR, "("}, + {ID_CLOSEPAR, ")"}, + {ID_OPBLK, "{"}, + {ID_CLBLK, "}"}, + {ID_SEP, "},"}, + {ID_COMMA, ","}, + {ID_DOTS, ":"}, + {ID_DOT, "."}, + {ID_OPBRK, "["}, + {ID_CLBRK, "]"}, + {ID_DBLDOTS, "::"}, + {ID_LOGIC, "?"}, + {ID_ADD, "+"}, + {ID_SUB, "-"}, + {ID_MUL, "*"}, + {ID_DIV, "/"}, + {ID_ASS, "="}, + {ID_ASSADD, "+="}, + {ID_ASSSUB, "-="}, + {ID_ASSMUL, "*="}, + {ID_ASSDIV, "/="}, + {ID_ASSOR, "|="}, + {ID_ASSAND, "&="}, + {ID_ASSXOR, "^="}, + {ID_ASSSL, "<<="}, + {ID_ASSSR, ">>>="}, + {ID_ASSASR, ">>="}, + {ID_SL, "<<"}, + {ID_SR, ">>"}, + {ID_ASR, ">>"}, + {ID_INC, "++"}, + {ID_DEC, "--"}, + {ID_LO, "<"}, + {ID_HI, ">"}, + {ID_LS, "<<"}, + {ID_HS, ">="}, + {ID_EQ, "=="}, + {ID_NE, "!="}, + {ID_AND, "&"}, + {ID_XOR, "^"}, + {ID_OR, "|"}, + {ID_LOG_AND, "&&"}, + {ID_LOG_OR, "||"}, + {ID_LOG_NOT, "!"}, + {ID_NOT, "~"}, + {ID_MODULO, "%"}, + {ID_POWER, "**"}, + {ID_ASSMODULO, "%="}, + {TX_UNDEF, "undefined"}, + {TX_NAN, "not a number"}, + {ID_SUPER, "super"} +}; CBotString::CBotString() { - m_ptr = NULL; // chaine vide - m_lg = 0; + m_ptr = NULL; + m_lg = 0; } CBotString::~CBotString() { - if (m_ptr != NULL) free(m_ptr); + free(m_ptr); //we can call free on null pointer as it's save } CBotString::CBotString(const char* p) { - m_lg = strlen( p ); + m_lg = strlen(p); - m_ptr = NULL; - if (m_lg>0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, p); - } + m_ptr = NULL; + if (m_lg>0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, p); + } } CBotString::CBotString(const CBotString& srcString) { - m_lg = srcString.m_lg; + m_lg = srcString.m_lg; - m_ptr = NULL; - if (m_lg>0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, srcString.m_ptr); - } + m_ptr = NULL; + if (m_lg>0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, srcString.m_ptr); + } } @@ -67,539 +159,533 @@ CBotString::CBotString(const CBotString& srcString) int CBotString::GivLength() { - if ( m_ptr == NULL ) return 0; - return strlen( m_ptr ); + if (m_ptr == NULL) return 0; + return strlen( m_ptr ); } CBotString CBotString::Left(int nCount) const { - char chaine[2000]; - - int i; - for (i = 0; i < m_lg && i < nCount && i < 1999; i++) - { - chaine[i] = m_ptr[i]; - } - chaine[i] = 0 ; + char chain[2000]; + + size_t i; + for (i = 0; i < m_lg && i < nCount && i < 1999; ++i) + { + chain[i] = m_ptr[i]; + } + chain[i] = 0 ; - return CBotString( chaine ); + return CBotString(chain); } CBotString CBotString::Right(int nCount) const { - char chaine[2000]; - - int i = m_lg - nCount; - if ( i < 0 ) i = 0; + char chain[2000]; + + int i = m_lg - nCount; + if ( i < 0 ) i = 0; - int j; - for ( j = 0 ; i < m_lg && i < 1999; i++) - { - chaine[j++] = m_ptr[i]; - } - chaine[j] = 0 ; + size_t j; + for (size_t j = 0 ; i < m_lg && i < 1999; ++i) + { + chain[j++] = m_ptr[i]; + } + chain[j] = 0 ; - return CBotString( chaine ); + return CBotString(chain); } CBotString CBotString::Mid(int nFirst, int nCount) const { - char chaine[2000]; - - int i; + char chain[2000]; - for ( i = nFirst; i < m_lg && i < 1999 && i <= nFirst + nCount; i++) - { - chaine[i] = m_ptr[i]; - } - chaine[i] = 0 ; + size_t i; + for (i = nFirst; i < m_lg && i < 1999 && i <= nFirst + nCount; ++i) + { + chain[i] = m_ptr[i]; + } + chain[i] = 0 ; - return CBotString( chaine ); + return CBotString(chain); } CBotString CBotString::Mid(int nFirst) const { - char chaine[2000]; - - int i; + char chain[2000]; - for ( i = nFirst; i < m_lg && i < 1999 ; i++) - { - chaine[i] = m_ptr[i]; - } - chaine[i] = 0 ; + size_t i; + for (i = nFirst; i < m_lg && i < 1999 ; ++i) + { + chain[i] = m_ptr[i]; + } + chain[i] = 0 ; - return CBotString( chaine ); + return CBotString(chain); } int CBotString::Find(const char c) { - int i; - for (i = 0; i < m_lg; i++) - { - if (m_ptr[i] == c) return i; - } - return -1; + for (size_t i = 0; i < m_lg; ++i) + { + if (m_ptr[i] == c) return i; + } + return -1; } int CBotString::Find(const char * lpsz) { - int i, j; - int l = strlen(lpsz); + int l = strlen(lpsz); - for (i = 0; i <= m_lg-l; i++) - { - for (j = 0; j < l; j++) - { - if (m_ptr[i+j] != lpsz[j]) goto bad; - } - return i; + for (size_t i = 0; i <= m_lg-l; ++i) + { + for (size_t j = 0; j < l; ++j) + { + if (m_ptr[i+j] != lpsz[j]) goto bad; + } + return i; bad:; - } - return -1; + } + return -1; } int CBotString::ReverseFind(const char c) { - int i; - for (i = m_lg-1; i >= 0; i--) - { - if (m_ptr[i] == c) return i; - } - return -1; + int i; + for (i = m_lg-1; i >= 0; --i) + { + if (m_ptr[i] == c) return i; + } + return -1; } int CBotString::ReverseFind(const char * lpsz) { - int i, j; - int l = strlen(lpsz); + int i, j; + int l = strlen(lpsz); - for (i = m_lg-l; i >= 0; i--) - { - for (j = 0; j < l; j++) - { - if (m_ptr[i+j] != lpsz[j]) goto bad; - } - return i; + for (i = m_lg-l; i >= 0; --i) + { + for (j = 0; j < l; ++j) + { + if (m_ptr[i+j] != lpsz[j]) goto bad; + } + return i; bad:; - } - return -1; + } + return -1; } CBotString CBotString::Mid(int start, int lg) { - CBotString res; - if (start >= m_lg) return res; + CBotString res; + if (start >= m_lg) return res; - if ( lg < 0 ) lg = m_lg - start; + if ( lg < 0 ) lg = m_lg - start; - char* p = (char*)malloc(m_lg+1); - strcpy(p, m_ptr+start); - p[lg] = 0; + char* p = (char*)malloc(m_lg+1); + strcpy(p, m_ptr+start); + p[lg] = 0; - res = p; - free(p); - return res; + res = p; + free(p); + return res; } void CBotString::MakeUpper() { - int i; - - for ( i = 0; i < m_lg && i < 1999 ; i++) - { - char c = m_ptr[i]; - if ( c >= 'a' && c <= 'z' ) m_ptr[i] = c - 'a' + 'A'; - } + for (size_t i = 0; i < m_lg && i < 1999 ; ++i) + { + char c = m_ptr[i]; + if ( c >= 'a' && c <= 'z' ) m_ptr[i] = c - 'a' + 'A'; + } } void CBotString::MakeLower() { - int i; - - for ( i = 0; i < m_lg && i < 1999 ; i++) - { - char c = m_ptr[i]; - if ( c >= 'A' && c <= 'Z' ) m_ptr[i] = c - 'A' + 'a'; - } + for (size_t i = 0; i < m_lg && i < 1999 ; ++i) + { + char c = m_ptr[i]; + if ( c >= 'A' && c <= 'Z' ) m_ptr[i] = c - 'A' + 'a'; + } } - - -#define MAXSTRING 256 - bool CBotString::LoadString(unsigned int id) { - char buffer[MAXSTRING]; - /// \TODO implement loading strings from resources. Figure out how to do it - // m_lg = ::LoadString( m_hInstance, id, buffer, MAXSTRING ); - - if (m_ptr != NULL) free(m_ptr); + const char * str = NULL; + str = MapIdToString((EID)id); + if (m_ptr != NULL) free(m_ptr); - m_ptr = NULL; - if (m_lg > 0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, buffer); - return true; - } - return false; + m_lg = strlen(str); + m_ptr = NULL; + if (m_lg > 0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, str); + return true; + } + return false; } - + const CBotString& CBotString::operator=(const CBotString& stringSrc) { - if (m_ptr != NULL) free(m_ptr); + free(m_ptr); + m_ptr = NULL; - m_lg = stringSrc.m_lg; - m_ptr = NULL; + m_lg = stringSrc.m_lg; - if (m_lg > 0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, stringSrc.m_ptr); - } + if (m_lg > 0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, stringSrc.m_ptr); + } - return *this; + return *this; } CBotString operator+(const CBotString& string, const char * lpsz) { - CBotString s ( string ); - s += lpsz; - return s; + CBotString s(string); + s += lpsz; + return s; } const CBotString& CBotString::operator+(const CBotString& stringSrc) { - char* p = (char*)malloc(m_lg+stringSrc.m_lg+1); + char* p = (char*)malloc(m_lg+stringSrc.m_lg+1); - strcpy(p, m_ptr); - char* pp = p + m_lg; - strcpy(pp, stringSrc.m_ptr); + strcpy(p, m_ptr); + char* pp = p + m_lg; + strcpy(pp, stringSrc.m_ptr); - if (m_ptr != NULL) free(m_ptr); - m_ptr = p; - m_lg += stringSrc.m_lg; + free(m_ptr); + m_ptr = p; + m_lg += stringSrc.m_lg; - return *this; + return *this; } const CBotString& CBotString::operator=(const char ch) { - if (m_ptr != NULL) free(m_ptr); + free(m_ptr); + + m_lg = 1; - m_lg = 1; + m_ptr = (char*)malloc(2); + m_ptr[0] = ch; + m_ptr[1] = 0; - m_ptr = (char*)malloc(2); - m_ptr[0] = ch; - m_ptr[1] = 0; - - return *this; + return *this; } const CBotString& CBotString::operator=(const char* pString) { - if (m_ptr != NULL) free(m_ptr); - m_ptr = NULL; + free(m_ptr); + m_ptr = NULL; + + if (pString != NULL) + { + m_lg = strlen(pString); - if ( pString != NULL ) - { - m_lg = strlen(pString); + if (m_lg != 0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, pString); + } + } - if (m_lg != 0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, pString); - } - } - - return *this; + return *this; } const CBotString& CBotString::operator+=(const char ch) { - char* p = (char*)malloc(m_lg+2); + char* p = (char*)malloc(m_lg+2); - if (m_ptr!=NULL) strcpy(p, m_ptr); - p[m_lg++] = ch; - p[m_lg] = 0; + if (m_ptr!=NULL) strcpy(p, m_ptr); + p[m_lg++] = ch; + p[m_lg] = 0; - if (m_ptr != NULL) free(m_ptr); + free(m_ptr); - m_ptr = p; + m_ptr = p; - return *this; + return *this; } const CBotString& CBotString::operator+=(const CBotString& str) { - char* p = (char*)malloc(m_lg+str.m_lg+1); + char* p = (char*)malloc(m_lg+str.m_lg+1); - strcpy(p, m_ptr); - char* pp = p + m_lg; - strcpy(pp, str.m_ptr); + strcpy(p, m_ptr); + char* pp = p + m_lg; + strcpy(pp, str.m_ptr); - m_lg = m_lg + str.m_lg; + m_lg = m_lg + str.m_lg; - if (m_ptr != NULL) free(m_ptr); + free(m_ptr); - m_ptr = p; + m_ptr = p; - return *this; + return *this; } bool CBotString::operator==(const CBotString& str) { - return Compare(str) == 0; + return Compare(str) == 0; } bool CBotString::operator==(const char* p) { - return Compare(p) == 0; + return Compare(p) == 0; } bool CBotString::operator!=(const CBotString& str) { - return Compare(str) != 0; + return Compare(str) != 0; } bool CBotString::operator!=(const char* p) { - return Compare(p) != 0; + return Compare(p) != 0; } bool CBotString::operator>(const CBotString& str) { - return Compare(str) > 0; + return Compare(str) > 0; } bool CBotString::operator>(const char* p) { - return Compare(p) > 0; + return Compare(p) > 0; } bool CBotString::operator>=(const CBotString& str) { - return Compare(str) >= 0; + return Compare(str) >= 0; } bool CBotString::operator>=(const char* p) { - return Compare(p) >= 0; + return Compare(p) >= 0; } bool CBotString::operator<(const CBotString& str) { - return Compare(str) < 0; + return Compare(str) < 0; } bool CBotString::operator<(const char* p) { - return Compare(p) < 0; + return Compare(p) < 0; } bool CBotString::operator<=(const CBotString& str) { - return Compare(str) <= 0; + return Compare(str) <= 0; } bool CBotString::operator<=(const char* p) { - return Compare(p) <= 0; + return Compare(p) <= 0; } bool CBotString::IsEmpty() const { - return (m_lg == 0); + return (m_lg == 0); } void CBotString::Empty() { - if (m_ptr != NULL) free(m_ptr); - m_ptr = NULL; - m_lg = 0; + free(m_ptr); + m_ptr = NULL; + m_lg = 0; } static char emptyString[] = {0}; CBotString::operator const char * () const { - if (this == NULL || m_ptr == NULL) return emptyString; - return m_ptr; + if (this == NULL || m_ptr == NULL) return emptyString; + return m_ptr; } int CBotString::Compare(const char * lpsz) const { - char* p = m_ptr; - if (lpsz == NULL) lpsz = emptyString; - if (m_ptr == NULL) p = emptyString; - return strcmp(p, lpsz); // wcscmp + char* p = m_ptr; + if (lpsz == NULL) lpsz = emptyString; + if (m_ptr == NULL) p = emptyString; + return strcmp(p, lpsz); // wcscmp } - +const char * CBotString::MapIdToString(EID id) +{ + if (s_keywordString.find(id) != s_keywordString.end()) + { + return s_keywordString.at(id); + } + else + { + return emptyString; + } +} /////////////////////////////////////////////////////////////////////////////////////////// -// tableaux de chaines +// arrays of strings CBotStringArray::CBotStringArray() { - m_pData = NULL; - m_nSize = m_nMaxSize = 0; + m_pData = NULL; + m_nSize = m_nMaxSize = 0; } CBotStringArray::~CBotStringArray() { - SetSize(0); // dtruit les donnes ! + SetSize(0); // destroys data ! } int CBotStringArray::GivSize() { - return m_nSize; + return m_nSize; } void CBotStringArray::Add(const CBotString& str) { - SetSize(m_nSize+1); + SetSize(m_nSize+1); - m_pData[m_nSize-1] = str; + m_pData[m_nSize-1] = str; } - /////////////////////////////////////////////////////////////////////// -// routines utilitaires +// utility routines static inline void ConstructElement(CBotString* pNewData) { - memset(pNewData, 0, sizeof(CBotString)); + memset(pNewData, 0, sizeof(CBotString)); } static inline void DestructElement(CBotString* pOldData) { - pOldData->~CBotString(); + pOldData->~CBotString(); } static inline void CopyElement(CBotString* pSrc, CBotString* pDest) { - *pSrc = *pDest; + *pSrc = *pDest; } static void ConstructElements(CBotString* pNewData, int nCount) { - while (nCount--) - { - ConstructElement(pNewData); - pNewData++; - } + while (nCount--) + { + ConstructElement(pNewData); + pNewData++; + } } static void DestructElements(CBotString* pOldData, int nCount) { - while (nCount--) - { - DestructElement(pOldData); - pOldData++; - } + while (nCount--) + { + DestructElement(pOldData); + pOldData++; + } } static void CopyElements(CBotString* pDest, CBotString* pSrc, int nCount) { - while (nCount--) - { - *pDest = *pSrc; - ++pDest; - ++pSrc; - } + while (nCount--) + { + *pDest = *pSrc; + ++pDest; + ++pSrc; + } } -// slect la taille du tableau +// set the array size void CBotStringArray::SetSize(int nNewSize) { - if (nNewSize == 0) - { - // shrink to nothing - - DestructElements(m_pData, m_nSize); - delete[] (unsigned char *)m_pData; - m_pData = NULL; - m_nSize = m_nMaxSize = 0; - } - else if (m_pData == NULL) - { - // create one with exact size - m_pData = (CBotString*) new unsigned char[nNewSize * sizeof(CBotString)]; - - ConstructElements(m_pData, nNewSize); - - m_nSize = m_nMaxSize = nNewSize; - } - else if (nNewSize <= m_nMaxSize) - { - // it fits - if (nNewSize > m_nSize) - { - // initialize the new elements - - ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); - - } - - else if (m_nSize > nNewSize) // destroy the old elements - DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); - - m_nSize = nNewSize; - } - else - { - // otherwise, grow array - int nGrowBy; - { - // heuristically determine growth when nGrowBy == 0 - // (this avoids heap fragmentation in many situations) - nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); - } - int nNewMax; - if (nNewSize < m_nMaxSize + nGrowBy) - nNewMax = m_nMaxSize + nGrowBy; // granularity - else - nNewMax = nNewSize; // no slush - - CBotString* pNewData = (CBotString*) new unsigned char[nNewMax * sizeof(CBotString)]; - - // copy new data from old - memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); - - // construct remaining elements - ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); - - - // Ret rid of old stuff (note: no destructors called) - delete[] (unsigned char *)m_pData; - m_pData = pNewData; - m_nSize = nNewSize; - m_nMaxSize = nNewMax; - } + if (nNewSize == 0) + { + // shrink to nothing + + DestructElements(m_pData, m_nSize); + delete[] (unsigned char *)m_pData; + m_pData = NULL; + m_nSize = m_nMaxSize = 0; + } + else if (m_pData == NULL) + { + // create one with exact size + m_pData = (CBotString*) new unsigned char[nNewSize * sizeof(CBotString)]; + + ConstructElements(m_pData, nNewSize); + + m_nSize = m_nMaxSize = nNewSize; + } + else if (nNewSize <= m_nMaxSize) + { + // it fits + if (nNewSize > m_nSize) + { + // initialize the new elements + + ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); + + } + + else if (m_nSize > nNewSize) // destroy the old elements + DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); + + m_nSize = nNewSize; + } + else + { + // otherwise, grow array + int nGrowBy; + { + // heuristically determine growth when nGrowBy == 0 + // (this avoids heap fragmentation in many situations) + nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); + } + int nNewMax; + if (nNewSize < m_nMaxSize + nGrowBy) + nNewMax = m_nMaxSize + nGrowBy; // granularity + else + nNewMax = nNewSize; // no slush + + CBotString* pNewData = (CBotString*) new unsigned char[nNewMax * sizeof(CBotString)]; + + // copy new data from old + memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); + + // construct remaining elements + ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); + + + // Get rid of old stuff (note: no destructors called) + delete[] (unsigned char *)m_pData; + m_pData = pNewData; + m_nSize = nNewSize; + m_nMaxSize = nNewMax; + } } CBotString& CBotStringArray::operator[](int nIndex) { - return ElementAt(nIndex); + return ElementAt(nIndex); } CBotString& CBotStringArray::ElementAt(int nIndex) { - return m_pData[nIndex]; + return m_pData[nIndex]; } - - diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index 1b6392c..03a5337 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -464,6 +464,7 @@ bool CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) // reprend la liste des mots clefs dans les ressources +/// \todo Fixme Figure out how this should work. void CBotToken::LoadKeyWords() { CBotString s; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 9933e9c..409ef3b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -10,7 +10,6 @@ CBotToken.cpp CBotTwoOpExpr.cpp CBotVar.cpp CBotWhile.cpp -CBot.rc ) add_library(CBot SHARED ${SOURCES}) diff --git a/src/CBot/resource.h b/src/CBot/resource.h index 7e57d32..f82fb9c 100644 --- a/src/CBot/resource.h +++ b/src/CBot/resource.h @@ -15,98 +15,105 @@ // * along with this program. If not, see http://www.gnu.org/licenses/.//{{NO_DEPENDENCIES}} // Microsoft Developer Studio generated include file. // Used by CBot.rc -// -#define ID_KEYWORDS 2000 -#define ID_IF 2000 -#define ID_ELSE 2001 -#define ID_WHILE 2002 -#define ID_DO 2003 -#define ID_FOR 2004 -#define ID_BREAK 2005 -#define ID_CONTINUE 2006 -#define ID_SWITCH 2007 -#define ID_CASE 2008 -#define ID_DEFAULT 2009 -#define ID_TRY 2010 -#define ID_THROW 2011 -#define ID_CATCH 2012 -#define ID_FINALLY 2013 -#define ID_TXT_AND 2014 -#define ID_TXT_OR 2015 -#define ID_TXT_NOT 2016 -#define ID_RETURN 2017 -#define ID_CLASS 2018 -#define ID_EXTENDS 2019 -#define ID_SYNCHO 2020 -#define ID_NEW 2021 -#define ID_PUBLIC 2022 -#define ID_EXTERN 2023 -#define ID_FINAL 2024 -#define ID_STATIC 2025 -#define ID_PROTECTED 2026 -#define ID_PRIVATE 2027 -#define ID_REPEAT 2028 -#define ID_DEBUGDD 2099 -#define ID_INT 2100 -#define ID_FLOAT 2101 -#define ID_BOOLEAN 2102 -#define ID_STRING 2103 -#define ID_VOID 2104 -#define ID_BOOL 2105 -#define ID_TRUE 2200 -#define ID_FALSE 2201 -#define ID_NULL 2202 -#define ID_NAN 2203 -#define ID_OPENPAR 2300 -#define ID_CLOSEPAR 2301 -#define ID_OPBLK 2302 -#define ID_CLBLK 2303 -#define ID_SEP 2304 -#define ID_COMMA 2305 -#define ID_DOTS 2306 -#define ID_DOT 2307 -#define ID_OPBRK 2308 -#define ID_CLBRK 2309 -#define ID_DBLDOTS 2310 -#define ID_LOGIC 2311 -#define ID_ADD 2312 -#define ID_SUB 2313 -#define ID_MUL 2314 -#define ID_DIV 2315 -#define ID_ASS 2316 -#define ID_ASSADD 2317 -#define ID_ASSSUB 2318 -#define ID_ASSMUL 2319 -#define ID_ASSDIV 2320 -#define ID_ASSOR 2321 -#define ID_ASSAND 2322 -#define ID_ASSXOR 2323 -#define ID_ASSSL 2324 -#define ID_ASSSR 2325 -#define ID_ASSASR 2326 -#define ID_SL 2327 -#define ID_SR 2328 -#define ID_ASR 2329 -#define ID_INC 2330 -#define ID_DEC 2331 -#define ID_LO 2332 -#define ID_HI 2333 -#define ID_LS 2334 -#define ID_HS 2335 -#define ID_EQ 2336 -#define ID_NE 2337 -#define ID_AND 2338 -#define ID_XOR 2339 -#define ID_OR 2340 -#define ID_LOG_AND 2341 -#define ID_LOG_OR 2342 -#define ID_LOG_NOT 2343 -#define ID_NOT 2344 -#define ID_MODULO 2345 -#define ID_POWER 2346 -#define ID_ASSMODULO 2347 -#define TX_UNDEF 4000 -#define TX_NAN 4001 +#ifndef _RESOURCE_H_ +#define _RESOURCE_H_ + +enum EID +{ + ID_IF = 2000, + ID_ELSE, + ID_WHILE, + ID_DO, + ID_FOR, + ID_BREAK, + ID_CONTINUE, + ID_SWITCH, + ID_CASE, + ID_DEFAULT, + ID_TRY, + ID_THROW, + ID_CATCH, + ID_FINALLY, + ID_TXT_AND, + ID_TXT_OR, + ID_TXT_NOT, + ID_RETURN, + ID_CLASS, + ID_EXTENDS, + ID_SYNCHO, + ID_NEW, + ID_PUBLIC, + ID_EXTERN, + ID_FINAL, + ID_STATIC, + ID_PROTECTED, + ID_PRIVATE, + ID_REPEAT, + ID_DEBUGDD, + ID_INT, + ID_FLOAT, + ID_BOOLEAN, + ID_STRING, + ID_VOID, + ID_BOOL, + + ID_TRUE = 2200, + ID_FALSE, + ID_NULL, + ID_NAN, + + ID_OPENPAR = 2300, + ID_CLOSEPAR, + ID_OPBLK, + ID_CLBLK, + ID_SEP, + ID_COMMA, + ID_DOTS, + ID_DOT, + ID_OPBRK, + ID_CLBRK, + ID_DBLDOTS, + ID_LOGIC, + ID_ADD, + ID_SUB, + ID_MUL, + ID_DIV, + ID_ASS, + ID_ASSADD, + ID_ASSSUB, + ID_ASSMUL, + ID_ASSDIV, + ID_ASSOR, + ID_ASSAND, + ID_ASSXOR, + ID_ASSSL, + ID_ASSSR, + ID_ASSASR, + ID_SL, + ID_SR, + ID_ASR, + ID_INC, + ID_DEC, + ID_LO, + ID_HI, + ID_LS, + ID_HS, + ID_EQ, + ID_NE, + ID_AND, + ID_XOR, + ID_OR, + ID_LOG_AND, + ID_LOG_OR, + ID_LOG_NOT, + ID_NOT, + ID_MODULO, + ID_POWER, + ID_ASSMODULO, + TX_UNDEF = 4000, + TX_NAN, + ID_SUPER = 6000 +}; #define TX_OPENPAR 5000 #define TX_CLOSEPAR 5001 #define TX_NOTBOOL 5002 @@ -166,15 +173,6 @@ #define TX_NOTOPEN 6013 #define TX_ERRREAD 6014 #define TX_ERRWRITE 6015 -#define ID_SUPER 62020 -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 101 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1000 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif +#endif //_RESOURCE_H_ + -- cgit v1.2.3-1-g7c22 From 2383a42347176aa04fc8a67a83ffa9def4c706de Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 11 Jul 2012 20:50:42 +0200 Subject: Rewritten model loading - written new implementation of CModelFile (old CModFile) - added stringutils and ioutils in src/common - removed old CModel (model viewer) --- src/CMakeLists.txt | 4 +- src/common/ioutils.h | 81 +++ src/common/stringutils.cpp | 149 +++++ src/common/stringutils.h | 77 +++ src/graphics/common/model.cpp | 23 - src/graphics/common/model.h | 141 ----- src/graphics/common/modelfile.cpp | 815 ++++++++++++++++++++++++++++ src/graphics/common/modelfile.h | 118 ++++ src/graphics/common/modfile.cpp | 23 - src/graphics/common/modfile.h | 114 ---- src/graphics/common/test/CMakeLists.txt | 7 + src/graphics/common/test/modelfile_test.cpp | 48 ++ src/graphics/common/vertex.h | 8 + 13 files changed, 1305 insertions(+), 303 deletions(-) create mode 100644 src/common/ioutils.h create mode 100644 src/common/stringutils.cpp create mode 100644 src/common/stringutils.h delete mode 100644 src/graphics/common/model.cpp delete mode 100644 src/graphics/common/model.h create mode 100644 src/graphics/common/modelfile.cpp create mode 100644 src/graphics/common/modelfile.h delete mode 100644 src/graphics/common/modfile.cpp delete mode 100644 src/graphics/common/modfile.h create mode 100644 src/graphics/common/test/CMakeLists.txt create mode 100644 src/graphics/common/test/modelfile_test.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fada14c..7f52e8d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,7 @@ common/iman.cpp # common/modfile.cpp # common/profile.cpp # common/restext.cpp +common/stringutils.cpp graphics/common/camera.cpp graphics/common/cloud.cpp graphics/common/color.cpp @@ -45,8 +46,7 @@ graphics/common/device.cpp graphics/common/engine.cpp graphics/common/light.cpp graphics/common/lightning.cpp -graphics/common/model.cpp -graphics/common/modfile.cpp +graphics/common/modelfile.cpp graphics/common/particle.cpp graphics/common/planet.cpp graphics/common/pyro.cpp diff --git a/src/common/ioutils.h b/src/common/ioutils.h new file mode 100644 index 0000000..f17c961 --- /dev/null +++ b/src/common/ioutils.h @@ -0,0 +1,81 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// ioutils.h + +#pragma once + + +#include + + +namespace IOUtils { + +//! Writes a binary number to output stream +/** + \c T is a numeric type (int, unsigned int, etc.) + \c N is number of bytes + Write order is little-endian */ +template +void WriteBinary(T value, std::ostream &ostr) +{ + for (int i = 0; i < N; ++i) + { + unsigned char byte = (value >> (i*8)) & 0xFF; + ostr.write((char*)&byte, 1); + } +} + +//! Reads a binary number from input stream +/** + \c T is a numeric type (int, unsigned int, etc.) + \c N is number of bytes + Read order is little-endian */ +template +T ReadBinary(std::istream &istr) +{ + T value = 0; + for (int i = 0; i < N; ++i) + { + unsigned char byte = 0; + istr.read((char*)&byte, 1); + value |= byte << (i*8); + } + return value; +} + +//! Writes a binary 32-bit float to output stream +/** + Write order is little-endian + NOTE: code is probably not portable as there are platforms with other float representations. */ +void WriteBinaryFloat(float value, std::ostream &ostr) +{ + unsigned int iValue = *( (unsigned int*)( (void*)(&value) ) ); + IOUtils::WriteBinary<4, unsigned int>(iValue, ostr); +} + +//! Reads a binary 32-bit float from input stream +/** + Read order is little-endian + NOTE: code is probably not portable as there are platforms with other float representations. */ +float ReadBinaryFloat(std::istream &istr) +{ + unsigned int iValue = IOUtils::ReadBinary<4, unsigned int>(istr); + float result = *( (float*)( (void*)(&iValue) ) ); + return result; +} + +}; // namespace IOUtils diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp new file mode 100644 index 0000000..9a7aa61 --- /dev/null +++ b/src/common/stringutils.cpp @@ -0,0 +1,149 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// stringutils.cpp + +#include "stringutils.h" + + +std::string StrUtils::Replace(const std::string &str, const std::string &oldStr, const std::string &newStr) +{ + std::string result = str; + size_t pos = 0; + while ((pos = str.find(oldStr, pos)) != std::string::npos) + { + result.replace(pos, oldStr.length(), newStr); + pos += newStr.length(); + } + return result; +} + +std::string StrUtils::UnicodeCharToUtf8(unsigned int ch) +{ + std::string result; + if (ch < 0x0080) + { + result += (char)(ch); + } + else if (ch < 0x0800) + { + char ch1 = 0xC0 | ((ch & 0x07C0) >> 6); + char ch2 = 0x80 | (ch & 0x3F); + result += ch1; + result += ch2; + } + else + { + char ch1 = 0xE0 | ((ch & 0xF000) >> 12); + char ch2 = 0x80 | ((ch & 0x07C0) >> 6); + char ch3 = 0x80 | (ch & 0x3F); + result += ch1; + result += ch2; + result += ch3; + } + return result; +} + +std::string StrUtils::UnicodeStringToUtf8(const std::wstring &str) +{ + std::string result; + for (unsigned int i = 0; i < str.size(); ++i) + result += StrUtils::UnicodeCharToUtf8((unsigned int)str[i]); + + return result; +} + +unsigned int StrUtils::Utf8CharToUnicode(const std::string &ch) +{ + if (ch.empty()) + return 0; + + unsigned int result = 0; + if ((ch[0] & 0x80) == 0) + { + if (ch.size() == 1) + result = (unsigned int)ch[0]; + } + else if ((ch[0] & 0xC0) == 0xC0) + { + if (ch.size() == 2) + { + unsigned int ch1 = (ch[0] & 0x1F) << 6; + unsigned int ch2 = (ch[1] & 0x3F); + result = ch1 | ch2; + } + } + else + { + if (ch.size() == 3) + { + unsigned int ch1 = (ch[0] & 0xF0) << 12; + unsigned int ch2 = (ch[1] & 0xC0) << 6; + unsigned int ch3 = (ch[2] & 0xC0); + result = ch1 | ch2 | ch3; + } + } + + return result; +} + +std::wstring StrUtils::Utf8StringToUnicode(const std::string &str) +{ + std::wstring result; + unsigned int pos = 0; + while (pos < str.size()) + { + int len = StrUtils::Utf8CharSizeAt(str, pos); + if (len == 0) + break; + + std::string ch = str.substr(pos, len); + result += (wchar_t)(StrUtils::Utf8CharToUnicode(ch)); + pos += len; + } + return result; +} + +int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos) +{ + if (pos >= str.size()) + return 0; + + if ((str[pos] & 0x80) == 0) + return 1; + else if ((str[pos] & 0xC0) == 0xC0) + return 2; + else + return 3; + + return 0; +} + +size_t StrUtils::Utf8StringLength(const std::string &str) +{ + size_t result = 0; + for (unsigned int i = 0; i < str.size(); ++i) + { + char ch = str[i]; + if ((ch & 0x80) == 0) + ++result; + else if ((ch & 0xC0) == 0xC0) + result += 2; + else + result += 3; + } + return result; +} diff --git a/src/common/stringutils.h b/src/common/stringutils.h new file mode 100644 index 0000000..a0cae70 --- /dev/null +++ b/src/common/stringutils.h @@ -0,0 +1,77 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// stringutils.h + +#pragma once + + +#include +#include + +namespace StrUtils { + +//! Converts a value to string +/** If given, \a ok is set to true/false on success/failure. + Warning: To avoid unnecessary problems, *always* give full template qualifier e.g. ToString\ */ +template +std::string ToString(T value, bool *ok = NULL) +{ + std::ostringstream s; + s << value; + if (ok != NULL) + *ok = !s.fail(); + return s.str(); +} + +//! Converts a value to string +/** If given, \a ok is set to true/false on success/failure. + Warning: To avoid unnecessary problems, *always* give full template qualifier e.g. FromString\ */ +template +T FromString(const std::string &str, bool *ok = NULL) +{ + std::istringstream s; + s.str(str); + T value; + s >> value; + if (ok != NULL) + *ok = !s.fail(); + return value; +} + +//! Returns a string with every occurence of \a oldStr in \a str replaced to \a newStr +std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr); + + +//! Converts a wide Unicode char to a single UTF-8 encoded char +std::string UnicodeCharToUtf8(unsigned int ch); + +//! Converts a wide Unicode string to a UTF-8 encoded string +std::string UnicodeStringToUtf8(const std::wstring &str); + +//! Converts a UTF-8 encoded single character to wide Unicode char +unsigned int Utf8CharToUnicode(const std::string &ch); + +//! Converts a UTF-8 encoded string to wide Unicode string +std::wstring Utf8StringToUnicode(const std::string &str); + +//! Returns the size in bytes of UTF-8 character at given \a pos in a UTF-8 \a str +int Utf8CharSizeAt(const std::string &str, unsigned int pos); + +//! Returns the length in characters of UTF-8 string \a str +size_t Utf8StringLength(const std::string &str); + +}; // namespace StrUtil diff --git a/src/graphics/common/model.cpp b/src/graphics/common/model.cpp deleted file mode 100644 index c415fb8..0000000 --- a/src/graphics/common/model.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// model.cpp - -#include "graphics/common/model.h" - - -// TODO implementation diff --git a/src/graphics/common/model.h b/src/graphics/common/model.h deleted file mode 100644 index e8a5f19..0000000 --- a/src/graphics/common/model.h +++ /dev/null @@ -1,141 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// model.h - -#pragma once - -#include "engine.h" -#include "common/event.h" -#include "modfile.h" -#include "vertex.h" -#include "math/point.h" - - -class CInstanceManager; -class CModFile; -class CInterface; - - -namespace Gfx { - -class CEngine; - - -class CModel { - public: - CModel(CInstanceManager* iMan); - ~CModel(); - - void StartUserAction(); - void StopUserAction(); - - bool EventProcess(const Event &event); - - void InitView(); - void InitViewFromSelect(); - void UpdateView(); - void ViewMove(const Event &event, float speed); - - protected: - bool EventFrame(const Event &event); - bool GetVertex(int rank, Gfx::VertexTex2 &vertex); - bool SetVertex(int rank, Gfx::VertexTex2 &vertex); - Math::Vector RetSelectCDG(); - Math::Vector RetSelectNormal(); - void SmoothSelect(); - void PlaneSelect(); - void ColorSelect(); - void StateSelect(); - void MoveSelect(Math::Vector move); - void OperSelect(Math::Vector move, char oper); - void ReadScript(char *filename); - void BBoxCompute(Math::Vector &min, Math::Vector &max); - bool IsMappingSelectPlausible(Gfx::Mapping D3Dmode); - void MappingSelect(int mode, int rotate, bool bMirrorX, bool bMirrorY, Math::Point ti, Math::Point ts, char *texName); - void MappingSelectSpherical(int mode, int rotate, bool bMirrorX, bool bMirrorY, Math::Point ti, Math::Point ts, char *texName); - Math::Vector RetMappingCenter(Math::Vector pos, Math::Vector min); - void MappingSelectCylindrical(int mode, int rotate, bool bMirrorX, bool bMirrorY, Math::Point ti, Math::Point ts, char *texName); - void MappingSelectFace(int mode, int rotate, bool bMirrorX, bool bMirrorY, Math::Point ti, Math::Point ts, char *texName); - void MappingSelect2(int texNum2, int subdiv, int offsetU, int offsetV, bool bMirrorX, bool bMirrorY); - void MappingSelectPlane2(int mode, bool bMirrorX, bool bMirrorY); - void MappingSelectSpherical2(bool bMirrorX, bool bMirrorY); - void MappingSelectMagic2(bool bMirrorX, bool bMirrorY); - int SearchNext(int rank, int step); - int SearchSamePlane(int first, int step); - void CurrentSearchNext(int step, bool bControl); - void CurrentInit(); - void CurrentSelect(bool bSelect); - void DeselectAll(); - void SelectAll(); - void SelectZone(int first, int last); - void SelectTerm(); - void DefaultSelect(); - void SelectDelete(); - void Compress(); - void MinMaxSelect(); - void MinMaxChange(); - void UpdateInfoText(); - int* RetTextureTable(); - void TexturePartUpdate(); - void TextureRankChange(int step); - void TexturePartChange(int step); - void PutTextureValues(); - void GetTextureValues(); - void GetModelName(char *buffer); - void GetDXFName(char *buffer); - void GetScriptName(char *buffer); - bool IsEditFocus(); - - protected: - CInstanceManager* m_iMan; - Gfx::CEngine* m_engine; - CModFile* m_modFile; - CInterface* m_interface; - - float m_time; - ModelTriangle* m_triangleTable; - int m_triangleSel1; - int m_triangleSel2; - int m_mode; - int m_textureMode; - int m_textureRotate; - bool m_bTextureMirrorX; - bool m_bTextureMirrorY; - Math::Point m_textureInf; - Math::Point m_textureSup; - int m_texturePart; - int m_textureRank; - char m_textureName[20]; - bool m_bDisplayTransparent; - bool m_bDisplayOnlySelection; - float m_viewHeight; - float m_viewDist; - float m_viewAngleH; - float m_viewAngleV; - int m_color; - int m_state; - int m_secondTexNum; - int m_secondSubdiv; - int m_secondOffsetU; - int m_secondOffsetV; - char m_oper; - float m_min; - float m_max; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/modelfile.cpp b/src/graphics/common/modelfile.cpp new file mode 100644 index 0000000..8e509fe --- /dev/null +++ b/src/graphics/common/modelfile.cpp @@ -0,0 +1,815 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// modelfile.cpp (aka modfile.cpp) + +#include "graphics/common/modelfile.h" + +#include "common/iman.h" +#include "common/ioutils.h" +#include "common/stringutils.h" +#include "math/geometry.h" + +#include + +#include + + +//! How big the triangle vector is by default +const int TRIANGLE_PREALLOCATE_COUNT = 2000; + +/** + \struct ModelHeader + \brief Header info for model file + */ +struct ModelHeader +{ + //! Revision number + int revision; + //! Version number + int version; + //! Total number of vertices + int totalVertices; + //! Reserved area + int reserved[10]; + + ModelHeader() + { + memset(this, 0, sizeof(*this)); + } +}; + + +struct OldModelTriangle1 +{ + char used; + char selected; + Gfx::Vertex p1; + Gfx::Vertex p2; + Gfx::Vertex p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + + OldModelTriangle1() + { + memset(this, 0, sizeof(*this)); + } +}; + +struct OldModelTriangle2 +{ + char used; + char selected; + Gfx::Vertex p1; + Gfx::Vertex p2; + Gfx::Vertex p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + long state; + short reserved1; + short reserved2; + short reserved3; + short reserved4; + OldModelTriangle2() + { + memset(this, 0, sizeof(*this)); + } +}; + + +struct NewModelTriangle +{ + char used; + char selected; + Gfx::VertexTex2 p1; + Gfx::VertexTex2 p2; + Gfx::VertexTex2 p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + long state; + short texNum2; + short reserved2; + short reserved3; + short reserved4; + + NewModelTriangle() + { + memset(this, 0, sizeof(*this)); + } +}; + + +Gfx::Vertex ReadBinaryVertex(std::istream &stream) +{ + Gfx::Vertex result; + + result.coord.x = IOUtils::ReadBinaryFloat(stream); + result.coord.y = IOUtils::ReadBinaryFloat(stream); + result.coord.z = IOUtils::ReadBinaryFloat(stream); + result.normal.x = IOUtils::ReadBinaryFloat(stream); + result.normal.y = IOUtils::ReadBinaryFloat(stream); + result.normal.z = IOUtils::ReadBinaryFloat(stream); + result.texCoord.x = IOUtils::ReadBinaryFloat(stream); + result.texCoord.y = IOUtils::ReadBinaryFloat(stream); + + return result; +} + +void WriteBinaryVertex(Gfx::Vertex vertex, std::ostream &stream) +{ + IOUtils::WriteBinaryFloat(vertex.coord.x, stream); + IOUtils::WriteBinaryFloat(vertex.coord.y, stream); + IOUtils::WriteBinaryFloat(vertex.coord.z, stream); + IOUtils::WriteBinaryFloat(vertex.normal.x, stream); + IOUtils::WriteBinaryFloat(vertex.normal.y, stream); + IOUtils::WriteBinaryFloat(vertex.normal.z, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); +} + +Gfx::VertexTex2 ReadBinaryVertexTex2(std::istream &stream) +{ + Gfx::VertexTex2 result; + + result.coord.x = IOUtils::ReadBinaryFloat(stream); + result.coord.y = IOUtils::ReadBinaryFloat(stream); + result.coord.z = IOUtils::ReadBinaryFloat(stream); + result.normal.x = IOUtils::ReadBinaryFloat(stream); + result.normal.y = IOUtils::ReadBinaryFloat(stream); + result.normal.z = IOUtils::ReadBinaryFloat(stream); + result.texCoord.x = IOUtils::ReadBinaryFloat(stream); + result.texCoord.y = IOUtils::ReadBinaryFloat(stream); + result.texCoord2.x = IOUtils::ReadBinaryFloat(stream); + result.texCoord2.y = IOUtils::ReadBinaryFloat(stream); + + return result; +} + +void WriteBinaryVertexTex2(Gfx::VertexTex2 vertex, std::ostream &stream) +{ + IOUtils::WriteBinaryFloat(vertex.coord.x, stream); + IOUtils::WriteBinaryFloat(vertex.coord.y, stream); + IOUtils::WriteBinaryFloat(vertex.coord.z, stream); + IOUtils::WriteBinaryFloat(vertex.normal.x, stream); + IOUtils::WriteBinaryFloat(vertex.normal.y, stream); + IOUtils::WriteBinaryFloat(vertex.normal.z, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord2.x, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord2.y, stream); +} + +Gfx::Material ReadBinaryMaterial(std::istream &stream) +{ + Gfx::Material result; + + result.diffuse.r = IOUtils::ReadBinaryFloat(stream); + result.diffuse.g = IOUtils::ReadBinaryFloat(stream); + result.diffuse.b = IOUtils::ReadBinaryFloat(stream); + result.diffuse.a = IOUtils::ReadBinaryFloat(stream); + + result.ambient.r = IOUtils::ReadBinaryFloat(stream); + result.ambient.g = IOUtils::ReadBinaryFloat(stream); + result.ambient.b = IOUtils::ReadBinaryFloat(stream); + result.ambient.a = IOUtils::ReadBinaryFloat(stream); + + result.specular.r = IOUtils::ReadBinaryFloat(stream); + result.specular.g = IOUtils::ReadBinaryFloat(stream); + result.specular.b = IOUtils::ReadBinaryFloat(stream); + result.specular.a = IOUtils::ReadBinaryFloat(stream); + + /* emissive.r = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.g = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.b = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.a = */ IOUtils::ReadBinaryFloat(stream); + + /* power = */ IOUtils::ReadBinaryFloat(stream); + + /* padding? */ IOUtils::ReadBinary<2, unsigned int>(stream); + + return result; +} + +void WriteBinaryMaterial(Gfx::Material material, std::ostream &stream) +{ + IOUtils::WriteBinaryFloat(material.diffuse.r, stream); + IOUtils::WriteBinaryFloat(material.diffuse.g, stream); + IOUtils::WriteBinaryFloat(material.diffuse.b, stream); + IOUtils::WriteBinaryFloat(material.diffuse.a, stream); + + IOUtils::WriteBinaryFloat(material.ambient.r, stream); + IOUtils::WriteBinaryFloat(material.ambient.g, stream); + IOUtils::WriteBinaryFloat(material.ambient.b, stream); + IOUtils::WriteBinaryFloat(material.ambient.a, stream); + + IOUtils::WriteBinaryFloat(material.specular.r, stream); + IOUtils::WriteBinaryFloat(material.specular.g, stream); + IOUtils::WriteBinaryFloat(material.specular.b, stream); + IOUtils::WriteBinaryFloat(material.specular.a, stream); + + /* emissive.r */ IOUtils::WriteBinaryFloat(0.0f, stream); + /* emissive.g */ IOUtils::WriteBinaryFloat(0.0f, stream); + /* emissive.b */ IOUtils::WriteBinaryFloat(0.0f, stream); + /* emissive.a */ IOUtils::WriteBinaryFloat(0.0f, stream); + + /* power */ IOUtils::WriteBinaryFloat(0.0f, stream); + + /* padding? */ IOUtils::WriteBinary<2, unsigned int>(0, stream); +} + +Gfx::ModelTriangle::ModelTriangle() +{ + min = 0.0f; + max = 0.0f; + state = 0L; +} + + +Gfx::CModelFile::CModelFile(CInstanceManager* iMan) +{ + m_iMan = iMan; + + m_engine = (CEngine*)m_iMan->SearchInstance(CLASS_ENGINE); + + m_triangles.reserve(TRIANGLE_PREALLOCATE_COUNT); +} + +Gfx::CModelFile::~CModelFile() +{ +} + +std::string Gfx::CModelFile::GetError() +{ + return m_error; +} + + +bool Gfx::CModelFile::ReadModel(const std::string &filename, bool edit, bool meta) +{ + m_triangles.clear(); + m_error = ""; + + std::ifstream stream; + stream.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); + if (! stream.good()) + { + m_error = std::string("Could not open file '") + filename + std::string("'"); + return false; + } + + return ReadModel(stream, edit, meta); +} + +bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) +{ + m_triangles.clear(); + m_error = ""; + + // FIXME: for now, reading models only from files, not metafile + + ModelHeader header; + + header.revision = IOUtils::ReadBinary<4, int>(stream); + header.version = IOUtils::ReadBinary<4, int>(stream); + header.totalVertices = IOUtils::ReadBinary<4, int>(stream); + for (int i = 0; i < 10; ++i) + header.reserved[i] = IOUtils::ReadBinary<4, int>(stream); + + + if (! stream.good()) + { + m_error = "Error reading model file header"; + return false; + } + + // Old model version #1 + if ( (header.revision == 1) && (header.version == 0) ) + { + for (int i = 0; i < header.totalVertices; ++i) + { + OldModelTriangle1 t; + t.used = IOUtils::ReadBinary<1, char>(stream); + t.selected = IOUtils::ReadBinary<1, char>(stream); + + t.p1 = ReadBinaryVertex(stream); + t.p2 = ReadBinaryVertex(stream); + t.p3 = ReadBinaryVertex(stream); + + t.material = ReadBinaryMaterial(stream); + stream.read(t.texName, 20); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + + if (! stream.good()) + { + m_error = "Error reading model data"; + return false; + } + + Gfx::ModelTriangle triangle; + triangle.p1.FromVertex(t.p1); + triangle.p2.FromVertex(t.p2); + triangle.p3.FromVertex(t.p3); + + triangle.material = t.material; + triangle.tex1Name = std::string(t.texName); + triangle.min = t.min; + triangle.max = t.max; + + m_triangles.push_back(triangle); + } + } + else if ( header.revision == 1 && header.version == 1 ) + { + for (int i = 0; i < header.totalVertices; ++i) + { + OldModelTriangle2 t; + t.used = IOUtils::ReadBinary<1, char>(stream); + t.selected = IOUtils::ReadBinary<1, char>(stream); + + t.p1 = ReadBinaryVertex(stream); + t.p2 = ReadBinaryVertex(stream); + t.p3 = ReadBinaryVertex(stream); + + t.material = ReadBinaryMaterial(stream); + stream.read(t.texName, 20); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + t.state = IOUtils::ReadBinary<4, long>(stream); + + t.reserved1 = IOUtils::ReadBinary<2, short>(stream); + t.reserved2 = IOUtils::ReadBinary<2, short>(stream); + t.reserved3 = IOUtils::ReadBinary<2, short>(stream); + t.reserved4 = IOUtils::ReadBinary<2, short>(stream); + + if (! stream.good()) + { + m_error = "Error reading model data"; + return false; + } + + Gfx::ModelTriangle triangle; + triangle.p1.FromVertex(t.p1); + triangle.p2.FromVertex(t.p2); + triangle.p3.FromVertex(t.p3); + + triangle.material = t.material; + triangle.tex1Name = std::string(t.texName); + triangle.min = t.min; + triangle.max = t.max; + triangle.state = t.state; + + m_triangles.push_back(triangle); + } + } + else + { + for (int i = 0; i < header.totalVertices; ++i) + { + NewModelTriangle t; + t.used = IOUtils::ReadBinary<1, char>(stream); + t.selected = IOUtils::ReadBinary<1, char>(stream); + + t.p1 = ReadBinaryVertexTex2(stream); + t.p2 = ReadBinaryVertexTex2(stream); + t.p3 = ReadBinaryVertexTex2(stream); + + t.material = ReadBinaryMaterial(stream); + stream.read(t.texName, 20); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + t.state = IOUtils::ReadBinary<4, long>(stream); + t.texNum2 = IOUtils::ReadBinary<2, short>(stream); + + t.reserved2 = IOUtils::ReadBinary<2, short>(stream); + t.reserved3 = IOUtils::ReadBinary<2, short>(stream); + t.reserved4 = IOUtils::ReadBinary<2, short>(stream); + + if (! stream.good()) + { + m_error = "Error reading model data"; + return false; + } + + Gfx::ModelTriangle triangle; + triangle.p1 = t.p1; + triangle.p2 = t.p2; + triangle.p3 = t.p3; + + triangle.material = t.material; + triangle.tex1Name = std::string(t.texName); + char tex2Name[20] = { 0 }; + triangle.min = t.min; + triangle.max = t.max; + triangle.state = t.state; + sprintf(tex2Name, "dirty%.2d.tga", t.texNum2); // hardcoded as in the original code + triangle.tex2Name = std::string(tex2Name); + + m_triangles.push_back(triangle); + } + } + + for (int i = 0; i < (int) m_triangles.size(); ++i) + m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "tga"); + + /* + if (! edit) + { + float limit[2]; + limit[0] = m_engine->RetLimitLOD(0); // frontier AB as config + limit[1] = m_engine->RetLimitLOD(1); // frontier BC as config + + // Standard frontiers -> config. + for (int i = 0; i < m_triangles.size(); ++i) + { + if ( m_triangles[i].min == 0.0f && + m_triangles[i].max == 100.0f ) // resolution A ? + { + m_triangles[i].max = limit[0]; + } + else if ( m_triangles[i].min == 100.0f && + m_triangles[i].max == 200.0f ) // resolution B ? + { + m_triangles[i].min = limit[0]; + m_triangles[i].max = limit[1]; + } + else if ( m_triangles[i].min == 200.0f && + m_triangles[i].max == 1000000.0f ) // resolution C ? + { + m_triangles[i].min = limit[1]; + } + } + }*/ + + return true; +} + +bool Gfx::CModelFile::WriteModel(const std::string &filename) +{ + m_error = ""; + + std::ofstream stream; + stream.open(filename.c_str(), std::ios_base::out | std::ios_base::binary); + if (! stream.good()) + { + m_error = std::string("Could not open file '") + filename + std::string("'"); + return false; + } + + return WriteModel(stream); +} + +bool Gfx::CModelFile::WriteModel(std::ostream &stream) +{ + m_error = ""; + + if (m_triangles.size() == 0) + { + m_error = "Empty model"; + return false; + } + + ModelHeader header; + header.revision = 1; + header.version = 2; + header.totalVertices = m_triangles.size(); + + IOUtils::WriteBinary<4, int>(header.revision, stream); + IOUtils::WriteBinary<4, int>(header.version, stream); + IOUtils::WriteBinary<4, int>(header.totalVertices, stream); + for (int i = 0; i < 10; ++i) + IOUtils::WriteBinary<4, int>(header.reserved[i], stream); + + for (int i = 0; i < (int)m_triangles.size(); ++i) + { + NewModelTriangle t; + + t.used = true; + + t.p1 = m_triangles[i].p1; + t.p2 = m_triangles[i].p2; + t.p3 = m_triangles[i].p3; + + t.material = m_triangles[i].material; + strncpy(t.texName, m_triangles[i].tex1Name.c_str(), 20); + t.min = m_triangles[i].min; + t.max = m_triangles[i].max; + t.state = m_triangles[i].state; + int no = 0; + sscanf(m_triangles[i].tex2Name.c_str(), "dirty%d.tga", &no); // hardcoded as in the original code + t.texNum2 = no; + + + IOUtils::WriteBinary<1, char>(t.used, stream); + IOUtils::WriteBinary<1, char>(t.selected, stream); + + WriteBinaryVertexTex2(t.p1, stream); + WriteBinaryVertexTex2(t.p2, stream); + WriteBinaryVertexTex2(t.p3, stream); + + WriteBinaryMaterial(t.material, stream); + stream.write(t.texName, 20); + IOUtils::WriteBinaryFloat(t.min, stream); + IOUtils::WriteBinaryFloat(t.max, stream); + IOUtils::WriteBinary<4, long>(t.state, stream); + IOUtils::WriteBinary<2, short>(t.texNum2, stream); + + IOUtils::WriteBinary<2, short>(t.reserved2, stream); + IOUtils::WriteBinary<2, short>(t.reserved3, stream); + IOUtils::WriteBinary<2, short>(t.reserved4, stream); + } + + return true; +} + +bool Gfx::CModelFile::ReadDXF(const std::string &filename, float min, float max) +{ + m_triangles.clear(); + m_error = ""; + + std::ifstream stream; + stream.open(filename.c_str(), std::ios_base::in); + if (! stream.good()) + { + m_error = std::string("Couldn't open file '") + filename + std::string("'"); + return false; + } + + return ReadDXF(stream, min, max); +} + +bool Gfx::CModelFile::ReadDXF(std::istream &stream, float min, float max) +{ + m_triangles.clear(); + m_error = ""; + + if (! stream.good()) + { + m_error = "Invalid stream"; + return false; + } + + // Input state + bool waitNumVertex = false; + bool waitNumFace = false; + bool waitVertexX = false; + bool waitVertexY = false; + bool waitVertexZ = false; + bool waitFaceX = false; + bool waitFaceY = false; + bool waitFaceZ = false; + + // Vertex array + std::vector vertices; + vertices.reserve(TRIANGLE_PREALLOCATE_COUNT); + + // Number of vertices & faces of the primitive to be read + int vertexNum = 0, faceNum = 0; + // Vertex coords + Math::Vector coords; + // Indexes of face (triangle) points + int p1 = 0, p2 = 0, p3 = 0; + + // Input line + std::string line; + while (! stream.eof() ) + { + // Read line with command + std::getline(stream, line); + int command = StrUtils::FromString(line); + + // Read line with param + std::getline(stream, line); + + bool ok = true; + + + if (command == 66) + { + waitNumVertex = true; + } + + if ( command == 71 && waitNumVertex ) + { + waitNumVertex = false; + vertexNum = StrUtils::FromString(line, &ok); + waitNumFace = true; + } + + if ( command == 72 && waitNumFace ) + { + waitNumFace = false; + faceNum = StrUtils::FromString(line, &ok); + waitVertexX = true; + } + + if ( command == 10 && waitVertexX ) + { + waitVertexX = false; + coords.x = StrUtils::FromString(line, &ok); + waitVertexY = true; + } + + if ( command == 20 && waitVertexY ) + { + waitVertexY = false; + coords.y = StrUtils::FromString(line, &ok); + waitVertexZ = true; + } + + if ( command == 30 && waitVertexZ ) + { + waitVertexZ = false; + coords.z = StrUtils::FromString(line, &ok); + + vertexNum --; + if ( vertexNum >= 0 ) + { + Math::Vector p(coords.x, coords.z, coords.y); // permutation of Y and Z! + vertices.push_back(p); + waitVertexX = true; + } + else + { + waitFaceX = true; + } + } + + if ( command == 71 && waitFaceX ) + { + waitFaceX = false; + p1 = StrUtils::FromString(line, &ok); + if ( p1 < 0 ) p1 = -p1; + waitFaceY = true; + } + + if ( command == 72 && waitFaceY ) + { + waitFaceY = false; + p2 = StrUtils::FromString(line, &ok); + if ( p2 < 0 ) p2 = -p2; + waitFaceZ = true; + } + + if ( command == 73 && waitFaceZ ) + { + waitFaceZ = false; + p3 = StrUtils::FromString(line, &ok); + if ( p3 < 0 ) p3 = -p3; + + faceNum --; + if ( faceNum >= 0 ) + { + assert( (p1-1 >= 0) && (p1-1 < (int)vertices.size() ) ); + assert( (p2-1 >= 0) && (p2-1 < (int)vertices.size() ) ); + assert( (p3-1 >= 0) && (p3-1 < (int)vertices.size() ) ); + + CreateTriangle(vertices[p3-1], vertices[p2-1], vertices[p1-1], min, max); + waitFaceX = true; + } + } + + if (! ok) + { + m_error = "Error reading data"; + return false; + } + + } + + return true; +} + +bool Gfx::CModelFile::CreateEngineObject(int objRank, int addState) +{ + /*char texName1[20]; + char texName2[20]; + int texNum, i, state; + + for (int i = 0; i < m_trianglesUsed; i++) + { + if (! m_triangles[i].used) continue; + + state = m_triangles[i].state; + strcpy(texName1, m_triangles[i].texName); + texName2[0] = 0; + + if ( strcmp(texName1, "plant.tga") == 0 ) // ??? + { + state |= D3DSTATEALPHA; + } + + if ( m_triangles[i].texNum2 != 0 ) + { + if ( m_triangles[i].texNum2 == 1 ) + { + texNum = m_engine->RetSecondTexture(); + } + else + { + texNum = m_triangles[i].texNum2; + } + + if ( texNum >= 1 && texNum <= 10 ) + { + state |= D3DSTATEDUALb; + } + if ( texNum >= 11 && texNum <= 20 ) + { + state |= D3DSTATEDUALw; + } + sprintf(texName2, "dirty%.2d.tga", texNum); // ??? + } + + m_engine->AddTriangle(objRank, &m_triangles[i].p1, 3, + m_triangles[i].material, + state + addState, + texName1, texName2, + m_triangles[i].min, + m_triangles[i].max, false); + }*/ + return true; +} + +void Gfx::CModelFile::Mirror() +{ + for (int i = 0; i < (int)m_triangles.size(); i++) + { + Gfx::VertexTex2 t = m_triangles[i].p1; + m_triangles[i].p1 = m_triangles[i].p2; + m_triangles[i].p2 = t; + + m_triangles[i].p1.coord.z = -m_triangles[i].p1.coord.z; + m_triangles[i].p2.coord.z = -m_triangles[i].p2.coord.z; + m_triangles[i].p3.coord.z = -m_triangles[i].p3.coord.z; + + m_triangles[i].p1.normal.z = -m_triangles[i].p1.normal.z; + m_triangles[i].p2.normal.z = -m_triangles[i].p2.normal.z; + m_triangles[i].p3.normal.z = -m_triangles[i].p3.normal.z; + } +} + +int Gfx::CModelFile::GetTriangleCount() +{ + return m_triangles.size(); +} + +float Gfx::CModelFile::GetHeight(Math::Vector pos) +{ + float limit = 5.0f; + + for (int i = 0; i < (int)m_triangles.size(); i++) + { + if ( fabs(pos.x - m_triangles[i].p1.coord.x) < limit && + fabs(pos.z - m_triangles[i].p1.coord.z) < limit ) + return m_triangles[i].p1.coord.y; + + if ( fabs(pos.x - m_triangles[i].p2.coord.x) < limit && + fabs(pos.z - m_triangles[i].p2.coord.z) < limit ) + return m_triangles[i].p2.coord.y; + + if ( fabs(pos.x - m_triangles[i].p3.coord.x) < limit && + fabs(pos.z - m_triangles[i].p3.coord.z) < limit ) + return m_triangles[i].p3.coord.y; + } + + return 0.0f; +} + +void Gfx::CModelFile::CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max) +{ + Gfx::ModelTriangle triangle; + + Math::Vector n = Math::NormalToPlane(p3, p2, p1); + triangle.p1 = Gfx::VertexTex2(p1, n); + triangle.p2 = Gfx::VertexTex2(p2, n); + triangle.p3 = Gfx::VertexTex2(p3, n); + + triangle.material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f, 0.0f); + triangle.material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f, 0.0f); + + triangle.min = min; + triangle.max = max; + + m_triangles.push_back(triangle); +} diff --git a/src/graphics/common/modelfile.h b/src/graphics/common/modelfile.h new file mode 100644 index 0000000..625df50 --- /dev/null +++ b/src/graphics/common/modelfile.h @@ -0,0 +1,118 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// modelfile.h (aka modfile.h) + +#include "graphics/common/engine.h" +#include "graphics/common/vertex.h" +#include "graphics/common/material.h" +#include "math/vector.h" + +#include +#include +#include + + +class CInstanceManager; + + +namespace Gfx { + +/** + \struct ModelTriangle + \brief Triangle of a 3D model + */ +struct ModelTriangle +{ + //! 1st vertex + Gfx::VertexTex2 p1; + //! 2nd vertex + Gfx::VertexTex2 p2; + //! 3rd vertex + Gfx::VertexTex2 p3; + //! Material + Gfx::Material material; + //! Name of 1st texture + std::string tex1Name; + //! Name of 2nd texture + std::string tex2Name; + //! Min LOD threshold + float min; + //! Max LOD threshold + float max; + //! Rendering state to be set + long state; + + ModelTriangle(); +}; + + +/** + \class CModelFile + \brief Model file reader/writer + + Allows reading and writing model objects. Models are collections of ModelTriangle structs. */ +class CModelFile +{ +public: + CModelFile(CInstanceManager* iMan); + ~CModelFile(); + + //! Returns the last error encountered + std::string GetError(); + + //! Reads a binary Colobot model from file + bool ReadModel(const std::string &filename, bool edit = false, bool meta = true); + //! Reads a binary Colobot model from stream + bool ReadModel(std::istream &stream, bool edit = false, bool meta = true); + //! Writes the model to Colobot binary model file + bool WriteModel(const std::string &filename); + //! Writes the model to Colobot binary model file + bool WriteModel(std::ostream &stream); + + //! Reads a DXF model from file + bool ReadDXF(const std::string &filename, float min, float max); + //! Reads a DXF model from stream + bool ReadDXF(std::istream &stream, float min, float max); + + //! Returns the number of triangles in model + int GetTriangleCount(); + //! Returns the height of model -- closest point to X and Z coords of \a pos + float GetHeight(Math::Vector pos); + + //! Mirrors the model along the Z axis + void Mirror(); + + //! Creates an object in the graphics engine from the model + bool CreateEngineObject(int objRank, int addState = 0); + +protected: + //! Adds a triangle to the list + void CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + + //! Last error + std::string m_error; + + //! Model triangles + std::vector m_triangles; +}; + +}; // namespace Gfx diff --git a/src/graphics/common/modfile.cpp b/src/graphics/common/modfile.cpp deleted file mode 100644 index 6f80be7..0000000 --- a/src/graphics/common/modfile.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// modfile.cpp - -#include "graphics/common/modfile.h" - - -// TODO implementation \ No newline at end of file diff --git a/src/graphics/common/modfile.h b/src/graphics/common/modfile.h deleted file mode 100644 index c81739b..0000000 --- a/src/graphics/common/modfile.h +++ /dev/null @@ -1,114 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// modfile.h - -#include "engine.h" -#include "vertex.h" -#include "material.h" -#include "math/vector.h" - - -class CInstanceManager; - - -namespace Gfx { - -struct OldModelTriangle1 -{ - char bUsed; // TRUE -> using - char bSelect; // TRUE -> selected - Vertex p1; - Vertex p2; - Vertex p3; - Material material; - char texName[20]; - float min; - float max; -}; // length = 196 bytes - -struct OldModelTriangle2 -{ - char bUsed; // TRUE -> used - char bSelect; // TRUE -> selected - Vertex p1; - Vertex p2; - Vertex p3; - Material material; - char texName[20]; - float min; - float max; - long state; - short reserve1; - short reserve2; - short reserve3; - short reserve4; -}; - -struct ModelTriangle -{ - char bUsed; // TRUE -> used - char bSelect; // TRUE -> selected - VertexTex2 p1; - VertexTex2 p2; - VertexTex2 p3; - Material material; - char texName[20]; - float min; - float max; - long state; - short texNum2; - short reserve2; - short reserve3; - short reserve4; -}; // length = 208 bytes - - - - -class CModFile { -public: - CModFile(CInstanceManager* iMan); - ~CModFile(); - - bool ReadDXF(char *filename, float min, float max); - bool AddModel(char *filename, int first, bool bEdit=false, bool bMeta=true); - bool ReadModel(char *filename, bool bEdit=false, bool bMeta=true); - bool WriteModel(char *filename); - - bool CreateEngineObject(int objRank, int addState=0); - void Mirror(); - - void SetTriangleUsed(int total); - int RetTriangleUsed(); - int RetTriangleMax(); - ModelTriangle* RetTriangleList(); - - float RetHeight(Math::Vector pos); - -protected: - bool CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - - ModelTriangle* m_triangleTable; - int m_triangleUsed; -}; - -}; diff --git a/src/graphics/common/test/CMakeLists.txt b/src/graphics/common/test/CMakeLists.txt new file mode 100644 index 0000000..7274169 --- /dev/null +++ b/src/graphics/common/test/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 2.8) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") + +include_directories(. ../../..) +add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp) diff --git a/src/graphics/common/test/modelfile_test.cpp b/src/graphics/common/test/modelfile_test.cpp new file mode 100644 index 0000000..cc44f98 --- /dev/null +++ b/src/graphics/common/test/modelfile_test.cpp @@ -0,0 +1,48 @@ +#include "graphics/common/modelfile.h" +#include "common/iman.h" + +#include + + +int main(int argc, char *argv[]) +{ + if (argc != 4) + { + std::cerr << "Usage: " << argv[0] << " {mod|dxf} in_file out_file" << std::endl; + return 1; + } + + CInstanceManager iMan; + Gfx::CModelFile modfile(&iMan); + + std::string mode(argv[1]); + if (mode == "mod") + { + if (! modfile.ReadModel(argv[2], false, false) ) + { + std::cerr << "Read error: " << modfile.GetError() << std::endl; + return 2; + } + } + else if (mode == "dxf") + { + if (! modfile.ReadDXF(argv[2], false, false) ) + { + std::cerr << "Read error: " << modfile.GetError() << std::endl; + return 2; + } + } + else + { + std::cerr << "Usage: " << argv[0] << " {mod|dxf} in_file out_file" << std::endl; + return 1; + } + + if (! modfile.WriteModel(argv[3]) ) + { + std::cerr << "Write error: " << modfile.GetError() << std::endl; + return 3; + } + + return 0; +} diff --git a/src/graphics/common/vertex.h b/src/graphics/common/vertex.h index 430c84c..c6375b9 100644 --- a/src/graphics/common/vertex.h +++ b/src/graphics/common/vertex.h @@ -94,6 +94,14 @@ struct VertexTex2 Math::Point aTexCoord = Math::Point(), Math::Point aTexCoord2 = Math::Point()) : coord(aCoord), normal(aNormal), texCoord(aTexCoord), texCoord2(aTexCoord2) {} + + void FromVertex(const Gfx::Vertex &v) + { + coord = v.coord; + normal = v.normal; + texCoord = v.texCoord; + texCoord2 = Math::Point(); + } }; }; // namespace Gfx -- cgit v1.2.3-1-g7c22 From eca6d26459200d2956d64ccf8a2713d96a82c989 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 11 Jul 2012 20:56:09 +0200 Subject: Minor changes - moved mainpage src/doc/docmain.doc.txt to src/app/main.cpp - removed old modfile modules from src/common - removed Snd namespace in engine.h --- Doxyfile | 3 +- src/app/main.cpp | 36 +++ src/common/modfile.cpp | 695 ------------------------------------------- src/common/modfile.h | 115 ------- src/graphics/common/engine.h | 5 +- 5 files changed, 38 insertions(+), 816 deletions(-) delete mode 100644 src/common/modfile.cpp delete mode 100644 src/common/modfile.h diff --git a/Doxyfile b/Doxyfile index e57ead3..9e4fe82 100644 --- a/Doxyfile +++ b/Doxyfile @@ -690,8 +690,7 @@ INPUT_ENCODING = UTF-8 # *.f90 *.f *.for *.vhd *.vhdl FILE_PATTERNS = *.h \ - *.cpp \ - *.doc.txt + *.cpp # The RECURSIVE tag can be used to turn specify whether or not subdirectories # should be searched for input files as well. Possible values are YES and NO. diff --git a/src/app/main.cpp b/src/app/main.cpp index ece18d3..f857037 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -24,6 +24,42 @@ #include "common/restext.h" +/* Doxygen main page */ + +/** + +\mainpage + +Doxygen documentation of Colobot project + +\section Intro Introduction + +The source code released by Epitec was sparsely documented. This documentation, written from scratch, +will aim to describe the various components of the code. + +Currently, the only documented classes are the ones written from scratch or the old ones rewritten to match the new code. +In time, the documentation will be extended to cover every major part of the code. + +\section Structure Code structure + +The source code was split from the original all-in-one directory to subdirectories, each containing one major part of the project. +The current layout is this: + - src/CBot - separate library with CBot language + - src/app - class CApplication and everything concerned with SDL plus other system-dependent code such as displaying a message box, finding files, etc. + - src/common - shared structs, enums, defines, etc.; should not have any external dependencies + - src/graphics/common - interface of graphics engine (CEngine) and device (CDevice), without concrete implementation, shared structs such as Vertex, Material, etc., “effects” classes: CCamera, CLight, CParticle that will use the graphics engine interface + - src/graphics/opengl - concrete implementation of CEngine and CDevice classes in OpenGL: CGLEngine and CGLDevice + - src/graphics/d3d - in (far) future - perhaps a newer implementation in DirectX (9? 10?) + - src/math - mathematical structures and functions + - src/object - non-graphical game engine, that is robots, buildings, etc.; dependent only on interface of graphics engine, not on concrete implementation + - src/ui - 2D user interface (menu, buttons, check boxes, etc.); also without dependencies to concrete implementation of graphics engine + - src/sound - sound and music engine written using fmod library + - src/physics - physics engine + - src/script - link with the CBot library + - src/metafile - separate program for packing data files to .dat format +*/ + + //! Entry point to the program int main(int argc, char *argv[]) { diff --git a/src/common/modfile.cpp b/src/common/modfile.cpp deleted file mode 100644 index fc202b7..0000000 --- a/src/common/modfile.cpp +++ /dev/null @@ -1,695 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// modfile.cpp - - -#include -#include -#include - -#include "common/struct.h" -#include "math/geometry.h" -#include "old/d3dengine.h" -#include "old/d3dmath.h" -#include "common/language.h" -#include "common/event.h" -#include "common/misc.h" -#include "common/iman.h" -#include "old/math3d.h" -#include "common/modfile.h" - - - -const int MAX_VERTICES = 2000; - - - -// Object's constructor. - -CModFile::CModFile(CInstanceManager* iMan) -{ - m_iMan = iMan; - - m_engine = (CD3DEngine*)m_iMan->SearchInstance(CLASS_ENGINE); - - m_triangleUsed = 0; - m_triangleTable = (ModelTriangle*)malloc(sizeof(ModelTriangle)*MAX_VERTICES); - ZeroMemory(m_triangleTable, sizeof(ModelTriangle)*MAX_VERTICES); -} - -// Object's destructor. - -CModFile::~CModFile() -{ - free(m_triangleTable); -} - - - - -// Creates a triangle in the internal structure. - -bool CModFile::CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, - float min, float max) -{ - Math::Vector n; - int i; - - if ( m_triangleUsed >= MAX_VERTICES ) - { - OutputDebugString("ERROR: CreateTriangle::Too many triangles\n"); - return false; - } - - i = m_triangleUsed++; - - ZeroMemory(&m_triangleTable[i], sizeof(ModelTriangle)); - - m_triangleTable[i].bUsed = true; - m_triangleTable[i].bSelect = false; - - n = Math::NormalToPlane(p3, p2, p1); - m_triangleTable[i].p1 = D3DVERTEX2( p1, n); - m_triangleTable[i].p2 = D3DVERTEX2( p2, n); - m_triangleTable[i].p3 = D3DVERTEX2( p3, n); - - m_triangleTable[i].material.diffuse.r = 1.0f; - m_triangleTable[i].material.diffuse.g = 1.0f; - m_triangleTable[i].material.diffuse.b = 1.0f; // white - m_triangleTable[i].material.ambient.r = 0.5f; - m_triangleTable[i].material.ambient.g = 0.5f; - m_triangleTable[i].material.ambient.b = 0.5f; - - m_triangleTable[i].min = min; - m_triangleTable[i].max = max; - - return true; -} - -// Reads a DXF file. - -bool CModFile::ReadDXF(char *filename, float min, float max) -{ - FILE* file = NULL; - char line[100]; - int command, rankSommet, nbSommet, nbFace; - Math::Vector table[MAX_VERTICES]; - bool bWaitNbSommet; - bool bWaitNbFace; - bool bWaitSommetX; - bool bWaitSommetY; - bool bWaitSommetZ; - bool bWaitFaceX; - bool bWaitFaceY; - bool bWaitFaceZ; - float x,y,z; - int p1,p2,p3; - - file = fopen(filename, "r"); - if ( file == NULL ) return false; - - m_triangleUsed = 0; - - rankSommet = 0; - bWaitNbSommet = false; - bWaitNbFace = false; - bWaitSommetX = false; - bWaitSommetY = false; - bWaitSommetZ = false; - bWaitFaceX = false; - bWaitFaceY = false; - bWaitFaceZ = false; - - while ( fgets(line, 100, file) != NULL ) - { - sscanf(line, "%d", &command); - if ( fgets(line, 100, file) == NULL ) break; - - if ( command == 66 ) - { - bWaitNbSommet = true; - } - - if ( command == 71 && bWaitNbSommet ) - { - bWaitNbSommet = false; - sscanf(line, "%d", &nbSommet); - if ( nbSommet > MAX_VERTICES ) nbSommet = MAX_VERTICES; - rankSommet = 0; - bWaitNbFace = true; - -//? sprintf(s, "Waiting for %d sommets\n", nbSommet); -//? OutputDebugString(s); - } - - if ( command == 72 && bWaitNbFace ) - { - bWaitNbFace = false; - sscanf(line, "%d", &nbFace); - bWaitSommetX = true; - -//? sprintf(s, "Waiting for %d faces\n", nbFace); -//? OutputDebugString(s); - } - - if ( command == 10 && bWaitSommetX ) - { - bWaitSommetX = false; - sscanf(line, "%f", &x); - bWaitSommetY = true; - } - - if ( command == 20 && bWaitSommetY ) - { - bWaitSommetY = false; - sscanf(line, "%f", &y); - bWaitSommetZ = true; - } - - if ( command == 30 && bWaitSommetZ ) - { - bWaitSommetZ = false; - sscanf(line, "%f", &z); - - nbSommet --; - if ( nbSommet >= 0 ) - { - Math::Vector p(x,z,y); // permutation of Y and Z! - table[rankSommet++] = p; - bWaitSommetX = true; - -//? sprintf(s, "Sommet[%d]=%f;%f;%f\n", rankSommet, p.x,p.y,p.z); -//? OutputDebugString(s); - } - else - { - bWaitFaceX = true; - } - } - - if ( command == 71 && bWaitFaceX ) - { - bWaitFaceX = false; - sscanf(line, "%d", &p1); - if ( p1 < 0 ) p1 = -p1; - bWaitFaceY = true; - } - - if ( command == 72 && bWaitFaceY ) - { - bWaitFaceY = false; - sscanf(line, "%d", &p2); - if ( p2 < 0 ) p2 = -p2; - bWaitFaceZ = true; - } - - if ( command == 73 && bWaitFaceZ ) - { - bWaitFaceZ = false; - sscanf(line, "%d", &p3); - if ( p3 < 0 ) p3 = -p3; - - nbFace --; - if ( nbFace >= 0 ) - { - CreateTriangle( table[p3-1], table[p2-1], table[p1-1], min,max ); - bWaitFaceX = true; - -//? sprintf(s, "Face=%d;%d;%d\n", p1,p2,p3); -//? OutputDebugString(s); - } - } - - } - - fclose(file); - return true; -} - - - -struct InfoMOD -{ - int rev; - int vers; - int total; - int reserve[10]; -}; - - -// Change nom.bmp to nom.tga - -void ChangeBMPtoTGA(char *filename) -{ - char* p; - - p = strstr(filename, ".bmp"); - if ( p != 0 ) strcpy(p, ".tga"); -} - - -// Reads a MOD file. - -bool CModFile::AddModel(char *filename, int first, bool bEdit, bool bMeta) -{ - FILE* file; - InfoMOD info; - float limit[2]; - int i, nb, err; - char* p; - - if ( m_engine->RetDebugMode() ) - { - bMeta = false; - } - - if ( bMeta ) - { - p = strchr(filename, '\\'); - if ( p == 0 ) - { -#if _SCHOOL - err = g_metafile.Open("ceebot2.dat", filename); -#else - err = g_metafile.Open("colobot2.dat", filename); -#endif - } - else - { -#if _SCHOOL - err = g_metafile.Open("ceebot2.dat", p+1); -#else - err = g_metafile.Open("colobot2.dat", p+1); -#endif - } - if ( err != 0 ) bMeta = false; - } - if ( !bMeta ) - { - file = fopen(filename, "rb"); - if ( file == NULL ) return false; - } - - if ( bMeta ) - { - g_metafile.Read(&info, sizeof(InfoMOD)); - } - else - { - fread(&info, sizeof(InfoMOD), 1, file); - } - nb = info.total; - m_triangleUsed += nb; - - if ( info.rev == 1 && info.vers == 0 ) - { - OldModelTriangle1 old; - - for ( i=first ; iRetLimitLOD(0); // frontier AB as config - limit[1] = m_engine->RetLimitLOD(1); // frontier BC as config - - // Standard frontiers -> config. - for ( i=first ; iRetSecondTexture(); - } - else - { - texNum = m_triangleTable[i].texNum2; - } - - if ( texNum >= 1 && texNum <= 10 ) - { - state = m_triangleTable[i].state|D3DSTATEDUALb; - } - if ( texNum >= 11 && texNum <= 20 ) - { - state = m_triangleTable[i].state|D3DSTATEDUALw; - } - sprintf(texName2, "dirty%.2d.bmp", texNum); - } - - m_engine->AddTriangle(objRank, &m_triangleTable[i].p1, 3, - m_triangleTable[i].material, - state+addState, - m_triangleTable[i].texName, texName2, - m_triangleTable[i].min, - m_triangleTable[i].max, false); - } - return true; -#else - char texName1[20]; - char texName2[20]; - int texNum, i, state; - - for ( i=0 ; iRetSecondTexture(); - } - else - { - texNum = m_triangleTable[i].texNum2; - } - - if ( texNum >= 1 && texNum <= 10 ) - { - state |= D3DSTATEDUALb; - } - if ( texNum >= 11 && texNum <= 20 ) - { - state |= D3DSTATEDUALw; - } - sprintf(texName2, "dirty%.2d.tga", texNum); - } - - m_engine->AddTriangle(objRank, &m_triangleTable[i].p1, 3, - m_triangleTable[i].material, - state+addState, - texName1, texName2, - m_triangleTable[i].min, - m_triangleTable[i].max, false); - } - return true; -#endif -} - - -// Performs a mirror according to Z. - -void CModFile::Mirror() -{ - D3DVERTEX2 t; - int i; - - for ( i=0 ; i using - char bSelect; // true -> selected - D3DVERTEX p1; - D3DVERTEX p2; - D3DVERTEX p3; - D3DMATERIAL7 material; - char texName[20]; - float min; - float max; -}; // length = 196 bytes - -struct OldModelTriangle2 -{ - char bUsed; // true -> used - char bSelect; // true -> selected - D3DVERTEX p1; - D3DVERTEX p2; - D3DVERTEX p3; - D3DMATERIAL7 material; - char texName[20]; - float min; - float max; - long state; - short reserve1; - short reserve2; - short reserve3; - short reserve4; -}; - -struct ModelTriangle -{ - char bUsed; // true -> used - char bSelect; // true -> selected - D3DVERTEX2 p1; - D3DVERTEX2 p2; - D3DVERTEX2 p3; - D3DMATERIAL7 material; - char texName[20]; - float min; - float max; - long state; - short texNum2; - short reserve2; - short reserve3; - short reserve4; -}; // length = 208 bytes - - - - -class CModFile -{ -public: - CModFile(CInstanceManager* iMan); - ~CModFile(); - - bool ReadDXF(char *filename, float min, float max); - bool AddModel(char *filename, int first, bool bEdit=false, bool bMeta=true); - bool ReadModel(char *filename, bool bEdit=false, bool bMeta=true); - bool WriteModel(char *filename); - - bool CreateEngineObject(int objRank, int addState=0); - void Mirror(); - - void SetTriangleUsed(int total); - int RetTriangleUsed(); - int RetTriangleMax(); - ModelTriangle* RetTriangleList(); - - float RetHeight(Math::Vector pos); - -protected: - bool CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max); - -protected: - CInstanceManager* m_iMan; - CD3DEngine* m_engine; - - ModelTriangle* m_triangleTable; - int m_triangleUsed; -}; - - diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index 04c478c..735d72a 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -35,10 +35,7 @@ class CApplication; class CInstanceManager; class CObject; - -namespace Snd { class CSound; -}; namespace Gfx { @@ -614,7 +611,7 @@ protected: Gfx::CLightning* m_lightning; Gfx::CPlanet* m_planet; Gfx::CTerrain* m_terrain; - Snd::CSound* m_sound; + CSound* m_sound; bool m_wasInit; std::string m_error; -- cgit v1.2.3-1-g7c22 From 54f4da87923465a5387e2e854b58616647deb7af Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 15 Jul 2012 19:17:49 +0200 Subject: Fix in model loading; simple model viewer - fixed model loading code - added simple model viewer (model_test) in src/graphics/opengl/test - added system time stamp code - split the code in app/system modules to separate headers - added debug messages in model loading - minor fixes in OpenGL engine --- src/app/system.cpp | 270 +++++++-------------- src/app/system.h | 46 ++++ src/app/system_linux.h | 101 ++++++++ src/app/system_other.h | 146 +++++++++++ src/app/system_windows.h | 122 ++++++++++ src/common/singleton.h | 2 +- src/graphics/common/color.h | 21 ++ src/graphics/common/modelfile.cpp | 38 ++- src/graphics/common/modelfile.h | 4 +- src/graphics/common/vertex.h | 34 +++ src/graphics/opengl/gldevice.cpp | 76 +++--- src/graphics/opengl/test/CMakeLists.txt | 42 +++- src/graphics/opengl/test/README.txt | 7 + src/graphics/opengl/test/model_test.cpp | 385 ++++++++++++++++++++++++++++++ src/graphics/opengl/test/texture_test.cpp | 1 + src/math/point.h | 10 + src/math/vector.h | 11 + 17 files changed, 1080 insertions(+), 236 deletions(-) create mode 100644 src/app/system_linux.h create mode 100644 src/app/system_other.h create mode 100644 src/app/system_windows.h create mode 100644 src/graphics/opengl/test/README.txt create mode 100644 src/graphics/opengl/test/model_test.cpp diff --git a/src/app/system.cpp b/src/app/system.cpp index a765e11..eb0321b 100644 --- a/src/app/system.cpp +++ b/src/app/system.cpp @@ -21,19 +21,21 @@ #include "common/config.h" + #if defined(PLATFORM_WINDOWS) -#include +#include "system_windows.h" + #elif defined(PLATFORM_LINUX) -#include +#include "system_linux.h" + #else -#include +#include "system_other.h" + #endif +#include -SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message); -SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message); -SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message); /** * Displays a system dialog with info, error, question etc. message. @@ -45,209 +47,103 @@ SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& */ SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) { - #if defined(PLATFORM_WINDOWS) +#if defined(PLATFORM_WINDOWS) return SystemDialog_Windows(type, title, message); - #elif defined(PLATFORM_LINUX) +#elif defined(PLATFORM_LINUX) return SystemDialog_Linux(type, title, message); - #else +#else return SystemDialog_Other(type, title, message); - #endif +#endif } - - -#if defined(PLATFORM_WINDOWS) - -// Convert a wide Unicode string to an UTF8 string -std::string UTF8_Encode_Windows(const std::wstring &wstr) +SystemTimeStamp* CreateTimeStamp() { - int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); - return strTo; + return new SystemTimeStamp(); } -// Convert an UTF8 string to a wide Unicode String -std::wstring UTF8_Decode_Windows(const std::string &str) +void DestroyTimeStamp(SystemTimeStamp *stamp) { - int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); - return wstrTo; + delete stamp; } -SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message) +void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src) { - unsigned int windowsType = 0; - std::wstring windowsMessage = UTF8_Decode_Windows(message); - std::wstring windowsTitle = UTF8_Decode_Windows(title); - - switch (type) - { - case SDT_INFO: - default: - windowsType = MB_ICONINFORMATION|MB_OK; - break; - case SDT_WARNING: - windowsType = MB_ICONWARNING|MB_OK; - break; - case SDT_ERROR: - windowsType = MB_ICONERROR|MB_OK; - break; - case SDT_YES_NO: - windowsType = MB_ICONQUESTION|MB_YESNO; - break; - case SDT_OK_CANCEL: - windowsType = MB_ICONWARNING|MB_OKCANCEL; - break; - } - - switch (MessageBoxW(NULL, windowsMessage.c_str(), windowsTitle.c_str(), windowsType)) - { - case IDOK: - return SDR_OK; - case IDCANCEL: - return SDR_CANCEL; - case IDYES: - return SDR_YES; - case IDNO: - return SDR_NO; - default: - break; - } - - return SDR_OK; + *dst = *src; } +void GetCurrentTimeStamp(SystemTimeStamp *stamp) +{ +#if defined(PLATFORM_WINDOWS) + GetCurrentTimeStamp_Windows(stamp); #elif defined(PLATFORM_LINUX) + GetCurrentTimeStamp_Linux(stamp); +#else + GetCurrentTimeStamp_Other(stamp); +#endif +} -SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message) +float GetTimeStampResolution(SystemTimeUnit unit) { - std::string options = ""; - switch (type) - { - case SDT_INFO: - default: - options = "--info"; - break; - case SDT_WARNING: - options = "--warning"; - break; - case SDT_ERROR: - options = "--error"; - break; - case SDT_YES_NO: - options = "--question --ok-label=\"Yes\" --cancel-label=\"No\""; - break; - case SDT_OK_CANCEL: - options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\""; - break; - } - - std::string command = "zenity " + options + " --text=\"" + message + "\" --title=\"" + title + "\""; - int code = system(command.c_str()); - - SystemDialogResult result = SDR_OK; - switch (type) - { - case SDT_YES_NO: - result = code ? SDR_NO : SDR_YES; - break; - case SDT_OK_CANCEL: - result = code ? SDR_CANCEL : SDR_OK; - break; - default: - break; - } - + unsigned long long exact = 0; +#if defined(PLATFORM_WINDOWS) + exact = GetTimeStampExactResolution_Windows(); +#elif defined(PLATFORM_LINUX) + exact = GetTimeStampExactResolution_Linux(); +#else + exact = GetTimeStampExactResolution_Other(); +#endif + float result = 0.0f; + if (unit == STU_SEC) + result = exact * 1e-9; + else if (unit == STU_MSEC) + result = exact * 1e-6; + else if (unit == STU_USEC) + result = exact * 1e-3; + else + assert(false); return result; } +long long GetTimeStampExactResolution() +{ +#if defined(PLATFORM_WINDOWS) + return GetTimeStampExactResolution_Windows(); +#elif defined(PLATFORM_LINUX) + return GetTimeStampExactResolution_Linux(); #else + return GetTimeStampExactResolution_Other(); +#endif +} -SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message) +float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit) { - switch (type) - { - case SDT_INFO: - std::cout << "INFO: "; - break; - case SDT_WARNING: - std::cout << "WARNING:"; - break; - case SDT_ERROR: - std::cout << "ERROR: "; - break; - case SDT_YES_NO: - case SDT_OK_CANCEL: - std::cout << "QUESTION: "; - break; - } - - std::cout << message << std::endl; - - std::string line; - - SystemDialogResult result = SDR_OK; - - bool done = false; - while (!done) - { - switch (type) - { - case SDT_INFO: - case SDT_WARNING: - case SDT_ERROR: - std::cout << "Press ENTER to continue"; - break; - - case SDT_YES_NO: - std::cout << "Type 'Y' for Yes or 'N' for No"; - break; - - case SDT_OK_CANCEL: - std::cout << "Type 'O' for OK or 'C' for Cancel"; - break; - } - - std::getline(std::cin, line); - - switch (type) - { - case SDT_INFO: - case SDT_WARNING: - case SDT_ERROR: - done = true; - break; - - case SDT_YES_NO: - if (line == "Y" || line == "y") - { - result = SDR_YES; - done = true; - } - else if (line == "N" || line == "n") - { - result = SDR_NO; - done = true; - } - break; - - case SDT_OK_CANCEL: - if (line == "O" || line == "o") - { - done = true; - result = SDR_OK; - } - else if (line == "C" || line == "c") - { - done = true; - result = SDR_CANCEL; - } - break; - } - } - + long long exact = 0; +#if defined(PLATFORM_WINDOWS) + exact = TimeStampExactDiff_Windows(before, after); +#elif defined(PLATFORM_LINUX) + exact = TimeStampExactDiff_Linux(before, after); +#else + exact = TimeStampExactDiff_Other(before, after); +#endif + float result = 0.0f; + if (unit == STU_SEC) + result = exact * 1e-9; + else if (unit == STU_MSEC) + result = exact * 1e-6; + else if (unit == STU_USEC) + result = exact * 1e-3; + else + assert(false); return result; } -#endif // if defined(PLATFORM_WINDOWS) + +long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) +{ +#if defined(PLATFORM_WINDOWS) + return TimeStampExactDiff_Windows(before, after); +#elif defined(PLATFORM_LINUX) + return TimeStampExactDiff_Linux(before, after); +#else + return TimeStampExactDiff_Other(before, after); +#endif +} diff --git a/src/app/system.h b/src/app/system.h index 3bf6457..3c04760 100644 --- a/src/app/system.h +++ b/src/app/system.h @@ -23,6 +23,8 @@ #include +/* Dialog utils */ + /** * \enum SysDialogType * \brief Type of system dialog @@ -57,3 +59,47 @@ enum SystemDialogResult //! Displays a system dialog SystemDialogResult SystemDialog(SystemDialogType, const std::string &title, const std::string &message); + + +/* Time utils */ + +enum SystemTimeUnit +{ + //! seconds + STU_SEC, + //! milliseconds + STU_MSEC, + //! microseconds + STU_USEC +}; + +/* Forward declaration of time stamp struct + * SystemTimeStamp should be used in a pointer context. + * The implementation details are hidden because of platform dependence. */ +struct SystemTimeStamp; + +//! Creates a new time stamp object +SystemTimeStamp* CreateTimeStamp(); + +//! Destroys a time stamp object +void DestroyTimeStamp(SystemTimeStamp *stamp); + +//! Copies the time stamp from \a src to \a dst +void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src); + +//! Returns a time stamp associated with current time +void GetCurrentTimeStamp(SystemTimeStamp *stamp); + +//! Returns the platform's expected time stamp resolution +float GetTimeStampResolution(SystemTimeUnit unit = STU_SEC); + +//! Returns the platform's exact (in nanosecond units) expected time stamp resolution +long long GetTimeStampExactResolution(); + +//! Returns a difference between two timestamps in given time unit +/** The difference is \a after - \a before. */ +float TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit = STU_SEC); + +//! Returns the exact (in nanosecond units) difference between two timestamps +/** The difference is \a after - \a before. */ +long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after); diff --git a/src/app/system_linux.h b/src/app/system_linux.h new file mode 100644 index 0000000..f58c9a1 --- /dev/null +++ b/src/app/system_linux.h @@ -0,0 +1,101 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// system_linux.h + +/* This header contains Linux-specific code for system utils + from system.h. There is no separate .cpp module for simplicity.*/ + +#include +#include +#include + + +SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message); + +void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp); +long long GetTimeStampExactResolution_Linux(); +long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after); + +struct SystemTimeStamp +{ + timespec clockTime; + + SystemTimeStamp() + { + clockTime.tv_sec = clockTime.tv_nsec = 0; + } +}; + + +SystemDialogResult SystemDialog_Linux(SystemDialogType type, const std::string& title, const std::string& message) +{ + std::string options = ""; + switch (type) + { + case SDT_INFO: + default: + options = "--info"; + break; + case SDT_WARNING: + options = "--warning"; + break; + case SDT_ERROR: + options = "--error"; + break; + case SDT_YES_NO: + options = "--question --ok-label=\"Yes\" --cancel-label=\"No\""; + break; + case SDT_OK_CANCEL: + options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\""; + break; + } + + std::string command = "zenity " + options + " --text=\"" + message + "\" --title=\"" + title + "\""; + int code = system(command.c_str()); + + SystemDialogResult result = SDR_OK; + switch (type) + { + case SDT_YES_NO: + result = code ? SDR_NO : SDR_YES; + break; + case SDT_OK_CANCEL: + result = code ? SDR_CANCEL : SDR_OK; + break; + default: + break; + } + + return result; +} + +void GetCurrentTimeStamp_Linux(SystemTimeStamp *stamp) +{ + clock_gettime(CLOCK_MONOTONIC, &stamp->clockTime); +} + +long long GetTimeStampExactResolution_Linux() +{ + return 1ll; +} + +long long TimeStampExactDiff_Linux(SystemTimeStamp *before, SystemTimeStamp *after) +{ + return (after->clockTime.tv_nsec - before->clockTime.tv_nsec) + + (after->clockTime.tv_sec - before->clockTime.tv_sec) * 1000000000ll; +} diff --git a/src/app/system_other.h b/src/app/system_other.h new file mode 100644 index 0000000..9f13ffa --- /dev/null +++ b/src/app/system_other.h @@ -0,0 +1,146 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// system_other.h + +/* This header contains fallback code for other platforms for system utils + from system.h. There is no separate .cpp module for simplicity.*/ + +#include + +#include + + +SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message); + +void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp); +long long GetTimeStampExactResolution_Other(); +long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after); + +struct SystemTimeStamp +{ + Uint32 sdlTicks; + + SystemTimeStamp() + { + sdlTicks = 0; + } +}; + + +SystemDialogResult SystemDialog_Other(SystemDialogType type, const std::string& title, const std::string& message) +{ + switch (type) + { + case SDT_INFO: + std::cout << "INFO: "; + break; + case SDT_WARNING: + std::cout << "WARNING:"; + break; + case SDT_ERROR: + std::cout << "ERROR: "; + break; + case SDT_YES_NO: + case SDT_OK_CANCEL: + std::cout << "QUESTION: "; + break; + } + + std::cout << message << std::endl; + + std::string line; + + SystemDialogResult result = SDR_OK; + + bool done = false; + while (!done) + { + switch (type) + { + case SDT_INFO: + case SDT_WARNING: + case SDT_ERROR: + std::cout << "Press ENTER to continue"; + break; + + case SDT_YES_NO: + std::cout << "Type 'Y' for Yes or 'N' for No"; + break; + + case SDT_OK_CANCEL: + std::cout << "Type 'O' for OK or 'C' for Cancel"; + break; + } + + std::getline(std::cin, line); + + switch (type) + { + case SDT_INFO: + case SDT_WARNING: + case SDT_ERROR: + done = true; + break; + + case SDT_YES_NO: + if (line == "Y" || line == "y") + { + result = SDR_YES; + done = true; + } + else if (line == "N" || line == "n") + { + result = SDR_NO; + done = true; + } + break; + + case SDT_OK_CANCEL: + if (line == "O" || line == "o") + { + done = true; + result = SDR_OK; + } + else if (line == "C" || line == "c") + { + done = true; + result = SDR_CANCEL; + } + break; + } + } + + return result; +} + + + +void GetCurrentTimeStamp_Other(SystemTimeStamp *stamp) +{ + stamp->sdlTicks = SDL_GetTicks(); +} + +long long GetTimeStampExactResolution_Other() +{ + return 1000000ll; +} + +long long TimeStampExactDiff_Other(SystemTimeStamp *before, SystemTimeStamp *after) +{ + return (after->sdlTicks - before->sdlTicks) * 1000000ll; +} diff --git a/src/app/system_windows.h b/src/app/system_windows.h new file mode 100644 index 0000000..eb6beec --- /dev/null +++ b/src/app/system_windows.h @@ -0,0 +1,122 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// system_windows.h + +/* This header contains Windows-specific code for system utils + from system.h. There is no separate .cpp module for simplicity.*/ + +#include + + +std::string UTF8_Encode_Windows(const std::wstring &wstr); +std::wstring UTF8_Decode_Windows(const std::string &str); +SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message); + +void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp); +long long GetTimeStampExactResolution_Windows(); +long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after); + +struct SystemTimeStamp +{ + FILETIME fileTime; + + SystemTimeStamp() + { + fileTime.dwHighDateTime = fileTime.dwLowDateTime = 0; + } +}; + + +// Convert a wide Unicode string to an UTF8 string +std::string UTF8_Encode_Windows(const std::wstring &wstr) +{ + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); + return strTo; +} + +// Convert an UTF8 string to a wide Unicode String +std::wstring UTF8_Decode_Windows(const std::string &str) +{ + int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); + std::wstring wstrTo(size_needed, 0); + MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); + return wstrTo; +} + +SystemDialogResult SystemDialog_Windows(SystemDialogType type, const std::string& title, const std::string& message) +{ + unsigned int windowsType = 0; + std::wstring windowsMessage = UTF8_Decode_Windows(message); + std::wstring windowsTitle = UTF8_Decode_Windows(title); + + switch (type) + { + case SDT_INFO: + default: + windowsType = MB_ICONINFORMATION|MB_OK; + break; + case SDT_WARNING: + windowsType = MB_ICONWARNING|MB_OK; + break; + case SDT_ERROR: + windowsType = MB_ICONERROR|MB_OK; + break; + case SDT_YES_NO: + windowsType = MB_ICONQUESTION|MB_YESNO; + break; + case SDT_OK_CANCEL: + windowsType = MB_ICONWARNING|MB_OKCANCEL; + break; + } + + switch (MessageBoxW(NULL, windowsMessage.c_str(), windowsTitle.c_str(), windowsType)) + { + case IDOK: + return SDR_OK; + case IDCANCEL: + return SDR_CANCEL; + case IDYES: + return SDR_YES; + case IDNO: + return SDR_NO; + default: + break; + } + + return SDR_OK; +} + + +void GetCurrentTimeStamp_Windows(SystemTimeStamp *stamp) +{ + GetSystemTimeAsFileTime(&stamp->fileTime); +} + +long long GetTimeStampExactResolution_Windows() +{ + return 100ll; +} + +long long TimeStampExactDiff_Windows(SystemTimeStamp *before, SystemTimeStamp *after) +{ + long long tH = (1ll << 32) * (after->fileTime.dwHighDateTime - before->fileTime.dwHighDateTime); + long long tL = after->fileTime.dwLowDateTime - before->fileTime.dwLowDateTime; + return (tH + tL) * 100ll; +} diff --git a/src/common/singleton.h b/src/common/singleton.h index dc09645..ee01b24 100644 --- a/src/common/singleton.h +++ b/src/common/singleton.h @@ -47,7 +47,7 @@ template class CSingleton mInstance = static_cast(this); } - ~CSingleton() { + virtual ~CSingleton() { mInstance = NULL; } diff --git a/src/graphics/common/color.h b/src/graphics/common/color.h index 3b19cf2..68421c7 100644 --- a/src/graphics/common/color.h +++ b/src/graphics/common/color.h @@ -19,6 +19,9 @@ #pragma once +#include + + namespace Gfx { /** @@ -38,6 +41,15 @@ struct Color { return (float*)this; } + + //! Returns a string (r, g, b, a) + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(" << r << ", " << g << ", " << b << ", " << a << ")"; + return s.str(); + } }; /** @@ -49,6 +61,15 @@ struct ColorHSV ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f) : h(aH), s(aS), v(aV) {} + + //! Returns a string "(h, s, v)" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(" << h << ", " << s << ", " << v << ")"; + return s.str(); + } }; //! Converts a RGB color to HSV color diff --git a/src/graphics/common/modelfile.cpp b/src/graphics/common/modelfile.cpp index 8e509fe..22801e8 100644 --- a/src/graphics/common/modelfile.cpp +++ b/src/graphics/common/modelfile.cpp @@ -21,6 +21,7 @@ #include "common/iman.h" #include "common/ioutils.h" +#include "common/logger.h" #include "common/stringutils.h" #include "math/geometry.h" @@ -205,8 +206,6 @@ Gfx::Material ReadBinaryMaterial(std::istream &stream) /* power = */ IOUtils::ReadBinaryFloat(stream); - /* padding? */ IOUtils::ReadBinary<2, unsigned int>(stream); - return result; } @@ -233,8 +232,6 @@ void WriteBinaryMaterial(Gfx::Material material, std::ostream &stream) /* emissive.a */ IOUtils::WriteBinaryFloat(0.0f, stream); /* power */ IOUtils::WriteBinaryFloat(0.0f, stream); - - /* padding? */ IOUtils::WriteBinary<2, unsigned int>(0, stream); } Gfx::ModelTriangle::ModelTriangle() @@ -390,6 +387,8 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) t.used = IOUtils::ReadBinary<1, char>(stream); t.selected = IOUtils::ReadBinary<1, char>(stream); + /* padding */ IOUtils::ReadBinary<2, unsigned int>(stream); + t.p1 = ReadBinaryVertexTex2(stream); t.p2 = ReadBinaryVertexTex2(stream); t.p3 = ReadBinaryVertexTex2(stream); @@ -422,16 +421,38 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) triangle.min = t.min; triangle.max = t.max; triangle.state = t.state; - sprintf(tex2Name, "dirty%.2d.tga", t.texNum2); // hardcoded as in the original code + + if (t.texNum2 != 0) + sprintf(tex2Name, "dirty%.2d.tga", t.texNum2); // hardcoded as in the original code + triangle.tex2Name = std::string(tex2Name); m_triangles.push_back(triangle); - } + } } for (int i = 0; i < (int) m_triangles.size(); ++i) + { m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "tga"); + GetLogger()->Info("ModelTriangle %d\n", i+1); + std::string s1 = m_triangles[i].p1.ToString(); + GetLogger()->Info(" p1: %s\n", s1.c_str()); + std::string s2 = m_triangles[i].p2.ToString(); + GetLogger()->Info(" p2: %s\n", s2.c_str()); + std::string s3 = m_triangles[i].p3.ToString(); + GetLogger()->Info(" p3: %s\n", s3.c_str()); + + std::string d = m_triangles[i].material.diffuse.ToString(); + std::string a = m_triangles[i].material.ambient.ToString(); + std::string s = m_triangles[i].material.specular.ToString(); + GetLogger()->Info(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); + + GetLogger()->Info(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), m_triangles[i].tex2Name.c_str()); + GetLogger()->Info(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); + GetLogger()->Info(" state: %ld\n", m_triangles[i].state); + } + /* if (! edit) { @@ -769,6 +790,11 @@ void Gfx::CModelFile::Mirror() } } +std::vector& Gfx::CModelFile::GetTriangles() +{ + return m_triangles; +} + int Gfx::CModelFile::GetTriangleCount() { return m_triangles.size(); diff --git a/src/graphics/common/modelfile.h b/src/graphics/common/modelfile.h index 625df50..f8cb022 100644 --- a/src/graphics/common/modelfile.h +++ b/src/graphics/common/modelfile.h @@ -91,6 +91,8 @@ public: //! Returns the number of triangles in model int GetTriangleCount(); + //! Returns the triangle vector + std::vector& GetTriangles(); //! Returns the height of model -- closest point to X and Z coords of \a pos float GetHeight(Math::Vector pos); @@ -106,7 +108,7 @@ protected: protected: CInstanceManager* m_iMan; - CEngine* m_engine; + Gfx::CEngine* m_engine; //! Last error std::string m_error; diff --git a/src/graphics/common/vertex.h b/src/graphics/common/vertex.h index c6375b9..0a74587 100644 --- a/src/graphics/common/vertex.h +++ b/src/graphics/common/vertex.h @@ -23,6 +23,8 @@ #include "math/vector.h" #include "math/point.h" +#include + namespace Gfx { /** @@ -46,6 +48,17 @@ struct Vertex Math::Vector aNormal = Math::Vector(), Math::Point aTexCoord = Math::Point()) : coord(aCoord), normal(aNormal), texCoord(aTexCoord) {} + + + //! Returns a string "(c: [...], n: [...], tc: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", n: " << normal.ToString() + << ", tc: " << texCoord.ToString() << ")"; + return s.str(); + } }; /** @@ -72,6 +85,16 @@ struct VertexCol Gfx::Color aSpecular = Gfx::Color(), Math::Point aTexCoord = Math::Point()) : coord(aCoord), color(aColor), specular(aSpecular), texCoord(aTexCoord) {} + + //! Returns a string "(c: [...], col: [...], sp: [...], tc: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", col: " << color.ToString() << ", sp: " + << specular.ToString() << ", tc: " << texCoord.ToString() << ")"; + return s.str(); + } }; @@ -95,6 +118,7 @@ struct VertexTex2 Math::Point aTexCoord2 = Math::Point()) : coord(aCoord), normal(aNormal), texCoord(aTexCoord), texCoord2(aTexCoord2) {} + //! Sets the fields from Gfx::Vertex with texCoord2 = (0,0) void FromVertex(const Gfx::Vertex &v) { coord = v.coord; @@ -102,6 +126,16 @@ struct VertexTex2 texCoord = v.texCoord; texCoord2 = Math::Point(); } + + //! Returns a string "(c: [...], n: [...], tc: [...], tc2: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", n: " << normal.ToString() + << ", tc: " << texCoord.ToString() << ", tc2: " << texCoord2.ToString() << ")"; + return s.str(); + } }; }; // namespace Gfx diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index f253568..eb4eb31 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -19,6 +19,8 @@ #include "common/image.h" #include "graphics/opengl/gldevice.h" +#define GL_GLEXT_PROTOTYPES + #include #include #include @@ -121,6 +123,9 @@ bool Gfx::CGLDevice::Create() // So turn it on permanently glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + // To use separate specular color in drawing primitives + glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); + // Set just to be sure glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glMatrixMode(GL_PROJECTION); @@ -174,7 +179,7 @@ void Gfx::CGLDevice::BeginScene() void Gfx::CGLDevice::EndScene() { - glFlush(); + glFinish(); } void Gfx::CGLDevice::Clear() @@ -256,9 +261,9 @@ void Gfx::CGLDevice::SetMaterial(Gfx::Material &material) { m_material = material; - glMaterialfv(GL_AMBIENT, GL_FRONT_AND_BACK, m_material.ambient.Array()); - glMaterialfv(GL_DIFFUSE, GL_FRONT_AND_BACK, m_material.diffuse.Array()); - glMaterialfv(GL_SPECULAR, GL_FRONT_AND_BACK, m_material.specular.Array()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_material.ambient.Array()); + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_material.diffuse.Array()); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_material.specular.Array()); } const Gfx::Material& Gfx::CGLDevice::GetMaterial() @@ -517,78 +522,77 @@ void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶m // Don't actually do anything if texture not set if (m_textures[index] == NULL) - { - printf("No texture set: %d\n", index); return; - } // Enable the given stage glActiveTextureARB(GL_TEXTURE0_ARB + index); glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_textures[index]->id); + // Selection of operation and arguments glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); // Color operation if (params.colorOperation == Gfx::TEX_MIX_OPER_MODULATE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB_ARB, GL_MODULATE); else if (params.colorOperation == Gfx::TEX_MIX_OPER_ADD) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_ADD); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB_ARB, GL_ADD); else assert(false); // Color arg1 if (params.colorArg1 == Gfx::TEX_MIX_ARG_CURRENT) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS); // that's right - stupid D3D enum values - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS); // that's right - stupid D3D enum values + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR); } else if (params.colorArg1 == Gfx::TEX_MIX_ARG_TEXTURE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR); } else if (params.colorArg1 == Gfx::TEX_MIX_ARG_DIFFUSE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PRIMARY_COLOR); // here as well - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR); // here as well + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR); } else assert(false); // Color arg2 if (params.colorArg2 == Gfx::TEX_MIX_ARG_CURRENT) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR); } else if (params.colorArg2 == Gfx::TEX_MIX_ARG_TEXTURE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR); } else if (params.colorArg2 == Gfx::TEX_MIX_ARG_DIFFUSE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR); } else assert(false); // Alpha operation if (params.alphaOperation == Gfx::TEX_MIX_OPER_MODULATE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA_ARB, GL_MODULATE); else if (params.alphaOperation == Gfx::TEX_MIX_OPER_ADD) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_ADD); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA_ARB, GL_ADD); else assert(false); // Alpha arg1 if (params.alphaArg1 == Gfx::TEX_MIX_ARG_CURRENT) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA); } else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_TEXTURE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA); } else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_DIFFUSE) { @@ -600,18 +604,18 @@ void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶m // Alpha arg2 if (params.alphaArg2 == Gfx::TEX_MIX_ARG_CURRENT) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA); } else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_TEXTURE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA); } else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_DIFFUSE) { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA); } else assert(false); @@ -670,7 +674,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, in else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) glBegin(GL_TRIANGLE_STRIP); - glColor3f(1.0f, 1.0f, 1.0f); // TODO: set global color? + glColor3f(1.0f, 1.0f, 1.0f); for (int i = 0; i < vertexCount; ++i) { @@ -693,8 +697,8 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vert for (int i = 0; i < vertexCount; ++i) { - // TODO: specular through EXT_separate_specular_color? glColor4fv((GLfloat*)vertices[i].color.Array()); + glSecondaryColor3fv((GLfloat*)vertices[i].specular.Array()); glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array()); glVertex3fv((GLfloat*)vertices[i].coord.Array()); } @@ -750,7 +754,7 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) switch (state) { - case Gfx::RENDER_STATE_LIGHTING: flag = GL_DEPTH_WRITEMASK; break; + case Gfx::RENDER_STATE_LIGHTING: flag = GL_LIGHTING; break; case Gfx::RENDER_STATE_BLENDING: flag = GL_BLEND; break; case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index ce2a83f..36bd738 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -8,16 +8,46 @@ find_package(PNG REQUIRED) set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") - -set(SOURCES +if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") + set(PLATFORM_WINDOWS 1) + set(PLATFORM_LINUX 0) + set(PLATFORM_OTHER 0) +elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + set(PLATFORM_WINDOWS 0) + set(PLATFORM_LINUX 1) + set(PLATFORM_OTHER 0) +else() + set(PLATFORM_WINDOWS 0) + set(PLATFORM_LINUX 0) + set(PLATFORM_OTHER 1) +endif() + +configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h) + + +set(TEXTURE_SOURCES ../gldevice.cpp ../../common/device.cpp +../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp ) -include_directories(../../../) +set(MODEL_SOURCES +../gldevice.cpp +../../common/device.cpp +../../common/modelfile.cpp +../../common/texture.cpp +../../../common/logger.cpp +../../../common/image.cpp +../../../common/iman.cpp +../../../common/stringutils.cpp +../../../app/system.cpp +model_test.cpp +) + +include_directories(../../../ ${CMAKE_CURRENT_BINARY_DIR}) set(LIBS ${SDL_LIBRARY} @@ -26,6 +56,8 @@ ${OPENGL_LIBRARY} ${PNG_LIBRARIES} ) -add_executable(texture_test ${SOURCES}) - +add_executable(texture_test ${TEXTURE_SOURCES}) target_link_libraries(texture_test ${LIBS}) + +add_executable(model_test ${MODEL_SOURCES}) +target_link_libraries(model_test ${LIBS}) diff --git a/src/graphics/opengl/test/README.txt b/src/graphics/opengl/test/README.txt new file mode 100644 index 0000000..c309f23 --- /dev/null +++ b/src/graphics/opengl/test/README.txt @@ -0,0 +1,7 @@ +Test programs for OpenGL engine: + - texture_test -> multitexturing test with 2 textures (included as files: ./tex1.png, ./tex2.png) + - model_test -> simple model viewer to test model loading + usage: ./model_test {dxf|mod} model_file + second argument is the loaded format (DXF or Colobot .mod files) + requires ./tex folder (or symlink) with Colobot textures + viewer is controlled from keyboard - the bindings can be found in code diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp new file mode 100644 index 0000000..b73dc71 --- /dev/null +++ b/src/graphics/opengl/test/model_test.cpp @@ -0,0 +1,385 @@ +#include "app/system.h" +#include "common/logger.h" +#include "common/image.h" +#include "common/iman.h" +#include "graphics/common/modelfile.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include +#include + +enum KeySlots +{ + K_RotXUp, + K_RotXDown, + K_RotYLeft, + K_RotYRight, + K_Forward, + K_Back, + K_Left, + K_Right, + K_Up, + K_Down, + K_Count +}; +bool KEYMAP[K_Count] = { false }; + +Math::Vector TRANSLATION(0.0f, 0.0f, 30.0f); +Math::Vector ROTATION; + +const int FRAME_DELAY = 5000; + +std::map TEXS; + +SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; + +Gfx::Texture* GetTexture(const std::string &name) +{ + std::map::iterator it = TEXS.find(name); + if (it == TEXS.end()) + return NULL; + + return (*it).second; +} + +void LoadTexture(Gfx::CGLDevice *device, const std::string &name) +{ + if (name.empty()) + return; + + if (GetTexture(name) != NULL) + return; + + Gfx::Texture *tex = NULL; + + CImage img; + if (! img.Load(std::string("tex/") + name)) + { + std::string err = img.GetError(); + GetLogger()->Error("Texture not loaded, error: %s!\n", err.c_str()); + } + else + { + Gfx::TextureCreateParams texCreateParams; + texCreateParams.alpha = false; + texCreateParams.mipmap = true; + texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; + texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; + texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; + + tex = device->CreateTexture(&img, texCreateParams); + } + + TEXS[name] = tex; +} + +void Init(Gfx::CGLDevice *device, Gfx::CModelFile *model) +{ + std::vector &triangles = model->GetTriangles(); + + for (int i = 0; i < (int) triangles.size(); ++i) + { + LoadTexture(device, triangles[i].tex1Name); + LoadTexture(device, triangles[i].tex2Name); + } + + device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + device->SetRenderState(Gfx::RENDER_STATE_LIGHTING, true); + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); + device->SetShadeModel(Gfx::SHADE_SMOOTH); + + Gfx::Light light; + light.type = Gfx::LIGHT_DIRECTIONAL; + light.ambient = Gfx::Color(0.4f, 0.4f, 0.4f, 0.0f); + light.diffuse = Gfx::Color(0.8f, 0.8f, 0.8f, 0.0f); + light.specular = Gfx::Color(0.2f, 0.2f, 0.2f, 0.0f); + light.position = Math::Vector(0.0f, 0.0f, -1.0f); + light.direction = Math::Vector(0.0f, 0.0f, 1.0f); + + device->SetGlobalAmbient(Gfx::Color(0.5f, 0.5f, 0.5f, 0.0f)); + device->SetLight(0, light); + device->SetLightEnabled(0, true); +} + +void Render(Gfx::CGLDevice *device, Gfx::CModelFile *modelFile) +{ + device->BeginScene(); + + Math::Matrix persp; + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); + + Math::Matrix id; + id.LoadIdentity(); + device->SetTransform(Gfx::TRANSFORM_WORLD, id); + + Math::Matrix viewMat; + Math::LoadTranslationMatrix(viewMat, TRANSLATION); + Math::Matrix rot; + Math::LoadRotationXZYMatrix(rot, ROTATION); + viewMat = Math::MultiplyMatrices(viewMat, rot); + device->SetTransform(Gfx::TRANSFORM_VIEW, viewMat); + + std::vector &triangles = modelFile->GetTriangles(); + + Gfx::VertexTex2 tri[3]; + + for (int i = 0; i < (int) triangles.size(); ++i) + { + device->SetTexture(0, GetTexture(triangles[i].tex1Name)); + device->SetTexture(1, GetTexture(triangles[i].tex2Name)); + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + + Gfx::TextureParams tex1Params; + tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(0, tex1Params); + + Gfx::TextureParams tex2Params; + tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(1, tex2Params); + + device->SetMaterial(triangles[i].material); + + tri[0] = triangles[i].p1; + tri[1] = triangles[i].p2; + tri[2] = triangles[i].p3; + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, tri, 3); + } + + device->EndScene(); +} + +void Update() +{ + const float ROT_SPEED = 80.0f * Math::DEG_TO_RAD; // rad / sec + const float TRANS_SPEED = 3.0f; // units / sec + + GetCurrentTimeStamp(CURR_TIME); + float timeDiff = TimeStampDiff(PREV_TIME, CURR_TIME, STU_SEC); + CopyTimeStamp(PREV_TIME, CURR_TIME); + + if (KEYMAP[K_RotYLeft]) + ROTATION.y -= ROT_SPEED * timeDiff; + if (KEYMAP[K_RotYRight]) + ROTATION.y += ROT_SPEED * timeDiff; + if (KEYMAP[K_RotXDown]) + ROTATION.x -= ROT_SPEED * timeDiff; + if (KEYMAP[K_RotXUp]) + ROTATION.x += ROT_SPEED * timeDiff; + + if (KEYMAP[K_Forward]) + TRANSLATION.z -= TRANS_SPEED * timeDiff; + if (KEYMAP[K_Back]) + TRANSLATION.z += TRANS_SPEED * timeDiff; + if (KEYMAP[K_Left]) + TRANSLATION.x += TRANS_SPEED * timeDiff; + if (KEYMAP[K_Right]) + TRANSLATION.x -= TRANS_SPEED * timeDiff; + if (KEYMAP[K_Up]) + TRANSLATION.y += TRANS_SPEED * timeDiff; + if (KEYMAP[K_Down]) + TRANSLATION.y -= TRANS_SPEED * timeDiff; +} + +void KeyboardDown(SDLKey key) +{ + switch (key) + { + case SDLK_LEFT: + KEYMAP[K_RotYLeft] = true; + break; + case SDLK_RIGHT: + KEYMAP[K_RotYRight] = true; + break; + case SDLK_UP: + KEYMAP[K_RotXUp] = true; + break; + case SDLK_DOWN: + KEYMAP[K_RotXDown] = true; + break; + case SDLK_w: + KEYMAP[K_Forward] = true; + break; + case SDLK_s: + KEYMAP[K_Back] = true; + break; + case SDLK_a: + KEYMAP[K_Left] = true; + break; + case SDLK_d: + KEYMAP[K_Right] = true; + break; + case SDLK_z: + KEYMAP[K_Down] = true; + break; + case SDLK_x: + KEYMAP[K_Up] = true; + break; + default: + break; + } +} + +void KeyboardUp(SDLKey key) +{ + switch (key) + { + case SDLK_LEFT: + KEYMAP[K_RotYLeft] = false; + break; + case SDLK_RIGHT: + KEYMAP[K_RotYRight] = false; + break; + case SDLK_UP: + KEYMAP[K_RotXUp] = false; + break; + case SDLK_DOWN: + KEYMAP[K_RotXDown] = false; + break; + case SDLK_w: + KEYMAP[K_Forward] = false; + break; + case SDLK_s: + KEYMAP[K_Back] = false; + break; + case SDLK_a: + KEYMAP[K_Left] = false; + break; + case SDLK_d: + KEYMAP[K_Right] = false; + break; + case SDLK_z: + KEYMAP[K_Down] = false; + break; + case SDLK_x: + KEYMAP[K_Up] = false; + break; + default: + break; + } +} + +int main(int argc, char *argv[]) +{ + CLogger logger; + + PREV_TIME = CreateTimeStamp(); + CURR_TIME = CreateTimeStamp(); + + GetCurrentTimeStamp(PREV_TIME); + GetCurrentTimeStamp(CURR_TIME); + + if (argc != 3) + { + std::cerr << "Usage: " << argv[0] << "{mod|dxf} model_file" << std::endl; + return 1; + } + + CInstanceManager iMan; + + Gfx::CModelFile *modelFile = new Gfx::CModelFile(&iMan); + if (std::string(argv[1]) == "mod") + { + if (! modelFile->ReadModel(argv[2], false, false)) + { + std::cerr << "Error reading MOD: " << modelFile->GetError() << std::endl; + return 1; + } + } + else if (std::string(argv[1]) == "dxf") + { + if (! modelFile->ReadDXF(argv[2], 0.0f, 0.0f)) + { + std::cerr << "Error reading DXF: " << modelFile->GetError() << std::endl; + return 1; + } + } + else + { + std::cerr << "Usage: " << argv[0] << "{mod|dxf} model_file" << std::endl; + return 1; + } + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Model Test", "Model Test"); + + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device, modelFile); + + bool done = false; + while (! done) + { + Render(device, modelFile); + Update(); + + SDL_GL_SwapBuffers(); + + SDL_Event event; + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) + done = true; + else if (event.type == SDL_KEYDOWN) + KeyboardDown(event.key.keysym.sym); + else if (event.type == SDL_KEYUP) + KeyboardUp(event.key.keysym.sym); + + usleep(FRAME_DELAY); + } + + delete modelFile; + + device->Destroy(); + delete device; + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + DestroyTimeStamp(PREV_TIME); + DestroyTimeStamp(CURR_TIME); + + return 0; +} diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 764e127..79518f8 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -170,6 +170,7 @@ int main() } device->Destroy(); + delete device; SDL_FreeSurface(surface); diff --git a/src/math/point.h b/src/math/point.h index d6768c8..80f80f4 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -24,6 +24,7 @@ #include "func.h" #include +#include // Math module namespace @@ -147,6 +148,15 @@ struct Point return Point(left.x / right, left.y / right); } + + //! Returns a string "[x, y]" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "[" << x << ", " << y << "]"; + return s.str(); + } }; // struct Point diff --git a/src/math/vector.h b/src/math/vector.h index 41f11a8..65fc2f9 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -24,6 +24,7 @@ #include "func.h" #include +#include // Math module namespace @@ -202,6 +203,16 @@ struct Vector return Vector(left.x / right, left.y / right, left.z / right); } + + //! Returns a string "[x, y, z]" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "[" << x << ", " << y << ", " << z << "]"; + return s.str(); + } + }; // struct Point //! Checks if two vectors are equal within given \a tolerance -- cgit v1.2.3-1-g7c22 From 68a7bafe37adef0e5ef12c2d0e8461a21e05363b Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 16 Jul 2012 19:17:26 +0200 Subject: Fixes in texture loading - added other texture formats: BGR and BGRA - fixed texture loading in model viewer - moved code from texture.cpp module to texture.h --- src/CMakeLists.txt | 1 - src/graphics/common/texture.cpp | 43 ------------------------------- src/graphics/common/texture.h | 38 ++++++++++++++++++++++++--- src/graphics/opengl/gldevice.cpp | 10 +++++-- src/graphics/opengl/test/CMakeLists.txt | 6 +++-- src/graphics/opengl/test/model_test.cpp | 5 +++- src/graphics/opengl/test/texture_test.cpp | 4 +-- 7 files changed, 52 insertions(+), 55 deletions(-) delete mode 100644 src/graphics/common/texture.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7f52e8d..fc3cfe3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,7 +52,6 @@ graphics/common/planet.cpp graphics/common/pyro.cpp graphics/common/terrain.cpp graphics/common/text.cpp -graphics/common/texture.cpp graphics/common/water.cpp graphics/opengl/gldevice.cpp graphics/opengl/glengine.cpp diff --git a/src/graphics/common/texture.cpp b/src/graphics/common/texture.cpp deleted file mode 100644 index 50e71cd..0000000 --- a/src/graphics/common/texture.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// texture.cpp - -#include "graphics/common/texture.h" - - -void Gfx::TextureCreateParams::LoadDefault() -{ - alpha = false; - mipmap = false; - - minFilter = Gfx::TEX_MIN_FILTER_NEAREST; - magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - - wrapS = Gfx::TEX_WRAP_REPEAT; - wrapT = Gfx::TEX_WRAP_REPEAT; -} - -void Gfx::TextureParams::LoadDefault() -{ - colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; - colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - - alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; - alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; -} diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index 55b2fc2..fa374ac 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -20,6 +20,17 @@ namespace Gfx { +/** + \enum TexImgFormat + \brief Format of image data */ +enum TexImgFormat +{ + TEX_IMG_RGB, + TEX_IMG_BGR, + TEX_IMG_RGBA, + TEX_IMG_BGRA +}; + /** \enum TexMinFilter \brief Minification texture filter @@ -81,10 +92,10 @@ enum TexMixArgument */ struct TextureCreateParams { - //! Whether the texture image contains alpha - bool alpha; //! Whether to generate mipmaps bool mipmap; + //! Format of source image data + Gfx::TexImgFormat format; //! Minification filter Gfx::TexMinFilter minFilter; //! Magnification filter @@ -99,7 +110,17 @@ struct TextureCreateParams { LoadDefault(); } //! Loads the default values - void LoadDefault(); + inline void LoadDefault() + { + format = Gfx::TEX_IMG_RGB; + mipmap = false; + + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + + wrapS = Gfx::TEX_WRAP_REPEAT; + wrapT = Gfx::TEX_WRAP_REPEAT; + } }; /** @@ -126,7 +147,16 @@ struct TextureParams { LoadDefault(); } //! Loads the default values - void LoadDefault(); + inline void LoadDefault() + { + colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + } }; /** \struct Texture*/ diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index eb4eb31..e329ff4 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -393,10 +393,16 @@ Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCre glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); GLenum sourceFormat = 0; - if (params.alpha) + if (params.format == Gfx::TEX_IMG_RGB) + sourceFormat = GL_RGB; + else if (params.format == Gfx::TEX_IMG_BGR) + sourceFormat = GL_BGR; + else if (params.format == Gfx::TEX_IMG_RGBA) sourceFormat = GL_RGBA; + else if (params.format == Gfx::TEX_IMG_BGRA) + sourceFormat = GL_BGRA; else - sourceFormat = GL_RGB; + assert(false); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->surface->w, data->surface->h, 0, sourceFormat, GL_UNSIGNED_BYTE, data->surface->pixels); diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 36bd738..4242c77 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -8,6 +8,8 @@ find_package(PNG REQUIRED) set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") +set(ADD_LIBS "") + if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(PLATFORM_WINDOWS 1) set(PLATFORM_LINUX 0) @@ -16,6 +18,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 1) set(PLATFORM_OTHER 0) + set(ADD_LIBS "-lrt") else() set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 0) @@ -28,7 +31,6 @@ configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common set(TEXTURE_SOURCES ../gldevice.cpp ../../common/device.cpp -../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp @@ -38,7 +40,6 @@ set(MODEL_SOURCES ../gldevice.cpp ../../common/device.cpp ../../common/modelfile.cpp -../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp @@ -54,6 +55,7 @@ ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${PNG_LIBRARIES} +${ADD_LIBS} ) add_executable(texture_test ${TEXTURE_SOURCES}) diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index b73dc71..88404b7 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -66,8 +66,11 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) else { Gfx::TextureCreateParams texCreateParams; - texCreateParams.alpha = false; texCreateParams.mipmap = true; + if (img.GetData()->surface->format->Amask == 0) + texCreateParams.format = Gfx::TEX_IMG_BGR; + else + texCreateParams.format = Gfx::TEX_IMG_BGRA; texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 79518f8..03847fc 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -27,15 +27,15 @@ void Init(Gfx::CGLDevice *device) } Gfx::TextureCreateParams tex1CreateParams; - tex1CreateParams.alpha = true; tex1CreateParams.mipmap = true; + tex1CreateParams.format = Gfx::TEX_IMG_RGBA; tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; Gfx::TextureCreateParams tex2CreateParams; - tex2CreateParams.alpha = true; tex2CreateParams.mipmap = true; + tex2CreateParams.format = Gfx::TEX_IMG_RGBA; tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; -- cgit v1.2.3-1-g7c22 From f364f378cf497faf61d78aadd8f1aebce678c0ec Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 18 Jul 2012 19:08:34 +0200 Subject: Fixed OpenGL transformations - fixed wrong order of transformations - added transform_test --- src/graphics/opengl/gldevice.cpp | 10 +- src/graphics/opengl/test/CMakeLists.txt | 13 ++ src/graphics/opengl/test/README.txt | 1 + src/graphics/opengl/test/transform_test.cpp | 339 ++++++++++++++++++++++++++++ src/math/geometry.h | 2 +- 5 files changed, 359 insertions(+), 6 deletions(-) create mode 100644 src/graphics/opengl/test/transform_test.cpp diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index e329ff4..b8f3bed 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -179,7 +179,7 @@ void Gfx::CGLDevice::BeginScene() void Gfx::CGLDevice::EndScene() { - glFinish(); + glFlush(); } void Gfx::CGLDevice::Clear() @@ -192,14 +192,14 @@ void Gfx::CGLDevice::SetTransform(Gfx::TransformType type, const Math::Matrix &m if (type == Gfx::TRANSFORM_WORLD) { m_worldMat = matrix; - m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(m_modelviewMat.Array()); } else if (type == Gfx::TRANSFORM_VIEW) { m_viewMat = matrix; - m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(m_modelviewMat.Array()); } @@ -234,14 +234,14 @@ void Gfx::CGLDevice::MultiplyTransform(Gfx::TransformType type, const Math::Matr if (type == Gfx::TRANSFORM_WORLD) { m_worldMat = Math::MultiplyMatrices(m_worldMat, matrix); - m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(m_modelviewMat.Array()); } else if (type == Gfx::TRANSFORM_VIEW) { m_viewMat = Math::MultiplyMatrices(m_viewMat, matrix); - m_modelviewMat = Math::MultiplyMatrices(m_worldMat, m_viewMat); + m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(m_modelviewMat.Array()); } diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 4242c77..58c4714 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -48,6 +48,16 @@ set(MODEL_SOURCES model_test.cpp ) +set(TRANSFORM_SOURCES +../gldevice.cpp +../../common/device.cpp +../../../common/logger.cpp +../../../common/image.cpp +../../../common/iman.cpp +../../../app/system.cpp +transform_test.cpp +) + include_directories(../../../ ${CMAKE_CURRENT_BINARY_DIR}) set(LIBS @@ -63,3 +73,6 @@ target_link_libraries(texture_test ${LIBS}) add_executable(model_test ${MODEL_SOURCES}) target_link_libraries(model_test ${LIBS}) + +add_executable(transform_test ${TRANSFORM_SOURCES}) +target_link_libraries(transform_test ${LIBS}) diff --git a/src/graphics/opengl/test/README.txt b/src/graphics/opengl/test/README.txt index c309f23..fe6f1d7 100644 --- a/src/graphics/opengl/test/README.txt +++ b/src/graphics/opengl/test/README.txt @@ -5,3 +5,4 @@ Test programs for OpenGL engine: second argument is the loaded format (DXF or Colobot .mod files) requires ./tex folder (or symlink) with Colobot textures viewer is controlled from keyboard - the bindings can be found in code + - transform_test -> simple "walk around" test for world & view transformations diff --git a/src/graphics/opengl/test/transform_test.cpp b/src/graphics/opengl/test/transform_test.cpp new file mode 100644 index 0000000..5982c4e --- /dev/null +++ b/src/graphics/opengl/test/transform_test.cpp @@ -0,0 +1,339 @@ +#include "app/system.h" +#include "common/logger.h" +#include "common/image.h" +#include "common/iman.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include +#include + +enum KeySlots +{ + K_Forward, + K_Back, + K_Left, + K_Right, + K_Up, + K_Down, + K_Count +}; +bool KEYMAP[K_Count] = { false }; + +Math::Point MOUSE_POS_BASE; + +Math::Vector TRANSLATION(0.0f, 2.0f, 0.0f); +Math::Vector ROTATION, ROTATION_BASE; + +const int FRAME_DELAY = 5000; + +SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; + +void Init(Gfx::CGLDevice *device) +{ + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); + device->SetShadeModel(Gfx::SHADE_SMOOTH); +} + +void Render(Gfx::CGLDevice *device) +{ + device->BeginScene(); + + Math::Matrix persp; + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); + + + Math::Matrix viewMat; + Math::Matrix mat; + + viewMat.LoadIdentity(); + + Math::LoadRotationXMatrix(mat, -ROTATION.x); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadRotationYMatrix(mat, -ROTATION.y); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadTranslationMatrix(mat, -TRANSLATION); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + device->SetTransform(Gfx::TRANSFORM_VIEW, viewMat); + + + Math::Matrix worldMat; + worldMat.LoadIdentity(); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + Gfx::VertexCol line[2] = { Gfx::VertexCol() }; + + for (int x = -40; x <= 40; ++x) + { + line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[0].coord.z = -40; + line[0].coord.x = x; + line[1].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[1].coord.z = 40; + line[1].coord.x = x; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + for (int z = -40; z <= 40; ++z) + { + line[0].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[0].coord.z = z; + line[0].coord.x = -40; + line[1].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[1].coord.z = z; + line[1].coord.x = 40; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + + Gfx::VertexCol quad[6] = { Gfx::VertexCol() }; + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 1.0f, 0.0f); + + quad[0].coord = Math::Vector(-1.0f, -1.0f, 0.0f); + quad[1].coord = Math::Vector( 1.0f, -1.0f, 0.0f); + quad[2].coord = Math::Vector( 1.0f, 1.0f, 0.0f); + quad[3].coord = Math::Vector( 1.0f, 1.0f, 0.0f); + quad[4].coord = Math::Vector(-1.0f, 1.0f, 0.0f); + quad[5].coord = Math::Vector(-1.0f, -1.0f, 0.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(40.0f, 2.0f, 40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(0.0f, 1.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(-40.0f, 2.0f, -40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 0.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(0.0f, 10.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + device->EndScene(); +} + +void Update() +{ + const float TRANS_SPEED = 6.0f; // units / sec + + GetCurrentTimeStamp(CURR_TIME); + float timeDiff = TimeStampDiff(PREV_TIME, CURR_TIME, STU_SEC); + CopyTimeStamp(PREV_TIME, CURR_TIME); + + Math::Vector incTrans; + + if (KEYMAP[K_Forward]) + incTrans.z = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Back]) + incTrans.z = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Right]) + incTrans.x = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Left]) + incTrans.x = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Up]) + incTrans.y = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Down]) + incTrans.y = -TRANS_SPEED * timeDiff; + + Math::Point rotTrans = Math::RotatePoint(-ROTATION.y, Math::Point(incTrans.x, incTrans.z)); + incTrans.x = rotTrans.x; + incTrans.z = rotTrans.y; + TRANSLATION += incTrans; +} + +void KeyboardDown(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = true; + break; + case SDLK_s: + KEYMAP[K_Back] = true; + break; + case SDLK_d: + KEYMAP[K_Right] = true; + break; + case SDLK_a: + KEYMAP[K_Left] = true; + break; + case SDLK_z: + KEYMAP[K_Down] = true; + break; + case SDLK_x: + KEYMAP[K_Up] = true; + break; + default: + break; + } +} + +void KeyboardUp(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = false; + break; + case SDLK_s: + KEYMAP[K_Back] = false; + break; + case SDLK_d: + KEYMAP[K_Right] = false; + break; + case SDLK_a: + KEYMAP[K_Left] = false; + break; + case SDLK_z: + KEYMAP[K_Down] = false; + break; + case SDLK_x: + KEYMAP[K_Up] = false; + break; + default: + break; + } +} + +void MouseMove(int x, int y) +{ + Math::Point currentPos((float)x, (float)y); + + static bool first = true; + if (first || (x < 10) || (y < 10) || (x > 790) || (y > 590)) + { + SDL_WarpMouse(400, 300); + MOUSE_POS_BASE.x = 400; + MOUSE_POS_BASE.y = 300; + ROTATION_BASE = ROTATION; + first = false; + return; + } + + ROTATION.y = ROTATION_BASE.y + ((float) (x - MOUSE_POS_BASE.x) / 800.0f) * Math::PI; + ROTATION.x = ROTATION_BASE.x + ((float) (y - MOUSE_POS_BASE.y) / 600.0f) * Math::PI; +} + +int main(int argc, char *argv[]) +{ + CLogger logger; + + PREV_TIME = CreateTimeStamp(); + CURR_TIME = CreateTimeStamp(); + + GetCurrentTimeStamp(PREV_TIME); + GetCurrentTimeStamp(CURR_TIME); + + CInstanceManager iMan; + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Transform Test", "Transform Test"); + + //SDL_WM_GrabInput(SDL_GRAB_ON); + SDL_ShowCursor(SDL_DISABLE); + + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device); + + bool done = false; + while (! done) + { + Render(device); + Update(); + + SDL_GL_SwapBuffers(); + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + break; + done = true; + } + else if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_q) + { + done = true; + break; + } + else + KeyboardDown(event.key.keysym.sym); + } + else if (event.type == SDL_KEYUP) + KeyboardUp(event.key.keysym.sym); + else if (event.type == SDL_MOUSEMOTION) + MouseMove(event.motion.x, event.motion.y); + } + + usleep(FRAME_DELAY); + } + + //SDL_WM_GrabInput(SDL_GRAB_OFF); + SDL_ShowCursor(SDL_ENABLE); + + device->Destroy(); + delete device; + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + DestroyTimeStamp(PREV_TIME); + DestroyTimeStamp(CURR_TIME); + + return 0; +} diff --git a/src/math/geometry.h b/src/math/geometry.h index e56ff10..8954655 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -284,7 +284,7 @@ inline void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from, //! Loads a perspective projection matrix /** \a fov field of view in radians - \a aspect aspect ratio (width / height) + \a aspect aspect ratio (height / width) \a nearPlane distance to near cut plane \a farPlane distance to far cut plane */ inline void LoadProjectionMatrix(Math::Matrix &mat, float fov = 1.570795f, float aspect = 1.0f, -- cgit v1.2.3-1-g7c22 From 86ea086790a677d6de6a836e7562814d3ba30bd1 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 18 Jul 2012 21:47:47 +0200 Subject: ComputeSphereVisibility function - borrowed implementation of ComputeSphereVisibility from libwine - added -lrt to Linux libs --- src/CMakeLists.txt | 5 +++ src/graphics/common/device.h | 23 +++++++++-- src/graphics/opengl/gldevice.cpp | 82 ++++++++++++++++++++++++++++++++++++++++ src/graphics/opengl/gldevice.h | 1 + 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fc3cfe3..9430da6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,8 @@ # Configure options option(DEBUG "Enable debug output" ON) +set(PLATFORM_LIBS "") + if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(PLATFORM_WINDOWS 1) set(PLATFORM_LINUX 0) @@ -13,6 +15,8 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 1) set(PLATFORM_OTHER 0) + # for clock_gettime + set(PLATFORM_LIBS "-lrt") else() set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 0) @@ -154,6 +158,7 @@ ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${PNG_LIBRARIES} #CBot -- not yet WinAPI-independent +${PLATFORM_LIBS} ) include_directories(. ${CMAKE_CURRENT_BINARY_DIR} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 6a71a8a..ceaf67a 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -181,6 +181,24 @@ enum PrimitiveType PRIMITIVE_TRIANGLE_STRIP }; +/** + \enum IntersectPlane + \brief Intersection plane of projection volume + + These flags can be OR'd together. */ +enum IntersectPlane +{ + INTERSECT_PLANE_LEFT = 0x01, + INTERSECT_PLANE_RIGHT = 0x02, + INTERSECT_PLANE_TOP = 0x04, + INTERSECT_PLANE_BOTTOM = 0x08, + INTERSECT_PLANE_FRONT = 0x10, + INTERSECT_PLANE_BACK = 0x20, + INTERSECT_PLANE_ALL = INTERSECT_PLANE_LEFT | INTERSECT_PLANE_RIGHT | + INTERSECT_PLANE_TOP | INTERSECT_PLANE_BOTTOM | + INTERSECT_PLANE_FRONT | INTERSECT_PLANE_BACK +}; + /* Notes for rewriting DirectX code: @@ -324,9 +342,8 @@ public: //! Renders primitive composed of vertices with multitexturing (2 textures) virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexTex2 *vertices, int vertexCount) = 0; - // TODO: - // virtual void ComputeSphereVisibility() = 0; - + //! Tests whether a sphere intersects the 6 clipping planes of projection volume + virtual int ComputeSphereVisibility(Math::Vector center, float radius) = 0; //! Enables/disables the given render state virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index b8f3bed..19a9cff 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -18,6 +18,7 @@ #include "common/image.h" #include "graphics/opengl/gldevice.h" +#include "math/geometry.h" #define GL_GLEXT_PROTOTYPES @@ -732,6 +733,87 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices glEnd(); } +bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius) +{ + float distance = (originPlane + Math::DotProduct(normal, center)) / normal.Length(); + + if (distance < -radius) + return true; + + return false; +} + +/* + The implementation of ComputeSphereVisibility is taken from libwine's device.c + Copyright of the WINE team, licensed under GNU LGPL v 2.1 + */ + +// TODO: testing +int Gfx::CGLDevice::ComputeSphereVisibility(Math::Vector center, float radius) +{ + Math::Matrix m; + m.LoadIdentity(); + m = Math::MultiplyMatrices(m, m_worldMat); + m = Math::MultiplyMatrices(m, m_viewMat); + m = Math::MultiplyMatrices(m, m_projectionMat); + + Math::Vector vec[6]; + float originPlane[6]; + + // Left plane + vec[0].x = m.Get(4, 1) + m.Get(1, 1); + vec[0].y = m.Get(4, 2) + m.Get(1, 2); + vec[0].z = m.Get(4, 3) + m.Get(1, 3); + originPlane[0] = m.Get(4, 4) + m.Get(1, 4); + + // Right plane + vec[1].x = m.Get(4, 1) - m.Get(1, 1); + vec[1].y = m.Get(4, 2) - m.Get(1, 2); + vec[1].z = m.Get(4, 3) - m.Get(1, 3); + originPlane[1] = m.Get(4, 4) - m.Get(1, 4); + + // Top plane + vec[2].x = m.Get(4, 1) - m.Get(2, 1); + vec[2].y = m.Get(4, 2) - m.Get(2, 2); + vec[2].z = m.Get(4, 3) - m.Get(2, 3); + originPlane[2] = m.Get(4, 4) - m.Get(2, 4); + + // Bottom plane + vec[3].x = m.Get(4, 1) + m.Get(2, 1); + vec[3].y = m.Get(4, 2) + m.Get(2, 2); + vec[3].z = m.Get(4, 3) + m.Get(2, 3); + originPlane[3] = m.Get(4, 4) + m.Get(2, 4); + + // Front plane + vec[4].x = m.Get(3, 1); + vec[4].y = m.Get(3, 2); + vec[4].z = m.Get(3, 3); + originPlane[4] = m.Get(3, 4); + + // Back plane + vec[5].x = m.Get(4, 1) - m.Get(3, 1); + vec[5].y = m.Get(4, 2) - m.Get(3, 2); + vec[5].z = m.Get(4, 3) - m.Get(3, 3); + originPlane[5] = m.Get(4, 4) - m.Get(3, 4); + + int result = 0; + + if (InPlane(vec[0], originPlane[0], center, radius)) + result |= Gfx::INTERSECT_PLANE_LEFT; + if (InPlane(vec[1], originPlane[1], center, radius)) + result |= Gfx::INTERSECT_PLANE_RIGHT; + if (InPlane(vec[2], originPlane[2], center, radius)) + result |= Gfx::INTERSECT_PLANE_TOP; + if (InPlane(vec[3], originPlane[3], center, radius)) + result |= Gfx::INTERSECT_PLANE_BOTTOM; + if (InPlane(vec[4], originPlane[4], center, radius)) + result |= Gfx::INTERSECT_PLANE_FRONT; + if (InPlane(vec[5], originPlane[5], center, radius)) + result |= Gfx::INTERSECT_PLANE_BACK; + + return result; +} + void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) { if (state == RENDER_STATE_DEPTH_WRITE) diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 2b1b05d..56ea2c0 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -117,6 +117,7 @@ public: virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount); virtual void DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount); + virtual int ComputeSphereVisibility(Math::Vector center, float radius); virtual void SetRenderState(Gfx::RenderState state, bool enabled); virtual bool GetRenderState(Gfx::RenderState state); -- cgit v1.2.3-1-g7c22 From 26f91499473ce0f7738fee0931a337ef19d713de Mon Sep 17 00:00:00 2001 From: Zaba999 Date: Fri, 20 Jul 2012 23:13:02 +0200 Subject: Further translations and doxygen comments. --- src/CBot/CBot.cpp | 1827 +++++++++++++++++------------------- src/CBot/CBot.h | 2020 +++++++++++++++++++++------------------- src/CBot/CBotDll.h | 102 +- src/CBot/CBotFunction.cpp | 4 - src/CBot/CBotStack.cpp | 1707 +++++++++++++++++----------------- src/CBot/CBotString.cpp | 6 +- src/CBot/CBotVar.cpp | 2270 ++++++++++++++++++++++----------------------- src/CBot/resource.h | 2 - 8 files changed, 3957 insertions(+), 3981 deletions(-) diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index e73eea0..fb25049 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -87,8 +87,8 @@ bool CBotInstr::ChkLvl(const CBotString& label, int type) while (--i>=0) { if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; - if ( label.IsEmpty() ) return true; - if ( m_labelLvl[i] == label ) return true; + if (label.IsEmpty()) return true; + if (m_labelLvl[i] == label) return true; } return false; } @@ -128,21 +128,21 @@ CBotToken* CBotInstr::GivToken() void CBotInstr::AddNext(CBotInstr* n) { CBotInstr* p = this; - while ( p->m_next != NULL ) p = p->m_next; + while (p->m_next != NULL) p = p->m_next; p->m_next = n; } void CBotInstr::AddNext3(CBotInstr* n) { CBotInstr* p = this; - while ( p->m_next3 != NULL ) p = p->m_next3; + while (p->m_next3 != NULL) p = p->m_next3; p->m_next3 = n; } void CBotInstr::AddNext3b(CBotInstr* n) { CBotInstr* p = this; - while ( p->m_next3b != NULL ) p = p->m_next3b; + while (p->m_next3b != NULL) p = p->m_next3b; p->m_next3b = n; } @@ -175,17 +175,17 @@ CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) { CBotToken* pp = p; - if ( p == NULL ) return NULL; + if (p == NULL) return NULL; int type = p->GivType(); // what is the next token // is it a lable? - if ( IsOfType( pp, TokenTypVar ) && - IsOfType( pp, ID_DOTS ) ) + if (IsOfType(pp, TokenTypVar) && + IsOfType(pp, ID_DOTS)) { type = pp->GivType(); // these instructions accept only lable - if (!IsOfTypeList( pp, ID_WHILE, ID_FOR, ID_DO, ID_REPEAT, 0 )) + if (!IsOfTypeList(pp, ID_WHILE, ID_FOR, ID_DO, ID_REPEAT, 0)) { pStack->SetError(TX_LABEL, pp->GivStart()); return NULL; @@ -255,7 +255,7 @@ CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) pStack->SetStartError(p->GivStart()); - // ne doit pas tre un mot rserv par DefineNum + // should not be a reserved word DefineNum if (p->GivType() == TokenTypDef) { pStack->SetError(TX_RESERVED, p); @@ -264,16 +264,16 @@ CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) // this might be an instance of class definnition CBotToken* ppp = p; - if (IsOfType( ppp, TokenTypVar )) + if (IsOfType(ppp, TokenTypVar)) { - if ( CBotClass::Find(p) != NULL ) + if (CBotClass::Find(p) != NULL) { - // oui, compile la dclaration de l'instance + // yes, compiles the declaration of the instance return CBotClassInst::Compile(p, pStack); } } - // ce peut tre une instruction arithmtique + // this can be an arythmetic instruction CBotInstr* inst = CBotExpression::Compile(p, pStack); if (IsOfType(p, ID_SEP)) { @@ -287,46 +287,46 @@ CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotInstr::Execute(CBotStack* &pj) { CBotString ClassManquante = name; - ASM_TRAP(); // ne doit jamais passer par cette routine - // mais utiliser les routines des classes filles + ASM_TRAP(); // should never go through this routine + // but use the routines of the subclasses return false; } bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) { - if ( !Execute(pj) ) return false; - pVar->SetVal( pj->GivVar() ); + if (!Execute(pj)) return false; + pVar->SetVal(pj->GivVar()); return true; } void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) { CBotString ClassManquante = name; - ASM_TRAP(); // ne doit jamais passer par cette routine - // mais utiliser les routines des classes filles + ASM_TRAP(); // should never go through this routine + // but use the routines of the subclasses } bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - ASM_TRAP(); // papa sait pas faire, voir les filles + ASM_TRAP(); // dad do not know, see the girls return false; } bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { - ASM_TRAP(); // papa sait pas faire, voir les filles + ASM_TRAP(); // dad do not know, see the girls return false; } void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) { - ASM_TRAP(); // papa sait pas faire, voir les filles + ASM_TRAP(); // dad do not know, see the girls } -// cette routine n'est dfinie que pour la classe fille CBotCase -// cela permet de faire l'appel CompCase sur toutes les instructions -// pour savoir s'il s'agit d'un case pour la valeur dsire. +// this routine is defined only for the subclass CBotCase +// this allows to make the call on all instructions CompCase +// to see if it's a case to the desired value. bool CBotInstr::CompCase(CBotStack* &pj, int val) { @@ -337,10 +337,11 @@ bool CBotInstr::CompCase(CBotStack* &pj, int val) ////////////////////////////////////////////////////////////////////////////////////// -// compile un bloc d'instruction " { i ; i ; } " +// compiles a statement block " { i ; i ; } " -// cette classe n'a pas de constructeur, car il n'y a jamais d'instance de cette classe -// l'objet retourn par Compile est gnralement de type CBotListInstr +// this class have no constructor because there is never an instance of this +// class (TODO what about default constructor?) +// the object returned by Compile is usually of type CBotListInstr CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) @@ -349,14 +350,14 @@ CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) if (IsOfType(p, ID_OPBLK)) { - CBotInstr* inst = CBotListInstr::Compile( p, pStack, bLocal ); + CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); if (IsOfType(p, ID_CLBLK)) { return inst; } - pStack->SetError(TX_CLOSEBLK, p->GivStart()); // manque la parenthse + pStack->SetError(TX_CLOSEBLK, p->GivStart()); // missing parenthesis delete inst; return NULL; } @@ -367,28 +368,28 @@ CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) { - // est-ce un nouveau bloc ? - if ( p->GivType() == ID_OPBLK ) return CBotBlock::Compile(p, pStack); + // is this a new block + if (p->GivType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); - // sinon, cherche une instruction unique la place + // otherwise, look for a single statement instead - // pour grer les cas avec dfinition local l'instructin (*) + // to handle the case with local definition instruction (*) CBotCStack* pStk = pStack->TokenStack(p, bLocal); - return pStack->Return( CBotInstr::Compile(p, pStk), // une instruction unique + return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction pStk); } -// (*) c'est le cas dans l'instruction suivante -// if ( 1 == 1 ) int x = 0; -// o la variable x n'est connue que dans le bloc qui suit le if. +// (*) is the case in the following statement +// if (1 == 1) int x = 0; +// where the variable x is known only in the block following the if ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// compile une liste d'instruction, spars par des points-virgules +// compiles a list of instructions separated by semicolons CBotListInstr::CBotListInstr() { @@ -403,16 +404,16 @@ CBotListInstr::~CBotListInstr() CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) { - CBotCStack* pStk = pStack->TokenStack(p, bLocal); // les variables sont locales + CBotCStack* pStk = pStack->TokenStack(p, bLocal); // variables are local CBotListInstr* inst = new CBotListInstr(); while (true) { - if ( p == NULL ) break; + if (p == NULL) break; - if (IsOfType(p, ID_SEP)) continue; // instruction vide ignore - if ( p->GivType() == ID_CLBLK ) break; // dja plus d'instruction + if (IsOfType(p, ID_SEP)) continue; // empty statement ignored + if (p->GivType() == ID_CLBLK) break; if (IsOfType(p, 0)) { @@ -421,7 +422,7 @@ CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal return pStack->Return(NULL, pStk); } - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk ); // compile la suivante + CBotInstr* i = CBotBlock::CompileBlkOrInst(p, pStk); // compiles next if (!pStk->IsOk()) { @@ -429,62 +430,60 @@ CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal return pStack->Return(NULL, pStk); } - if ( inst->m_Instr == NULL ) inst->m_Instr = i; - else inst->m_Instr->AddNext(i); // ajoute la suite + if (inst->m_Instr == NULL) inst->m_Instr = i; + else inst->m_Instr->AddNext(i); // added a result } return pStack->Return(inst, pStk); } -// excute une liste d'instructions +// executes a set of instructions bool CBotListInstr::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, true);//indispensable pour SetState() - if ( pile->StackOver() ) return pj->Return( pile ); + CBotStack* pile = pj->AddStack(this, true); //needed for SetState() + if (pile->StackOver() ) return pj->Return( pile); - CBotInstr* p = m_Instr; // la premire expression + CBotInstr* p = m_Instr; // the first expression int state = pile->GivState(); - while (state-->0) p = p->GivNext(); // revient sur l'opration interrompue + while (state-->0) p = p->GivNext(); // returns to the interrupted operation - if ( p != NULL ) while (true) + if (p != NULL) while (true) { -// DEBUG( "CBotListInstr", pile->GivState(), pile ); - - if ( !p->Execute(pile) ) return false; + if (!p->Execute(pile)) return false; p = p->GivNext(); - if ( p == NULL ) break; - if (!pile->IncState()) ;//return false; // prt pour la suivante + if (p == NULL) break; + if (!pile->IncState()) ;//return false; // ready for next } - return pj->Return( pile ); // transmet en dessous + return pj->Return(pile); } void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; - CBotInstr* p = m_Instr; // la premire expression + CBotInstr* p = m_Instr; // the first expression int state = pile->GivState(); while ( p != NULL && state-- > 0) { p->RestoreState(pile, false); - p = p->GivNext(); // revient sur l'opration interrompue + p = p->GivNext(); // returns to the interrupted operation } - if ( p != NULL ) p->RestoreState(pile, true); + if (p != NULL) p->RestoreState(pile, true); } ////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// compilation d'un lment se trouvant gauche d'une assignation +// compilation of an element to the left of an assignment CBotLeftExprVar::CBotLeftExprVar() { @@ -499,7 +498,7 @@ CBotLeftExprVar::~CBotLeftExprVar() CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) { - // vrifie que le token est un nom de variable + // verifies that the token is a variable name if (p->GivType() != TokenTypVar) { pStack->SetError( TX_NOVAR, p->GivStart()); @@ -513,20 +512,20 @@ CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) return inst; } -// cre une variable et lui assigne le rsultat de la pile +// creates a variable and assigns the result to the stack bool CBotLeftExprVar::Execute(CBotStack* &pj) { CBotVar* var1; CBotVar* var2; var1 = CBotVar::Create(m_token.GivString(), m_typevar); - var1->SetUniqNum(m_nIdent); // avec cet identificateur unique - pj->AddVar(var1); // la place sur la pile - - var2 = pj->GivVar(); // resultat sur la pile - if ( var2 ) var1->SetVal(var2); // fait l'assignation + var1->SetUniqNum(m_nIdent); // with the unique identifier + pj->AddVar(var1); // place it on the stack + + var2 = pj->GivVar(); // result on the stack + if (var2) var1->SetVal(var2); // do the assignment - return true; // opration faite + return true; } void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) @@ -534,9 +533,9 @@ void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) CBotVar* var1; var1 = pj->FindVar(m_token.GivString()); - if ( var1 == NULL ) ASM_TRAP(); + if (var1 == NULL) ASM_TRAP(); - var1->SetUniqNum(m_nIdent); // avec cet identificateur unique + var1->SetUniqNum(m_nIdent); // with the unique identifier } ////////////////////////////////////////////////////////////////////////////////////// @@ -544,7 +543,7 @@ void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// dfinition d'un tableau de n'importe quel type +// defining an array of any type // int a[12]; // point x[]; @@ -566,52 +565,51 @@ CBotInstr* CBotInstArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu { CBotCStack* pStk = pStack->TokenStack(p); - CBotInstArray* inst = new CBotInstArray(); // cre l'objet + CBotInstArray* inst = new CBotInstArray(); CBotToken* vartoken = p; inst->SetToken(vartoken); - // dtermine l'expression valable pour l'lment gauche - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + // determinse the expression is valid for the item on the left side + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) { - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable ? + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable? { pStk->SetError(TX_REDEFVAR, vartoken); goto error; } CBotInstr* i; - while (IsOfType(p, ID_OPBRK)) // avec des indices ? + while (IsOfType(p, ID_OPBRK)) { - if ( p->GivType() != ID_CLBRK ) - i = CBotExpression::Compile( p, pStk ); // expression pour la valeur + if (p->GivType() != ID_CLBRK) + i = CBotExpression::Compile(p, pStk); // expression for the value else - i = new CBotEmpty(); // spcial si pas de formule + i = new CBotEmpty(); // if no special formula - inst->AddNext3b(i); // construit une liste + inst->AddNext3b(i); // construct a list type = CBotTypResult(CBotTypArrayPointer, type); - if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) + if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK )) { pStk->SetError(TX_CLBRK, p->GivStart()); goto error; } } - CBotVar* var = CBotVar::Create(vartoken, type); // cre avec une instance + CBotVar* var = CBotVar::Create(vartoken, type); // create an instance inst->m_typevar = type; var->SetUniqNum( ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + pStack->AddVar(var); // place it on the stack - if ( IsOfType(p, ID_ASS) ) // avec une assignation + if (IsOfType(p, ID_ASS)) // with an assignment { - inst->m_listass = CBotListArray::Compile( p, pStk, type.GivTypElem() ); + inst->m_listass = CBotListArray::Compile(p, pStk, type.GivTypElem()); } - if ( pStk->IsOk() ) return pStack->Return(inst, pStk); + if (pStk->IsOk()) return pStack->Return(inst, pStk); } error: @@ -620,86 +618,85 @@ error: } -// excute la dfinition d'un tableau +// executes the definition of an array bool CBotInstArray::Execute(CBotStack* &pj) { CBotStack* pile1 = pj->AddStack(this); -// if ( pile1 == EOX ) return true; CBotStack* pile = pile1; - if ( pile1->GivState() == 0 ) + if (pile1->GivState() == 0) { - // cherche les dimensions max du tableau - CBotInstr* p = GivNext3b(); // les diffrentes formules + // seek the maximum dimension of the table + CBotInstr* p = GivNext3b(); // the different formulas int nb = 0; while (p != NULL) { - pile = pile->AddStack(); // petite place pour travailler + pile = pile->AddStack(); // little room to work nb++; - if ( pile->GivState() == 0 ) + if (pile->GivState() == 0) { - if ( !p->Execute(pile) ) return false; // calcul de la taille // interrompu? + if (!p->Execute(pile)) return false; // size calculation //interrupted? pile->IncState(); } p = p->GivNext3b(); } p = GivNext3b(); - pile = pile1; // revient sur la pile + pile = pile1; // returns to the stack int n = 0; int max[100]; while (p != NULL) { - pile = pile->AddStack(); // rcupre la mme petite place - CBotVar* v = pile->GivVar(); // rsultat - max[n] = v->GivValInt(); // valeur + pile = pile->AddStack(); + CBotVar* v = pile->GivVar(); // result + max[n] = v->GivValInt(); // value if (max[n]>MAXARRAYSIZE) { pile->SetError(TX_OUTARRAY, &m_token); - return pj->Return ( pile ); + return pj->Return (pile); } n++; p = p->GivNext3b(); } while (n<100) max[n++] = 0; - m_typevar.SetArray( max ); // mmorise les limitations + m_typevar.SetArray(max); // store the limitations // cre simplement un pointeur null CBotVar* var = CBotVar::Create(m_var->GivToken(), m_typevar); var->SetPointer(NULL); var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); - pj->AddVar(var); // inscrit le tableau de base sur la pile + pj->AddVar(var); #if STACKMEM pile1->AddStack()->Delete(); #else - delete pile1->AddStack(); // plus besoin des indices + delete pile1->AddStack(); // need more indices #endif pile1->IncState(); } - if ( pile1->GivState() == 1 ) + if (pile1->GivState() == 1) { - if ( m_listass != NULL ) // il y a des assignation pour ce tableau + if (m_listass != NULL) // there is the assignment for this table { CBotVar* pVar = pj->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); - if ( !m_listass->Execute(pile1, pVar) ) return false; + if (!m_listass->Execute(pile1, pVar)) return false; } pile1->IncState(); } - if ( pile1->IfStep() ) return false; // montre ce pas ? + if (pile1->IfStep()) return false; if ( m_next2b && - !m_next2b->Execute( pile1 ) ) return false; + !m_next2b->Execute(pile1 )) return false; - return pj->Return( pile1 ); // transmet en dessous + return pj->Return(pile1); } void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) @@ -707,47 +704,46 @@ void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) CBotStack* pile1 = pj; CBotVar* var = pj->FindVar(m_var->GivToken()->GivString()); - if ( var != NULL ) var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); + if (var != NULL) var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); - if ( bMain ) + if (bMain) { - pile1 = pj->RestoreStack(this); + pile1 = pj->RestoreStack(this); CBotStack* pile = pile1; - if ( pile == NULL ) return; + if (pile == NULL) return; - if ( pile1->GivState() == 0 ) + if (pile1->GivState() == 0) { - // cherche les dimensions max du tableau - CBotInstr* p = GivNext3b(); // les diffrentes formules + // seek the maximum dimension of the table + CBotInstr* p = GivNext3b(); while (p != NULL) { - pile = pile->RestoreStack(); // petite place pour travailler - if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) + pile = pile->RestoreStack(); + if (pile == NULL) return; + if (pile->GivState() == 0) { - p->RestoreState(pile, bMain); // calcul de la taille // interrompu! + p->RestoreState(pile, bMain); return; } p = p->GivNext3b(); } } - if ( pile1->GivState() == 1 && m_listass != NULL ) + if (pile1->GivState() == 1 && m_listass != NULL) { m_listass->RestoreState(pile1, bMain); } } - - if ( m_next2b ) m_next2b->RestoreState( pile1, bMain ); + if (m_next2b ) m_next2b->RestoreState( pile1, bMain); } -// cas particulier pour les indices vides +// special case for empty indexes bool CBotEmpty :: Execute(CBotStack* &pj) { CBotVar* pVar = CBotVar::Create("", CBotTypInt); - pVar->SetValInt(-1); // met la valeur -1 sur la pile + pVar->SetValInt(-1); pj->SetVar(pVar); return true; } @@ -757,8 +753,8 @@ void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) } ////////////////////////////////////////////////////////////////////////////////////// -// dfinition d'une liste d'initialisation pour un tableau -// int [ ] a [ ] = ( ( 1, 2, 3 ) , ( 3, 2, 1 ) ) ; +// defining a list table initialization +// int [ ] a [ ] = (( 1, 2, 3 ) , ( 3, 2, 1 )) ; CBotListArray::CBotListArray() @@ -779,36 +775,34 @@ CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu CBotToken* pp = p; - if ( IsOfType( p, ID_NULL ) ) + if (IsOfType( p, ID_NULL )) { CBotInstr* inst = new CBotExprNull (); - inst->SetToken( pp ); -// CBotVar* var = CBotVar::Create("", CBotTypNullPointer); -// pStk->SetVar(var); - return pStack->Return(inst, pStk); // ok avec lment vide + inst->SetToken(pp); + return pStack->Return(inst, pStk); // ok with empty element } - CBotListArray* inst = new CBotListArray(); // cre l'objet + CBotListArray* inst = new CBotListArray(); - if ( IsOfType( p, ID_OPENPAR ) ) + if (IsOfType( p, ID_OPENPAR )) { - // prend chaque lment l'un aprs l'autre - if ( type.Eq( CBotTypArrayPointer ) ) + // each element takes the one after the other + if (type.Eq( CBotTypArrayPointer )) { type = type.GivTypElem(); pStk->SetStartError(p->GivStart()); - if ( NULL == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) ) ) + if (NULL == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) )) { goto error; } - while ( IsOfType( p, ID_COMMA ) ) // d'autres lments ? + while (IsOfType( p, ID_COMMA )) // other elements? { pStk->SetStartError(p->GivStart()); - CBotInstr* i = CBotListArray::Compile( p, pStk, type ); - if ( NULL == i ) + CBotInstr* i = CBotListArray::Compile(p, pStk, type); + if (NULL == i) { goto error; } @@ -819,31 +813,31 @@ CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResu else { pStk->SetStartError(p->GivStart()); - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) { goto error; } - CBotVar* pv = pStk->GivVar(); // le rsultat de l'expression + CBotVar* pv = pStk->GivVar(); // result of the expression - if ( pv == NULL || !TypesCompatibles( type, pv->GivTypResult() )) // type compatible ? + if (pv == NULL || !TypesCompatibles( type, pv->GivTypResult())) // compatible type? { pStk->SetError(TX_BADTYPE, p->GivStart()); goto error; } - while ( IsOfType( p, ID_COMMA ) ) // d'autres lments ? + while (IsOfType( p, ID_COMMA )) // other elements? { pStk->SetStartError(p->GivStart()); - CBotInstr* i = CBotTwoOpExpr::Compile( p, pStk ) ; - if ( NULL == i ) + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStk) ; + if (NULL == i) { goto error; } - CBotVar* pv = pStk->GivVar(); // le rsultat de l'expression + CBotVar* pv = pStk->GivVar(); // result of the expression - if ( pv == NULL || !TypesCompatibles( type, pv->GivTypResult() )) // type compatible ? + if (pv == NULL || !TypesCompatibles( type, pv->GivTypResult())) // compatible type? { pStk->SetError(TX_BADTYPE, p->GivStart()); goto error; @@ -867,58 +861,57 @@ error: } -// excute la dfinition d'un tableau +// executes the definition of an array bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) { CBotStack* pile1 = pj->AddStack(); -// if ( pile1 == EOX ) return true; CBotVar* pVar2; CBotInstr* p = m_expr; int n = 0; - for ( ; p != NULL ; n++, p = p->GivNext3() ) + for (; p != NULL ; n++, p = p->GivNext3()) { - if ( pile1->GivState() > n ) continue; + if (pile1->GivState() > n) continue; pVar2 = pVar->GivItem(n, true); - if ( !p->Execute(pile1, pVar2) ) return false; // value l'expression + if (!p->Execute(pile1, pVar2)) return false; // evaluate expression pile1->IncState(); } - return pj->Return( pile1 ); // transmet en dessous + return pj->Return(pile1); } void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) + if (bMain) { CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; CBotInstr* p = m_expr; int state = pile->GivState(); - while( state-- > 0 ) p = p->GivNext3() ; + while(state-- > 0) p = p->GivNext3() ; - p->RestoreState(pile, bMain); // calcul de la taille // interrompu! + p->RestoreState(pile, bMain); // size calculation //interrupted! } } ////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// dfinition d'une variable entire +// definition of an integer variable // int a, b = 12; CBotInt::CBotInt() { - m_next = NULL; // pour les dfinitions multiples + m_next = NULL; // for multiple definitions m_var = m_expr = NULL; name = "CBotInt"; @@ -928,32 +921,31 @@ CBotInt::~CBotInt() { delete m_var; delete m_expr; -// delete m_next; // fait par le destructeur de la classe de base ~CBotInstr() } CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) { - if ( IsOfType(p, ID_OPBRK) ) + if (IsOfType(p, ID_OPBRK)) { - if ( !IsOfType(p, ID_CLBRK) ) + if (!IsOfType(p, ID_CLBRK)) { pStack->SetError(TX_CLBRK, p->GivStart()); return NULL; } - CBotInstr* inst = CompileArray(p, pStack, CBotTypResult( CBotTypArrayPointer, type ), false); - if ( inst != NULL || !pStack->IsOk() ) return inst; + CBotInstr* inst = CompileArray(p, pStack, CBotTypResult(CBotTypArrayPointer, type), false); + if (inst != NULL || !pStack->IsOk()) return inst; } - // compile une dclaration de tableau + // compiles an array declaration if (first) return NULL ; - CBotInstr* inst = CBotInstArray::Compile( p, pStack, type ); - if ( inst == NULL ) return NULL; + CBotInstr* inst = CBotInstArray::Compile(p, pStack, type); + if (inst == NULL) return NULL; - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + if (IsOfType(p, ID_COMMA)) // several definitions { - if ( NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false) )) // compile la suivante + if (NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false))) // compiles next one { return inst; } @@ -961,7 +953,7 @@ CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypRes return NULL; } - if (IsOfType(p, ID_SEP)) // instruction termine + if (IsOfType(p, ID_SEP)) // end of instruction { return inst; } @@ -973,40 +965,40 @@ CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypRes CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) { - CBotToken* pp = cont ? NULL : p; // pas de rptition du token "int" + CBotToken* pp = cont ? NULL : p; // no repetition of the token "int" if (!cont && !IsOfType(p, ID_INT)) return NULL; CBotInt* inst = (CBotInt*)CompileArray(p, pStack, CBotTypInt); - if ( inst != NULL || !pStack->IsOk() ) return inst; + if (inst != NULL || !pStack->IsOk()) return inst; CBotCStack* pStk = pStack->TokenStack(pp); - inst = new CBotInt(); // cre l'objet + inst = new CBotInt(); inst->m_expr = NULL; CBotToken* vartoken = p; - inst->SetToken( vartoken ); + inst->SetToken(vartoken); - // dtermine l'expression valable pour l'lment gauche - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + // determines the expression is valid for the item on the left side + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) { ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypInt; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable { pStk->SetError(TX_REDEFVAR, vartoken); goto error; } - if (IsOfType(p, ID_OPBRK)) // avec des indices ? + if (IsOfType(p, ID_OPBRK)) { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable + delete inst; // type is not CBotInt + p = vartoken; // returns the variable name - // compile une dclaration de tableau + // compiles an array declaration - CBotInstr* inst2 = CBotInstArray::Compile( p, pStk, CBotTypInt ); + CBotInstr* inst2 = CBotInstArray::Compile(p, pStk, CBotTypInt); if (!pStk->IsOk() ) { @@ -1014,24 +1006,24 @@ CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool n goto error; } - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + if (IsOfType(p, ID_COMMA)) // several definition chained { - if ( NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante + if (NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile the next one { return pStack->Return(inst2, pStk); } } inst = (CBotInt*)inst2; - goto suite; // pas d'assignation, variable dj cre + goto suite; // no assignment, variable already created } - if (IsOfType(p, ID_ASS)) // avec une assignation ? + if (IsOfType(p, ID_ASS)) // with an assignment? { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) { goto error; } - if ( pStk->GivType() >= CBotTypBoolean ) // type compatible ? + if (pStk->GivType() >= CBotTypBoolean) // compatible type ? { pStk->SetError(TX_BADTYPE, p->GivStart()); goto error; @@ -1039,23 +1031,22 @@ CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool n } { - CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation - var->SetUniqNum( + CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// create the variable (evaluated after the assignment) + var->SetInit(inst->m_expr != NULL); // if initialized with assignment + var->SetUniqNum( //set it with a unique number ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + pStack->AddVar(var); // place it on the stack } - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + if (IsOfType(p, ID_COMMA)) // chained several definitions { - if ( NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip) )) // compile la suivante + if (NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile next one { return pStack->Return(inst, pStk); } } suite: - if (noskip || IsOfType(p, ID_SEP)) // instruction termine + if (noskip || IsOfType(p, ID_SEP)) // instruction is completed { return pStack->Return(inst, pStk); } @@ -1068,54 +1059,53 @@ error: return pStack->Return(NULL, pStk); } -// excute la dfinition de la variable entire +// execute the definition of the integer variable bool CBotInt::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); //indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); // essential for SetState() if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if (m_expr && !m_expr->Execute(pile)) return false; // initial value // interrupted? + m_var->Execute(pile); // creates and assign the result if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; // other(s) definition(s) - return pj->Return( pile ); // transmet en dessous + return pj->Return(pile); // forward below } void CBotInt::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; - if ( bMain ) + if (bMain) { pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; if ( pile->GivState()==0) { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale // interrompu! + if (m_expr) m_expr->RestoreState(pile, bMain); // initial value // interrupted? return; } } m_var->RestoreState(pile, bMain); - if ( m_next2b ) m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if (m_next2b) m_next2b->RestoreState(pile, bMain); // other(s) definition(s) } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// dfinition d'une variable boolen +// defining a boolean variable // int a, b = false; CBotBoolean::CBotBoolean() @@ -1138,7 +1128,7 @@ CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bo if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return NULL; CBotBoolean* inst = (CBotBoolean*)CompileArray(p, pStack, CBotTypBoolean); - if ( inst != NULL || !pStack->IsOk() ) return inst; + if (inst != NULL || !pStack->IsOk()) return inst; CBotCStack* pStk = pStack->TokenStack(pp); @@ -1147,64 +1137,63 @@ CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bo inst->m_expr = NULL; CBotToken* vartoken = p; - inst->SetToken( vartoken ); + inst->SetToken(vartoken); CBotVar* var = NULL; - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) { ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypBoolean; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable { pStk->SetError(TX_REDEFVAR, vartoken); goto error; } - if (IsOfType(p, ID_OPBRK)) // avec des indices ? + if (IsOfType(p, ID_OPBRK)) { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable + delete inst; // type is not CBotInt + p = vartoken; // resutns to the variable name - // compile une dclaration de tableau + // compiles an array declaration - inst = (CBotBoolean*)CBotInstArray::Compile( p, pStk, CBotTypBoolean ); + inst = (CBotBoolean*)CBotInstArray::Compile(p, pStk, CBotTypBoolean); if (!pStk->IsOk() ) { pStk->SetError(TX_CLBRK, p->GivStart()); goto error; } - goto suite; // pas d'assignation, variable dj cre + goto suite; // no assignment, variable already created } - if (IsOfType(p, ID_ASS)) // avec une assignation ? + if (IsOfType(p, ID_ASS)) { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) { goto error; } - if ( !pStk->GivTypResult().Eq(CBotTypBoolean) ) // type compatible ? + if (!pStk->GivTypResult().Eq(CBotTypBoolean)) { pStk->SetError(TX_BADTYPE, p->GivStart()); goto error; } } - var = CBotVar::Create(vartoken, CBotTypBoolean);// cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + var = CBotVar::Create(vartoken, CBotTypBoolean);// create the variable (evaluated after the assignment) + var->SetInit(inst->m_expr != NULL); var->SetUniqNum( ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + pStack->AddVar(var); suite: - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + if (IsOfType(p, ID_COMMA)) { - if ( NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip) )) // compile la suivante + if (NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip))) { return pStack->Return(inst, pStk); } } - if (noskip || IsOfType(p, ID_SEP)) // instruction termine + if (noskip || IsOfType(p, ID_SEP)) { return pStack->Return(inst, pStk); } @@ -1217,55 +1206,54 @@ error: return pStack->Return(NULL, pStk); } -// excute une dfinition de variable boolenne +// executes a boolean variable definition bool CBotBoolean::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this);//essential for SetState() if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; - return pj->Return( pile ); // transmet en dessous + return pj->Return(pile); } void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; - if ( bMain ) + if (bMain) { pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; if ( pile->GivState()==0) { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? + if (m_expr) m_expr->RestoreState(pile, bMain); // initial value interrupted? return; } } - m_var->RestoreState( pile, bMain ); // + m_var->RestoreState(pile, bMain); - if ( m_next2b ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if (m_next2b) + m_next2b->RestoreState(pile, bMain); // other(s) definition(s) } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// dfinition d'une variable relle +// definition of a real/float variable // int a, b = 12.4; CBotFloat::CBotFloat() @@ -1288,7 +1276,7 @@ CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool if (!cont && !IsOfType(p, ID_FLOAT)) return NULL; CBotFloat* inst = (CBotFloat*)CompileArray(p, pStack, CBotTypFloat); - if ( inst != NULL || !pStack->IsOk() ) return inst; + if (inst != NULL || !pStack->IsOk()) return inst; CBotCStack* pStk = pStack->TokenStack(pp); @@ -1300,62 +1288,58 @@ CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool CBotVar* var = NULL; inst->SetToken(vartoken); - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) { ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypFloat; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + if (pStk->CheckVarLocal(vartoken)) // redefinition of a variable { pStk->SetStartError(vartoken->GivStart()); pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); goto error; } - if (IsOfType(p, ID_OPBRK)) // avec des indices ? + if (IsOfType(p, ID_OPBRK)) { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable - - // compile une dclaration de tableau - - inst = (CBotFloat*)CBotInstArray::Compile( p, pStk, CBotTypFloat ); + delete inst; + p = vartoken; + inst = (CBotFloat*)CBotInstArray::Compile(p, pStk, CBotTypFloat); if (!pStk->IsOk() ) { pStk->SetError(TX_CLBRK, p->GivStart()); goto error; } - goto suite; // pas d'assignation, variable dj cre + goto suite; // no assignment, variable already created } - if (IsOfType(p, ID_ASS)) // avec une assignation ? + if (IsOfType(p, ID_ASS)) { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) { goto error; } - if ( pStk->GivType() >= CBotTypBoolean ) // type compatible ? + if (pStk->GivType() >= CBotTypBoolean) { pStk->SetError(TX_BADTYPE, p->GivStart()); goto error; } } - var = CBotVar::Create(vartoken, CBotTypFloat); // cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + var = CBotVar::Create(vartoken, CBotTypFloat); + var->SetInit(inst->m_expr != NULL); var->SetUniqNum( ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + pStack->AddVar(var); suite: - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + if (IsOfType(p, ID_COMMA)) { - if ( NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip) )) // compile la suivante + if (NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip))) { return pStack->Return(inst, pStk); } } - if (noskip || IsOfType(p, ID_SEP)) // instruction termine + if (noskip || IsOfType(p, ID_SEP)) { return pStack->Return(inst, pStk); } @@ -1368,55 +1352,54 @@ error: return pStack->Return(NULL, pStk); } -// excute la dfintion de la variable relle +// executes the definition of a real variable bool CBotFloat::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; - return pj->Return( pile ); // transmet en dessous + return pj->Return(pile); } void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; - if ( bMain ) + if (bMain) { pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; if ( pile->GivState()==0) { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? + if (m_expr) m_expr->RestoreState(pile, bMain); return; } } - m_var->RestoreState( pile, bMain ); // + m_var->RestoreState(pile, bMain); - if ( m_next2b ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if (m_next2b) + m_next2b->RestoreState(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// dfinition d'une variable chane de caractres +// define a string variable // int a, b = "salut"; CBotIString::CBotIString() @@ -1439,7 +1422,7 @@ CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bo if (!cont && !IsOfType(p, ID_STRING)) return NULL; CBotIString* inst = (CBotIString*)CompileArray(p, pStack, CBotTypString); - if ( inst != NULL || !pStack->IsOk() ) return inst; + if (inst != NULL || !pStack->IsOk()) return inst; CBotCStack* pStk = pStack->TokenStack(pp); @@ -1448,47 +1431,46 @@ CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bo inst->m_expr = NULL; CBotToken* vartoken = p; - inst->SetToken( vartoken ); + inst->SetToken(vartoken); - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) { ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypString; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable + if (pStk->CheckVarLocal(vartoken)) { pStk->SetStartError(vartoken->GivStart()); pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); goto error; } - if (IsOfType(p, ID_ASS)) // avec une assignation ? + if (IsOfType(p, ID_ASS)) { - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) { goto error; } -/* if ( !pStk->GivTypResult().Eq(CBotTypString) ) // type compatible ? +/* if (!pStk->GivTypResult().Eq(CBotTypString)) // type compatible ? { pStk->SetError(TX_BADTYPE, p->GivStart()); goto error; }*/ } - CBotVar* var = CBotVar::Create(vartoken, CBotTypString); // cre la variable (aprs l'assignation value) - var->SetInit(inst->m_expr != NULL); // la marque initialise si avec assignation + CBotVar* var = CBotVar::Create(vartoken, CBotTypString); + var->SetInit(inst->m_expr != NULL); var->SetUniqNum( ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile + pStack->AddVar(var); - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes + if (IsOfType(p, ID_COMMA)) { - if ( NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip) )) // compile la suivante + if (NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip))) { return pStack->Return(inst, pStk); } } - - if (noskip || IsOfType(p, ID_SEP)) // instruction termine + + if (noskip || IsOfType(p, ID_SEP)) { return pStack->Return(inst, pStk); } @@ -1501,49 +1483,48 @@ error: return pStack->Return(NULL, pStk); } -// excute la dfinition de la variable string +// executes the definition of the string variable bool CBotIString::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); if ( pile->GivState()==0) { - if (m_expr && !m_expr->Execute(pile)) return false; // valeur initiale // interrompu? - m_var->Execute( pile ); // cre et fait l'assigation du rsultat + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); if (!pile->SetState(1)) return false; } - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; if ( m_next2b && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) + !m_next2b->Execute(pile)) return false; - return pj->Return( pile ); // transmet en dessous + return pj->Return(pile); } void CBotIString::RestoreState(CBotStack* &pj, bool bMain) { CBotStack* pile = pj; - - if ( bMain ) + + if (bMain) { pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; if ( pile->GivState()==0) { - if (m_expr) m_expr->RestoreState(pile, bMain); // valeur initiale interrompu? + if (m_expr) m_expr->RestoreState(pile, bMain); return; } } - m_var->RestoreState( pile, bMain ); // + m_var->RestoreState(pile, bMain); - if ( m_next2b ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) + if (m_next2b) + m_next2b->RestoreState(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -1551,8 +1532,8 @@ void CBotIString::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////// -// compile une instruction de type " x = 123 " ou " z * 5 + 4 " -// avec ou sans assignation donc +// compiles a statement such as " x = 123 " ou " z * 5 + 4 " +// with or without assignment CBotExpression::CBotExpression() { @@ -1583,7 +1564,7 @@ CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) ID_ASSAND, ID_ASSXOR, ID_ASSOR, ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) { - if ( inst->m_leftop == NULL ) + if (inst->m_leftop == NULL) { pStack->SetError(TX_BADLEFT, p->GivEnd()); delete inst; @@ -1599,10 +1580,10 @@ CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) CBotTypResult type1 = pStack->GivTypResult(); - // rcupre la variable pour la marquer assigne + // get the variable assigned to mark CBotVar* var = NULL; inst->m_leftop->ExecuteVar(var, pStack); - if ( var == NULL ) + if (var == NULL) { delete inst; return NULL; @@ -1617,18 +1598,18 @@ CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) CBotTypResult type2 = var->GivTypResult(); - // quels sont les types acceptables ? + // what types are acceptable? switch (OpType) { case ID_ASS: - // if (type2 == CBotTypClass) type2 = -1; // pas de classe - if ( (type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer) ) || + // if (type2 == CBotTypClass) type2 = -1; + if ((type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer)) || (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) { /* CBotClass* c1 = type1.GivClass(); CBotClass* c2 = type2.GivClass(); - if ( !c1->IsChildOf(c2) ) type2.SetType(-1); // pas la mme classe -//- if ( !type1.Eq(CBotTypClass) ) var->SetPointer(pStack->GivVar()->GivPointer());*/ + if (!c1->IsChildOf(c2)) type2.SetType(-1); +//- if (!type1.Eq(CBotTypClass)) var->SetPointer(pStack->GivVar()->GivPointer());*/ var->SetInit(2); } else @@ -1637,46 +1618,43 @@ CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) break; case ID_ASSADD: if (type2.Eq(CBotTypBoolean) || - type2.Eq(CBotTypPointer) ) type2 = -1; // nombres et chaines + type2.Eq(CBotTypPointer) ) type2 = -1; // numbers and strings break; case ID_ASSSUB: case ID_ASSMUL: case ID_ASSDIV: case ID_ASSMODULO: - if (type2.GivType() >= CBotTypBoolean) type2 = -1; // nombres uniquement + if (type2.GivType() >= CBotTypBoolean) type2 = -1; // numbers only break; } - if (!TypeCompatible( type1, type2, OpType )) + if (!TypeCompatible(type1, type2, OpType)) { pStack->SetError(TX_BADTYPE, &inst->m_token); delete inst; return NULL; } - return inst; // types compatibles ? + return inst; // compatible type? } delete inst; -// p = p->GivNext(); int start, end, error = pStack->GivError(start, end); - p = pp; // revient au dbut - pStack->SetError(0,0); // oublie l'erreur + p = pp; // returns to the top + pStack->SetError(0,0); // forget the error -// return CBotTwoOpExpr::Compile(p, pStack); // essaie sans assignation - CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // essaie sans assignation - if ( i != NULL && error == TX_PRIVATE && p->GivType() == ID_ASS ) - pStack->ResetError( error, start, end ); + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // tries without assignment + if (i != NULL && error == TX_PRIVATE && p->GivType() == ID_ASS) + pStack->ResetError(error, start, end); return i; } -// excute une expression avec assignation +// executes an expression with assignment bool CBotExpression::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; CBotToken* pToken = m_leftop->GivToken(); CBotVar* pVar = NULL; @@ -1686,33 +1664,30 @@ bool CBotExpression::Execute(CBotStack* &pj) bool IsInit = true; CBotVar* result = NULL; - // doit tre fait avant pour les indices ventuels (pile peut tre change) - if ( !m_leftop->ExecuteVar(pVar, pile, NULL, false) ) return false; // variable avant valuation de la valeur droite + // must be done before any indexes (stack can be changed) + if (!m_leftop->ExecuteVar(pVar, pile, NULL, false)) return false; // variable before accessing the value on the right -// DEBUG( "CBotExpression::Execute", -1, pj); if ( pile1->GivState()==0) { - pile1->SetCopyVar(pVar); // garde une copie sur la pile (si interrompu) + pile1->SetCopyVar(pVar); // keeps the copy on the stack (if interrupted) pile1->IncState(); } - CBotStack* pile2 = pile->AddStack(); // attention pile et surtout pas pile1 + CBotStack* pile2 = pile->AddStack(); if ( pile2->GivState()==0) { -// DEBUG( "CBotExpression::Execute", -2, pj); - if (m_rightop && !m_rightop->Execute(pile2)) return false; // valeur initiale // interrompu? + if (m_rightop && !m_rightop->Execute(pile2)) return false; // initial value // interrupted? pile2->IncState(); } - if ( pile1->GivState() == 1 ) + if (pile1->GivState() == 1) { -// DEBUG( "CBotExpression::Execute", -3, pj); - if ( m_token.GivType() != ID_ASS ) + if (m_token.GivType() != ID_ASS) { - pVar = pile1->GivVar(); // rcupre si interrompu + pVar = pile1->GivVar(); // recovers if interrupted IsInit = pVar->GivInit(); - if ( IsInit == IS_NAN ) + if (IsInit == IS_NAN) { pile2->SetError(TX_OPNAN, m_leftop->GivToken()); return pj->Return(pile2); @@ -1720,57 +1695,57 @@ bool CBotExpression::Execute(CBotStack* &pj) result = CBotVar::Create("", pVar->GivTypResult(2)); } - switch ( m_token.GivType() ) + switch (m_token.GivType()) { case ID_ASS: break; case ID_ASSADD: - result->Add(pile1->GivVar(), pile2->GivVar()); // additionne - pile2->SetVar(result); // re-place le rsultat + result->Add(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); break; case ID_ASSSUB: - result->Sub(pile1->GivVar(), pile2->GivVar()); // soustrait - pile2->SetVar(result); // re-place le rsultat + result->Sub(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); break; case ID_ASSMUL: - result->Mul(pile1->GivVar(), pile2->GivVar()); // multiplie - pile2->SetVar(result); // re-place le rsultat + result->Mul(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); break; case ID_ASSDIV: if (IsInit && - result->Div(pile1->GivVar(), pile2->GivVar())) // divise + result->Div(pile1->GivVar(), pile2->GivVar())) pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; case ID_ASSMODULO: if (IsInit && - result->Modulo(pile1->GivVar(), pile2->GivVar())) // reste de la division + result->Modulo(pile1->GivVar(), pile2->GivVar())) pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; case ID_ASSAND: - result->And(pile1->GivVar(), pile2->GivVar()); // multiplie - pile2->SetVar(result); // re-place le rsultat + result->And(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); break; case ID_ASSXOR: result->XOr(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; case ID_ASSOR: result->Or(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; case ID_ASSSL: result->SL(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; case ID_ASSSR: result->SR(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; case ID_ASSASR: result->ASR(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); // re-place le rsultat + pile2->SetVar(result); break; default: ASM_TRAP(); @@ -1781,41 +1756,39 @@ bool CBotExpression::Execute(CBotStack* &pj) pile1->IncState(); } -// DEBUG( "CBotExpression::Execute", -4, pj); - if ( !m_leftop->Execute( pile2, pile1 ) ) - return false; // cre et fait l'assigation du rsultat + if (!m_leftop->Execute( pile2, pile1 )) + return false; - return pj->Return( pile2 ); // transmet en dessous + return pj->Return(pile2); } void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) + if (bMain) { CBotToken* pToken = m_leftop->GivToken(); CBotVar* pVar = NULL; CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; CBotStack* pile1 = pile; - if ( pile1->GivState()==0) { - m_leftop->RestoreStateVar(pile, true); // variable avant valuation de la valeur droite + m_leftop->RestoreStateVar(pile, true); return; } - m_leftop->RestoreStateVar(pile, false); // variable avant valuation de la valeur droite + m_leftop->RestoreStateVar(pile, false); - CBotStack* pile2 = pile->RestoreStack(); // attention pile et surtout pas pile1 - if ( pile2 == NULL ) return; + CBotStack* pile2 = pile->RestoreStack(); + if (pile2 == NULL) return; if ( pile2->GivState()==0) { - if (m_rightop) m_rightop->RestoreState(pile2, bMain); // valeur initiale // interrompu? + if (m_rightop) m_rightop->RestoreState(pile2, bMain); return; } } @@ -1825,55 +1798,58 @@ void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////// -// compile une instruction de type " ( condition ) " -// la condition doit tre de type boolen +// compile a statement such as "(condition)" +// the condition must be Boolean + +// this class has no constructor, because there is never an instance of this class +// the object returned by Compile is usually type CBotExpression -// cette classe n'a pas de constructeur, car il n'y a jamais d'instance de cette classe -// l'objet retourn par Compile est gnralement de type CBotExpression CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) { pStack->SetStartError(p->GivStart()); - if ( IsOfType(p, ID_OPENPAR )) + if (IsOfType(p, ID_OPENPAR)) { - CBotInstr* inst = CBotBoolExpr::Compile( p, pStack ); - if ( NULL != inst ) + CBotInstr* inst = CBotBoolExpr::Compile(p, pStack); + if (NULL != inst) { - if ( IsOfType(p, ID_CLOSEPAR )) + if (IsOfType(p, ID_CLOSEPAR)) { return inst; } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); // manque la parenthse + pStack->SetError(TX_CLOSEPAR, p->GivStart()); // missing parenthesis } delete inst; } - pStack->SetError(TX_OPENPAR, p->GivStart()); // manque la parenthse + pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis return NULL; } ////////////////////////////////////////////////////////////////////////////////////// -// compile une instruction de type " condition " -// la condition doit tre de type boolen - -// cette classe n'a pas de constructeur, car il n'y a jamais d'instance de cette classe -// l'objet retourn par Compile est gnralement de type CBotExpression +// compile a statement such as "(condition)" +// the condition must be Boolean +// +// this class has no constructor, because there is never an instance of this +// class +// the object returned by Compile is usually type CBotExpression +// CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) { pStack->SetStartError(p->GivStart()); - CBotInstr* inst = CBotTwoOpExpr::Compile( p, pStack ); + CBotInstr* inst = CBotTwoOpExpr::Compile(p, pStack); - if ( NULL != inst ) + if (NULL != inst) { - if ( pStack->GivTypResult().Eq(CBotTypBoolean) ) + if (pStack->GivTypResult().Eq(CBotTypBoolean)) { return inst; } - pStack->SetError(TX_NOTBOOL, p->GivStart()); // n'est pas un boolan + pStack->SetError(TX_NOTBOOL, p->GivStart()); // is not a boolean } delete inst; @@ -1884,18 +1860,19 @@ CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) ////////////////////////////////////////////////////////////////////////////////////// -// compile soit : -// une instruction entre parenthses (...) -// une expression unaire (ngatif, not) -// nom de variable -// les variables pr et post incrmentes ou dcrmentes -// un nombre donn par DefineNum -// une constante -// un appel de procdure -// l'instruction new - -// cette classe n'a pas de constructeur, car il n'y a jamais d'instance de cette classe -// l'objet retourn par Compile est de la classe correspondant l'instruction +// compile either: +// instruction in parentheses (...) +// a unary expression (negative, not) +// variable name +// variables pre and post-incremented or decremented +// a given number DefineNum +// a constant +// procedure call +// new statement +// +// this class has no constructor, because there is never an instance of this class +// the object returned by Compile is the class corresponding to the instruction + CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) { @@ -1903,12 +1880,12 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) pStk->SetStartError(p->GivStart()); - // est-ce une expression entre parenthse ? + // is it an expression in parentheses? if (IsOfType(p, ID_OPENPAR)) { - CBotInstr* inst = CBotExpression::Compile( p, pStk ); + CBotInstr* inst = CBotExpression::Compile(p, pStk); - if ( NULL != inst ) + if (NULL != inst) { if (IsOfType(p, ID_CLOSEPAR)) { @@ -1920,41 +1897,41 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(NULL, pStk); } - // est-ce une opration unaire ? + // is this a unary operation? CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); if (inst != NULL || !pStk->IsOk()) return pStack->Return(inst, pStk); - // est-ce un nom de variable ? + // is it a variable name? if (p->GivType() == TokenTypVar) { - // c'est peut-tre un appel de mthode sans le "this." devant + // this may be a method call without the "this." before inst = CBotExprVar::CompileMethode(p, pStk); - if ( inst != NULL ) return pStack->Return(inst, pStk); + if (inst != NULL) return pStack->Return(inst, pStk); - // est-ce un appel de procdure ? + // is it a procedure call? inst = CBotInstrCall::Compile(p, pStk); - if ( inst != NULL || !pStk->IsOk() ) + if (inst != NULL || !pStk->IsOk()) return pStack->Return(inst, pStk); CBotToken* pvar = p; - // non, c'est une variable "ordinaire" + // no, it an "ordinaty" variable inst = CBotExprVar::Compile(p, pStk); CBotToken* pp = p; - // post incrment ou dcrment ? + // post incremented or decremented? if (IsOfType(p, ID_INC, ID_DEC)) { - if ( pStk->GivType() >= CBotTypBoolean ) + if (pStk->GivType() >= CBotTypBoolean) { pStk->SetError(TX_BADTYPE, pp); delete inst; return pStack->Return(NULL, pStk); } - // recompile la variable pour read-only + // recompile the variable for read-only delete inst; p = pvar; inst = CBotExprVar::Compile(p, pStk, PR_READ); @@ -1962,13 +1939,13 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) CBotPostIncExpr* i = new CBotPostIncExpr(); i->SetToken(pp); - i->m_Instr = inst; // instruction associe + i->m_Instr = inst; // associated statement return pStack->Return(i, pStk); } return pStack->Return(inst, pStk); } - // est-ce une variable princrmente ou prdcrmente ? + // pre increpemted or pre decremented? CBotToken* pp = p; if (IsOfType(p, ID_INC, ID_DEC)) { @@ -1979,7 +1956,7 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) { if (NULL != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) { - if ( pStk->GivType() >= CBotTypBoolean ) + if (pStk->GivType() >= CBotTypBoolean) { pStk->SetError(TX_BADTYPE, pp); delete inst; @@ -1992,51 +1969,51 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) } } - // est-ce un nombre ou un DefineNum ? + // is it a number or DefineNum? if (p->GivType() == TokenTypNum || p->GivType() == TokenTypDef ) { - CBotInstr* inst = CBotExprNum::Compile( p, pStk ); + CBotInstr* inst = CBotExprNum::Compile(p, pStk); return pStack->Return(inst, pStk); } - // est-ce une chaine ? + // is this a chaine? if (p->GivType() == TokenTypString) { CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); return pStack->Return(inst, pStk); } - // est un lment "true" ou "false" + // is a "true" or "false" if (p->GivType() == ID_TRUE || p->GivType() == ID_FALSE ) { - CBotInstr* inst = CBotExprBool::Compile( p, pStk ); + CBotInstr* inst = CBotExprBool::Compile(p, pStk); return pStack->Return(inst, pStk); } - // est un objet crer avec new + // is an object to be created with new if (p->GivType() == ID_NEW) { - CBotInstr* inst = CBotNew::Compile( p, pStk ); + CBotInstr* inst = CBotNew::Compile(p, pStk); return pStack->Return(inst, pStk); } - // est un pointeur nul - if (IsOfType( p, ID_NULL )) + // is a null pointer + if (IsOfType(p, ID_NULL)) { CBotInstr* inst = new CBotExprNull (); - inst->SetToken( pp ); + inst->SetToken(pp); CBotVar* var = CBotVar::Create("", CBotTypNullPointer); pStk->SetVar(var); return pStack->Return(inst, pStk); } - // est un nombre nan - if (IsOfType( p, ID_NAN )) + // is a number nan + if (IsOfType(p, ID_NAN)) { CBotInstr* inst = new CBotExprNan (); - inst->SetToken( pp ); + inst->SetToken(pp); CBotVar* var = CBotVar::Create("", CBotTypInt); var->SetInit(IS_NAN); pStk->SetVar(var); @@ -2050,10 +2027,10 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// gestion du post et pr- incrment/dcrment +// Management of pre-and post increment / decrement +// There is no routine Compiles, the object is created directly +// Compiles in CBotParExpr :: -// il n'y a pas de routine Compile, l'objet est cr directement -// dans CBotParExpr::Compile CBotPostIncExpr::CBotPostIncExpr() { @@ -2084,88 +2061,89 @@ bool CBotPostIncExpr::Execute(CBotStack* &pj) CBotVar* var1 = NULL; - if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index + // retrieves the variable fields and indexes according + if (!((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true)) return false; pile1->SetState(1); pile1->SetCopyVar(var1); // place le rsultat (avant incrmentation); CBotStack* pile3 = pile2->AddStack(this); - if ( pile3->IfStep() ) return false; + if (pile3->IfStep()) return false; - if ( var1->GivInit() == IS_NAN ) + if (var1->GivInit() == IS_NAN) { - pile1->SetError( TX_OPNAN, &m_token ); + pile1->SetError(TX_OPNAN, &m_token); } - if ( var1->GivInit() != IS_DEF ) + if (var1->GivInit() != IS_DEF) { - pile1->SetError( TX_NOTINIT, &m_token ); + pile1->SetError(TX_NOTINIT, &m_token); } if (GivTokenType() == ID_INC) var1->Inc(); else var1->Dec(); - return pj->Return(pile1); // opration faite, rsultat sur pile2 + return pj->Return(pile1); // operation done, result on pile2 } void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; CBotStack* pile1 = pj->RestoreStack(this); - if ( pile1 == NULL ) return; + if (pile1 == NULL) return; ((CBotExprVar*)m_Instr)->RestoreStateVar(pile1, bMain); - if ( pile1 != NULL ) pile1->RestoreStack(this); + if (pile1 != NULL) pile1->RestoreStack(this); } bool CBotPreIncExpr::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; CBotVar* var1; - if ( pile->GivState() == 0 ) + if (pile->GivState() == 0) { CBotStack* pile2 = pile; - if ( !((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true) ) return false; // rcupre la variable selon champs et index - // pile2 est modifi en retour + // retrieves the variable fields and indexes according + // pile2 is modified on return + if (!((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true)) return false; - if ( var1->GivInit() == IS_NAN ) + if (var1->GivInit() == IS_NAN) { - pile->SetError( TX_OPNAN, &m_token ); - return pj->Return(pile); // opration faite + pile->SetError(TX_OPNAN, &m_token); + return pj->Return(pile); // operation performed } - if ( var1->GivInit() != IS_DEF ) + if (var1->GivInit() != IS_DEF) { - pile->SetError( TX_NOTINIT, &m_token ); - return pj->Return(pile); // opration faite + pile->SetError(TX_NOTINIT, &m_token); + return pj->Return(pile); // operation performed } if (GivTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); // ((CBotVarInt*)var1)->m_val + else var1->Dec(); // ((CBotVarInt*)var1)->m_val pile->IncState(); } - if ( !m_Instr->Execute(pile) ) return false; - return pj->Return(pile); // opration faite + if (!m_Instr->Execute(pile)) return false; + return pj->Return(pile); // operation performed } void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; - if ( pile->GivState() == 0 ) + if (pile->GivState() == 0) { return; } @@ -2175,7 +2153,7 @@ void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////// -// compile une expression unaire +// compile an unary expression // + // - // not @@ -2197,24 +2175,24 @@ CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) { int op = p->GivType(); CBotToken* pp = p; - if ( !IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 ) ) return NULL; + if (!IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 )) return NULL; CBotCStack* pStk = pStack->TokenStack(pp); CBotExprUnaire* inst = new CBotExprUnaire(); inst->SetToken(pp); - if ( NULL != (inst->m_Expr = CBotParExpr::Compile( p, pStk )) ) + if (NULL != (inst->m_Expr = CBotParExpr::Compile( p, pStk ))) { - if ( op == ID_ADD && pStk->GivType() < CBotTypBoolean ) // seulement avec des nombre + if (op == ID_ADD && pStk->GivType() < CBotTypBoolean) // only with the number return pStack->Return(inst, pStk); - if ( op == ID_SUB && pStk->GivType() < CBotTypBoolean ) // seulement avec des nombre + if (op == ID_SUB && pStk->GivType() < CBotTypBoolean) // only with the numer return pStack->Return(inst, pStk); - if ( op == ID_NOT && pStk->GivType() < CBotTypFloat ) // seulement avec des entiers + if (op == ID_NOT && pStk->GivType() < CBotTypFloat) // only with an integer return pStack->Return(inst, pStk); - if ( op == ID_LOG_NOT && pStk->GivTypResult().Eq(CBotTypBoolean) )// seulement avec des boolens + if (op == ID_LOG_NOT && pStk->GivTypResult().Eq(CBotTypBoolean))// only with boolean return pStack->Return(inst, pStk); - if ( op == ID_TXT_NOT && pStk->GivTypResult().Eq(CBotTypBoolean) )// seulement avec des boolens + if (op == ID_TXT_NOT && pStk->GivTypResult().Eq(CBotTypBoolean))// only with boolean return pStack->Return(inst, pStk); pStk->SetError(TX_BADTYPE, &inst->m_token); @@ -2223,30 +2201,29 @@ CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(NULL, pStk); } -// excute l'expresson unaire +// executes unary expression bool CBotExprUnaire::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) + if (pile->GivState() == 0) { - if (!m_Expr->Execute( pile )) return false; // interrompu ? + if (!m_Expr->Execute(pile)) return false; // interrupted ? pile->IncState(); } CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return false; + if (pile2->IfStep()) return false; - CBotVar* var = pile->GivVar(); // rcupre le rsultat sur la pile + CBotVar* var = pile->GivVar(); // get the result on the stack switch (GivTokenType()) { case ID_ADD: - break; // ne fait donc rien + break; case ID_SUB: - var->Neg(); // change le signe + var->Neg(); // change the sign break; case ID_NOT: case ID_LOG_NOT: @@ -2254,19 +2231,19 @@ bool CBotExprUnaire::Execute(CBotStack* &pj) var->Not(); break; } - return pj->Return(pile); // transmet en dessous + return pj->Return(pile); // forwards below } void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; CBotStack* pile = pj->RestoreStack(this); if ( pile == NULL) return; - if ( pile->GivState() == 0 ) + if (pile->GivState() == 0) { - m_Expr->RestoreState( pile, bMain ); // interrompu ici ! + m_Expr->RestoreState(pile, bMain); // interrupted here! return; } } @@ -2274,7 +2251,7 @@ void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// gestion des index pour les tableaux +// index management for arrays // array [ expression ] @@ -2289,95 +2266,89 @@ CBotIndexExpr::~CBotIndexExpr() delete m_expr; } -// trouve un champ partir de l'instance la compilation +// finds a field from the instance at compile time bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - if ( pVar->GivType(1) != CBotTypArrayPointer ) + if (pVar->GivType(1) != CBotTypArrayPointer) ASM_TRAP(); - pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // la compilation rend l'lment [0] - if ( pVar == NULL ) + pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // at compile time makes the element [0] + if (pVar == NULL) { pile->SetError(TX_OUTARRAY, m_token.GivEnd()); return false; } - if ( m_next3 != NULL ) return m_next3->ExecuteVar(pVar, pile); + if (m_next3 != NULL) return m_next3->ExecuteVar(pVar, pile); return true; } -// idem l'excution -// attention, modifie le pointeur la pile volontairement -// place les index calculs sur la pile supplmentaire +// attention, changes the pointer to the stack intentionally +// place the index calculated on the additional stack + bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { CBotStack* pj = pile; -// DEBUG( "CBotIndexExpr::ExecuteVar", -1 , pj); - if ( pVar->GivType(1) != CBotTypArrayPointer ) + if (pVar->GivType(1) != CBotTypArrayPointer) ASM_TRAP(); pile = pile->AddStack(); -// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) + if (pile->GivState() == 0) { - if ( !m_expr->Execute(pile) ) return false; + if (!m_expr->Execute(pile)) return false; pile->IncState(); } - // traite les tableaux + // handles array - CBotVar* p = pile->GivVar(); // rsultat sur la pile + CBotVar* p = pile->GivVar(); // result on the stack - if ( p == NULL || p->GivType() > CBotTypDouble ) + if (p == NULL || p->GivType() > CBotTypDouble) { pile->SetError(TX_BADINDEX, prevToken); return pj->Return(pile); } - int n = p->GivValInt(); // position dans le tableau -// DEBUG( "CBotIndexExpr::ExecuteVar", n , pj); + int n = p->GivValInt(); // position in the table pVar = ((CBotVarArray*)pVar)->GivItem(n, bExtend); - if ( pVar == NULL ) + if (pVar == NULL) { pile->SetError(TX_OUTARRAY, prevToken); return pj->Return(pile); } -// DEBUG( "CBotIndexExpr::ExecuteVar", -2 , pj); - //if ( bUpdate ) - pVar->Maj(pile->GivPUser(), true); + pVar->Maj(pile->GivPUser(), true); -// DEBUG( "CBotIndexExpr::ExecuteVar", -3 , pj); if ( m_next3 != NULL && !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; -// DEBUG( "CBotIndexExpr::ExecuteVar", -4 , pj); - return true; // ne libre pas la pile - // pour viter de recalculer les index deux fois le cas chant + // does not release the stack + // to avoid recalculation of the index twice where appropriate + return true; } void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) { pile = pile->RestoreStack(); - if ( pile == NULL ) return; + if (pile == NULL) return; - if ( bMain && pile->GivState() == 0 ) + if (bMain && pile->GivState() == 0) { m_expr->RestoreState(pile, true); return; } - if ( m_next3 ) + if (m_next3) m_next3->RestoreStateVar(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// gestion des champs dans une instance (oprateur point) +// field management in an instance (dot operator) // toto.x @@ -2397,16 +2368,15 @@ void CBotFieldExpr::SetUniqNum(int num) } -// trouve un champ partir de l'instance la compilation +// find a field from the instance at compile bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { - if ( pVar->GivType(1) != CBotTypPointer ) + if (pVar->GivType(1) != CBotTypPointer) ASM_TRAP(); -// pVar = pVar->GivItem(m_token.GivString()); pVar = pVar->GivItemRef(m_nIdent); - if ( pVar == NULL ) + if (pVar == NULL) { pile->SetError(TX_NOITEM, &m_token); return false; @@ -2418,74 +2388,69 @@ bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) return true; } -// idem l'excution - bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) { CBotStack* pj = pile; - pile = pile->AddStack(this); // modifie pile en sortie - if ( pile == EOX ) return true; + pile = pile->AddStack(this); // changes in output stack + if (pile == EOX) return true; -// DEBUG( "CBotFieldExpre::ExecuteVar "+m_token.GivString(), 0, pj ); - if ( pVar->GivType(1) != CBotTypPointer ) + if (pVar->GivType(1) != CBotTypPointer) ASM_TRAP(); CBotVarClass* pItem = pVar->GivPointer(); - if ( pItem == NULL ) + if (pItem == NULL) { pile->SetError(TX_NULLPT, prevToken); - return pj->Return( pile ); + return pj->Return(pile); } - if ( pItem->GivUserPtr() == OBJECTDELETED ) + if (pItem->GivUserPtr() == OBJECTDELETED) { pile->SetError(TX_DELETEDPT, prevToken); - return pj->Return( pile ); + return pj->Return(pile); } - if ( bStep && pile->IfStep() ) return false; + if (bStep && pile->IfStep()) return false; -// pVar = pVar->GivItem(m_token.GivString()); pVar = pVar->GivItemRef(m_nIdent); - if ( pVar == NULL ) + if (pVar == NULL) { pile->SetError(TX_NOITEM, &m_token); - return pj->Return( pile ); + return pj->Return(pile); } - if ( pVar->IsStatic() ) + if (pVar->IsStatic()) { -// DEBUG( "IsStatic", 0, pj) ; - // pour une variable statique, la prend dans la classe elle-mme + // for a static variable, takes it in the class itself CBotClass* pClass = pItem->GivClass(); pVar = pClass->GivItem(m_token.GivString()); -// DEBUG( "done "+pVar->GivName(), 0, pj) ; } - // demande la mise jour de l'lment, s'il y a lieu + // request the update of the element, if applicable pVar->Maj(pile->GivPUser(), true); if ( m_next3 != NULL && !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; - return true; // ne libre pas la pile - // pour conserver l'tat SetState() correspondant l'tape + // does not release the stack + // to maintain the state SetState () corresponding to step + + return true; } void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) { - pj = pj->RestoreStack(this); // modifie pj en sortie - if ( pj == NULL ) return; + pj = pj->RestoreStack(this); + if (pj == NULL) return; - if ( m_next3 != NULL ) + if (m_next3 != NULL) m_next3->RestoreStateVar(pj, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// compile un oprande gauche pour une assignation - +// compile a left operand for an assignment CBotLeftExpr::CBotLeftExpr() { name = "CBotLeftExpr"; @@ -2515,13 +2480,13 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) // is it a variable name? if (p->GivType() == TokenTypVar) { - CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object + CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object inst->SetToken(p); CBotVar* var; - if ( NULL != (var = pStk->FindVar(p)) ) // seek if known variable + if (NULL != (var = pStk->FindVar(p))) // seek if known variable { inst->m_nIdent = var->GivUniqNum(); if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) @@ -2529,81 +2494,81 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) if ( var->IsPrivate(PR_READ) && !pStk->GivBotCall()->m_bCompileClass) { - pStk->SetError( TX_PRIVATE, p ); + pStk->SetError(TX_PRIVATE, p); goto err; } - // il s'agit d'un lement de la classe courante - // ajoute l'quivalent d'un this. devant + // this is an element of the current class + // adds the equivalent of this. before CBotToken pthis("this"); inst->SetToken(&pthis); - inst->m_nIdent = -2; // ident pour this + inst->m_nIdent = -2; // indent for this - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( p ); // garde le nom du token - inst->AddNext3(i); // ajoute la suite + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(p); // keeps the name of the token + inst->AddNext3(i); // add after var = pStk->FindVar(pthis); var = var->GivItem(p->GivString()); i->SetUniqNum(var->GivUniqNum()); } - p = p->GivNext(); // token suivant + p = p->GivNext(); // next token while (true) { - if ( var->GivType() == CBotTypArrayPointer ) // s'il sagit d'un tableau + if (var->GivType() == CBotTypArrayPointer) { - if ( IsOfType( p, ID_OPBRK ) ) // regarde s'il y a un index + if (IsOfType( p, ID_OPBRK )) { CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); // compile la formule - inst->AddNext3(i); // ajoute la chaine + i->m_expr = CBotExpression::Compile(p, pStk); + inst->AddNext3(i); // add to the chain - var = ((CBotVarArray*)var)->GivItem(0,true); // donne le composant [0] + var = ((CBotVarArray*)var)->GivItem(0,true); // gets the component [0] - if ( i->m_expr == NULL ) + if (i->m_expr == NULL) { - pStk->SetError( TX_BADINDEX, p->GivStart() ); + pStk->SetError(TX_BADINDEX, p->GivStart()); goto err; } - if ( !pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) { - pStk->SetError( TX_CLBRK, p->GivStart() ); + pStk->SetError(TX_CLBRK, p->GivStart()); goto err; } continue; } } - if ( var->GivType(1) == CBotTypPointer ) // pour les classes + if (var->GivType(1) == CBotTypPointer) // for classes { - if ( IsOfType(p, ID_DOT) ) + if (IsOfType(p, ID_DOT)) { CBotToken* pp = p; - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( pp ); // garde le nom du token - inst->AddNext3(i); // ajoute la suite + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(pp); // keeps the name of the token + inst->AddNext3(i); // adds after - if ( p->GivType() == TokenTypVar ) // doit tre un nom + if (p->GivType() == TokenTypVar) // must be a name { - var = var->GivItem(p->GivString()); // rcupre l'item correpondant - if ( var != NULL ) + var = var->GivItem(p->GivString()); // get item correspondent + if (var != NULL) { if ( var->IsPrivate(PR_READ) && !pStk->GivBotCall()->m_bCompileClass) { - pStk->SetError( TX_PRIVATE, pp ); + pStk->SetError(TX_PRIVATE, pp); goto err; } i->SetUniqNum(var->GivUniqNum()); - p = p->GivNext(); // saute le nom + p = p->GivNext(); // skips the name continue; } - pStk->SetError( TX_NOITEM, p ); + pStk->SetError(TX_NOITEM, p); } - pStk->SetError( TX_DOT, p->GivStart() ); + pStk->SetError(TX_DOT, p->GivStart()); goto err; } } @@ -2611,7 +2576,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) } - if ( pStk->IsOk() ) return (CBotLeftExpr*) pStack->Return(inst, pStk); + if (pStk->IsOk()) return (CBotLeftExpr*) pStack->Return(inst, pStk); } pStk->SetError(TX_UNDEFVAR, p); err: @@ -2622,31 +2587,26 @@ err: return (CBotLeftExpr*) pStack->Return(NULL, pStk); } -// excute, trouve une variable et lui assigne le rsultat de la pile - +// runs, is a variable and assigns the result to the stack bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) { CBotStack* pile = pj->AddStack(); -// if ( pile == EOX ) return true; - -// if ( pile->IfStep() ) return false; CBotVar* var1 = NULL; CBotVar* var2 = NULL; + // fetch a variable (not copy) + if (!ExecuteVar(var1, array, NULL, false)) return false; -// var1 = pile->FindVar(m_token, false, true); - if (!ExecuteVar( var1, array, NULL, false )) return false; - // retrouve la variable (et pas la copie) if (pile->IfStep()) return false; - if ( var1 ) + if (var1) { - var2 = pj->GivVar(); // resultat sur la pile d'entre - if ( var2 ) + var2 = pj->GivVar(); // result on the input stack + if (var2) { CBotTypResult t1 = var1->GivTypResult(); CBotTypResult t2 = var2->GivTypResult(); - if ( t2.Eq(CBotTypPointer) ) + if (t2.Eq(CBotTypPointer)) { CBotClass* c1 = t1.GivClass(); CBotClass* c2 = t2.GivClass(); @@ -2654,24 +2614,24 @@ bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) { CBotToken* pt = &m_token; pile->SetError(TX_BADTYPE, pt); - return pj->Return(pile); // opration faite + return pj->Return(pile); // operation performed } } - var1->SetVal(var2); // fait l'assignation + var1->SetVal(var2); // do assignment } - pile->SetCopyVar( var1 ); // remplace sur la pile par une copie de la variable elle-mme - // (pour avoir le nom) + pile->SetCopyVar(var1); // replace the stack with the copy of the variable + // (for name) } - return pj->Return(pile); // opration faite + return pj->Return(pile); // operation performed } -// retrouve une variable pendant la compilation +// fetch a variable during compilation bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) { pVar = pile->FindVar(m_token); - if ( pVar == NULL ) return false; + if (pVar == NULL) return false; if ( m_next3 != NULL && !m_next3->ExecuteVar(pVar, pile) ) return false; @@ -2679,14 +2639,14 @@ bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) return true; } -// retrouve une variable l'excution +// fetch the variable at runtume bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) { - pile = pile->AddStack( this ); // dplace la pile + pile = pile->AddStack(this); pVar = pile->FindVar(m_nIdent); - if ( pVar == NULL ) + if (pVar == NULL) { #ifdef _DEBUG ASM_TRAP(); @@ -2695,7 +2655,7 @@ bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevT return false; } - if ( bStep && m_next3 == NULL && pile->IfStep() ) return false; + if (bStep && m_next3 == NULL && pile->IfStep()) return false; if ( m_next3 != NULL && !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; @@ -2705,19 +2665,19 @@ bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevT void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) { - pile = pile->RestoreStack( this ); // dplace la pile - if ( pile == NULL ) return; + pile = pile->RestoreStack(this); + if (pile == NULL) return; - if ( m_next3 != NULL ) + if (m_next3 != NULL) m_next3->RestoreStateVar(pile, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// -// transforme une chane en nombre entier -// peut tre de la forme 0xabc123 +// converts a string into integer +// may be of the form 0xabc123 -long GivNumInt( const char* p ) +long GivNumInt(const char* p) { long num = 0; while (*p >= '0' && *p <= '9') @@ -2725,21 +2685,21 @@ long GivNumInt( const char* p ) num = num * 10 + *p - '0'; p++; } - if ( *p == 'x' || *p == 'X' ) + if (*p == 'x' || *p == 'X') { while (*++p != 0) { - if ( *p >= '0' && *p <= '9' ) + if (*p >= '0' && *p <= '9') { num = num * 16 + *p - '0'; continue; } - if ( *p >= 'A' && *p <= 'F' ) + if (*p >= 'A' && *p <= 'F') { num = num * 16 + *p - 'A' + 10; continue; } - if ( *p >= 'a' && *p <= 'f' ) + if (*p >= 'a' && *p <= 'f') { num = num * 16 + *p - 'a' + 10; continue; @@ -2750,9 +2710,8 @@ long GivNumInt( const char* p ) return num; } -// transforme une chane en un nombre rel - -extern float GivNumFloat( const char* p ) +// converts a string into a float number +extern float GivNumFloat(const char* p) { double num = 0; double div = 10; @@ -2769,7 +2728,7 @@ extern float GivNumFloat( const char* p ) p++; } - if ( *p == '.' ) + if (*p == '.') { p++; while (*p >= '0' && *p <= '9') @@ -2781,33 +2740,33 @@ extern float GivNumFloat( const char* p ) } int exp = 0; - if ( *p == 'e' || *p == 'E' ) + if (*p == 'e' || *p == 'E') { char neg = 0; p++; - if ( *p == '-' || *p == '+' ) neg = *p++; + if (*p == '-' || *p == '+') neg = *p++; while (*p >= '0' && *p <= '9') { exp = exp * 10 + (*p - '0'); p++; } - if ( neg == '-' ) exp = -exp; + if (neg == '-') exp = -exp; } - while ( exp > 0 ) + while (exp > 0) { num *= 10.0; exp--; } - while ( exp < 0 ) + while (exp < 0) { num /= 10.0; exp++; } - if ( bNeg ) num = -num; + if (bNeg) num = -num; return (float)num; } @@ -2815,8 +2774,7 @@ extern float GivNumFloat( const char* p ) ////////////////////////////////////////////////////////////////////////////////////// -// compile un token reprsentant un nombre - +// compiles a token representing a number CBotExprNum::CBotExprNum() { name = "CBotExprNum"; @@ -2836,13 +2794,13 @@ CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) CBotString s = p->GivString(); inst->m_numtype = CBotTypInt; - if ( p->GivType() == TokenTypDef ) + if (p->GivType() == TokenTypDef) { inst->m_valint = p->GivIdKey(); } else { - if ( s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) ) ) + if (s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) )) { inst->m_numtype = CBotTypFloat; inst->m_valfloat = GivNumFloat(s); @@ -2864,19 +2822,18 @@ CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(NULL, pStk); } -// excute, retourne le nombre correspondant +// execute, returns the corresponding number bool CBotExprNum::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, m_numtype); CBotString nombre ; - if ( m_token.GivType() == TokenTypDef ) + if (m_token.GivType() == TokenTypDef) { nombre = m_token.GivString(); } @@ -2885,27 +2842,27 @@ bool CBotExprNum::Execute(CBotStack* &pj) { case CBotTypShort: case CBotTypInt: - var->SetValInt( m_valint, nombre ); // valeur du nombre + var->SetValInt(m_valint, nombre); break; case CBotTypFloat: - var->SetValFloat( m_valfloat ); // valeur du nombre + var->SetValFloat(m_valfloat); break; } - pile->SetVar( var ); // mis sur la pile + pile->SetVar(var); // place on the stack - return pj->Return(pile); // c'est ok + return pj->Return(pile); // it's ok } void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if (bMain) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// compile un token reprsentant une chaine de caractres +// compile a token representing a string CBotExprAlpha::CBotExprAlpha() { @@ -2931,37 +2888,36 @@ CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(inst, pStk); } -// excute, retourne la chane correspondante +// execute, returns the corresponding string bool CBotExprAlpha::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); CBotString chaine = m_token.GivString(); - chaine = chaine.Mid(1, chaine.GivLength()-2); // enlve les guillemets + chaine = chaine.Mid(1, chaine.GivLength()-2); // removes the quotes - var->SetValString( chaine ); // valeur du nombre + var->SetValString(chaine); // value of the number - pile->SetVar( var ); // mis sur la pile + pile->SetVar(var); // put on the stack return pj->Return(pile); } void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if (bMain) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// -// compile un token reprsentant true ou false +// compile a token representing true or false CBotExprBool::CBotExprBool() { @@ -2981,7 +2937,7 @@ CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) p->GivType() == ID_FALSE ) { inst = new CBotExprBool(); - inst->SetToken(p); // mmorise l'opration false ou true + inst->SetToken(p); // stores the operation false or true p = p->GivNext(); CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); @@ -2991,32 +2947,31 @@ CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(inst, pStk); } -// excute, retourne true ou false +// executes, returns true or false bool CBotExprBool::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); if (GivTokenType() == ID_TRUE) var->SetValInt(1); else var->SetValInt(0); - pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // transmet en dessous + pile->SetVar(var); // put on the stack + return pj->Return(pile); // forwards below } void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if (bMain) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// -// gestion de l'oprande "null" +// management of the operand "null" CBotExprNull::CBotExprNull() { @@ -3027,29 +2982,28 @@ CBotExprNull::~CBotExprNull() { } -// excute, retourne un pointeur vide +// executes, returns an empty pointer bool CBotExprNull::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypNullPointer); - var->SetInit(true); // pointeur null valide - pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // transmet en dessous + var->SetInit(true); // null pointer valid + pile->SetVar(var); // place on the stack + return pj->Return(pile); // forwards below } void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if (bMain) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////////// -// gestion de l'oprande "nan" +// management of the operand "nan" CBotExprNan::CBotExprNan() { @@ -3060,30 +3014,29 @@ CBotExprNan::~CBotExprNan() { } -// excute, retourne un pointeur vide +// executes, returns null pointer bool CBotExprNan::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypInt); - var->SetInit(IS_NAN); // nombre nan - pile->SetVar( var ); // mis sur la pile - return pj->Return(pile); // transmet en dessous + var->SetInit(IS_NAN); // nan + pile->SetVar(var); // put on the stack + return pj->Return(pile); // forward below } void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if (bMain) pj->RestoreStack(this); } ////////////////////////////////////////////////////////////////////////////////////// -// compile un nom de variable -// vrifie qu'elle est connue sur la pile -// et qu'elle a t initialise +// compile a variable name +// check that it is known on the stack +// and it has been initialized CBotExprVar::CBotExprVar() { @@ -3105,23 +3058,23 @@ CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) // is it a variable? if (p->GivType() == TokenTypVar) { - CBotInstr* inst = new CBotExprVar(); // create the object + CBotInstr* inst = new CBotExprVar(); // create the object inst->SetToken(p); CBotVar* var; - if ( NULL != (var = pStk->FindVar(p)) ) // seek if known variable + if (NULL != (var = pStk->FindVar(p))) // seek if known variable { int ident = var->GivUniqNum(); - ((CBotExprVar*)inst)->m_nIdent = ident; // identifies variable by its number + ((CBotExprVar*)inst)->m_nIdent = ident; // identifies variable by its number if (ident > 0 && ident < 9000) { if ( var->IsPrivate(privat) && !pStk->GivBotCall()->m_bCompileClass) { - pStk->SetError( TX_PRIVATE, p ); + pStk->SetError(TX_PRIVATE, p); goto err; } @@ -3131,85 +3084,84 @@ CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) ///tests CBotToken token("this"); inst->SetToken(&token); - ((CBotExprVar*)inst)->m_nIdent = -2; // identificator for this + ((CBotExprVar*)inst)->m_nIdent = -2; // identificator for this - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken( p ); // keeps the name of the token + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(p); // keeps the name of the token i->SetUniqNum(ident); - inst->AddNext3(i); // added after + inst->AddNext3(i); // added after } - p = p->GivNext(); // next token + p = p->GivNext(); // next token while (true) { - if ( var->GivType() == CBotTypArrayPointer ) // s'il sagit d'un tableau + if (var->GivType() == CBotTypArrayPointer) { - if ( IsOfType( p, ID_OPBRK ) ) // regarde s'il y a un index + if (IsOfType( p, ID_OPBRK )) // check if there is an aindex { CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); // compile la formule - inst->AddNext3(i); // ajoute la chaine + i->m_expr = CBotExpression::Compile(p, pStk); // compile the formula + inst->AddNext3(i); // add to the chain - var = ((CBotVarArray*)var)->GivItem(0,true); // donne le composant [0] + var = ((CBotVarArray*)var)->GivItem(0,true); // gets the component [0] - if ( i->m_expr == NULL ) + if (i->m_expr == NULL) { - pStk->SetError( TX_BADINDEX, p->GivStart() ); + pStk->SetError(TX_BADINDEX, p->GivStart()); goto err; } - if ( !pStk->IsOk() || !IsOfType( p, ID_CLBRK ) ) + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) { - pStk->SetError( TX_CLBRK, p->GivStart() ); + pStk->SetError(TX_CLBRK, p->GivStart()); goto err; } continue; } - //// pStk->SetError( TX_OPBRK, p->GivStart() ); } - if ( var->GivType(1) == CBotTypPointer ) // pour les classes + if (var->GivType(1) == CBotTypPointer) // for classes { - if ( IsOfType(p, ID_DOT) ) + if (IsOfType(p, ID_DOT)) { CBotToken* pp = p; - if ( p->GivType() == TokenTypVar ) // doit tre un nom + if (p->GivType() == TokenTypVar) // must be a name { - if ( p->GivNext()->GivType() == ID_OPENPAR )// un appel de mthode ? + if (p->GivNext()->GivType() == ID_OPENPAR) // a method call? { CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); - if ( !pStk->IsOk() ) goto err; - inst->AddNext3(i); // ajoute la suite + if (!pStk->IsOk()) goto err; + inst->AddNext3(i); // added after return pStack->Return(inst, pStk); } else { - CBotFieldExpr* i = new CBotFieldExpr(); // nouvel lment - i->SetToken( pp ); // garde le nom du token - inst->AddNext3(i); // ajoute la suite - var = var->GivItem(p->GivString()); // rcupre l'item correpondant - if ( var != NULL ) + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(pp); // keeps the name of the token + inst->AddNext3(i); // add after + var = var->GivItem(p->GivString()); // get item correspondent + if (var != NULL) { i->SetUniqNum(var->GivUniqNum()); if ( var->IsPrivate() && !pStk->GivBotCall()->m_bCompileClass) { - pStk->SetError( TX_PRIVATE, pp ); + pStk->SetError(TX_PRIVATE, pp); goto err; } } } - if ( var != NULL ) + if (var != NULL) { - p = p->GivNext(); // saute le nom + p = p->GivNext(); // skips the name continue; } - pStk->SetError( TX_NOITEM, p ); + pStk->SetError(TX_NOITEM, p); goto err; } - pStk->SetError( TX_DOT, p->GivStart() ); + pStk->SetError(TX_DOT, p->GivStart()); goto err; } } @@ -3217,8 +3169,8 @@ CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) break; } - pStk->SetCopyVar(var); // place une copie de la variable sur la pile (pour le type) - if ( pStk->IsOk() ) return pStack->Return(inst, pStk); + pStk->SetCopyVar(var); // place the copy of the variable on the stack (for type) + if (pStk->IsOk()) return pStack->Return(inst, pStk); } pStk->SetError(TX_UNDEFVAR, p); err: @@ -3236,34 +3188,35 @@ CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) pStk->SetStartError(pp->GivStart()); - // est-ce un nom de variable ? + // is it a variable ? if (pp->GivType() == TokenTypVar) { CBotToken pthis("this"); CBotVar* var = pStk->FindVar(pthis); - if ( var == 0 ) return pStack->Return(NULL, pStk); + if (var == 0) return pStack->Return(NULL, pStk); + + CBotInstr* inst = new CBotExprVar(); + + // this is an element of the current class + // adds the equivalent of this. before - CBotInstr* inst = new CBotExprVar(); // cre l'objet - - // il s'agit d'un lement de la classe courante - // ajoute l'quivalent d'un this. devant inst->SetToken(&pthis); - ((CBotExprVar*)inst)->m_nIdent = -2; // ident pour this + ((CBotExprVar*)inst)->m_nIdent = -2; // ident for this CBotToken* pp = p; - if ( pp->GivType() == TokenTypVar ) // doit tre un nom + if (pp->GivType() == TokenTypVar) { - if ( pp->GivNext()->GivType() == ID_OPENPAR ) // un appel de mthode ? + if (pp->GivNext()->GivType() == ID_OPENPAR) // a method call? { CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); - if ( pStk->IsOk() ) + if (pStk->IsOk()) { - inst->AddNext3(i); // ajoute la suite - p = pp; // instructions passes + inst->AddNext3(i); // add after + p = pp; // previous instruction return pStack->Return(inst, pStk); } - pStk->SetError(0,0); // l'erreur n'est pas traite ici + pStk->SetError(0,0); // the error is not adressed here } } delete inst; @@ -3272,77 +3225,71 @@ CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) } -// excute, rend la valeur d'une variable +// execute, making the value of a variable bool CBotExprVar::Execute(CBotStack* &pj) { CBotVar* pVar = NULL; CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - -// if ( pile->IfStep() ) return false; CBotStack* pile1 = pile; - if ( pile1->GivState() == 0 ) + if (pile1->GivState() == 0) { - if ( !ExecuteVar(pVar, pile, NULL, true) ) return false; // rcupre la variable selon champs et index -// DEBUG("CBotExprVar::Execute", 1 , pj); + if (!ExecuteVar(pVar, pile, NULL, true)) return false; // Get the variable fields and indexes according - if ( pVar ) pile1->SetCopyVar(pVar); // la place une copie sur la pile + if (pVar) pile1->SetCopyVar(pVar); // place a copy on the stack else { -//-- pile1->SetVar(NULL); // la pile contient dj le resultat (mthode) return pj->Return(pile1); } pile1->IncState(); } - pVar = pile1->GivVar(); // rcupre si interrompu + pVar = pile1->GivVar(); - if ( pVar == NULL ) + if (pVar == NULL) { -// pile1->SetError(TX_NULLPT, &m_token); return pj->Return(pile1); } - if ( pVar->GivInit() == IS_UNDEF ) + if (pVar->GivInit() == IS_UNDEF) { CBotToken* pt = &m_token; - while ( pt->GivNext() != NULL ) pt = pt->GivNext(); + while (pt->GivNext() != NULL) pt = pt->GivNext(); pile1->SetError(TX_NOTINIT, pt); return pj->Return(pile1); } - return pj->Return(pile1); // opration faite + return pj->Return(pile1); // operation completed } void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + if (pile == NULL) return; CBotStack* pile1 = pile; - if ( pile1->GivState() == 0 ) + if (pile1->GivState() == 0) { - RestoreStateVar(pile, bMain); // rcupre la variable selon champs et index + RestoreStateVar(pile, bMain); // retrieves the variable fields and indexes according return; } } -// retrouve une variable l'excution +// fetch a variable at runtime bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) { CBotStack* pile = pj; - pj = pj->AddStack( this ); + pj = pj->AddStack(this); - if ( bStep && m_nIdent>0 && pj->IfStep() ) return false; + if (bStep && m_nIdent>0 && pj->IfStep()) return false; - pVar = pj->FindVar(m_nIdent, true); // cherche la variable avec mise jour si ncessaire - if ( pVar == NULL ) + pVar = pj->FindVar(m_nIdent, true); // tries with the variable update if necessary + if (pVar == NULL) { #ifdef _DEBUG ASM_TRAP(); @@ -3352,61 +3299,60 @@ bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToke } if ( m_next3 != NULL && !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) - return false; // Champs d'une instance, tableau, mthode ? + return false; // field of an instance, table, methode - return pile->ReturnKeep( pj ); // ne rend pas la pile mais rcupre le rsultat si une mthode a t appele + return pile->ReturnKeep(pj); // does not put on stack but get the result if a method was called } -// retrouve une variable l'excution +// fetch variable at runtime void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) { - pj = pj->RestoreStack( this ); - if ( pj == NULL ) return; + pj = pj->RestoreStack(this); + if (pj == NULL) return; - if ( m_next3 != NULL ) + if (m_next3 != NULL) m_next3->RestoreStateVar(pj, bMain); } ////////////////////////////////////////////////////////////////////////////////////////// -// compile une liste de paramtres +// compile a list of parameters CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) { bool first = true; - CBotInstr* ret = NULL; // pour la liste retourner + CBotInstr* ret = NULL; // to return to the list -// pStack->SetStartError(p->GivStart()); CBotCStack* pile = pStack; int i = 0; - if ( IsOfType(p, ID_OPENPAR) ) + if (IsOfType(p, ID_OPENPAR)) { int start, end; if (!IsOfType(p, ID_CLOSEPAR)) while (true) { start = p->GivStart(); - pile = pile->TokenStack(); // garde les rsultats sur la pile + pile = pile->TokenStack(); // keeps the result on the stack - if ( first ) pStack->SetStartError(start); + if (first) pStack->SetStartError(start); first = false; CBotInstr* param = CBotExpression::Compile(p, pile); end = p->GivStart(); - if ( !pile->IsOk() ) + if (!pile->IsOk()) { return pStack->Return(NULL, pile); } - if ( ret == NULL ) ret = param; - else ret->AddNext(param); // construit la liste + if (ret == NULL) ret = param; + else ret->AddNext(param); // construct the list - if ( param != NULL ) + if (param != NULL) { - if ( pile->GivTypResult().Eq(99) ) + if (pile->GivTypResult().Eq(99)) { delete pStack->TokenStack(); pStack->SetError(TX_VOID, p->GivStart()); @@ -3416,7 +3362,7 @@ CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) ppVars[i]->GivToken()->SetPos(start, end); i++; - if (IsOfType(p, ID_COMMA)) continue; // saute la virgule + if (IsOfType(p, ID_COMMA)) continue; // skips the comma if (IsOfType(p, ID_CLOSEPAR)) break; } @@ -3433,13 +3379,12 @@ CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) ////////////////////////////////////////////////////////////////////////////////////// -// compile un appel d'une mthode +// compile a method call CBotInstrMethode::CBotInstrMethode() { m_Parameters = NULL; m_MethodeIdent = 0; -// m_nThisIdent = 0; name = "CBotInstrMethode"; } @@ -3451,30 +3396,28 @@ CBotInstrMethode::~CBotInstrMethode() CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* var) { CBotInstrMethode* inst = new CBotInstrMethode(); - inst->SetToken(p); // token correspondant - -// inst->m_nThisIdent = CBotVar::NextUniqNum(); + inst->SetToken(p); // corresponding token - if ( NULL != var ) + if (NULL != var) { CBotToken* pp = p; p = p->GivNext(); - if ( p->GivType() == ID_OPENPAR ) + if (p->GivType() == ID_OPENPAR) { inst->m_NomMethod = pp->GivString(); - // compile la liste des paramtres + // compiles the list of parameters CBotVar* ppVars[1000]; inst->m_Parameters = CompileParams(p, pStack, ppVars); - if ( pStack->IsOk() ) + if (pStack->IsOk()) { - CBotClass* pClass = var->GivClass(); // pointeur la classe - inst->m_ClassName = pClass->GivName(); // le nom de la classe + CBotClass* pClass = var->GivClass(); // pointer to the class + inst->m_ClassName = pClass->GivName(); // name of the class CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, pStack, inst->m_MethodeIdent); - delete pStack->TokenStack(); // libres les paramtres encore sur la pile + delete pStack->TokenStack(); // release parameters on the stack inst->m_typRes = r; if (inst->m_typRes.GivType() > 20) @@ -3483,13 +3426,12 @@ CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* delete inst; return NULL; } - // met un rsultat sur la pile pour avoir quelque chose + // put the result on the stack to have something if (inst->m_typRes.GivType() > 0) { CBotVar* pResult = CBotVar::Create("", inst->m_typRes); if (inst->m_typRes.Eq(CBotTypClass)) { -// CBotClass* pClass = CBotClass::Find(inst->m_RetClassName); pResult->SetClass(inst->m_typRes.GivClass()); } pStack->SetVar(pResult); @@ -3500,39 +3442,36 @@ CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* return NULL; } } - pStack->SetError( 1234, p ); + pStack->SetError(1234, p); delete inst; return NULL; } -// excute l'appel de mthode +// execute the method call bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) { CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This -// if ( pile1 == EOX ) return true; + CBotStack* pile1 = pj->AddStack(this, true); // a place for the copy of This -// DEBUG( "CBotInstrMethode::ExecuteVar", 0, pj ); - - if ( pVar->GivPointer() == NULL ) + if (pVar->GivPointer() == NULL) { - pj->SetError( TX_NULLPT, prevToken ); + pj->SetError(TX_NULLPT, prevToken); } - if ( pile1->IfStep() ) return false; + if (pile1->IfStep()) return false; - CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir + CBotStack* pile2 = pile1->AddStack(); // for the next parameters if ( pile1->GivState() == 0) { CBotVar* pThis = CBotVar::Create(pVar); pThis->Copy(pVar); - // la valeur de This doit tre prise avant l'valuation des paramtres - // Test.Action( Test = Autre ); - // Action doit agir sur la valeur avant Test = Autre !! + // this value should be taken before the evaluation parameters + // Test.Action (Test = Other); + // action must act on the value before test = Other! + pThis->SetName("this"); -// pThis->SetUniqNum(m_nThisIdent); pThis->SetUniqNum(-2); pile1->AddVar(pThis); pile1->IncState(); @@ -3540,18 +3479,19 @@ bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* pre int i = 0; CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != NULL) while ( true) { - if ( pile2->GivState() == 0 ) + if (pile2->GivState() == 0) { - if (!p->Execute(pile2)) return false; // interrompu ici ? - if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres + if (!p->Execute(pile2)) return false; // interrupted here? + if (!pile2->SetState(1)) return false; // special mark to recognize parameters } - ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats + ppVars[i++] = pile2->GivVar(); // construct the list of pointers + pile2 = pile2->AddStack(); // space on the stack for the result p = p->GivNext(); if ( p == NULL) break; } @@ -3563,51 +3503,50 @@ bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* pre if (m_typRes.GivType() > 0) pResult = CBotVar::Create("", m_typRes); if (m_typRes.Eq(CBotTypClass)) { -// CBotClass* pClass = CBotClass::Find(m_RetClassName); pResult->SetClass(m_typRes.GivClass()); } CBotVar* pRes = pResult; if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu + pResult, pile2, GivToken())) return false; if (pRes != pResult) delete pRes; - pVar = NULL; // ne retourne pas une valeur par cela - return pj->Return(pile2); // libre toute la pile + pVar = NULL; // does not return value for this + return pj->Return(pile2); // release the entire stack } void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; CBotVar* ppVars[1000]; - CBotStack* pile1 = pile->RestoreStack(this); // une place pour la copie de This - if ( pile1 == NULL ) return; + CBotStack* pile1 = pile->RestoreStack(this); // place for the copy of This + if (pile1 == NULL) return; - CBotStack* pile2 = pile1->RestoreStack(); // et pour les paramtres venir - if ( pile2 == NULL ) return; + CBotStack* pile2 = pile1->RestoreStack(); // and for the parameters coming + if (pile2 == NULL) return; CBotVar* pThis = pile1->FindVar("this"); -// pThis->SetUniqNum(m_nThisIdent); pThis->SetUniqNum(-2); int i = 0; CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != NULL) while ( true) { - if ( pile2->GivState() == 0 ) + if (pile2->GivState() == 0) { - p->RestoreState(pile2, true); // interrompu ici ! + p->RestoreState(pile2, true); // interrupted here! return; } - ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs + ppVars[i++] = pile2->GivVar(); // construct the list of pointers pile2 = pile2->RestoreStack(); - if ( pile2 == NULL ) return; + if (pile2 == NULL) return; p = p->GivNext(); if ( p == NULL) break; @@ -3627,19 +3566,18 @@ void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) bool CBotInstrMethode::Execute(CBotStack* &pj) { CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // une place pour la copie de This -// if ( pile1 == EOX ) return true; + CBotStack* pile1 = pj->AddStack(this, true); // place for the copy of This - if ( pile1->IfStep() ) return false; + if (pile1->IfStep()) return false; - CBotStack* pile2 = pile1->AddStack(); // et pour les paramtres venir + CBotStack* pile2 = pile1->AddStack(); // and for the parameters coming if ( pile1->GivState() == 0) { CBotVar* pThis = pile1->CopyVar(m_token); - // la valeur de This doit tre prise avant l'valuation des paramtres - // Test.Action( Test = Autre ); - // Action doit agir sur la valeur avant Test = Autre !! + // this value should be taken before the evaluation parameters + // Test.Action (Test = Other); + // Action must act on the value before test = Other! pThis->SetName("this"); pile1->AddVar(pThis); pile1->IncState(); @@ -3647,18 +3585,18 @@ bool CBotInstrMethode::Execute(CBotStack* &pj) int i = 0; CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + if (p != NULL) while ( true) { - if ( pile2->GivState() == 0 ) + if (pile2->GivState() == 0) { - if (!p->Execute(pile2)) return false; // interrompu ici ? - if (!pile2->SetState(1)) return false; // marque spciale pour reconnare les paramtres + if (!p->Execute(pile2)) return false; // interrupted here? + if (!pile2->SetState(1)) return false; // special mark to recognize parameters } - ppVars[i++] = pile2->GivVar(); // construit la liste des pointeurs - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats + ppVars[i++] = pile2->GivVar(); // construct the list of pointers + pile2 = pile2->AddStack(); // space on the stack for the results p = p->GivNext(); if ( p == NULL) break; } @@ -3670,35 +3608,33 @@ bool CBotInstrMethode::Execute(CBotStack* &pj) if (m_typRes.GivType()>0) pResult = CBotVar::Create("", m_typRes); if (m_typRes.Eq(CBotTypClass)) { -// CBotClass* pClass = CBotClass::Find(m_RetClassName); pResult->SetClass(m_typRes.GivClass()); } CBotVar* pRes = pResult; if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu + pResult, pile2, GivToken())) return false; // interupted - // met la nouvelle valeur de this la place de l'ancienne variable + // set the new value of this in place of the old variable CBotVar* old = pile1->FindVar(m_token); old->Copy(pThis, false); if (pRes != pResult) delete pRes; - return pj->Return(pile2); // libre toute la pile + return pj->Return(pile2); // release the entire stack } /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// -// compile une instruction "new" +// compile an instruction "new" CBotNew::CBotNew() { name = "CBotNew"; m_Parameters = NULL; m_nMethodeIdent = 0; -// m_nThisIdent = 0; } CBotNew::~CBotNew() @@ -3708,9 +3644,9 @@ CBotNew::~CBotNew() CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) { CBotToken* pp = p; - if ( !IsOfType(p, ID_NEW) ) return NULL; + if (!IsOfType(p, ID_NEW)) return NULL; - // vrifie que le token est un nom de classe + // verifies that the token is a class name if (p->GivType() != TokenTypVar) return NULL; CBotClass* pClass = CBotClass::Find(p); @@ -3719,11 +3655,6 @@ CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) pStack->SetError(TX_BADNEW, p); return NULL; } -/* if ( !pClass->m_IsDef ) - { - pStack->SetError(TX_BADNEW, p); - return NULL; - }*/ CBotNew* inst = new CBotNew(); inst->SetToken(pp); @@ -3731,28 +3662,26 @@ CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) inst->m_vartoken = p; p = p->GivNext(); - // cre l'objet sur le "tas" - // avec un pointeur sur cet objet + // creates the object on the "job" + // with a pointer to the object CBotVar* pVar = CBotVar::Create("", pClass); -// inst->m_nThisIdent = CBotVar::NextUniqNum(); - // fait l'appel du crateur + // do the call of the creator CBotCStack* pStk = pStack->TokenStack(); { - // regarde s'il y a des paramtres + // check if there are parameters CBotVar* ppVars[1000]; inst->m_Parameters = CompileParams(p, pStk, ppVars); - if ( !pStk->IsOk() ) goto error; + if (!pStk->IsOk()) goto error; - // le constructeur existe-il ? -// CBotString noname; + // constructor exist? CBotTypResult r = pClass->CompileMethode(pClass->GivName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // libre le supplment de pile + delete pStk->TokenStack(); // release extra stack int typ = r.GivType(); - // s'il n'y a pas de constructeur, et pas de paramtres non plus, c'est ok - if ( typ == TX_UNDEFCALL && inst->m_Parameters == NULL ) typ = 0; - pVar->SetInit(true); // marque l'instance comme init + // if there is no constructor, and no parameters either, it's ok + if (typ == TX_UNDEFCALL && inst->m_Parameters == NULL) typ = 0; + pVar->SetInit(true); // mark the instance as init if (typ>20) { @@ -3760,14 +3689,14 @@ CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) goto error; } - // si le constructeur n'existe pas, mais qu'il y a des paramtres + // if the constructor does not exist, but there are parameters if (typ<0 && inst->m_Parameters != NULL) { pStk->SetError(TX_NOCONST, &inst->m_vartoken); goto error; } - // rend le pointeur l'objet sur la pile + // makes pointer to the object on the stack pStk->SetVar(pVar); return pStack->Return(inst, pStk); } @@ -3776,47 +3705,46 @@ error: return pStack->Return(NULL, pStk); } -// excute une instruction "new" +// executes instruction "new" bool CBotNew::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); //pile principale -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); //main stack - if ( pile->IfStep() ) return false; + if (pile->IfStep()) return false; - CBotStack* pile1 = pj->AddStack2(); //pile secondaire + CBotStack* pile1 = pj->AddStack2(); //secondary stack CBotVar* pThis = NULL; CBotToken* pt = &m_vartoken; CBotClass* pClass = CBotClass::Find(pt); - // cre la variable "this" de type pointeur l'objet + // create the variable "this" pointer type to the stack if ( pile->GivState()==0) { - // cre une instance de la classe demande - // et initialise le pointeur cet objet + // create an instance of the requested class + // and initialize the pointer to that object + pThis = CBotVar::Create("this", pClass); -// pThis->SetUniqNum( m_nThisIdent ) ; - pThis->SetUniqNum( -2 ) ; + pThis->SetUniqNum(-2) ; - pile1->SetVar(pThis); // la place sur la pile1 + pile1->SetVar(pThis); // place on stack1 pile->IncState(); } - // retrouve le pointeur this si on a t interrompu + // fetch the this pointer if it was interrupted if ( pThis == NULL) { - pThis = pile1->GivVar(); // retrouve le pointeur + pThis = pile1->GivVar(); // find the pointer } - // y a-t-il une assignation ou des paramtres (constructeur) + // is there an assignment or parameters (constructor) if ( pile->GivState()==1) - { - // value le constructeur de l'instance + { + // evaluates the constructor of the instance CBotVar* ppVars[1000]; CBotStack* pile2 = pile; @@ -3824,16 +3752,16 @@ bool CBotNew::Execute(CBotStack* &pj) int i = 0; CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time - if ( p != NULL) while ( true ) + if (p != NULL) while ( true) { - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats - if ( pile2->GivState() == 0 ) + pile2 = pile2->AddStack(); // space on the stack for the result + if (pile2->GivState() == 0) { - if (!p->Execute(pile2)) return false; // interrompu ici ? + if (!p->Execute(pile2)) return false; // interrupted here? pile2->SetState(1); } ppVars[i++] = pile2->GivVar(); @@ -3842,49 +3770,45 @@ bool CBotNew::Execute(CBotStack* &pj) } ppVars[i] = NULL; - // cre une variable pour le rsultat - CBotVar* pResult = NULL; // constructeurs toujours void + // create a variable for the result + CBotVar* pResult = NULL; // constructos still void if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu - - pThis->ConstructorSet(); // signale que le constructeur a t appel -// pile->Return(pile2); // libre un bout de pile + pResult, pile2, GivToken())) return false; // interrupt -// pile->IncState(); + pThis->ConstructorSet(); // indicates that the constructor has been called } - return pj->Return( pile1 ); // transmet en dessous + return pj->Return(pile1); // passes below } void CBotNew::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if (!bMain) return; - CBotStack* pile = pj->RestoreStack(this); //pile principale - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); //primary stack + if (pile == NULL) return; - CBotStack* pile1 = pj->AddStack2(); //pile secondaire + CBotStack* pile1 = pj->AddStack2(); //secondary stack CBotToken* pt = &m_vartoken; CBotClass* pClass = CBotClass::Find(pt); - // cre la variable "this" de type pointeur l'objet + // create the variable "this" pointer type to the object if ( pile->GivState()==0) { return; } - CBotVar* pThis = pile1->GivVar(); // retrouve le pointeur -// pThis->SetUniqNum( m_nThisIdent ); - pThis->SetUniqNum( -2 ); + CBotVar* pThis = pile1->GivVar(); // find the pointer + pThis->SetUniqNum(-2); - // y a-t-il une assignation ou des paramtres (constructeur) + // is ther an assignment or parameters (constructor) if ( pile->GivState()==1) - { - // value le constructeur de l'instance + { + // evaluates the constructor of the instance CBotVar* ppVars[1000]; CBotStack* pile2 = pile; @@ -3892,18 +3816,18 @@ void CBotNew::RestoreState(CBotStack* &pj, bool bMain) int i = 0; CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time - if ( p != NULL) while ( true ) + if (p != NULL) while ( true) { - pile2 = pile2->RestoreStack(); // de la place sur la pile pour les rsultats - if ( pile2 == NULL ) return; + pile2 = pile2->RestoreStack(); // space on the stack for the result + if (pile2 == NULL) return; - if ( pile2->GivState() == 0 ) + if (pile2->GivState() == 0) { - p->RestoreState(pile2, bMain); // interrompu ici ! + p->RestoreState(pile2, bMain); // interrupt here! return; } ppVars[i++] = pile2->GivVar(); @@ -3913,28 +3837,28 @@ void CBotNew::RestoreState(CBotStack* &pj, bool bMain) ppVars[i] = NULL; pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GivString(), pThis, - ppVars, pile2) ; // interrompu ici ! + ppVars, pile2) ; // interrupt here! } } ///////////////////////////////////////////////////////////// -// regarde si deux rsultats sont compatibles pour faire une opration +// check if two results are consistent to make an operation -bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) +bool TypeCompatible(CBotTypResult& type1, CBotTypResult& type2, int op) { int t1 = type1.GivType(); int t2 = type2.GivType(); int max = (t1 > t2) ? t1 : t2; - if ( max == 99 ) return false; // un rsultat est void ? + if (max == 99) return false; // result is void? - // cas particulier pour les concatnation de chanes + // special case for strin concatenation if (op == ID_ADD && max >= CBotTypString) return true; if (op == ID_ASSADD && max >= CBotTypString) return true; if (op == ID_ASS && t1 == CBotTypString) return true; - if ( max >= CBotTypBoolean ) + if (max >= CBotTypBoolean) { if ( (op == ID_EQ || op == ID_NE) && (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; @@ -3954,8 +3878,9 @@ bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) CBotClass* c2 = type2.GivClass(); return c1->IsChildOf(c2) || c2->IsChildOf(c1); - // accepte le caste l'envers, - // l'opration sera refuse l'excution si le pointeur n'est pas compatible + // accept the case in reverse + // the transaction will be denied at runtime if the pointer is not + // compatible } return true; @@ -3966,28 +3891,28 @@ bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op ) return true; } -// regarde si deux variables sont compatible pour un passage de paramtre +// check if two variables are compatible for parameter passing -bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) +bool TypesCompatibles(const CBotTypResult& type1, const CBotTypResult& type2) { int t1 = type1.GivType(); int t2 = type2.GivType(); - if ( t1 == CBotTypIntrinsic ) t1 = CBotTypClass; - if ( t2 == CBotTypIntrinsic ) t2 = CBotTypClass; + if (t1 == CBotTypIntrinsic) t1 = CBotTypClass; + if (t2 == CBotTypIntrinsic) t2 = CBotTypClass; int max = (t1 > t2) ? t1 : t2; - if ( max == 99 ) return false; // un rsultat est void ? + if (max == 99) return false; // result is void? - if ( max >= CBotTypBoolean ) + if (max >= CBotTypBoolean) { - if ( t2 != t1 ) return false; + if (t2 != t1) return false; - if ( max == CBotTypArrayPointer ) + if (max == CBotTypArrayPointer) return TypesCompatibles(type1.GivTypElem(), type2.GivTypElem()); - if ( max == CBotTypClass || max == CBotTypPointer ) + if (max == CBotTypClass || max == CBotTypPointer) return type1.GivClass() == type2.GivClass() ; return true ; @@ -3997,10 +3922,11 @@ bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ) ///////////////////////////////////////////////////////////////////////////////////// -// Gestion des fichiers +// file management + +// necessary because it is not possible to do the fopen in the main program +// fwrite and fread in a dll or using the FILE * returned. -// ncessaire car il n'est pas possible de faire le fopen dans le programme principal -// et les fwrite ou fread dans une dll en utilisant le FILE* rendu. FILE* fOpen(const char* name, const char* mode) { @@ -4035,34 +3961,3 @@ size_t fRead(void *buffer, size_t length, FILE* filehandle) //////////////////////////////////////// - -#if false - -CBotString num(int n) -{ - CBotString s; - if ( n<0 ) {n = -n; s += "-";} - if ( n > 9 ) - { - s += num(n/10); - } - s += '0' + n%10; - return s; -} - -extern void DEBUG( const char* text, int val, CBotStack* pile ) -{ - CBotProgram* p = pile->GivBotCall(true); - if ( !p->m_bDebugDD ) return; - - FILE* pf = fopen("CbotDebug.txt", "a"); - - fputs( text, pf ); - - CBotString v = " " + num(val) + "\n"; - fputs( v, pf ); - - fclose( pf); -} - -#endif diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 6f18be8..d618131 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -13,50 +13,50 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/.//////////////////////////////////////////////////////////////////// -// interprteur pour le language CBot du jeu COLOBOT +/** + * \file CBot.h + * \brief Interpreter of the language CBot for COLOBOT game + */ -// dernire rvision : 03/10/2002 DD - -#define EXTENDS true #include "resource.h" -#include "CBotDll.h" // dfinitions publiques -#include "CBotToken.h" // gestion des tokens +#include "CBotDll.h" // public definitions +#include "CBotToken.h" // token management -#define STACKRUN true // reprise de l'excution direct sur une routine suspendue -#define STACKMEM true // prrserve la mmoire pour la pile d'excution -#define MAXSTACK 990 // taille du stack rserv +#define STACKRUN true /// \def return execution directly on a suspended routine +#define STACKMEM true /// \def preserve memory for the execution stack +#define MAXSTACK 990 /// \def stack size reserved -#define EOX (CBotStack*)-1 // marqueur condition spciale +#define EOX (CBotStack*)-1 /// \def tag special condition // fix for MSVC instruction __asm int 3 (setting a trap) #ifdef __MINGW32__ -#define ASM_TRAP() asm("int $3"); +#define ASM_TRAP() asm("int $3"); #else -#define ASM_TRAP() __asm int 3; +#define ASM_TRAP() __asm int 3; #endif ///////////////////////////////////////////////////////////////////// -// rsum des classes utilises, dfinies ci-aprs - -class CBotCompExpr; // une expression telle que - // () <= () -class CBotAddExpr; // une expression telle que - // () + () -class CBotParExpr; // un lment de base ou entre parenthse - // Toto.truc - // 12.5 - // "chaine" - // ( expression ) -class CBotExprVar; // un nom de variable tel que - // Toto - // chose.truc.machin -class CBotWhile; // while (...) {...}; -class CBotIf; // if (...) {...} else {...} -class CBotDefParam; // liste de paramtres d'une fonction -class CBotRepeat; // repeat (nb) {...} +// forward declaration + +class CBotCompExpr; // an expression like + // () <= () +class CBotAddExpr; // an expression like + // () + () +class CBotParExpr; // basic type or instruction in parenthesis + // Toto.truc + // 12.5 + // "string" + // ( expression ) +class CBotExprVar; // a variable name as + // Toto + // chose.truc.machin +class CBotWhile; // while (...) {...}; +class CBotIf; // if (...) {...} else {...} +class CBotDefParam; // paramerer list of a function +class CBotRepeat; // repeat (nb) {...} @@ -65,139 +65,232 @@ class CBotRepeat; // repeat (nb) {...} //////////////////////////////////////////////////////////////////////// // actually, externally, the only thing he can do -// this is to create an instance of a stack +// is to create an instance of a stack // to use for routine CBotProgram :: Execute (CBotStack) +/**\class CBotStack + * \brief Management of the execution stack. + * \brief Actually the only thing it can do is to create an instance of a stack + * \brief to use for routine CBotProgram :: Execute(CBotStack)*/ class CBotStack { -private: - CBotStack* m_next; - CBotStack* m_next2; - CBotStack* m_prev; - friend class CBotInstArray; - -#ifdef _DEBUG - int m_index; +public: +#if STACKMEM + /** + * \brief FirstStack Allocate first stack + * \return pointer to created stack + */ + static CBotStack * FirstStack(); + + /** \brief Delete Remove current stack */ + void Delete(); #endif - int m_state; - int m_step; - static int m_error; - static int m_start; - static int m_end; - static - CBotVar* m_retvar; // rsultat d'un return - - CBotVar* m_var; // rsultat des oprations - CBotVar* m_listVar; // les variables dclares ce niveau - - bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) - bool m_bOver; // limites de la pile ? -// bool m_bDontDelete; // spcial, ne pas dtruire les variables au delete - CBotProgram* m_prog; // les fonctions dfinies par user - - static - int m_initimer; - static - int m_timer; - static - CBotString m_labelBreak; - static - void* m_pUser; - - CBotInstr* m_instr; // l'instruction correspondante - bool m_bFunc; // une entre d'une fonction ? - CBotCall* m_call; // point de reprise dans un call extern - friend class CBotTry; -public: -#if STACKMEM - static - CBotStack* FirstStack(); - void Delete(); + /** + * \brief CBotStack Constructor of the stack + * \param ppapa Not used. + */ + CBotStack(CBotStack* ppapa); + + + /** \brief ~CBotStack Destructor */ + ~CBotStack(); + + /** + * \brief StackOver Check if end of stack is reached + * \return true if end of stack + */ + bool StackOver(); + + /** + * \brief GivError Get error number of the stack + * \param [out] start beginning of the stack + * \param [out] end end of stack + * \return error number + */ + int GivError(int& start, int& end); + + /** + * \brief GivError Get error number + * \return eror number + */ + int GivError();// rend le numro d'erreur retourn + + /** + * \brief Reset Reset error at and set user + * \param [in] pUser User of stack + */ + void Reset(void* pUser); + + /** + * \brief SetType Determines the type. + * \param type Type of instruction on the stack. + */ + void SetType(CBotTypResult& type); + + /** + * \brief GivType Get the type of value on the stack. + * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). + * \return Type number. + */ + int GivType(int mode = 0); + + /** + * \brief Gives the type of complete value on the stack. + * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). + * \return Type of an element. + */ + CBotTypResult GivTypResult(int mode = 0); + + /** + * \brief Adds a local variable. + * \param [in] p Variable to be added. + */ + void AddVar(CBotVar* p); + + /** + * \brief Fetch a variable by its token. + * \brief This may be a composite variable + * \param [in] pToken Token upon which search is performed + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Fetch a variable by its token. + * \brief This may be a composite variable + * \param [in] pToken Token upon which search is performed + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(CBotToken& Token, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Fetch variable by its name + * \param [in] name Name of variable to find + * \return Found variable + */ + CBotVar* FindVar(const char* name); + + /** + * \brief Fetch a variable on the stack according to its identification number + * \brief This is faster than comparing names + * \param [in] ident Identifier of a variable + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(long ident, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Find variable by its token and returns a copy of it. + * \param Token Token upon which search is performed + * \param bUpdate Not used. + * \return Found variable, NULL if not found + */ + CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); + + + CBotStack* AddStack(CBotInstr* instr = NULL, bool bBlock = false); // tend le stack + CBotStack* AddStackEOX(CBotCall* instr = NULL, bool bBlock = false); // tend le stack + CBotStack* RestoreStack(CBotInstr* instr = NULL); + CBotStack* RestoreStackEOX(CBotCall* instr = NULL); + + CBotStack* AddStack2(bool bBlock = false); // tend le stack + bool Return(CBotStack* pFils); // transmet le rsultat au dessus + bool ReturnKeep(CBotStack* pFils); // transmet le rsultat sans rduire la pile + bool BreakReturn(CBotStack* pfils, const char* name = NULL); + // en cas de break ventuel + bool IfContinue(int state, const char* name); + // ou de "continue" + + bool IsOk(); + + bool SetState(int n, int lim = -10); // slectionne un tat + int GivState(); // dans quel tat j're ? + bool IncState(int lim = -10); // passe l'tat suivant + bool IfStep(); // faire du pas pas ? + bool Execute(); + + void SetVar( CBotVar* var ); + void SetCopyVar( CBotVar* var ); + CBotVar* GivVar(); + CBotVar* GivCopyVar(); + CBotVar* GivPtVar(); + bool GivRetVar(bool bRet); + long GivVal(); + + void SetStartError(int pos); + void SetError(int n, CBotToken* token = NULL); + void SetPosError(CBotToken* token); + void ResetError(int n, int start, int end); + void SetBreak(int val, const char* name); + + void SetBotCall(CBotProgram* p); + CBotProgram* GivBotCall(bool bFirst = false); + void* GivPUser(); + bool GivBlock(); + + + bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); + void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); + + bool SaveState(FILE* pf); + bool RestoreState(FILE* pf, CBotStack* &pStack); + + static + void SetTimer(int n); + + void GetRunPos(const char* &FunctionName, int &start, int &end); + CBotVar* GivStackVars(const char* &FunctionName, int level); + + int m_temp; + +private: + CBotStack* m_next; + CBotStack* m_next2; + CBotStack* m_prev; + friend class CBotInstArray; + +#ifdef _DEBUG + int m_index; #endif - CBotStack(CBotStack* ppapa); - ~CBotStack(); - bool StackOver(); - - int GivError(int& start, int& end); - int GivError(); // rend le numro d'erreur retourn - - void Reset(void* pUser); // plus d'erreur, timer au dbut - void SetType(CBotTypResult& type); // dtermine le type - int GivType(int mode = 0); // donne le type de valeur sur le stack - CBotTypResult GivTypResult(int mode = 0); // donne le type complet de valeur sur le stack - -// void AddVar(CBotVar* p, bool bDontDelete=false); // ajoute une variable locale - void AddVar(CBotVar* p); // ajoute une variable locale -// void RestoreVar(CBotVar* pVar); - - CBotVar* FindVar(CBotToken* &p, bool bUpdate = false, - bool bModif = false); // trouve une variable - CBotVar* FindVar(CBotToken& Token, bool bUpdate = false, - bool bModif = false); - CBotVar* FindVar(const char* name); - CBotVar* FindVar(long ident, bool bUpdate = false, - bool bModif = false); - - CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); // trouve et rend une copie - - - CBotStack* AddStack(CBotInstr* instr = NULL, bool bBlock = false); // tend le stack - CBotStack* AddStackEOX(CBotCall* instr = NULL, bool bBlock = false); // tend le stack - CBotStack* RestoreStack(CBotInstr* instr = NULL); - CBotStack* RestoreStackEOX(CBotCall* instr = NULL); - - CBotStack* AddStack2(bool bBlock = false); // tend le stack - bool Return(CBotStack* pFils); // transmet le rsultat au dessus - bool ReturnKeep(CBotStack* pFils); // transmet le rsultat sans rduire la pile - bool BreakReturn(CBotStack* pfils, const char* name = NULL); - // en cas de break ventuel - bool IfContinue(int state, const char* name); - // ou de "continue" - - bool IsOk(); - - bool SetState(int n, int lim = -10); // slectionne un tat - int GivState(); // dans quel tat j're ? - bool IncState(int lim = -10); // passe l'tat suivant - bool IfStep(); // faire du pas pas ? - bool Execute(); - - void SetVar( CBotVar* var ); - void SetCopyVar( CBotVar* var ); - CBotVar* GivVar(); - CBotVar* GivCopyVar(); - CBotVar* GivPtVar(); - bool GivRetVar(bool bRet); - long GivVal(); - - void SetStartError(int pos); - void SetError(int n, CBotToken* token = NULL); - void SetPosError(CBotToken* token); - void ResetError(int n, int start, int end); - void SetBreak(int val, const char* name); - - void SetBotCall(CBotProgram* p); - CBotProgram* GivBotCall(bool bFirst = false); - void* GivPUser(); - bool GivBlock(); - - -// bool ExecuteCall(CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); - bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); - void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); - - bool SaveState(FILE* pf); - bool RestoreState(FILE* pf, CBotStack* &pStack); - - static - void SetTimer(int n); - - void GetRunPos(const char* &FunctionName, int &start, int &end); - CBotVar* GivStackVars(const char* &FunctionName, int level); - - int m_temp; + int m_state; + int m_step; + static int m_error; + static int m_start; + static int m_end; + static + CBotVar* m_retvar; // rsultat d'un return + + CBotVar* m_var; // rsultat des oprations + CBotVar* m_listVar; // les variables dclares ce niveau + + bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) + bool m_bOver; // limites de la pile ? +// bool m_bDontDelete; // spcial, ne pas dtruire les variables au delete + CBotProgram* m_prog; // les fonctions dfinies par user + + static + int m_initimer; + static + int m_timer; + static + CBotString m_labelBreak; + static + void* m_pUser; + + CBotInstr* m_instr; // l'instruction correspondante + bool m_bFunc; // une entre d'une fonction ? + CBotCall* m_call; // point de reprise dans un call extern + friend class CBotTry; }; // les routines inline doivent tre dclares dans le fichier .h @@ -214,7 +307,7 @@ inline int CBotStack::GivState() inline int CBotStack::GivError() { - return m_error; + return m_error; } //////////////////////////////////////////////////////////////////////// @@ -225,70 +318,70 @@ inline int CBotStack::GivError() class CBotCStack { private: - CBotCStack* m_next; - CBotCStack* m_prev; + CBotCStack* m_next; + CBotCStack* m_prev; - static - int m_error; - static - int m_end; - int m_start; + static + int m_error; + static + int m_end; + int m_start; - CBotVar* m_var; // rsultat des oprations + CBotVar* m_var; // rsultat des oprations - bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) - CBotVar* m_listVar; + bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) + CBotVar* m_listVar; - static - CBotProgram* m_prog; // liste des fonctions compiles - static - CBotTypResult m_retTyp; -// static -// CBotToken* m_retClass; + static + CBotProgram* m_prog; // liste des fonctions compiles + static + CBotTypResult m_retTyp; +// static +// CBotToken* m_retClass; public: - CBotCStack(CBotCStack* ppapa); - ~CBotCStack(); - - bool IsOk(); - int GivError(); - int GivError(int& start, int& end); - // rend le numro d'erreur retourn - - void SetType(CBotTypResult& type);// dtermine le type - CBotTypResult GivTypResult(int mode = 0); // donne le type de valeur sur le stack - int GivType(int mode = 0); // donne le type de valeur sur le stack - CBotClass* GivClass(); // donne la classe de la valeur sur le stack - - void AddVar(CBotVar* p); // ajoute une variable locale - CBotVar* FindVar(CBotToken* &p); // trouve une variable - CBotVar* FindVar(CBotToken& Token); - bool CheckVarLocal(CBotToken* &pToken); - CBotVar* CopyVar(CBotToken& Token); // trouve et rend une copie - - CBotCStack* TokenStack(CBotToken* pToken = NULL, bool bBlock = false); - CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); // transmet le rsultat au dessus - CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); // transmet le rsultat au dessus - - void SetVar( CBotVar* var ); - void SetCopyVar( CBotVar* var ); - CBotVar* GivVar(); - - void SetStartError(int pos); - void SetError(int n, int pos); - void SetError(int n, CBotToken* p); - void ResetError(int n, int start, int end); - - void SetRetType(CBotTypResult& type); - CBotTypResult GivRetType(); - -// void SetBotCall(CBotFunction* &pFunc); - void SetBotCall(CBotProgram* p); - CBotProgram* GivBotCall(); - CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); - bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); - - bool NextToken(CBotToken* &p); + CBotCStack(CBotCStack* ppapa); + ~CBotCStack(); + + bool IsOk(); + int GivError(); + int GivError(int& start, int& end); + // rend le numro d'erreur retourn + + void SetType(CBotTypResult& type);// dtermine le type + CBotTypResult GivTypResult(int mode = 0); // donne le type de valeur sur le stack + int GivType(int mode = 0); // donne le type de valeur sur le stack + CBotClass* GivClass(); // donne la classe de la valeur sur le stack + + void AddVar(CBotVar* p); // ajoute une variable locale + CBotVar* FindVar(CBotToken* &p); // trouve une variable + CBotVar* FindVar(CBotToken& Token); + bool CheckVarLocal(CBotToken* &pToken); + CBotVar* CopyVar(CBotToken& Token); // trouve et rend une copie + + CBotCStack* TokenStack(CBotToken* pToken = NULL, bool bBlock = false); + CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); // transmet le rsultat au dessus + CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); // transmet le rsultat au dessus + + void SetVar( CBotVar* var ); + void SetCopyVar( CBotVar* var ); + CBotVar* GivVar(); + + void SetStartError(int pos); + void SetError(int n, int pos); + void SetError(int n, CBotToken* p); + void ResetError(int n, int start, int end); + + void SetRetType(CBotTypResult& type); + CBotTypResult GivRetType(); + +// void SetBotCall(CBotFunction* &pFunc); + void SetBotCall(CBotProgram* p); + CBotProgram* GivBotCall(); + CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + + bool NextToken(CBotToken* &p); }; @@ -300,105 +393,104 @@ extern bool SaveVar(FILE* pf, CBotVar* pVar); class CBotInstr { private: - static - CBotStringArray - m_labelLvl; + static + CBotStringArray + m_labelLvl; protected: - CBotToken m_token; // keeps the token - CBotString name; // debug - CBotInstr* m_next; // linked command - CBotInstr* m_next2b; // second list definition chain - CBotInstr* m_next3; // third list for indices and fields - CBotInstr* m_next3b; // necessary for reporting tables + CBotToken m_token; // keeps the token + CBotString name; // debug + CBotInstr* m_next; // linked command + CBotInstr* m_next2b; // second list definition chain + CBotInstr* m_next3; // third list for indices and fields + CBotInstr* m_next3b; // necessary for reporting tables /* - for example, the following program - int x[]; x[1] = 4; - int y[x[1]][10], z; + for example, the following program + int x[]; x[1] = 4; + int y[x[1]][10], z; is generated - CBotInstrArray - m_next3b-> CBotEmpty - m_next-> - CBotExpression .... - m_next-> - CBotInstrArray - m_next3b-> CBotExpression ('x') ( m_next3-> CBotIndexExpr ('1') ) - m_next3b-> CBotExpression ('10') - m_next2-> 'z' - m_next->... + CBotInstrArray + m_next3b-> CBotEmpty + m_next-> + CBotExpression .... + m_next-> + CBotInstrArray + m_next3b-> CBotExpression ('x') ( m_next3-> CBotIndexExpr ('1') ) + m_next3b-> CBotExpression ('10') + m_next2-> 'z' + m_next->... */ - static - int m_LoopLvl; - friend class CBotClassInst; - friend class CBotInt; - friend class CBotListArray; + static + int m_LoopLvl; + friend class CBotClassInst; + friend class CBotInt; + friend class CBotListArray; public: - CBotInstr(); - virtual - ~CBotInstr(); - -// DllExport//debug - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - static - CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first = true); - - virtual - bool Execute(CBotStack* &pj); - virtual - bool Execute(CBotStack* &pj, CBotVar* pVar); - virtual - void RestoreState(CBotStack* &pj, bool bMain); - - virtual - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - virtual - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - virtual - void RestoreStateVar(CBotStack* &pile, bool bMain); - - virtual - bool CompCase(CBotStack* &pj, int val); - - void SetToken(CBotToken* p); - int GivTokenType(); - CBotToken* GivToken(); - - void AddNext(CBotInstr* n); - CBotInstr* GivNext(); - void AddNext3(CBotInstr* n); - CBotInstr* GivNext3(); - void AddNext3b(CBotInstr* n); - CBotInstr* GivNext3b(); - - static - void IncLvl(CBotString& label); - static - void IncLvl(); - static - void DecLvl(); - static - bool ChkLvl(const CBotString& label, int type); - - bool IsOfClass(CBotString name); + CBotInstr(); + virtual + ~CBotInstr(); + + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first = true); + + virtual + bool Execute(CBotStack* &pj); + virtual + bool Execute(CBotStack* &pj, CBotVar* pVar); + virtual + void RestoreState(CBotStack* &pj, bool bMain); + + virtual + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + virtual + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + virtual + void RestoreStateVar(CBotStack* &pile, bool bMain); + + virtual + bool CompCase(CBotStack* &pj, int val); + + void SetToken(CBotToken* p); + int GivTokenType(); + CBotToken* GivToken(); + + void AddNext(CBotInstr* n); + CBotInstr* GivNext(); + void AddNext3(CBotInstr* n); + CBotInstr* GivNext3(); + void AddNext3b(CBotInstr* n); + CBotInstr* GivNext3b(); + + static + void IncLvl(CBotString& label); + static + void IncLvl(); + static + void DecLvl(); + static + bool ChkLvl(const CBotString& label, int type); + + bool IsOfClass(CBotString name); }; class CBotWhile : public CBotInstr { private: - CBotInstr* m_Condition; // la condition - CBotInstr* m_Block; // les instructions - CBotString m_label; // une tiquette s'il y a + CBotInstr* m_Condition; // la condition + CBotInstr* m_Block; // les instructions + CBotString m_label; // une tiquette s'il y a public: - CBotWhile(); - ~CBotWhile(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotWhile(); + ~CBotWhile(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotRepeat : public CBotInstr @@ -430,144 +522,144 @@ public: class CBotDo : public CBotInstr { private: - CBotInstr* m_Block; // les instructions - CBotInstr* m_Condition; // la condition - CBotString m_label; // une tiquette s'il y a + CBotInstr* m_Block; // les instructions + CBotInstr* m_Condition; // la condition + CBotString m_label; // une tiquette s'il y a public: - CBotDo(); - ~CBotDo(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotDo(); + ~CBotDo(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotFor : public CBotInstr { private: - CBotInstr* m_Init; // intruction initiale - CBotInstr* m_Test; // la condition de test - CBotInstr* m_Incr; // instruction pour l'incrment - CBotInstr* m_Block; // les instructions - CBotString m_label; // une tiquette s'il y a + CBotInstr* m_Init; // intruction initiale + CBotInstr* m_Test; // la condition de test + CBotInstr* m_Incr; // instruction pour l'incrment + CBotInstr* m_Block; // les instructions + CBotString m_label; // une tiquette s'il y a public: - CBotFor(); - ~CBotFor(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotFor(); + ~CBotFor(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotBreak : public CBotInstr { private: - CBotString m_label; // une tiquette s'il y a + CBotString m_label; // une tiquette s'il y a public: - CBotBreak(); - ~CBotBreak(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotBreak(); + ~CBotBreak(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotReturn : public CBotInstr { private: - CBotInstr* m_Instr; // le paramtre retourner + CBotInstr* m_Instr; // le paramtre retourner public: - CBotReturn(); - ~CBotReturn(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotReturn(); + ~CBotReturn(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotSwitch : public CBotInstr { private: - CBotInstr* m_Value; // value chercher - CBotInstr* m_Block; // les instructions + CBotInstr* m_Value; // value chercher + CBotInstr* m_Block; // les instructions public: - CBotSwitch(); - ~CBotSwitch(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotSwitch(); + ~CBotSwitch(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotCase : public CBotInstr { private: - CBotInstr* m_Value; // valeur comparer + CBotInstr* m_Value; // valeur comparer public: - CBotCase(); - ~CBotCase(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - bool CompCase(CBotStack* &pj, int val); + CBotCase(); + ~CBotCase(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + bool CompCase(CBotStack* &pj, int val); }; class CBotCatch : public CBotInstr { private: - CBotInstr* m_Block; // les instructions - CBotInstr* m_Cond; // la condition - CBotCatch* m_next; // le catch suivant - friend class CBotTry; + CBotInstr* m_Block; // les instructions + CBotInstr* m_Cond; // la condition + CBotCatch* m_next; // le catch suivant + friend class CBotTry; public: - CBotCatch(); - ~CBotCatch(); - static - CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); - bool TestCatch(CBotStack* &pj, int val); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - void RestoreCondState(CBotStack* &pj, bool bMain); + CBotCatch(); + ~CBotCatch(); + static + CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); + bool TestCatch(CBotStack* &pj, int val); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + void RestoreCondState(CBotStack* &pj, bool bMain); }; class CBotTry : public CBotInstr { private: - CBotInstr* m_Block; // les instructions - CBotCatch* m_ListCatch; // les catches - CBotInstr* m_FinalInst; // instruction finale + CBotInstr* m_Block; // les instructions + CBotCatch* m_ListCatch; // les catches + CBotInstr* m_FinalInst; // instruction finale public: - CBotTry(); - ~CBotTry(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotTry(); + ~CBotTry(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotThrow : public CBotInstr { private: - CBotInstr* m_Value; // la valeur envoyer + CBotInstr* m_Value; // la valeur envoyer public: - CBotThrow(); - ~CBotThrow(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotThrow(); + ~CBotThrow(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -576,28 +668,28 @@ class CBotStartDebugDD : public CBotInstr private: public: - CBotStartDebugDD(); - ~CBotStartDebugDD(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); + CBotStartDebugDD(); + ~CBotStartDebugDD(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); }; class CBotIf : public CBotInstr { private: - CBotInstr* m_Condition; // la condition - CBotInstr* m_Block; // les instructions - CBotInstr* m_BlockElse; // les instructions + CBotInstr* m_Condition; // la condition + CBotInstr* m_Block; // les instructions + CBotInstr* m_BlockElse; // les instructions public: - CBotIf(); - ~CBotIf(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotIf(); + ~CBotIf(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -606,17 +698,17 @@ public: class CBotInt : public CBotInstr { private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a -/// CBotInstr* m_next; // plusieurs dfinitions enchanes + CBotInstr* m_var; // la variable initialiser + CBotInstr* m_expr; // la valeur mettre, s'il y a +/// CBotInstr* m_next; // plusieurs dfinitions enchanes public: - CBotInt(); - ~CBotInt(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotInt(); + ~CBotInt(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un tableau @@ -624,19 +716,19 @@ public: class CBotInstArray : public CBotInstr { private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_listass; // liste d'assignations pour le tableau - CBotTypResult - m_typevar; // type d'lments -// CBotString m_ClassName; + CBotInstr* m_var; // la variable initialiser + CBotInstr* m_listass; // liste d'assignations pour le tableau + CBotTypResult + m_typevar; // type d'lments +// CBotString m_ClassName; public: - CBotInstArray(); - ~CBotInstArray(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotInstArray(); + ~CBotInstArray(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -646,22 +738,22 @@ public: class CBotListArray : public CBotInstr { private: - CBotInstr* m_expr; // expression pour un lment - // les autres sont chans avec CBotInstr::m_next3; + CBotInstr* m_expr; // expression pour un lment + // les autres sont chans avec CBotInstr::m_next3; public: - CBotListArray(); - ~CBotListArray(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - bool Execute(CBotStack* &pj, CBotVar* pVar); - void RestoreState(CBotStack* &pj, bool bMain); + CBotListArray(); + ~CBotListArray(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); + bool Execute(CBotStack* &pj, CBotVar* pVar); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotEmpty : public CBotInstr { - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un boolen @@ -669,16 +761,16 @@ class CBotEmpty : public CBotInstr class CBotBoolean : public CBotInstr { private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a + CBotInstr* m_var; // la variable initialiser + CBotInstr* m_expr; // la valeur mettre, s'il y a public: - CBotBoolean(); - ~CBotBoolean(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotBoolean(); + ~CBotBoolean(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -687,16 +779,16 @@ public: class CBotFloat : public CBotInstr { private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a + CBotInstr* m_var; // la variable initialiser + CBotInstr* m_expr; // la valeur mettre, s'il y a public: - CBotFloat(); - ~CBotFloat(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotFloat(); + ~CBotFloat(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un elment string @@ -704,16 +796,16 @@ public: class CBotIString : public CBotInstr { private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a + CBotInstr* m_var; // la variable initialiser + CBotInstr* m_expr; // la valeur mettre, s'il y a public: - CBotIString(); - ~CBotIString(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotIString(); + ~CBotIString(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // dfinition d'un elment dans une classe quelconque @@ -721,20 +813,20 @@ public: class CBotClassInst : public CBotInstr { private: - CBotInstr* m_var; // la variable initialiser - CBotClass* m_pClass; // rfrence la classe - CBotInstr* m_Parameters; // les paramtres valuer pour le constructeur - CBotInstr* m_expr; // la valeur mettre, s'il y a - bool m_hasParams; // il y a des paramtres ? - long m_nMethodeIdent; + CBotInstr* m_var; // la variable initialiser + CBotClass* m_pClass; // rfrence la classe + CBotInstr* m_Parameters; // les paramtres valuer pour le constructeur + CBotInstr* m_expr; // la valeur mettre, s'il y a + bool m_hasParams; // il y a des paramtres ? + long m_nMethodeIdent; public: - CBotClassInst(); - ~CBotClassInst(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = NULL); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotClassInst(); + ~CBotClassInst(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = NULL); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotCondition : public CBotInstr @@ -743,8 +835,8 @@ private: public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); }; @@ -754,18 +846,18 @@ public: class CBotLeftExpr : public CBotInstr { private: - long m_nIdent; + long m_nIdent; public: - CBotLeftExpr(); - ~CBotLeftExpr(); - static - CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack, CBotStack* array); - - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); - void RestoreStateVar(CBotStack* &pile, bool bMain); + CBotLeftExpr(); + ~CBotLeftExpr(); + static + CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack, CBotStack* array); + + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + void RestoreStateVar(CBotStack* &pile, bool bMain); }; @@ -774,18 +866,18 @@ public: class CBotFieldExpr : public CBotInstr { private: - friend class CBotExpression; - int m_nIdent; + friend class CBotExpression; + int m_nIdent; public: - CBotFieldExpr(); - ~CBotFieldExpr(); - void SetUniqNum(int num); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - void RestoreStateVar(CBotStack* &pj, bool bMain); + CBotFieldExpr(); + ~CBotFieldExpr(); + void SetUniqNum(int num); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; // gestion des index dans les tableaux @@ -793,18 +885,18 @@ public: class CBotIndexExpr : public CBotInstr { private: - CBotInstr* m_expr; // expression pour le calcul de l'index - friend class CBotLeftExpr; - friend class CBotExprVar; + CBotInstr* m_expr; // expression pour le calcul de l'index + friend class CBotLeftExpr; + friend class CBotExprVar; public: - CBotIndexExpr(); - ~CBotIndexExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - void RestoreStateVar(CBotStack* &pj, bool bMain); + CBotIndexExpr(); + ~CBotIndexExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; // une expression du genre @@ -814,47 +906,47 @@ public: class CBotExpression : public CBotInstr { private: - CBotLeftExpr* m_leftop; // lment de gauche - CBotInstr* m_rightop; // lment de droite + CBotLeftExpr* m_leftop; // lment de gauche + CBotInstr* m_rightop; // lment de droite public: - CBotExpression(); - ~CBotExpression(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); + CBotExpression(); + ~CBotExpression(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotListExpression : public CBotInstr { private: - CBotInstr* m_Expr; // la 1re expression valuer + CBotInstr* m_Expr; // la 1re expression valuer public: - CBotListExpression(); - ~CBotListExpression(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); + CBotListExpression(); + ~CBotListExpression(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotLogicExpr : public CBotInstr { private: - CBotInstr* m_condition; // test valuer - CBotInstr* m_op1; // lment de gauche - CBotInstr* m_op2; // lment de droite - friend class CBotTwoOpExpr; + CBotInstr* m_condition; // test valuer + CBotInstr* m_op1; // lment de gauche + CBotInstr* m_op2; // lment de droite + friend class CBotTwoOpExpr; public: - CBotLogicExpr(); - ~CBotLogicExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); + CBotLogicExpr(); + ~CBotLogicExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -864,8 +956,8 @@ class CBotBoolExpr : public CBotInstr private: public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); }; @@ -878,22 +970,22 @@ class CBotParExpr : public CBotInstr private: public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); }; // expression unaire class CBotExprUnaire : public CBotInstr { private: - CBotInstr* m_Expr; // l'expression valuer + CBotInstr* m_Expr; // l'expression valuer public: - CBotExprUnaire(); - ~CBotExprUnaire(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); + CBotExprUnaire(); + ~CBotExprUnaire(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; // toutes les oprations 2 oprandes @@ -901,15 +993,15 @@ public: class CBotTwoOpExpr : public CBotInstr { private: - CBotInstr* m_leftop; // lment de gauche - CBotInstr* m_rightop; // lment de droite + CBotInstr* m_leftop; // lment de gauche + CBotInstr* m_rightop; // lment de droite public: - CBotTwoOpExpr(); - ~CBotTwoOpExpr(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = NULL); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); + CBotTwoOpExpr(); + ~CBotTwoOpExpr(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = NULL); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -921,10 +1013,10 @@ class CBotBlock : public CBotInstr private: public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - static - CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + static + CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); }; @@ -932,35 +1024,35 @@ public: class CBotListInstr : public CBotInstr { private: - CBotInstr* m_Instr; // les instructions faire + CBotInstr* m_Instr; // les instructions faire public: - CBotListInstr(); - ~CBotListInstr(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotListInstr(); + ~CBotListInstr(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotInstrCall : public CBotInstr { private: - CBotInstr* m_Parameters; // les paramtres valuer -// int m_typeRes; // type du rsultat -// CBotString m_RetClassName; // class du rsultat - CBotTypResult - m_typRes; // type complet du rsultat - long m_nFuncIdent; // id de la fonction + CBotInstr* m_Parameters; // les paramtres valuer +// int m_typeRes; // type du rsultat +// CBotString m_RetClassName; // class du rsultat + CBotTypResult + m_typRes; // type complet du rsultat + long m_nFuncIdent; // id de la fonction public: - CBotInstrCall(); - ~CBotInstrCall(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotInstrCall(); + ~CBotInstrCall(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // un appel d'une mthode @@ -968,25 +1060,25 @@ public: class CBotInstrMethode : public CBotInstr { private: - CBotInstr* m_Parameters; // les paramtres valuer -// int m_typeRes; // type du rsultat -// CBotString m_RetClassName; // class du rsultat - CBotTypResult - m_typRes; // type complet du rsultat + CBotInstr* m_Parameters; // les paramtres valuer +// int m_typeRes; // type du rsultat +// CBotString m_RetClassName; // class du rsultat + CBotTypResult + m_typRes; // type complet du rsultat - CBotString m_NomMethod; // nom de la mthode - long m_MethodeIdent; // identificateur de la mthode -// long m_nThisIdent; // identificateur pour "this" - CBotString m_ClassName; // nom de la classe + CBotString m_NomMethod; // nom de la mthode + long m_MethodeIdent; // identificateur de la mthode +// long m_nThisIdent; // identificateur pour "this" + CBotString m_ClassName; // nom de la classe public: - CBotInstrMethode(); - ~CBotInstrMethode(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); - bool Execute(CBotStack* &pj); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend); - void RestoreStateVar(CBotStack* &pj, bool bMain); + CBotInstrMethode(); + ~CBotInstrMethode(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); + bool Execute(CBotStack* &pj); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; // expression for the variable name @@ -994,53 +1086,53 @@ public: class CBotExprVar : public CBotInstr { private: - long m_nIdent; - friend class CBotPostIncExpr; - friend class CBotPreIncExpr; + long m_nIdent; + friend class CBotPostIncExpr; + friend class CBotPreIncExpr; public: - CBotExprVar(); - ~CBotExprVar(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); - static - CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); - - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); - bool Execute2Var(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep); - void RestoreStateVar(CBotStack* &pj, bool bMain); + CBotExprVar(); + ~CBotExprVar(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); + static + CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); + + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + bool Execute2Var(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep); + void RestoreStateVar(CBotStack* &pj, bool bMain); }; class CBotPostIncExpr : public CBotInstr { private: - CBotInstr* m_Instr; - friend class CBotParExpr; + CBotInstr* m_Instr; + friend class CBotParExpr; public: - CBotPostIncExpr(); - ~CBotPostIncExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotPostIncExpr(); + ~CBotPostIncExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotPreIncExpr : public CBotInstr { private: - CBotInstr* m_Instr; - friend class CBotParExpr; + CBotInstr* m_Instr; + friend class CBotParExpr; public: - CBotPreIncExpr(); - ~CBotPreIncExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotPreIncExpr(); + ~CBotPreIncExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1048,17 +1140,17 @@ class CBotLeftExprVar : public CBotInstr { private: public: - CBotTypResult - m_typevar; // type de variable dclare - long m_nIdent; // identificateur unique pour cette variable + CBotTypResult + m_typevar; // type de variable dclare + long m_nIdent; // identificateur unique pour cette variable public: - CBotLeftExprVar(); - ~CBotLeftExprVar(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotLeftExprVar(); + ~CBotLeftExprVar(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1067,13 +1159,13 @@ class CBotExprBool : public CBotInstr private: public: - CBotExprBool(); - ~CBotExprBool(); + CBotExprBool(); + ~CBotExprBool(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1082,11 +1174,11 @@ class CBotExprNull : public CBotInstr private: public: - CBotExprNull(); - ~CBotExprNull(); + CBotExprNull(); + ~CBotExprNull(); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotExprNan : public CBotInstr @@ -1094,29 +1186,29 @@ class CBotExprNan : public CBotInstr private: public: - CBotExprNan(); - ~CBotExprNan(); + CBotExprNan(); + ~CBotExprNan(); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; class CBotNew : public CBotInstr { private: - CBotInstr* m_Parameters; // les paramtres valuer - long m_nMethodeIdent; -// long m_nThisIdent; - CBotToken m_vartoken; + CBotInstr* m_Parameters; // les paramtres valuer + long m_nMethodeIdent; +// long m_nThisIdent; + CBotToken m_vartoken; public: - CBotNew(); - ~CBotNew(); + CBotNew(); + ~CBotNew(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; // expression reprsentant un nombre @@ -1124,17 +1216,17 @@ public: class CBotExprNum : public CBotInstr { private: - int m_numtype; // et le type de nombre - long m_valint; // valeur pour un int - float m_valfloat; // valeur pour un float + int m_numtype; // et le type de nombre + long m_valint; // valeur pour un int + float m_valfloat; // valeur pour un float public: - CBotExprNum(); - ~CBotExprNum(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotExprNum(); + ~CBotExprNum(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; @@ -1146,68 +1238,68 @@ class CBotExprAlpha : public CBotInstr private: public: - CBotExprAlpha(); - ~CBotExprAlpha(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); + CBotExprAlpha(); + ~CBotExprAlpha(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); }; -#define MAX(a,b) ((a>b) ? a : b) +#define MAX(a,b) ((a>b) ? a : b) // classe pour la gestion des nombres entier (int) class CBotVarInt : public CBotVar { private: - int m_val; // la valeur - CBotString m_defnum; // le nom si donn par DefineNum - friend class CBotVar; + int m_val; // la valeur + CBotString m_defnum; // le nom si donn par DefineNum + friend class CBotVar; public: - CBotVarInt( const CBotToken* name ); -// ~CBotVarInt(); + CBotVarInt( const CBotToken* name ); +// ~CBotVarInt(); - void SetValInt(int val, const char* s = NULL); - void SetValFloat(float val); - int GivValInt(); - float GivValFloat(); - CBotString GivValString(); + void SetValInt(int val, const char* s = NULL); + void SetValFloat(float val); + int GivValInt(); + float GivValFloat(); + CBotString GivValString(); - void Copy(CBotVar* pSrc, bool bName=true); + void Copy(CBotVar* pSrc, bool bName=true); - void Add(CBotVar* left, CBotVar* right); // addition - void Sub(CBotVar* left, CBotVar* right); // soustraction - void Mul(CBotVar* left, CBotVar* right); // multiplication - int Div(CBotVar* left, CBotVar* right); // division - int Modulo(CBotVar* left, CBotVar* right); // reste de division - void Power(CBotVar* left, CBotVar* right); // puissance + void Add(CBotVar* left, CBotVar* right); // addition + void Sub(CBotVar* left, CBotVar* right); // soustraction + void Mul(CBotVar* left, CBotVar* right); // multiplication + int Div(CBotVar* left, CBotVar* right); // division + int Modulo(CBotVar* left, CBotVar* right); // reste de division + void Power(CBotVar* left, CBotVar* right); // puissance - bool Lo(CBotVar* left, CBotVar* right); - bool Hi(CBotVar* left, CBotVar* right); - bool Ls(CBotVar* left, CBotVar* right); - bool Hs(CBotVar* left, CBotVar* right); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - void XOr(CBotVar* left, CBotVar* right); - void Or(CBotVar* left, CBotVar* right); - void And(CBotVar* left, CBotVar* right); + void XOr(CBotVar* left, CBotVar* right); + void Or(CBotVar* left, CBotVar* right); + void And(CBotVar* left, CBotVar* right); - void SL(CBotVar* left, CBotVar* right); - void SR(CBotVar* left, CBotVar* right); - void ASR(CBotVar* left, CBotVar* right); + void SL(CBotVar* left, CBotVar* right); + void SR(CBotVar* left, CBotVar* right); + void ASR(CBotVar* left, CBotVar* right); - void Neg(); - void Not(); - void Inc(); - void Dec(); + void Neg(); + void Not(); + void Inc(); + void Dec(); - bool Save0State(FILE* pf); - bool Save1State(FILE* pf); + bool Save0State(FILE* pf); + bool Save1State(FILE* pf); }; @@ -1215,40 +1307,40 @@ public: class CBotVarFloat : public CBotVar { private: - float m_val; // la valeur + float m_val; // la valeur public: - CBotVarFloat( const CBotToken* name ); -// ~CBotVarFloat(); + CBotVarFloat( const CBotToken* name ); +// ~CBotVarFloat(); - void SetValInt(int val, const char* s = NULL); - void SetValFloat(float val); - int GivValInt(); - float GivValFloat(); - CBotString GivValString(); + void SetValInt(int val, const char* s = NULL); + void SetValFloat(float val); + int GivValInt(); + float GivValFloat(); + CBotString GivValString(); - void Copy(CBotVar* pSrc, bool bName=true); + void Copy(CBotVar* pSrc, bool bName=true); - void Add(CBotVar* left, CBotVar* right); // addition - void Sub(CBotVar* left, CBotVar* right); // soustraction - void Mul(CBotVar* left, CBotVar* right); // multiplication - int Div(CBotVar* left, CBotVar* right); // division - int Modulo(CBotVar* left, CBotVar* right); // reste de division - void Power(CBotVar* left, CBotVar* right); // puissance + void Add(CBotVar* left, CBotVar* right); // addition + void Sub(CBotVar* left, CBotVar* right); // soustraction + void Mul(CBotVar* left, CBotVar* right); // multiplication + int Div(CBotVar* left, CBotVar* right); // division + int Modulo(CBotVar* left, CBotVar* right); // reste de division + void Power(CBotVar* left, CBotVar* right); // puissance - bool Lo(CBotVar* left, CBotVar* right); - bool Hi(CBotVar* left, CBotVar* right); - bool Ls(CBotVar* left, CBotVar* right); - bool Hs(CBotVar* left, CBotVar* right); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - void Neg(); - void Inc(); - void Dec(); + void Neg(); + void Inc(); + void Dec(); - bool Save1State(FILE* pf); + bool Save1State(FILE* pf); }; @@ -1256,55 +1348,55 @@ public: class CBotVarString : public CBotVar { private: - CBotString m_val; // la valeur + CBotString m_val; // la valeur public: - CBotVarString( const CBotToken* name ); -// ~CBotVarString(); + CBotVarString( const CBotToken* name ); +// ~CBotVarString(); - void SetValString(const char* p); - CBotString GivValString(); + void SetValString(const char* p); + CBotString GivValString(); - void Copy(CBotVar* pSrc, bool bName=true); + void Copy(CBotVar* pSrc, bool bName=true); - void Add(CBotVar* left, CBotVar* right); // addition + void Add(CBotVar* left, CBotVar* right); // addition - bool Lo(CBotVar* left, CBotVar* right); - bool Hi(CBotVar* left, CBotVar* right); - bool Ls(CBotVar* left, CBotVar* right); - bool Hs(CBotVar* left, CBotVar* right); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - bool Save1State(FILE* pf); + bool Save1State(FILE* pf); }; // classe pour la gestion des boolean class CBotVarBoolean : public CBotVar { private: - bool m_val; // la valeur + bool m_val; // la valeur public: - CBotVarBoolean( const CBotToken* name ); -// ~CBotVarBoolean(); + CBotVarBoolean( const CBotToken* name ); +// ~CBotVarBoolean(); - void SetValInt(int val, const char* s = NULL); - void SetValFloat(float val); - int GivValInt(); - float GivValFloat(); - CBotString GivValString(); + void SetValInt(int val, const char* s = NULL); + void SetValFloat(float val); + int GivValInt(); + float GivValFloat(); + CBotString GivValString(); - void Copy(CBotVar* pSrc, bool bName=true); + void Copy(CBotVar* pSrc, bool bName=true); - void And(CBotVar* left, CBotVar* right); - void Or(CBotVar* left, CBotVar* right); - void XOr(CBotVar* left, CBotVar* right); - void Not(); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); + void And(CBotVar* left, CBotVar* right); + void Or(CBotVar* left, CBotVar* right); + void XOr(CBotVar* left, CBotVar* right); + void Not(); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - bool Save1State(FILE* pf); + bool Save1State(FILE* pf); }; @@ -1312,59 +1404,59 @@ public: class CBotVarClass : public CBotVar { private: - static - CBotVarClass* m_ExClass; // liste des instances existantes un moment donn - CBotVarClass* m_ExNext; // pour cette liste gnrale - CBotVarClass* m_ExPrev; // pour cette liste gnrale + static + CBotVarClass* m_ExClass; // liste des instances existantes un moment donn + CBotVarClass* m_ExNext; // pour cette liste gnrale + CBotVarClass* m_ExPrev; // pour cette liste gnrale private: - CBotClass* m_pClass; // la dfinition de la classe - CBotVarClass* m_pParent; // l'instance dans la classe parent - CBotVar* m_pVar; // contenu - friend class CBotVar; // mon papa est un copain - friend class CBotVarPointer; // et le pointeur aussi - int m_CptUse; // compteur d'utilisation - long m_ItemIdent; // identificateur (unique) de l'instance - bool m_bConstructor; // set si un constructeur a t appel + CBotClass* m_pClass; // la dfinition de la classe + CBotVarClass* m_pParent; // l'instance dans la classe parent + CBotVar* m_pVar; // contenu + friend class CBotVar; // mon papa est un copain + friend class CBotVarPointer; // et le pointeur aussi + int m_CptUse; // compteur d'utilisation + long m_ItemIdent; // identificateur (unique) de l'instance + bool m_bConstructor; // set si un constructeur a t appel public: - CBotVarClass( const CBotToken* name, const CBotTypResult& type ); -// CBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - ~CBotVarClass(); -// void InitCBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); + CBotVarClass( const CBotToken* name, const CBotTypResult& type ); +// CBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); + ~CBotVarClass(); +// void InitCBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - void Copy(CBotVar* pSrc, bool bName=true); - void SetClass(CBotClass* pClass); //, int &nIdent); - CBotClass* GivClass(); - CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) - CBotVar* GivItemRef(int nIdent); + void Copy(CBotVar* pSrc, bool bName=true); + void SetClass(CBotClass* pClass); //, int &nIdent); + CBotClass* GivClass(); + CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) + CBotVar* GivItemRef(int nIdent); - CBotVar* GivItem(int n, bool bExtend); - CBotVar* GivItemList(); + CBotVar* GivItem(int n, bool bExtend); + CBotVar* GivItemList(); - CBotString GivValString(); + CBotString GivValString(); - bool Save1State(FILE* pf); - void Maj(void* pUser, bool bContinue); + bool Save1State(FILE* pf); + void Maj(void* pUser, bool bContinue); - void IncrementUse(); // une rfrence en plus - void DecrementUse(); // une rfrence en moins + void IncrementUse(); // une rfrence en plus + void DecrementUse(); // une rfrence en moins - CBotVarClass* - GivPointer(); - void SetItemList(CBotVar* pVar); + CBotVarClass* + GivPointer(); + void SetItemList(CBotVar* pVar); - void SetIdent(long n); - - static CBotVarClass* Find(long id); + void SetIdent(long n); + + static CBotVarClass* Find(long id); -// CBotVar* GivMyThis(); +// CBotVar* GivMyThis(); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); - void ConstructorSet(); + void ConstructorSet(); }; @@ -1372,68 +1464,68 @@ public: class CBotVarPointer : public CBotVar { private: - CBotVarClass* - m_pVarClass; // contenu - CBotClass* m_pClass; // la classe prvue pour ce pointeur - friend class CBotVar; // mon papa est un copain + CBotVarClass* + m_pVarClass; // contenu + CBotClass* m_pClass; // la classe prvue pour ce pointeur + friend class CBotVar; // mon papa est un copain public: - CBotVarPointer( const CBotToken* name, CBotTypResult& type ); - ~CBotVarPointer(); - - void Copy(CBotVar* pSrc, bool bName=true); - void SetClass(CBotClass* pClass); - CBotClass* GivClass(); - CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) - CBotVar* GivItemRef(int nIdent); - CBotVar* GivItemList(); - - CBotString GivValString(); - void SetPointer(CBotVar* p); - CBotVarClass* - GivPointer(); - - void SetIdent(long n); // associe un numro d'identification (unique) - long GivIdent(); // donne le numro d'identification associ - void ConstructorSet(); - - bool Save1State(FILE* pf); - void Maj(void* pUser, bool bContinue); - - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); + CBotVarPointer( const CBotToken* name, CBotTypResult& type ); + ~CBotVarPointer(); + + void Copy(CBotVar* pSrc, bool bName=true); + void SetClass(CBotClass* pClass); + CBotClass* GivClass(); + CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) + CBotVar* GivItemRef(int nIdent); + CBotVar* GivItemList(); + + CBotString GivValString(); + void SetPointer(CBotVar* p); + CBotVarClass* + GivPointer(); + + void SetIdent(long n); // associe un numro d'identification (unique) + long GivIdent(); // donne le numro d'identification associ + void ConstructorSet(); + + bool Save1State(FILE* pf); + void Maj(void* pUser, bool bContinue); + + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); }; // classe pour les tableaux -#define MAXARRAYSIZE 9999 +#define MAXARRAYSIZE 9999 class CBotVarArray : public CBotVar { private: - CBotVarClass* - m_pInstance; // instance grant le tableau + CBotVarClass* + m_pInstance; // instance grant le tableau - friend class CBotVar; // papa est un copain + friend class CBotVar; // papa est un copain public: - CBotVarArray( const CBotToken* name, CBotTypResult& type ); - ~CBotVarArray(); - - void SetPointer(CBotVar* p); - CBotVarClass* - GivPointer(); - - void Copy(CBotVar* pSrc, bool bName=true); - CBotVar* GivItem(int n, bool bGrow=false); // rend un lment selon son index numrique - // agrandi le tableau si ncessaire si bExtend -// CBotVar* GivItem(const char* name); // rend un lment selon son index litral - CBotVar* GivItemList(); // donne le premier lment de la liste - - CBotString GivValString(); // donne le contenu du tableau dans une chane - - bool Save1State(FILE* pf); + CBotVarArray( const CBotToken* name, CBotTypResult& type ); + ~CBotVarArray(); + + void SetPointer(CBotVar* p); + CBotVarClass* + GivPointer(); + + void Copy(CBotVar* pSrc, bool bName=true); + CBotVar* GivItem(int n, bool bGrow=false); // rend un lment selon son index numrique + // agrandi le tableau si ncessaire si bExtend +// CBotVar* GivItem(const char* name); // rend un lment selon son index litral + CBotVar* GivItemList(); // donne le premier lment de la liste + + CBotString GivValString(); // donne le contenu du tableau dans une chane + + bool Save1State(FILE* pf); }; @@ -1455,7 +1547,7 @@ extern bool ReadType(FILE* pf, CBotTypResult& type); extern float GivNumFloat( const char* p ); -#if false +#if false extern void DEBUG( const char* text, int val, CBotStack* pile ); #endif @@ -1465,51 +1557,51 @@ extern void DEBUG( const char* text, int val, CBotStack* pile ); class CBotCall { private: - static - CBotCall* m_ListCalls; - static - void* m_pUser; - long m_nFuncIdent; + static + CBotCall* m_ListCalls; + static + void* m_pUser; + long m_nFuncIdent; private: - CBotString m_name; - bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; - CBotTypResult - (*m_rComp) (CBotVar* &pVar, void* pUser) ; - CBotCall* m_next; + CBotString m_name; + bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; + CBotTypResult + (*m_rComp) (CBotVar* &pVar, void* pUser) ; + CBotCall* m_next; public: - CBotCall(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - ~CBotCall(); - - static - bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - - static - CBotTypResult - CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); - static - bool CheckCall(const char* name); - -// static -// int DoCall(CBotToken* &p, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); - static - int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); -#if STACKRUN - bool Run(CBotStack* pStack); - static - bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); + CBotCall(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + ~CBotCall(); + + static + bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + + static + CBotTypResult + CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); + static + bool CheckCall(const char* name); + +// static +// int DoCall(CBotToken* &p, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); + static + int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); +#if STACKRUN + bool Run(CBotStack* pStack); + static + bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); #endif - CBotString GivName(); - CBotCall* Next(); - - static void SetPUser(void* pUser); - static void Free(); + CBotString GivName(); + CBotCall* Next(); + + static void SetPUser(void* pUser); + static void Free(); }; // classe grant les mthodes dclares par AddFunction sur une classe @@ -1517,31 +1609,31 @@ public: class CBotCallMethode { private: - CBotString m_name; - bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception); - CBotTypResult - (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); - CBotCallMethode* m_next; - friend class CBotClass; - long m_nFuncIdent; + CBotString m_name; + bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception); + CBotTypResult + (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); + CBotCallMethode* m_next; + friend class CBotClass; + long m_nFuncIdent; public: - CBotCallMethode(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - ~CBotCallMethode(); - - CBotTypResult - CompileCall(const char* name, CBotVar* pThis, - CBotVar** ppVars, CBotCStack* pStack, - long& nIdent); - - int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pFunc); - - CBotString GivName(); - CBotCallMethode* Next(); - void AddNext(CBotCallMethode* p); - + CBotCallMethode(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); + ~CBotCallMethode(); + + CBotTypResult + CompileCall(const char* name, CBotVar* pThis, + CBotVar** ppVars, CBotCStack* pStack, + long& nIdent); + + int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pFunc); + + CBotString GivName(); + CBotCallMethode* Next(); + void AddNext(CBotCallMethode* p); + }; // une liste de paramtres @@ -1549,26 +1641,26 @@ public: class CBotDefParam { private: - CBotToken m_token; // nom du paramtre - CBotString m_typename; // nom du type - CBotTypResult m_type; // type de paramtre - CBotDefParam* m_next; // paramtre suivant - long m_nIdent; + CBotToken m_token; // nom du paramtre + CBotString m_typename; // nom du type + CBotTypResult m_type; // type de paramtre + CBotDefParam* m_next; // paramtre suivant + long m_nIdent; public: - CBotDefParam(); - ~CBotDefParam(); - static - CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotVar** ppVars, CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - - void AddNext(CBotDefParam* p); - int GivType(); - CBotTypResult GivTypResult(); - CBotDefParam* GivNext(); - - CBotString GivParamString(); + CBotDefParam(); + ~CBotDefParam(); + static + CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotVar** ppVars, CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + + void AddNext(CBotDefParam* p); + int GivType(); + CBotTypResult GivTypResult(); + CBotDefParam* GivNext(); + + CBotString GivParamString(); }; @@ -1577,65 +1669,65 @@ public: class CBotFunction : CBotInstr { private: - // gestion d'une liste (static) de fonctions publiques - static - CBotFunction* m_listPublic; - CBotFunction* m_nextpublic; - CBotFunction* m_prevpublic; - friend class CBotCStack; -// long m_nThisIdent; - long m_nFuncIdent; - bool m_bSynchro; // mthode synchronise ? + // gestion d'une liste (static) de fonctions publiques + static + CBotFunction* m_listPublic; + CBotFunction* m_nextpublic; + CBotFunction* m_prevpublic; + friend class CBotCStack; +// long m_nThisIdent; + long m_nFuncIdent; + bool m_bSynchro; // mthode synchronise ? private: - CBotDefParam* m_Param; // liste des paramtres - CBotInstr* m_Block; // le bloc d'instructions - CBotFunction* m_next; - CBotToken m_retToken; // si retourne un CBotTypClass - CBotTypResult m_retTyp; // type complet du rsultat - - bool m_bPublic; // fonction publique - bool m_bExtern; // fonction extern - CBotString m_MasterClass; // nom de la classe qu'on drive - CBotProgram* m_pProg; - friend class CBotProgram; - friend class CBotClass; - - CBotToken m_extern; // pour la position du mot "extern" - CBotToken m_openpar; - CBotToken m_closepar; - CBotToken m_openblk; - CBotToken m_closeblk; + CBotDefParam* m_Param; // liste des paramtres + CBotInstr* m_Block; // le bloc d'instructions + CBotFunction* m_next; + CBotToken m_retToken; // si retourne un CBotTypClass + CBotTypResult m_retTyp; // type complet du rsultat + + bool m_bPublic; // fonction publique + bool m_bExtern; // fonction extern + CBotString m_MasterClass; // nom de la classe qu'on drive + CBotProgram* m_pProg; + friend class CBotProgram; + friend class CBotClass; + + CBotToken m_extern; // pour la position du mot "extern" + CBotToken m_openpar; + CBotToken m_closepar; + CBotToken m_openblk; + CBotToken m_closeblk; public: - CBotFunction(); - ~CBotFunction(); - static - CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, bool bLocal = true); - static - CBotFunction* Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass); - bool Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); - void RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); - - void AddNext(CBotFunction* p); - CBotTypResult CompileCall(const char* name, CBotVar** ppVars, long& nIdent); - CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic = true); - - int DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken); - void RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack); - int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass); - void RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass); - bool CheckParam(CBotDefParam* pParam); - - static - void AddPublic(CBotFunction* pfunc); - - CBotString GivName(); - CBotString GivParams(); - bool IsPublic(); - bool IsExtern(); - CBotFunction* Next(); - - bool GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); + CBotFunction(); + ~CBotFunction(); + static + CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, bool bLocal = true); + static + CBotFunction* Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass); + bool Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); + void RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); + + void AddNext(CBotFunction* p); + CBotTypResult CompileCall(const char* name, CBotVar** ppVars, long& nIdent); + CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic = true); + + int DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken); + void RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack); + int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass); + void RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass); + bool CheckParam(CBotDefParam* pParam); + + static + void AddPublic(CBotFunction* pfunc); + + CBotString GivName(); + CBotString GivParams(); + bool IsPublic(); + bool IsExtern(); + CBotFunction* Next(); + + bool GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); }; diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 514146f..f2e2297 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -49,20 +49,17 @@ class CBotCStack; // stack // Variables management //////////////////////////////////////////////////////////////////////// -// ces types sont calqus sur les types Java -// ne pas changer l'ordre de ces types - /** \brief CBotType Defines known types. This types are modeled on Java types. Do not change the order of elements */ enum CBotType { CBotTypVoid = 0, - CBotTypByte = 1, - CBotTypShort = 2, - CBotTypChar = 3, + CBotTypByte = 1, //n + CBotTypShort = 2, //n + CBotTypChar = 3, //n CBotTypInt = 4, - CBotTypLong = 5, + CBotTypLong = 5, //n CBotTypFloat = 6, - CBotTypDouble = 7, + CBotTypDouble = 7, //n CBotTypBoolean = 8, CBotTypString = 9, @@ -74,71 +71,79 @@ enum CBotType CBotTypClass = 15, CBotTypIntrinsic = 16 // instance of a class intrinsic }; -//n = non encore implment +//n = not implemented yet -// pour SetUserPtr lors de la suppression d'un objet +// for SetUserPtr when deleting an object #define OBJECTDELETED ((void*)-1) -// valeur mise avant initialisation +// value set before initialization #define OBJECTCREATED ((void*)-2) -// classe permettant de dfinir le type complet d'un rsultat +/** \brief CBotTypResult class to define the complete type of a result*/ class CBotTypResult { -private: - int m_type; - CBotTypResult* m_pNext; // pour les types de types - CBotClass* m_pClass; // pour les drivs de classe - int m_limite; // limitation des tableaux - friend class CBotVarClass; - friend class CBotVarPointer; - public: - // divers constructeurs selon les besoins - CBotTypResult(int type); - // pour les types simples (CBotTypInt CBotTypString) - CBotTypResult(int type, const char* name); - // pour les types pointeur et classe intrinsic - CBotTypResult(int type, CBotClass* pClass); - // idem partir de l'instance d'une classe - CBotTypResult(int type, CBotTypResult elem); - // pour les tableaux de variables - - CBotTypResult(const CBotTypResult& typ); - // pour les assignations - CBotTypResult(); - // pour par dfaut - ~CBotTypResult(); + /** + * \brief CBotTypResult constructor of an object + * \param type type of created result, see CBotType + */ + CBotTypResult(int type); + + /** + * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) + * \param type type of created result, see CBotType + * \param name + */ + // pour les types simples (CBotTypInt CBotTypString) + CBotTypResult(int type, const char* name); + // pour les types pointeur et classe intrinsic + CBotTypResult(int type, CBotClass* pClass); + // idem partir de l'instance d'une classe + CBotTypResult(int type, CBotTypResult elem); + // pour les tableaux de variables + + CBotTypResult(const CBotTypResult& typ); + // pour les assignations + CBotTypResult(); + // pour par dfaut + ~CBotTypResult(); int GivType(int mode = 0) const; - // rend le type CBotTyp* du rsultat + // rend le type CBotTyp* du rsultat void SetType(int n); - // modifie le type + // modifie le type CBotClass* GivClass() const; - // rend le pointeur la classe (pour les CBotTypClass, CBotTypPointer) + // rend le pointeur la classe (pour les CBotTypClass, CBotTypPointer) int GivLimite() const; - // rend la taille limite du tableau (CBotTypArray) + // rend la taille limite du tableau (CBotTypArray) void SetLimite(int n); - // fixe une limite au tableau + // fixe une limite au tableau void SetArray(int* max ); - // idem avec une liste de dimension (tableaux de tableaux) + // idem avec une liste de dimension (tableaux de tableaux) CBotTypResult& GivTypElem() const; - // rend le type des lments du tableau (CBotTypArray) + // rend le type des lments du tableau (CBotTypArray) bool Compare(const CBotTypResult& typ) const; - // compare si les types sont compatibles + // compare si les types sont compatibles bool Eq(int type) const; - // compare le type + // compare le type CBotTypResult& - operator=(const CBotTypResult& src); - // copie un type complet dans un autre + operator=(const CBotTypResult& src); + // copie un type complet dans un autre +private: + int m_type; + CBotTypResult* m_pNext; // pour les types de types + CBotClass* m_pClass; // pour les drivs de classe + int m_limite; // limitation des tableaux + friend class CBotVarClass; + friend class CBotVarPointer; }; /* @@ -236,11 +241,12 @@ public: //////////////////////////////////////////////////////////////////////// -// dfinie une classe pour l'utilisation des strings +// // car CString fait partie de MFC pas utilis ici. // // ( toutes les fonctions ne sont pas encore implmentes ) +/** \brief CBotString Class used to work on strings */ class CBotString { public: @@ -303,7 +309,7 @@ private: int m_lg; /** \brief Keeps the string corresponding to keyword ID */ - static const std::map s_keywordString; + static const std::map s_keywordString; /** * \brief MapIdToString maps given ID to its string equivalent diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 784e647..363b939 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -1379,7 +1379,6 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) if (IsOfType(p, TokenTypVar)) { CBotClass* pPapa = NULL; -#if EXTENDS if ( IsOfType( p, ID_EXTENDS ) ) { CBotString name = p->GivString(); @@ -1391,7 +1390,6 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) return NULL; } } -#endif CBotClass* classe = (pOld == NULL) ? new CBotClass(name, pPapa) : pOld; classe->Purge(); // vide les anciennes dfinitions classe->m_IsDef = false; // dfinition en cours @@ -1627,12 +1625,10 @@ CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) // la classe t cre par Compile1 CBotClass* pOld = CBotClass::Find(name); -#if EXTENDS if ( IsOfType( p, ID_EXTENDS ) ) { IsOfType(p, TokenTypVar); // forcment } -#endif IsOfType( p, ID_OPBLK); // forcment while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 291a1e5..419798f 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -14,10 +14,7 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/.////////////////////////////////////////////////////////////////////// -/** - * \file CBotStack.cpp - * \brief Management of the stack - */ +//Management of the stack #include "CBot.h" @@ -25,7 +22,7 @@ #include -#define ITIMER 100 +#define ITIMER 100 //////////////////////////////////////////////////////////////////////////// // gestion de la pile d'excution @@ -40,202 +37,202 @@ int CBotStack::m_end = 0; CBotString CBotStack::m_labelBreak=""; void* CBotStack::m_pUser = NULL; -#if STACKMEM +#if STACKMEM CBotStack* CBotStack::FirstStack() { - CBotStack* p; - - long size = sizeof(CBotStack); - size *= (MAXSTACK+10); - - // demande une tranche mmoire pour la pile - p = (CBotStack*)malloc(size); - - // la vide totalement - memset(p, 0, size); - - p-> m_bBlock = true; - m_timer = m_initimer; // met le timer au dbut - - CBotStack* pp = p; - pp += MAXSTACK; - int i; - for ( i = 0 ; i< 10 ; i++ ) - { - pp->m_bOver = true; - pp ++; - } -#ifdef _DEBUG - int n = 1; - pp = p; - for ( i = 0 ; i< MAXSTACK+10 ; i++ ) - { - pp->m_index = n++; - pp ++; - } + CBotStack* p; + + long size = sizeof(CBotStack); + size *= (MAXSTACK+10); + + // demande une tranche mmoire pour la pile + p = (CBotStack*)malloc(size); + + // la vide totalement + memset(p, 0, size); + + p-> m_bBlock = true; + m_timer = m_initimer; // met le timer au dbut + + CBotStack* pp = p; + pp += MAXSTACK; + int i; + for ( i = 0 ; i< 10 ; i++ ) + { + pp->m_bOver = true; + pp ++; + } +#ifdef _DEBUG + int n = 1; + pp = p; + for ( i = 0 ; i< MAXSTACK+10 ; i++ ) + { + pp->m_index = n++; + pp ++; + } #endif - m_error = 0; // vite des blocages car m_error est static - return p; + m_error = 0; // vite des blocages car m_error est static + return p; } CBotStack::CBotStack(CBotStack* ppapa) { - // constructor must exist or the destructor is never called! + // constructor must exist or the destructor is never called! ASM_TRAP(); } CBotStack::~CBotStack() { - ASM_TRAP(); // utiliser Delete() la place + ASM_TRAP(); // utiliser Delete() la place } void CBotStack::Delete() { - if ( this == NULL || this == EOX ) return; + if ( this == NULL || this == EOX ) return; - m_next->Delete(); - m_next2->Delete(); + m_next->Delete(); + m_next2->Delete(); - if (m_prev != NULL) - { - if ( m_prev->m_next == this ) - m_prev->m_next = NULL; // enlve de la chane + if (m_prev != NULL) + { + if ( m_prev->m_next == this ) + m_prev->m_next = NULL; // enlve de la chane - if ( m_prev->m_next2 == this ) - m_prev->m_next2 = NULL; // enlve de la chane - } + if ( m_prev->m_next2 == this ) + m_prev->m_next2 = NULL; // enlve de la chane + } - delete m_var; - delete m_listVar; + delete m_var; + delete m_listVar; - CBotStack* p = m_prev; - bool bOver = m_bOver; -#ifdef _DEBUG - int n = m_index; + CBotStack* p = m_prev; + bool bOver = m_bOver; +#ifdef _DEBUG + int n = m_index; #endif - // efface le bloc libr - memset(this, 0, sizeof(CBotStack)); - m_bOver = bOver; -#ifdef _DEBUG - m_index = n; + // efface le bloc libr + memset(this, 0, sizeof(CBotStack)); + m_bOver = bOver; +#ifdef _DEBUG + m_index = n; #endif - if ( p == NULL ) - free( this ); + if ( p == NULL ) + free( this ); } // routine optimise CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) { - if (m_next != NULL) - { - return m_next; // reprise dans une pile existante - } + if (m_next != NULL) + { + return m_next; // reprise dans une pile existante + } -#ifdef _DEBUG - int n = 0; +#ifdef _DEBUG + int n = 0; #endif - CBotStack* p = this; - do - { - p ++; -#ifdef _DEBUG - n ++; + CBotStack* p = this; + do + { + p ++; +#ifdef _DEBUG + n ++; #endif - } - while ( p->m_prev != NULL ); + } + while ( p->m_prev != NULL ); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_instr = instr; - p->m_prog = m_prog; - p->m_step = 0; - p->m_prev = this; - p->m_state = 0; - p->m_call = NULL; - p->m_bFunc = false; - return p; + m_next = p; // chane l'lment + p->m_bBlock = bBlock; + p->m_instr = instr; + p->m_prog = m_prog; + p->m_step = 0; + p->m_prev = this; + p->m_state = 0; + p->m_call = NULL; + p->m_bFunc = false; + return p; } CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) { - if (m_next != NULL) - { - if ( m_next == EOX ) - { - m_next = NULL; - return EOX; - } - return m_next; // reprise dans une pile existante - } - CBotStack* p = AddStack(NULL, bBlock); - p->m_call = instr; - p->m_bFunc = 2; // spcial - return p; + if (m_next != NULL) + { + if ( m_next == EOX ) + { + m_next = NULL; + return EOX; + } + return m_next; // reprise dans une pile existante + } + CBotStack* p = AddStack(NULL, bBlock); + p->m_call = instr; + p->m_bFunc = 2; // spcial + return p; } CBotStack* CBotStack::AddStack2(bool bBlock) { - if (m_next2 != NULL) - { - m_next2->m_prog = m_prog; // spcial vite un RestoreStack2 - return m_next2; // reprise dans une pile existante - } + if (m_next2 != NULL) + { + m_next2->m_prog = m_prog; // spcial vite un RestoreStack2 + return m_next2; // reprise dans une pile existante + } - CBotStack* p = this; - do - { - p ++; - } - while ( p->m_prev != NULL ); + CBotStack* p = this; + do + { + p ++; + } + while ( p->m_prev != NULL ); - m_next2 = p; // chane l'lment - p->m_prev = this; - p->m_bBlock = bBlock; - p->m_prog = m_prog; - p->m_step = 0; - return p; + m_next2 = p; // chane l'lment + p->m_prev = this; + p->m_bBlock = bBlock; + p->m_prog = m_prog; + p->m_step = 0; + return p; } bool CBotStack::GivBlock() { - return m_bBlock; + return m_bBlock; } bool CBotStack::Return(CBotStack* pfils) { - if ( pfils == this ) return true; // spcial + if ( pfils == this ) return true; // spcial - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable + if (m_var != NULL) delete m_var; // valeur remplace ? + m_var = pfils->m_var; // rsultat transmis + pfils->m_var = NULL; // ne pas dtruire la variable - m_next->Delete();m_next = NULL; // libre la pile au dessus - m_next2->Delete();m_next2 = NULL; // aussi la seconde pile (catch) + m_next->Delete();m_next = NULL; // libre la pile au dessus + m_next2->Delete();m_next2 = NULL; // aussi la seconde pile (catch) - return (m_error == 0); // interrompu si erreur + return (m_error == 0); // interrompu si erreur } bool CBotStack::ReturnKeep(CBotStack* pfils) { - if ( pfils == this ) return true; // spcial + if ( pfils == this ) return true; // spcial - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable + if (m_var != NULL) delete m_var; // valeur remplace ? + m_var = pfils->m_var; // rsultat transmis + pfils->m_var = NULL; // ne pas dtruire la variable - return (m_error == 0); // interrompu si erreur + return (m_error == 0); // interrompu si erreur } bool CBotStack::StackOver() { - if (!m_bOver) return false; - m_error = TX_STACKOVER; - return true; + if (!m_bOver) return false; + m_error = TX_STACKOVER; + return true; } #else @@ -246,118 +243,118 @@ CBotStack::CBotStack(CBotStack* ppapa) m_next2 = NULL; m_prev = ppapa; - m_bBlock = (ppapa == NULL) ? true : false; + m_bBlock = (ppapa == NULL) ? true : false; m_state = 0; - m_step = 1; + m_step = 1; - if (ppapa == NULL) m_timer = m_initimer; // met le timer au dbut + if (ppapa == NULL) m_timer = m_initimer; // met le timer au dbut - m_listVar = NULL; - m_bDontDelete = false; - - m_var = NULL; - m_prog = NULL; - m_instr = NULL; - m_call = NULL; - m_bFunc = false; + m_listVar = NULL; + m_bDontDelete = false; + + m_var = NULL; + m_prog = NULL; + m_instr = NULL; + m_call = NULL; + m_bFunc = false; } // destructeur CBotStack::~CBotStack() { - if ( m_next != EOX) delete m_next; - delete m_next2; - if (m_prev != NULL && m_prev->m_next == this ) - m_prev->m_next = NULL; // enlve de la chane + if ( m_next != EOX) delete m_next; + delete m_next2; + if (m_prev != NULL && m_prev->m_next == this ) + m_prev->m_next = NULL; // enlve de la chane - delete m_var; - if ( !m_bDontDelete ) delete m_listVar; + delete m_var; + if ( !m_bDontDelete ) delete m_listVar; } // routine optimiser CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) { - if (m_next != NULL) - { - return m_next; // reprise dans une pile existante - } - CBotStack* p = new CBotStack(this); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_instr = instr; - p->m_prog = m_prog; - p->m_step = 0; - return p; + if (m_next != NULL) + { + return m_next; // reprise dans une pile existante + } + CBotStack* p = new CBotStack(this); + m_next = p; // chane l'lment + p->m_bBlock = bBlock; + p->m_instr = instr; + p->m_prog = m_prog; + p->m_step = 0; + return p; } CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) { - if (m_next != NULL) - { - if ( m_next == EOX ) - { - m_next = NULL; - return EOX; - } - return m_next; // reprise dans une pile existante - } - CBotStack* p = new CBotStack(this); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_call = instr; - p->m_prog = m_prog; - p->m_step = 0; - p->m_bFunc = 2; // spcial - return p; + if (m_next != NULL) + { + if ( m_next == EOX ) + { + m_next = NULL; + return EOX; + } + return m_next; // reprise dans une pile existante + } + CBotStack* p = new CBotStack(this); + m_next = p; // chane l'lment + p->m_bBlock = bBlock; + p->m_call = instr; + p->m_prog = m_prog; + p->m_step = 0; + p->m_bFunc = 2; // spcial + return p; } CBotStack* CBotStack::AddStack2(bool bBlock) { - if (m_next2 != NULL) - { - m_next2->m_prog = m_prog; // spcial vite un RestoreStack2 - return m_next2; // reprise dans une pile existante - } + if (m_next2 != NULL) + { + m_next2->m_prog = m_prog; // spcial vite un RestoreStack2 + return m_next2; // reprise dans une pile existante + } - CBotStack* p = new CBotStack(this); - m_next2 = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_prog = m_prog; - p->m_step = 0; + CBotStack* p = new CBotStack(this); + m_next2 = p; // chane l'lment + p->m_bBlock = bBlock; + p->m_prog = m_prog; + p->m_step = 0; - return p; + return p; } bool CBotStack::Return(CBotStack* pfils) { - if ( pfils == this ) return true; // spcial + if ( pfils == this ) return true; // spcial - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruite la variable + if (m_var != NULL) delete m_var; // valeur remplace ? + m_var = pfils->m_var; // rsultat transmis + pfils->m_var = NULL; // ne pas dtruite la variable - if ( m_next != EOX ) delete m_next; // libre la pile au dessus - delete m_next2;m_next2 = NULL; // aussi la seconde pile (catch) + if ( m_next != EOX ) delete m_next; // libre la pile au dessus + delete m_next2;m_next2 = NULL; // aussi la seconde pile (catch) - return (m_error == 0); // interrompu si erreur + return (m_error == 0); // interrompu si erreur } bool CBotStack::StackOver() { - return false; // pas de test de dbordement dans cette version + return false; // pas de test de dbordement dans cette version } #endif void CBotStack::Reset(void* pUser) { - m_timer = m_initimer; // remet le timer - m_error = 0; -// m_start = 0; -// m_end = 0; - m_labelBreak.Empty(); - m_pUser = pUser; + m_timer = m_initimer; // remet le timer + m_error = 0; +// m_start = 0; +// m_end = 0; + m_labelBreak.Empty(); + m_pUser = pUser; } @@ -365,20 +362,20 @@ void CBotStack::Reset(void* pUser) CBotStack* CBotStack::RestoreStack(CBotInstr* instr) { - if (m_next != NULL) - { - m_next->m_instr = instr; // rinit (si reprise aprs restitution) - m_next->m_prog = m_prog; - return m_next; // reprise dans une pile existante - } - return NULL; + if (m_next != NULL) + { + m_next->m_instr = instr; // rinit (si reprise aprs restitution) + m_next->m_prog = m_prog; + return m_next; // reprise dans une pile existante + } + return NULL; } CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) { - CBotStack* p = RestoreStack(); - p->m_call = instr; - return p; + CBotStack* p = RestoreStack(); + p->m_call = instr; + return p; } @@ -386,181 +383,173 @@ CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) // routine pour l'excution pas pas bool CBotStack::IfStep() { - if ( m_initimer > 0 || m_step++ > 0 ) return false; - return true; + if ( m_initimer > 0 || m_step++ > 0 ) return false; + return true; } bool CBotStack::BreakReturn(CBotStack* pfils, const char* name) { - if ( m_error>=0 ) return false; // sortie normale - if ( m_error==-3 ) return false; // sortie normale (return en cours) + if ( m_error>=0 ) return false; // sortie normale + if ( m_error==-3 ) return false; // sortie normale (return en cours) - if (!m_labelBreak.IsEmpty() && (name[0] == 0 || m_labelBreak != name)) - return false; // c'est pas pour moi + if (!m_labelBreak.IsEmpty() && (name[0] == 0 || m_labelBreak != name)) + return false; // c'est pas pour moi - m_error = 0; - m_labelBreak.Empty(); - return Return(pfils); + m_error = 0; + m_labelBreak.Empty(); + return Return(pfils); } bool CBotStack::IfContinue(int state, const char* name) { - if ( m_error != -2 ) return false; + if ( m_error != -2 ) return false; - if (!m_labelBreak.IsEmpty() && (name == NULL || m_labelBreak != name)) - return false; // c'est pas pour moi + if (!m_labelBreak.IsEmpty() && (name == NULL || m_labelBreak != name)) + return false; // c'est pas pour moi - m_state = state; // o reprendre ? - m_error = 0; - m_labelBreak.Empty(); - if ( m_next != EOX ) m_next->Delete(); // purge la pile au dessus - return true; + m_state = state; // o reprendre ? + m_error = 0; + m_labelBreak.Empty(); + if ( m_next != EOX ) m_next->Delete(); // purge la pile au dessus + return true; } void CBotStack::SetBreak(int val, const char* name) { - m_error = -val; // ragit comme une Exception - m_labelBreak = name; - if (val == 3) // pour un return - { - m_retvar = m_var; - m_var = NULL; - } + m_error = -val; // ragit comme une Exception + m_labelBreak = name; + if (val == 3) // pour un return + { + m_retvar = m_var; + m_var = NULL; + } } // remet sur la pile la valeur calcule par le dernier CBotReturn bool CBotStack::GivRetVar(bool bRet) { - if (m_error == -3) - { - if ( m_var ) delete m_var; - m_var = m_retvar; - m_retvar = NULL; - m_error = 0; - return true; - } - return bRet; // interrompu par autre chose que return + if (m_error == -3) + { + if ( m_var ) delete m_var; + m_var = m_retvar; + m_retvar = NULL; + m_error = 0; + return true; + } + return bRet; // interrompu par autre chose que return } int CBotStack::GivError(int& start, int& end) { - start = m_start; - end = m_end; - return m_error; + start = m_start; + end = m_end; + return m_error; } -// type d'instruction sur la pile int CBotStack::GivType(int mode) { - if (m_var == NULL) return -1; - return m_var->GivType(mode); + if (m_var == NULL) return -1; + return m_var->GivType(mode); } -// type d'instruction sur la pile CBotTypResult CBotStack::GivTypResult(int mode) { - if (m_var == NULL) return -1; - return m_var->GivTypResult(mode); + if (m_var == NULL) return -1; + return m_var->GivTypResult(mode); } -// type d'instruction sur la pile void CBotStack::SetType(CBotTypResult& type) { - if (m_var == NULL) return; - m_var->SetType( type ); + if (m_var == NULL) return; + m_var->SetType( type ); } -// trouve une variable par son token -// ce peut tre une variable compose avec un point CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif) { - CBotStack* p = this; - CBotString name = pToken->GivString(); + CBotStack* p = this; + CBotString name = pToken->GivString(); - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (pp->GivName() == name) - { - if ( bUpdate ) - pp->Maj(m_pUser, false); + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (pp->GivName() == name) + { + if ( bUpdate ) + pp->Maj(m_pUser, false); - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; } CBotVar* CBotStack::FindVar(const char* name) { - CBotStack* p = this; - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (pp->GivName() == name) - { - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; -} - -// retrouve une variable sur la pile selon son numro d'identification -// ce qui va plus vite que de comparer les noms. + CBotStack* p = this; + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (pp->GivName() == name) + { + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; +} CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif) { - CBotStack* p = this; - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (pp->GivUniqNum() == ident) - { - if ( bUpdate ) - pp->Maj(m_pUser, false); + CBotStack* p = this; + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (pp->GivUniqNum() == ident) + { + if ( bUpdate ) + pp->Maj(m_pUser, false); - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; } CBotVar* CBotStack::FindVar(CBotToken& Token, bool bUpdate, bool bModif) { - CBotToken* pt = &Token; - return FindVar(pt, bUpdate, bModif); + CBotToken* pt = &Token; + return FindVar(pt, bUpdate, bModif); } CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) { - CBotVar* pVar = FindVar( Token, bUpdate ); + CBotVar* pVar = FindVar( Token, bUpdate ); - if ( pVar == NULL) return NULL; + if ( pVar == NULL) return NULL; - CBotVar* pCopy = CBotVar::Create(pVar); - pCopy->Copy(pVar); - return pCopy; + CBotVar* pCopy = CBotVar::Create(pVar); + pCopy->Copy(pVar); + return pCopy; } @@ -568,120 +557,120 @@ bool CBotStack::SetState(int n, int limite) { m_state = n; - m_timer--; // dcompte les oprations - return ( m_timer > limite ); // interrompu si timer pass + m_timer--; // dcompte les oprations + return ( m_timer > limite ); // interrompu si timer pass } bool CBotStack::IncState(int limite) { - m_state++; + m_state++; - m_timer--; // dcompte les oprations - return ( m_timer > limite ); // interrompu si timer pass + m_timer--; // dcompte les oprations + return ( m_timer > limite ); // interrompu si timer pass } void CBotStack::SetError(int n, CBotToken* token) { - if ( n!= 0 && m_error != 0) return; // ne change pas une erreur dj existante - m_error = n; - if (token != NULL) - { - m_start = token->GivStart(); - m_end = token->GivEnd(); - } + if ( n!= 0 && m_error != 0) return; // ne change pas une erreur dj existante + m_error = n; + if (token != NULL) + { + m_start = token->GivStart(); + m_end = token->GivEnd(); + } } void CBotStack::ResetError(int n, int start, int end) { - m_error = n; - m_start = start; - m_end = end; + m_error = n; + m_start = start; + m_end = end; } void CBotStack::SetPosError(CBotToken* token) { - m_start = token->GivStart(); - m_end = token->GivEnd(); + m_start = token->GivStart(); + m_end = token->GivEnd(); } void CBotStack::SetTimer(int n) { - m_initimer = n; + m_initimer = n; } bool CBotStack::Execute() { - CBotCall* instr = NULL; // instruction la plus leve - CBotStack* pile; + CBotCall* instr = NULL; // instruction la plus leve + CBotStack* pile; - CBotStack* p = this; + CBotStack* p = this; - while (p != NULL) - { - if ( p->m_next2 != NULL ) break; - if ( p->m_call != NULL ) - { - instr = p->m_call; - pile = p->m_prev ; - } - p = p->m_next; - } + while (p != NULL) + { + if ( p->m_next2 != NULL ) break; + if ( p->m_call != NULL ) + { + instr = p->m_call; + pile = p->m_prev ; + } + p = p->m_next; + } - if ( instr == NULL ) return true; // excution normale demande + if ( instr == NULL ) return true; // excution normale demande - if (!instr->Run(pile)) return false; // excution partir de l + if (!instr->Run(pile)) return false; // excution partir de l -#if STACKMEM - pile->m_next->Delete(); +#if STACKMEM + pile->m_next->Delete(); #else - delete pile->m_next; + delete pile->m_next; #endif - pile->m_next = EOX; // spcial pour reprise - return true; + pile->m_next = EOX; // spcial pour reprise + return true; } // met sur le stack le pointeur une variable void CBotStack::SetVar( CBotVar* var ) { - if (m_var) delete m_var; // remplacement d'une variable - m_var = var; + if (m_var) delete m_var; // remplacement d'une variable + m_var = var; } // met sur le stack une copie d'une variable void CBotStack::SetCopyVar( CBotVar* var ) { - if (m_var) delete m_var; // remplacement d'une variable + if (m_var) delete m_var; // remplacement d'une variable - m_var = CBotVar::Create("", var->GivTypResult(2)); - m_var->Copy( var ); + m_var = CBotVar::Create("", var->GivTypResult(2)); + m_var->Copy( var ); } CBotVar* CBotStack::GivVar() { - return m_var; + return m_var; } CBotVar* CBotStack::GivPtVar() { - CBotVar* p = m_var; - m_var = NULL; // ne sera pas dtruit donc - return p; + CBotVar* p = m_var; + m_var = NULL; // ne sera pas dtruit donc + return p; } CBotVar* CBotStack::GivCopyVar() { - if (m_var == NULL) return NULL; - CBotVar* v = CBotVar::Create("", m_var->GivType()); - v->Copy( m_var ); - return v; + if (m_var == NULL) return NULL; + CBotVar* v = CBotVar::Create("", m_var->GivType()); + v->Copy( m_var ); + return v; } long CBotStack::GivVal() { - if (m_var == NULL) return 0; - return m_var->GivValInt(); + if (m_var == NULL) return 0; + return m_var->GivValInt(); } @@ -689,431 +678,431 @@ long CBotStack::GivVal() void CBotStack::AddVar(CBotVar* pVar) { - CBotStack* p = this; + CBotStack* p = this; - // revient sur l'lement pre - while (p != NULL && p->m_bBlock == 0) p = p->m_prev; + // revient sur l'lement pre + while (p != NULL && p->m_bBlock == 0) p = p->m_prev; - if ( p == NULL ) return; - -/// p->m_bDontDelete = bDontDelete; + if ( p == NULL ) return; + +/// p->m_bDontDelete = bDontDelete; - CBotVar** pp = &p->m_listVar; - while ( *pp != NULL ) pp = &(*pp)->m_next; + CBotVar** pp = &p->m_listVar; + while ( *pp != NULL ) pp = &(*pp)->m_next; - *pp = pVar; // ajoute la suite + *pp = pVar; // ajoute la suite -#ifdef _DEBUG - if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); +#ifdef _DEBUG + if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); #endif } /*void CBotStack::RestoreVar(CBotVar* pVar) { - if ( !m_bDontDelete ) __asm int 3; - delete m_listVar; - m_listVar = pVar; // remplace directement + if ( !m_bDontDelete ) __asm int 3; + delete m_listVar; + m_listVar = pVar; // remplace directement }*/ void CBotStack::SetBotCall(CBotProgram* p) { - m_prog = p; - m_bFunc = true; + m_prog = p; + m_bFunc = true; } CBotProgram* CBotStack::GivBotCall(bool bFirst) { - if ( ! bFirst ) return m_prog; - CBotStack* p = this; - while ( p->m_prev != NULL ) p = p->m_prev; - return p->m_prog; + if ( ! bFirst ) return m_prog; + CBotStack* p = this; + while ( p->m_prev != NULL ) p = p->m_prev; + return p->m_prog; } void* CBotStack::GivPUser() { - return m_pUser; + return m_pUser; } bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype) { - CBotTypResult res; + CBotTypResult res; - // cherche d'abord selon l'identificateur + // cherche d'abord selon l'identificateur - res = CBotCall::DoCall(nIdent, NULL, ppVar, this, rettype ); - if (res.GivType() >= 0) return res.GivType(); + res = CBotCall::DoCall(nIdent, NULL, ppVar, this, rettype ); + if (res.GivType() >= 0) return res.GivType(); - res = m_prog->GivFunctions()->DoCall(nIdent, NULL, ppVar, this, token ); - if (res.GivType() >= 0) return res.GivType(); + res = m_prog->GivFunctions()->DoCall(nIdent, NULL, ppVar, this, token ); + if (res.GivType() >= 0) return res.GivType(); - // si pas trouv (recompil ?) cherche selon le nom + // si pas trouv (recompil ?) cherche selon le nom - nIdent = 0; - res = CBotCall::DoCall(nIdent, token, ppVar, this, rettype ); - if (res.GivType() >= 0) return res.GivType(); + nIdent = 0; + res = CBotCall::DoCall(nIdent, token, ppVar, this, rettype ); + if (res.GivType() >= 0) return res.GivType(); - res = m_prog->GivFunctions()->DoCall(nIdent, token->GivString(), ppVar, this, token ); - if (res.GivType() >= 0) return res.GivType(); + res = m_prog->GivFunctions()->DoCall(nIdent, token->GivString(), ppVar, this, token ); + if (res.GivType() >= 0) return res.GivType(); - SetError(TX_NOCALL, token); - return true; + SetError(TX_NOCALL, token); + return true; } void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) { - if ( m_next == NULL ) return; + if ( m_next == NULL ) return; - if ( !CBotCall::RestoreCall(nIdent, token, ppVar, this) ) - m_prog->GivFunctions()->RestoreCall(nIdent, token->GivString(), ppVar, this ); + if ( !CBotCall::RestoreCall(nIdent, token, ppVar, this) ) + m_prog->GivFunctions()->RestoreCall(nIdent, token->GivString(), ppVar, this ); } bool SaveVar(FILE* pf, CBotVar* pVar) { - while ( true ) - { - if ( pVar == NULL ) - { - return WriteWord(pf, 0); // met un terminateur - } + while ( true ) + { + if ( pVar == NULL ) + { + return WriteWord(pf, 0); // met un terminateur + } - if ( !pVar->Save0State(pf)) return false; // entte commune - if ( !pVar->Save1State(pf) ) return false; // sauve selon la classe fille + if ( !pVar->Save0State(pf)) return false; // entte commune + if ( !pVar->Save1State(pf) ) return false; // sauve selon la classe fille - pVar = pVar->GivNext(); - } + pVar = pVar->GivNext(); + } } void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) { - CBotProgram* prog = m_prog; // programme courrant + CBotProgram* prog = m_prog; // programme courrant - CBotInstr* funct = NULL; // fonction trouve - CBotInstr* instr = NULL; // instruction la plus leve + CBotInstr* funct = NULL; // fonction trouve + CBotInstr* instr = NULL; // instruction la plus leve - CBotStack* p = this; + CBotStack* p = this; - while (p->m_next != NULL) - { - if ( p->m_instr != NULL ) instr = p->m_instr; - if ( p->m_bFunc == 1 ) funct = p->m_instr; - if ( p->m_next->m_prog != prog ) break ; + while (p->m_next != NULL) + { + if ( p->m_instr != NULL ) instr = p->m_instr; + if ( p->m_bFunc == 1 ) funct = p->m_instr; + if ( p->m_next->m_prog != prog ) break ; - if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; - else p = p->m_next; - } + if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; + else p = p->m_next; + } - if ( p->m_instr != NULL ) instr = p->m_instr; - if ( p->m_bFunc == 1 ) funct = p->m_instr; + if ( p->m_instr != NULL ) instr = p->m_instr; + if ( p->m_bFunc == 1 ) funct = p->m_instr; - if ( funct == NULL ) return; + if ( funct == NULL ) return; - CBotToken* t = funct->GivToken(); - FunctionName = t->GivString(); + CBotToken* t = funct->GivToken(); + FunctionName = t->GivString(); -// if ( p->m_instr != NULL ) instr = p->m_instr; +// if ( p->m_instr != NULL ) instr = p->m_instr; - t = instr->GivToken(); - start = t->GivStart(); - end = t->GivEnd(); + t = instr->GivToken(); + start = t->GivStart(); + end = t->GivEnd(); } CBotVar* CBotStack::GivStackVars(const char* &FunctionName, int level) { - CBotProgram* prog = m_prog; // programme courrant - FunctionName = NULL; + CBotProgram* prog = m_prog; // programme courrant + FunctionName = NULL; - // remonte la pile dans le module courant - CBotStack* p = this; + // remonte la pile dans le module courant + CBotStack* p = this; - while (p->m_next != NULL) - { - if ( p->m_next->m_prog != prog ) break ; + while (p->m_next != NULL) + { + if ( p->m_next->m_prog != prog ) break ; - if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; - else p = p->m_next; - } + if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; + else p = p->m_next; + } - // descend sur les lments de block - while ( p != NULL && !p->m_bBlock ) p = p->m_prev; + // descend sur les lments de block + while ( p != NULL && !p->m_bBlock ) p = p->m_prev; - while ( p != NULL && level++ < 0 ) - { - p = p->m_prev; - while ( p != NULL && !p->m_bBlock ) p = p->m_prev; - } + while ( p != NULL && level++ < 0 ) + { + p = p->m_prev; + while ( p != NULL && !p->m_bBlock ) p = p->m_prev; + } - if ( p == NULL ) return NULL; + if ( p == NULL ) return NULL; - // recherche le nom de la fonction courante - CBotStack* pp = p; - while ( pp != NULL ) - { - if ( pp->m_bFunc == 1 ) break; - pp = pp->m_prev; - } + // recherche le nom de la fonction courante + CBotStack* pp = p; + while ( pp != NULL ) + { + if ( pp->m_bFunc == 1 ) break; + pp = pp->m_prev; + } - if ( pp == NULL || pp->m_instr == NULL ) return NULL; + if ( pp == NULL || pp->m_instr == NULL ) return NULL; - CBotToken* t = pp->m_instr->GivToken(); - FunctionName = t->GivString(); - - return p->m_listVar; + CBotToken* t = pp->m_instr->GivToken(); + FunctionName = t->GivString(); + + return p->m_listVar; } bool CBotStack::SaveState(FILE* pf) { - if ( this == NULL ) // fin de l'arbre ? - { - return WriteWord(pf, 0); // met un terminateur - } - - if ( m_next2 != NULL ) - { - if (!WriteWord(pf, 2)) return false; // une marque de poursuite - if (!m_next2->SaveState(pf)) return false; - } - else - { - if (!WriteWord(pf, 1)) return false; // une marque de poursuite - } - if (!WriteWord(pf, m_bBlock)) return false; // est-ce un bloc local - if (!WriteWord(pf, m_state)) return false; // dans quel tat - if (!WriteWord(pf, 0)) return false; // par compatibilit m_bDontDelete - if (!WriteWord(pf, m_step)) return false; // dans quel tat - - - if (!SaveVar(pf, m_var)) return false; // le rsultat courant - if (!SaveVar(pf, m_listVar)) return false; // les variables locales - - return m_next->SaveState(pf); // enregistre la suite + if ( this == NULL ) // fin de l'arbre ? + { + return WriteWord(pf, 0); // met un terminateur + } + + if ( m_next2 != NULL ) + { + if (!WriteWord(pf, 2)) return false; // une marque de poursuite + if (!m_next2->SaveState(pf)) return false; + } + else + { + if (!WriteWord(pf, 1)) return false; // une marque de poursuite + } + if (!WriteWord(pf, m_bBlock)) return false; // est-ce un bloc local + if (!WriteWord(pf, m_state)) return false; // dans quel tat + if (!WriteWord(pf, 0)) return false; // par compatibilit m_bDontDelete + if (!WriteWord(pf, m_step)) return false; // dans quel tat + + + if (!SaveVar(pf, m_var)) return false; // le rsultat courant + if (!SaveVar(pf, m_listVar)) return false; // les variables locales + + return m_next->SaveState(pf); // enregistre la suite } bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) { - unsigned short w; + unsigned short w; - pStack = NULL; - if (!ReadWord(pf, w)) return false; - if ( w == 0 ) return true; + pStack = NULL; + if (!ReadWord(pf, w)) return false; + if ( w == 0 ) return true; -#if STACKMEM - if ( this == NULL ) pStack = FirstStack(); - else pStack = AddStack(); +#if STACKMEM + if ( this == NULL ) pStack = FirstStack(); + else pStack = AddStack(); #else - pStack = new CBotStack(this); + pStack = new CBotStack(this); #endif - if ( w == 2 ) - { - if (!pStack->RestoreState(pf, pStack->m_next2)) return false; - } + if ( w == 2 ) + { + if (!pStack->RestoreState(pf, pStack->m_next2)) return false; + } - if (!ReadWord(pf, w)) return false; // est-ce un bloc local - pStack->m_bBlock = w; + if (!ReadWord(pf, w)) return false; // est-ce un bloc local + pStack->m_bBlock = w; - if (!ReadWord(pf, w)) return false; // dans quel tat j're ? - pStack->SetState((short)w); // dans le bon tat + if (!ReadWord(pf, w)) return false; // dans quel tat j're ? + pStack->SetState((short)w); // dans le bon tat - if (!ReadWord(pf, w)) return false; // dont delete ? - // plus utilis + if (!ReadWord(pf, w)) return false; // dont delete ? + // plus utilis - if (!ReadWord(pf, w)) return false; // pas pas - pStack->m_step = w; + if (!ReadWord(pf, w)) return false; // pas pas + pStack->m_step = w; - if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // la variable temp - if (!CBotVar::RestoreState(pf, pStack->m_listVar)) return false;// les variables locales + if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // la variable temp + if (!CBotVar::RestoreState(pf, pStack->m_listVar)) return false;// les variables locales - return pStack->RestoreState(pf, pStack->m_next); + return pStack->RestoreState(pf, pStack->m_next); } bool CBotVar::Save0State(FILE* pf) -{ - if (!WriteWord(pf, 100+m_mPrivate))return false; // variable prive ? - if (!WriteWord(pf, m_bStatic))return false; // variable static ? - if (!WriteWord(pf, m_type.GivType()))return false; // enregiste le type (toujours non nul) - if (!WriteWord(pf, m_binit))return false; // variable dfinie ? - return WriteString(pf, m_token->GivString()); // et le nom de la variable +{ + if (!WriteWord(pf, 100+m_mPrivate))return false; // variable prive ? + if (!WriteWord(pf, m_bStatic))return false; // variable static ? + if (!WriteWord(pf, m_type.GivType()))return false; // enregiste le type (toujours non nul) + if (!WriteWord(pf, m_binit))return false; // variable dfinie ? + return WriteString(pf, m_token->GivString()); // et le nom de la variable } bool CBotVarInt::Save0State(FILE* pf) -{ - if ( !m_defnum.IsEmpty() ) - { - if(!WriteWord(pf, 200 )) return false; // marqueur spcial - if(!WriteString(pf, m_defnum)) return false; // nom de la valeur - } +{ + if ( !m_defnum.IsEmpty() ) + { + if(!WriteWord(pf, 200 )) return false; // marqueur spcial + if(!WriteString(pf, m_defnum)) return false; // nom de la valeur + } - return CBotVar::Save0State(pf); + return CBotVar::Save0State(pf); } bool CBotVarInt::Save1State(FILE* pf) { - return WriteWord(pf, m_val); // la valeur de la variable + return WriteWord(pf, m_val); // la valeur de la variable } bool CBotVarBoolean::Save1State(FILE* pf) { - return WriteWord(pf, m_val); // la valeur de la variable + return WriteWord(pf, m_val); // la valeur de la variable } bool CBotVarFloat::Save1State(FILE* pf) { - return WriteFloat(pf, m_val); // la valeur de la variable + return WriteFloat(pf, m_val); // la valeur de la variable } bool CBotVarString::Save1State(FILE* pf) { - return WriteString(pf, m_val); // la valeur de la variable + return WriteString(pf, m_val); // la valeur de la variable } bool CBotVarClass::Save1State(FILE* pf) { - if ( !WriteType(pf, m_type) ) return false; - if ( !WriteLong(pf, m_ItemIdent) ) return false; + if ( !WriteType(pf, m_type) ) return false; + if ( !WriteLong(pf, m_ItemIdent) ) return false; - return SaveVar(pf, m_pVar); // contenu de l'objet + return SaveVar(pf, m_pVar); // contenu de l'objet } bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) { - unsigned short w, wi, prv, st; - float ww; - CBotString name, s; - - delete pVar; - - pVar = NULL; - CBotVar* pNew = NULL; - CBotVar* pPrev = NULL; - - while ( true ) // recupre toute une liste - { - if (!ReadWord(pf, w)) return false; // priv ou type ? - if ( w == 0 ) return true; - - CBotString defnum; - if ( w == 200 ) - { - if (!ReadString(pf, defnum)) return false; // nombre avec un identifiant - if (!ReadWord(pf, w)) return false; // type - } - - prv = 100; st = 0; - if ( w >= 100 ) - { - prv = w; - if (!ReadWord(pf, st)) return false; // statique - if (!ReadWord(pf, w)) return false; // type - } - - if ( w == CBotTypClass ) w = CBotTypIntrinsic; // forcment intrinsque - - if (!ReadWord(pf, wi)) return false; // init ? - - if (!ReadString(pf, name)) return false; // nom de la variable - - CBotToken token(name, CBotString()); - - switch (w) - { - case CBotTypInt: - case CBotTypBoolean: - pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadWord(pf, w)) return false; - pNew->SetValInt((short)w, defnum); - break; - case CBotTypFloat: - pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadFloat(pf, ww)) return false; - pNew->SetValFloat(ww); - break; - case CBotTypString: - pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadString(pf, s)) return false; - pNew->SetValString(s); - break; - - // restitue un objet intrinsic ou un lment d'un array - case CBotTypIntrinsic: - case CBotTypArrayBody: - { - CBotTypResult r; - long id; - if (!ReadType(pf, r)) return false; // type complet - if (!ReadLong(pf, id) ) return false; - -// if (!ReadString(pf, s)) return false; - { - CBotVar* p = NULL; - if ( id ) p = CBotVarClass::Find(id) ; - - pNew = new CBotVarClass(&token, r); // cre directement une instance - // attention cptuse = 0 - if ( !RestoreState(pf, ((CBotVarClass*)pNew)->m_pVar)) return false; - pNew->SetIdent(id); - - if ( p != NULL ) - { - delete pNew; - pNew = p; // reprend l'lment connu - } - } - } - break; - - case CBotTypPointer: - case CBotTypNullPointer: - if (!ReadString(pf, s)) return false; - { - pNew = CBotVar::Create(&token, CBotTypResult(w, s));// cre une variable - CBotVarClass* p = NULL; - long id; - ReadLong(pf, id); -// if ( id ) p = CBotVarClass::Find(id); // retrouve l'instance ( fait par RestoreInstance ) - - // restitue une copie de l'instance d'origine - CBotVar* pInstance = NULL; - if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; - ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus - -// if ( p != NULL ) ((CBotVarPointer*)pNew)->SetPointer( p ); // plutt celui-ci ! - - } - break; - - case CBotTypArrayPointer: - { - CBotTypResult r; - if (!ReadType(pf, r)) return false; - - pNew = CBotVar::Create(&token, r); // cre une variable - - // restitue une copie de l'instance d'origine - CBotVar* pInstance = NULL; - if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; - ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus - } - break; - default: - ASM_TRAP(); - } - - if ( pPrev != NULL ) pPrev->m_next = pNew; - if ( pVar == NULL ) pVar = pNew; - - pNew->m_binit = wi; // pNew->SetInit(wi); - pNew->SetStatic(st); - pNew->SetPrivate(prv-100); - pPrev = pNew; - } - return true; + unsigned short w, wi, prv, st; + float ww; + CBotString name, s; + + delete pVar; + + pVar = NULL; + CBotVar* pNew = NULL; + CBotVar* pPrev = NULL; + + while ( true ) // recupre toute une liste + { + if (!ReadWord(pf, w)) return false; // priv ou type ? + if ( w == 0 ) return true; + + CBotString defnum; + if ( w == 200 ) + { + if (!ReadString(pf, defnum)) return false; // nombre avec un identifiant + if (!ReadWord(pf, w)) return false; // type + } + + prv = 100; st = 0; + if ( w >= 100 ) + { + prv = w; + if (!ReadWord(pf, st)) return false; // statique + if (!ReadWord(pf, w)) return false; // type + } + + if ( w == CBotTypClass ) w = CBotTypIntrinsic; // forcment intrinsque + + if (!ReadWord(pf, wi)) return false; // init ? + + if (!ReadString(pf, name)) return false; // nom de la variable + + CBotToken token(name, CBotString()); + + switch (w) + { + case CBotTypInt: + case CBotTypBoolean: + pNew = CBotVar::Create(&token, w); // cre une variable + if (!ReadWord(pf, w)) return false; + pNew->SetValInt((short)w, defnum); + break; + case CBotTypFloat: + pNew = CBotVar::Create(&token, w); // cre une variable + if (!ReadFloat(pf, ww)) return false; + pNew->SetValFloat(ww); + break; + case CBotTypString: + pNew = CBotVar::Create(&token, w); // cre une variable + if (!ReadString(pf, s)) return false; + pNew->SetValString(s); + break; + + // restitue un objet intrinsic ou un lment d'un array + case CBotTypIntrinsic: + case CBotTypArrayBody: + { + CBotTypResult r; + long id; + if (!ReadType(pf, r)) return false; // type complet + if (!ReadLong(pf, id) ) return false; + +// if (!ReadString(pf, s)) return false; + { + CBotVar* p = NULL; + if ( id ) p = CBotVarClass::Find(id) ; + + pNew = new CBotVarClass(&token, r); // cre directement une instance + // attention cptuse = 0 + if ( !RestoreState(pf, ((CBotVarClass*)pNew)->m_pVar)) return false; + pNew->SetIdent(id); + + if ( p != NULL ) + { + delete pNew; + pNew = p; // reprend l'lment connu + } + } + } + break; + + case CBotTypPointer: + case CBotTypNullPointer: + if (!ReadString(pf, s)) return false; + { + pNew = CBotVar::Create(&token, CBotTypResult(w, s));// cre une variable + CBotVarClass* p = NULL; + long id; + ReadLong(pf, id); +// if ( id ) p = CBotVarClass::Find(id); // retrouve l'instance ( fait par RestoreInstance ) + + // restitue une copie de l'instance d'origine + CBotVar* pInstance = NULL; + if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; + ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus + +// if ( p != NULL ) ((CBotVarPointer*)pNew)->SetPointer( p ); // plutt celui-ci ! + + } + break; + + case CBotTypArrayPointer: + { + CBotTypResult r; + if (!ReadType(pf, r)) return false; + + pNew = CBotVar::Create(&token, r); // cre une variable + + // restitue une copie de l'instance d'origine + CBotVar* pInstance = NULL; + if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; + ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus + } + break; + default: + ASM_TRAP(); + } + + if ( pPrev != NULL ) pPrev->m_next = pNew; + if ( pVar == NULL ) pVar = pNew; + + pNew->m_binit = wi; // pNew->SetInit(wi); + pNew->SetStatic(st); + pNew->SetPrivate(prv-100); + pPrev = pNew; + } + return true; } @@ -1123,11 +1112,11 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) // gestion de la pile la compilation //////////////////////////////////////////////////////////////////////////// -CBotProgram* CBotCStack::m_prog = NULL; // init la variable statique -int CBotCStack::m_error = 0; -int CBotCStack::m_end = 0; -CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); -//CBotToken* CBotCStack::m_retClass= NULL; +CBotProgram* CBotCStack::m_prog = NULL; // init la variable statique +int CBotCStack::m_error = 0; +int CBotCStack::m_end = 0; +CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); +//CBotToken* CBotCStack::m_retClass= NULL; CBotCStack::CBotCStack(CBotCStack* ppapa) @@ -1135,125 +1124,125 @@ CBotCStack::CBotCStack(CBotCStack* ppapa) m_next = NULL; m_prev = ppapa; - if (ppapa == NULL) - { - m_error = 0; - m_start = 0; - m_end = 0; - m_bBlock = true; - } - else - { - m_start = ppapa->m_start; - m_bBlock = false; - } + if (ppapa == NULL) + { + m_error = 0; + m_start = 0; + m_end = 0; + m_bBlock = true; + } + else + { + m_start = ppapa->m_start; + m_bBlock = false; + } - m_listVar = NULL; - m_var = NULL; + m_listVar = NULL; + m_var = NULL; } // destructeur CBotCStack::~CBotCStack() { - if (m_next != NULL) delete m_next; - if (m_prev != NULL) m_prev->m_next = NULL; // enlve de la chane + if (m_next != NULL) delete m_next; + if (m_prev != NULL) m_prev->m_next = NULL; // enlve de la chane - delete m_var; - delete m_listVar; + delete m_var; + delete m_listVar; } // utilis uniquement la compilation CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) { - if (m_next != NULL) return m_next; // reprise dans une pile existante + if (m_next != NULL) return m_next; // reprise dans une pile existante - CBotCStack* p = new CBotCStack(this); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; + CBotCStack* p = new CBotCStack(this); + m_next = p; // chane l'lment + p->m_bBlock = bBlock; - if (pToken != NULL) p->SetStartError(pToken->GivStart()); + if (pToken != NULL) p->SetStartError(pToken->GivStart()); - return p; + return p; } CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) -{ - if ( pfils == this ) return inst; +{ + if ( pfils == this ) return inst; - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable + if (m_var != NULL) delete m_var; // valeur remplace ? + m_var = pfils->m_var; // rsultat transmis + pfils->m_var = NULL; // ne pas dtruire la variable - if (m_error) - { - m_start = pfils->m_start; // rcupre la position de l'erreur - m_end = pfils->m_end; - } + if (m_error) + { + m_start = pfils->m_start; // rcupre la position de l'erreur + m_end = pfils->m_end; + } - delete pfils; - return inst; + delete pfils; + return inst; } CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) -{ - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable +{ + if (m_var != NULL) delete m_var; // valeur remplace ? + m_var = pfils->m_var; // rsultat transmis + pfils->m_var = NULL; // ne pas dtruire la variable - if (m_error) - { - m_start = pfils->m_start; // rcupre la position de l'erreur - m_end = pfils->m_end; - } + if (m_error) + { + m_start = pfils->m_start; // rcupre la position de l'erreur + m_end = pfils->m_end; + } - delete pfils; - return inst; + delete pfils; + return inst; } int CBotCStack::GivError(int& start, int& end) { - start = m_start; - end = m_end; - return m_error; + start = m_start; + end = m_end; + return m_error; } int CBotCStack::GivError() { - return m_error; + return m_error; } // type d'instruction sur la pile CBotTypResult CBotCStack::GivTypResult(int mode) { - if (m_var == NULL) - return CBotTypResult(99); - return m_var->GivTypResult(mode); + if (m_var == NULL) + return CBotTypResult(99); + return m_var->GivTypResult(mode); } // type d'instruction sur la pile int CBotCStack::GivType(int mode) { - if (m_var == NULL) - return 99; - return m_var->GivType(mode); + if (m_var == NULL) + return 99; + return m_var->GivType(mode); } // pointeur sur la pile est de quelle classe ? CBotClass* CBotCStack::GivClass() { - if ( m_var == NULL ) - return NULL; - if ( m_var->GivType(1) != CBotTypPointer ) return NULL; + if ( m_var == NULL ) + return NULL; + if ( m_var->GivType(1) != CBotTypPointer ) return NULL; - return m_var->GivClass(); + return m_var->GivClass(); } // type d'instruction sur la pile void CBotCStack::SetType(CBotTypResult& type) { - if (m_var == NULL) return; - m_var->SetType( type ); + if (m_var == NULL) return; + m_var->SetType( type ); } // cherche une variable sur la pile @@ -1262,40 +1251,40 @@ void CBotCStack::SetType(CBotTypResult& type) CBotVar* CBotCStack::FindVar(CBotToken* &pToken) { - CBotCStack* p = this; - CBotString name = pToken->GivString(); - - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (name == pp->GivName()) - { - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; + CBotCStack* p = this; + CBotString name = pToken->GivString(); + + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (name == pp->GivName()) + { + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; } CBotVar* CBotCStack::FindVar(CBotToken& Token) { - CBotToken* pt = &Token; - return FindVar(pt); + CBotToken* pt = &Token; + return FindVar(pt); } CBotVar* CBotCStack::CopyVar(CBotToken& Token) { - CBotVar* pVar = FindVar( Token ); + CBotVar* pVar = FindVar( Token ); - if ( pVar == NULL) return NULL; + if ( pVar == NULL) return NULL; - CBotVar* pCopy = CBotVar::Create( "", pVar->GivType() ); - pCopy->Copy(pVar); - return pCopy; + CBotVar* pCopy = CBotVar::Create( "", pVar->GivType() ); + pCopy->Copy(pVar); + return pCopy; } bool CBotCStack::IsOk() @@ -1306,100 +1295,100 @@ bool CBotCStack::IsOk() void CBotCStack::SetStartError( int pos ) { - if ( m_error != 0) return; // ne change pas une erreur dj existante - m_start = pos; + if ( m_error != 0) return; // ne change pas une erreur dj existante + m_start = pos; } void CBotCStack::SetError(int n, int pos) { - if ( n!= 0 && m_error != 0) return; // ne change pas une erreur dj existante - m_error = n; - m_end = pos; + if ( n!= 0 && m_error != 0) return; // ne change pas une erreur dj existante + m_error = n; + m_end = pos; } void CBotCStack::SetError(int n, CBotToken* p) { - if (m_error) return; // ne change pas une erreur dj existante - m_error = n; - m_start = p->GivStart(); - m_end = p->GivEnd(); + if (m_error) return; // ne change pas une erreur dj existante + m_error = n; + m_start = p->GivStart(); + m_end = p->GivEnd(); } void CBotCStack::ResetError(int n, int start, int end) { - m_error = n; - m_start = start; - m_end = end; + m_error = n; + m_start = start; + m_end = end; } bool CBotCStack::NextToken(CBotToken* &p) { - CBotToken* pp = p; + CBotToken* pp = p; - p = p->GivNext(); - if (p!=NULL) return true; + p = p->GivNext(); + if (p!=NULL) return true; - SetError(TX_ENDOF, pp->GivEnd()); - return false; + SetError(TX_ENDOF, pp->GivEnd()); + return false; } void CBotCStack::SetBotCall(CBotProgram* p) { - m_prog = p; + m_prog = p; } CBotProgram* CBotCStack::GivBotCall() { - return m_prog; + return m_prog; } void CBotCStack::SetRetType(CBotTypResult& type) { - m_retTyp = type; + m_retTyp = type; } CBotTypResult CBotCStack::GivRetType() { - return m_retTyp; + return m_retTyp; } void CBotCStack::SetVar( CBotVar* var ) { - if (m_var) delete m_var; // remplacement d'une variable - m_var = var; + if (m_var) delete m_var; // remplacement d'une variable + m_var = var; } // met sur le stack une copie d'une variable void CBotCStack::SetCopyVar( CBotVar* var ) { - if (m_var) delete m_var; // remplacement d'une variable + if (m_var) delete m_var; // remplacement d'une variable - if ( var == NULL ) return; - m_var = CBotVar::Create("", var->GivTypResult(2)); - m_var->Copy( var ); + if ( var == NULL ) return; + m_var = CBotVar::Create("", var->GivTypResult(2)); + m_var->Copy( var ); } CBotVar* CBotCStack::GivVar() { - return m_var; + return m_var; } void CBotCStack::AddVar(CBotVar* pVar) { - CBotCStack* p = this; + CBotCStack* p = this; - // revient sur l'lement pre - while (p != NULL && p->m_bBlock == 0) p = p->m_prev; + // revient sur l'lement pre + while (p != NULL && p->m_bBlock == 0) p = p->m_prev; - if ( p == NULL ) return; + if ( p == NULL ) return; - CBotVar** pp = &p->m_listVar; - while ( *pp != NULL ) pp = &(*pp)->m_next; + CBotVar** pp = &p->m_listVar; + while ( *pp != NULL ) pp = &(*pp)->m_next; - *pp = pVar; // ajoute la suite + *pp = pVar; // ajoute la suite -#ifdef _DEBUG - if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); +#ifdef _DEBUG + if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); #endif } @@ -1407,76 +1396,76 @@ void CBotCStack::AddVar(CBotVar* pVar) bool CBotCStack::CheckVarLocal(CBotToken* &pToken) { - CBotCStack* p = this; - CBotString name = pToken->GivString(); + CBotCStack* p = this; + CBotString name = pToken->GivString(); - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (name == pp->GivName()) - return true; - pp = pp->m_next; - } - if ( p->m_bBlock ) return false; - p = p->m_prev; - } - return false; + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (name == pp->GivName()) + return true; + pp = pp->m_next; + } + if ( p->m_bBlock ) return false; + p = p->m_prev; + } + return false; } CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) { - nIdent = 0; - CBotTypResult val(-1); + nIdent = 0; + CBotTypResult val(-1); - val = CBotCall::CompileCall(p, ppVars, this, nIdent); - if (val.GivType() < 0) - { - val = m_prog->GivFunctions()->CompileCall(p->GivString(), ppVars, nIdent); - if ( val.GivType() < 0 ) - { - // pVar = NULL; // l'erreur n'est pas sur un paramtre en particulier - SetError( -val.GivType(), p ); - val.SetType(-val.GivType()); - return val; - } - } - return val; + val = CBotCall::CompileCall(p, ppVars, this, nIdent); + if (val.GivType() < 0) + { + val = m_prog->GivFunctions()->CompileCall(p->GivString(), ppVars, nIdent); + if ( val.GivType() < 0 ) + { + // pVar = NULL; // l'erreur n'est pas sur un paramtre en particulier + SetError( -val.GivType(), p ); + val.SetType(-val.GivType()); + return val; + } + } + return val; } // test si un nom de procdure est dj dfini quelque part bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { - CBotString name = pToken->GivString(); - - if ( CBotCall::CheckCall(name) ) return true; - - CBotFunction* pp = m_prog->GivFunctions(); - while ( pp != NULL ) - { - if ( pToken->GivString() == pp->GivName() ) - { - // les paramtres sont-ils exactement les mmes ? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->Next(); - } - - pp = CBotFunction::m_listPublic; - while ( pp != NULL ) - { - if ( pToken->GivString() == pp->GivName() ) - { - // les paramtres sont-ils exactement les mmes ? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->m_nextpublic; - } - - return false; + CBotString name = pToken->GivString(); + + if ( CBotCall::CheckCall(name) ) return true; + + CBotFunction* pp = m_prog->GivFunctions(); + while ( pp != NULL ) + { + if ( pToken->GivString() == pp->GivName() ) + { + // les paramtres sont-ils exactement les mmes ? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->Next(); + } + + pp = CBotFunction::m_listPublic; + while ( pp != NULL ) + { + if ( pToken->GivString() == pp->GivName() ) + { + // les paramtres sont-ils exactement les mmes ? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->m_nextpublic; + } + + return false; } diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 33e1d04..9d5d257 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -23,7 +23,7 @@ #include //Map is filled with id-string pars that are needed for CBot language parsing -const std::map CBotString::s_keywordString = +const std::map CBotString::s_keywordString = { {ID_IF, "if"}, {ID_ELSE, "else"}, @@ -113,9 +113,9 @@ const std::map CBotString::s_keywordString = {ID_MODULO, "%"}, {ID_POWER, "**"}, {ID_ASSMODULO, "%="}, + {ID_SUPER, "super"}, {TX_UNDEF, "undefined"}, - {TX_NAN, "not a number"}, - {ID_SUPER, "super"} + {TX_NAN, "not a number"} }; CBotString::CBotString() diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 79ba021..9070298 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -27,80 +27,80 @@ long CBotVar::m_identcpt = 0; CBotVar::CBotVar( ) { - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = -1; - m_binit = false; - m_ident = 0; - m_bStatic = false; - m_mPrivate = 0; + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = -1; + m_binit = false; + m_ident = 0; + m_bStatic = false; + m_mPrivate = 0; } CBotVarInt::CBotVarInt( const CBotToken* name ) { - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypInt; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypInt; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; - m_val = 0; + m_val = 0; } CBotVarFloat::CBotVarFloat( const CBotToken* name ) { - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypFloat; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypFloat; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; - m_val = 0; + m_val = 0; } CBotVarString::CBotVarString( const CBotToken* name ) { - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypString; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypString; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; - m_val.Empty(); + m_val.Empty(); } CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) { - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypBoolean; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypBoolean; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; - m_val = 0; + m_val = 0; } CBotVarClass* CBotVarClass::m_ExClass = NULL; @@ -108,171 +108,171 @@ CBotVarClass* CBotVarClass::m_ExClass = NULL; CBotVarClass::CBotVarClass( const CBotToken* name, const CBotTypResult& type) { /* -// int nIdent = 0; - InitCBotVarClass( name, type ) //, nIdent ); +// int nIdent = 0; + InitCBotVarClass( name, type ) //, nIdent ); } CBotVarClass::CBotVarClass( const CBotToken* name, CBotTypResult& type) //, int &nIdent ) { - InitCBotVarClass( name, type ); //, nIdent ); + InitCBotVarClass( name, type ); //, nIdent ); } void CBotVarClass::InitCBotVarClass( const CBotToken* name, CBotTypResult& type ) //, int &nIdent ) {*/ - if ( !type.Eq(CBotTypClass) && - !type.Eq(CBotTypIntrinsic) && // par comodit accepte ces types - !type.Eq(CBotTypPointer) && - !type.Eq(CBotTypArrayPointer) && - !type.Eq(CBotTypArrayBody)) ASM_TRAP(); - - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = OBJECTCREATED;//NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_pVar = NULL; - m_type = type; - if ( type.Eq(CBotTypArrayPointer) ) m_type.SetType( CBotTypArrayBody ); - else if ( !type.Eq(CBotTypArrayBody) ) m_type.SetType( CBotTypClass ); - // type officel pour cet object - - m_pClass = NULL; - m_pParent = NULL; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; - m_bConstructor = false; - m_CptUse = 0; - m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); - - // se place tout seul dans la liste - if (m_ExClass) m_ExClass->m_ExPrev = this; - m_ExNext = m_ExClass; - m_ExPrev = NULL; - m_ExClass = this; - - CBotClass* pClass = type.GivClass(); - CBotClass* pClass2 = pClass->GivParent(); - if ( pClass2 != NULL ) - { - // cre galement une instance dans la classe pre - m_pParent = new CBotVarClass(name, CBotTypResult(type.GivType(),pClass2) ); //, nIdent); - } - - SetClass( pClass ); //, nIdent ); + if ( !type.Eq(CBotTypClass) && + !type.Eq(CBotTypIntrinsic) && // par comodit accepte ces types + !type.Eq(CBotTypPointer) && + !type.Eq(CBotTypArrayPointer) && + !type.Eq(CBotTypArrayBody)) ASM_TRAP(); + + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = OBJECTCREATED;//NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_pVar = NULL; + m_type = type; + if ( type.Eq(CBotTypArrayPointer) ) m_type.SetType( CBotTypArrayBody ); + else if ( !type.Eq(CBotTypArrayBody) ) m_type.SetType( CBotTypClass ); + // type officel pour cet object + + m_pClass = NULL; + m_pParent = NULL; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; + m_bConstructor = false; + m_CptUse = 0; + m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); + + // se place tout seul dans la liste + if (m_ExClass) m_ExClass->m_ExPrev = this; + m_ExNext = m_ExClass; + m_ExPrev = NULL; + m_ExClass = this; + + CBotClass* pClass = type.GivClass(); + CBotClass* pClass2 = pClass->GivParent(); + if ( pClass2 != NULL ) + { + // cre galement une instance dans la classe pre + m_pParent = new CBotVarClass(name, CBotTypResult(type.GivType(),pClass2) ); //, nIdent); + } + + SetClass( pClass ); //, nIdent ); } CBotVarClass::~CBotVarClass( ) { - if ( m_CptUse != 0 ) - ASM_TRAP(); + if ( m_CptUse != 0 ) + ASM_TRAP(); - if ( m_pParent ) delete m_pParent; - m_pParent = NULL; + if ( m_pParent ) delete m_pParent; + m_pParent = NULL; - // libre l'objet indirect s'il y a lieu -// if ( m_Indirect != NULL ) -// m_Indirect->DecrementUse(); + // libre l'objet indirect s'il y a lieu +// if ( m_Indirect != NULL ) +// m_Indirect->DecrementUse(); - // retire la classe de la liste - if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; - else m_ExClass = m_ExNext; + // retire la classe de la liste + if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; + else m_ExClass = m_ExNext; - if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; - m_ExPrev = NULL; - m_ExNext = NULL; + if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; + m_ExPrev = NULL; + m_ExNext = NULL; - delete m_pVar; + delete m_pVar; } void CBotVarClass::ConstructorSet() { - m_bConstructor = true; + m_bConstructor = true; } CBotVar::~CBotVar( ) { - delete m_token; - delete m_next; + delete m_token; + delete m_next; } void CBotVar::debug() { - const char* p = (const char*) m_token->GivString(); - CBotString s = (const char*) GivValString(); - const char* v = (const char*) s; + const char* p = (const char*) m_token->GivString(); + CBotString s = (const char*) GivValString(); + const char* v = (const char*) s; - if ( m_type.Eq(CBotTypClass) ) - { - CBotVar* pv = ((CBotVarClass*)this)->m_pVar; - while (pv != NULL) - { - pv->debug(); - pv = pv->GivNext(); - } - } + if ( m_type.Eq(CBotTypClass) ) + { + CBotVar* pv = ((CBotVarClass*)this)->m_pVar; + while (pv != NULL) + { + pv->debug(); + pv = pv->GivNext(); + } + } } void CBotVar::ConstructorSet() { - // nop + // nop } void CBotVar::SetUserPtr(void* pUser) { - m_pUserPtr = pUser; - if (m_type.Eq(CBotTypPointer) && - ((CBotVarPointer*)this)->m_pVarClass != NULL ) - ((CBotVarPointer*)this)->m_pVarClass->SetUserPtr(pUser); + m_pUserPtr = pUser; + if (m_type.Eq(CBotTypPointer) && + ((CBotVarPointer*)this)->m_pVarClass != NULL ) + ((CBotVarPointer*)this)->m_pVarClass->SetUserPtr(pUser); } void CBotVar::SetIdent(long n) { - if (m_type.Eq(CBotTypPointer) && - ((CBotVarPointer*)this)->m_pVarClass != NULL ) - ((CBotVarPointer*)this)->m_pVarClass->SetIdent(n); + if (m_type.Eq(CBotTypPointer) && + ((CBotVarPointer*)this)->m_pVarClass != NULL ) + ((CBotVarPointer*)this)->m_pVarClass->SetIdent(n); } void CBotVar::SetUniqNum(long n) { - m_ident = n; + m_ident = n; - if ( n == 0 ) ASM_TRAP(); + if ( n == 0 ) ASM_TRAP(); } long CBotVar::NextUniqNum() { - if (++m_identcpt < 10000) m_identcpt = 10000; - return m_identcpt; + if (++m_identcpt < 10000) m_identcpt = 10000; + return m_identcpt; } long CBotVar::GivUniqNum() { - return m_ident; + return m_ident; } void* CBotVar::GivUserPtr() { - return m_pUserPtr; + return m_pUserPtr; } bool CBotVar::Save1State(FILE* pf) { - // cette routine "virtual" ne doit jamais tre appelle, - // il doit y avoir une routine pour chaque classe fille (CBotVarInt, CBotVarFloat, etc) - // ( voir le type dans m_type ) - ASM_TRAP(); - return false; + // cette routine "virtual" ne doit jamais tre appelle, + // il doit y avoir une routine pour chaque classe fille (CBotVarInt, CBotVarFloat, etc) + // ( voir le type dans m_type ) + ASM_TRAP(); + return false; } void CBotVar::Maj(void* pUser, bool bContinu) { -/* if (!bContinu && m_pMyThis != NULL) - m_pMyThis->Maj(pUser, true);*/ +/* if (!bContinu && m_pMyThis != NULL) + m_pMyThis->Maj(pUser, true);*/ } @@ -280,382 +280,382 @@ void CBotVar::Maj(void* pUser, bool bContinu) CBotVar* CBotVar::Create(const CBotToken* name, int type ) { - CBotTypResult t(type); - return Create(name, t); + CBotTypResult t(type); + return Create(name, t); } CBotVar* CBotVar::Create(const CBotToken* name, CBotTypResult type) { - switch (type.GivType()) - { - case CBotTypShort: - case CBotTypInt: - return new CBotVarInt(name); - case CBotTypFloat: - return new CBotVarFloat(name); - case CBotTypBoolean: - return new CBotVarBoolean(name); - case CBotTypString: - return new CBotVarString(name); - case CBotTypPointer: - case CBotTypNullPointer: - return new CBotVarPointer(name, type); - case CBotTypIntrinsic: - return new CBotVarClass(name, type); - - case CBotTypClass: - // cre une nouvelle instance d'une classe - // et retourne le POINTER sur cette instance - { - CBotVarClass* instance = new CBotVarClass(name, type); - CBotVarPointer* pointer = new CBotVarPointer(name, type); - pointer->SetPointer( instance ); - return pointer; - } - - case CBotTypArrayPointer: - return new CBotVarArray(name, type); - - case CBotTypArrayBody: - { - CBotVarClass* instance = new CBotVarClass(name, type); - CBotVarArray* array = new CBotVarArray(name, type); - array->SetPointer( instance ); - - CBotVar* pv = array; - while (type.Eq(CBotTypArrayBody)) - { - type = type.GivTypElem(); - pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] - } - - return array; - } - } - - ASM_TRAP(); - return NULL; + switch (type.GivType()) + { + case CBotTypShort: + case CBotTypInt: + return new CBotVarInt(name); + case CBotTypFloat: + return new CBotVarFloat(name); + case CBotTypBoolean: + return new CBotVarBoolean(name); + case CBotTypString: + return new CBotVarString(name); + case CBotTypPointer: + case CBotTypNullPointer: + return new CBotVarPointer(name, type); + case CBotTypIntrinsic: + return new CBotVarClass(name, type); + + case CBotTypClass: + // cre une nouvelle instance d'une classe + // et retourne le POINTER sur cette instance + { + CBotVarClass* instance = new CBotVarClass(name, type); + CBotVarPointer* pointer = new CBotVarPointer(name, type); + pointer->SetPointer( instance ); + return pointer; + } + + case CBotTypArrayPointer: + return new CBotVarArray(name, type); + + case CBotTypArrayBody: + { + CBotVarClass* instance = new CBotVarClass(name, type); + CBotVarArray* array = new CBotVarArray(name, type); + array->SetPointer( instance ); + + CBotVar* pv = array; + while (type.Eq(CBotTypArrayBody)) + { + type = type.GivTypElem(); + pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] + } + + return array; + } + } + + ASM_TRAP(); + return NULL; } CBotVar* CBotVar::Create( CBotVar* pVar ) { - CBotVar* p = Create(pVar->m_token->GivString(), pVar->GivTypResult(2)); - return p; + CBotVar* p = Create(pVar->m_token->GivString(), pVar->GivTypResult(2)); + return p; } CBotVar* CBotVar::Create( const char* n, CBotTypResult type) { - CBotToken name(n); - - switch (type.GivType()) - { - case CBotTypShort: - case CBotTypInt: - return new CBotVarInt(&name); - case CBotTypFloat: - return new CBotVarFloat(&name); - case CBotTypBoolean: - return new CBotVarBoolean(&name); - case CBotTypString: - return new CBotVarString(&name); - case CBotTypPointer: - case CBotTypNullPointer: - { - CBotVarPointer* p = new CBotVarPointer(&name, type); -// p->SetClass(type.GivClass()); - return p; - } - case CBotTypIntrinsic: - { - CBotVarClass* p = new CBotVarClass(&name, type); -// p->SetClass(type.GivClass()); - return p; - } - - case CBotTypClass: - // cre une nouvelle instance d'une classe - // et retourne le POINTER sur cette instance - { - CBotVarClass* instance = new CBotVarClass(&name, type); - CBotVarPointer* pointer = new CBotVarPointer(&name, type); - pointer->SetPointer( instance ); -// pointer->SetClass( type.GivClass() ); - return pointer; - } - - case CBotTypArrayPointer: - return new CBotVarArray(&name, type); - - case CBotTypArrayBody: - { - CBotVarClass* instance = new CBotVarClass(&name, type); - CBotVarArray* array = new CBotVarArray(&name, type); - array->SetPointer( instance ); - - CBotVar* pv = array; - while (type.Eq(CBotTypArrayBody)) - { - type = type.GivTypElem(); - pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] - } - - return array; - } - } - - ASM_TRAP(); - return NULL; + CBotToken name(n); + + switch (type.GivType()) + { + case CBotTypShort: + case CBotTypInt: + return new CBotVarInt(&name); + case CBotTypFloat: + return new CBotVarFloat(&name); + case CBotTypBoolean: + return new CBotVarBoolean(&name); + case CBotTypString: + return new CBotVarString(&name); + case CBotTypPointer: + case CBotTypNullPointer: + { + CBotVarPointer* p = new CBotVarPointer(&name, type); +// p->SetClass(type.GivClass()); + return p; + } + case CBotTypIntrinsic: + { + CBotVarClass* p = new CBotVarClass(&name, type); +// p->SetClass(type.GivClass()); + return p; + } + + case CBotTypClass: + // cre une nouvelle instance d'une classe + // et retourne le POINTER sur cette instance + { + CBotVarClass* instance = new CBotVarClass(&name, type); + CBotVarPointer* pointer = new CBotVarPointer(&name, type); + pointer->SetPointer( instance ); +// pointer->SetClass( type.GivClass() ); + return pointer; + } + + case CBotTypArrayPointer: + return new CBotVarArray(&name, type); + + case CBotTypArrayBody: + { + CBotVarClass* instance = new CBotVarClass(&name, type); + CBotVarArray* array = new CBotVarArray(&name, type); + array->SetPointer( instance ); + + CBotVar* pv = array; + while (type.Eq(CBotTypArrayBody)) + { + type = type.GivTypElem(); + pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] + } + + return array; + } + } + + ASM_TRAP(); + return NULL; } CBotVar* CBotVar::Create( const char* name, int type, CBotClass* pClass) { - CBotToken token( name, "" ); - CBotVar* pVar = Create( &token, type ); - - if ( type == CBotTypPointer && pClass == NULL ) // pointeur "null" ? - return pVar; - - if ( type == CBotTypClass || type == CBotTypPointer || - type == CBotTypIntrinsic ) - { - if (pClass == NULL) - { - delete pVar; - return NULL; - } - pVar->SetClass( pClass ); - } - return pVar; + CBotToken token( name, "" ); + CBotVar* pVar = Create( &token, type ); + + if ( type == CBotTypPointer && pClass == NULL ) // pointeur "null" ? + return pVar; + + if ( type == CBotTypClass || type == CBotTypPointer || + type == CBotTypIntrinsic ) + { + if (pClass == NULL) + { + delete pVar; + return NULL; + } + pVar->SetClass( pClass ); + } + return pVar; } CBotVar* CBotVar::Create( const char* name, CBotClass* pClass) { - CBotToken token( name, "" ); - CBotVar* pVar = Create( &token, CBotTypResult( CBotTypClass, pClass ) ); -// pVar->SetClass( pClass ); - return pVar; + CBotToken token( name, "" ); + CBotVar* pVar = Create( &token, CBotTypResult( CBotTypClass, pClass ) ); +// pVar->SetClass( pClass ); + return pVar; } CBotTypResult CBotVar::GivTypResult(int mode) { - CBotTypResult r = m_type; + CBotTypResult r = m_type; - if ( mode == 1 && m_type.Eq(CBotTypClass) ) - r.SetType(CBotTypPointer); - if ( mode == 2 && m_type.Eq(CBotTypClass) ) - r.SetType(CBotTypIntrinsic); + if ( mode == 1 && m_type.Eq(CBotTypClass) ) + r.SetType(CBotTypPointer); + if ( mode == 2 && m_type.Eq(CBotTypClass) ) + r.SetType(CBotTypIntrinsic); - return r; + return r; } int CBotVar::GivType(int mode) { - if ( mode == 1 && m_type.Eq(CBotTypClass) ) - return CBotTypPointer; - if ( mode == 2 && m_type.Eq(CBotTypClass) ) - return CBotTypIntrinsic; - return m_type.GivType(); + if ( mode == 1 && m_type.Eq(CBotTypClass) ) + return CBotTypPointer; + if ( mode == 2 && m_type.Eq(CBotTypClass) ) + return CBotTypIntrinsic; + return m_type.GivType(); } void CBotVar::SetType(CBotTypResult& type) { - m_type = type; + m_type = type; } int CBotVar::GivInit() { - if ( m_type.Eq(CBotTypClass) ) return IS_DEF; // toujours dfini ! + if ( m_type.Eq(CBotTypClass) ) return IS_DEF; // toujours dfini ! - return m_binit; + return m_binit; } void CBotVar::SetInit(int bInit) { - m_binit = bInit; - if ( bInit == 2 ) m_binit = IS_DEF; // cas spcial - - if ( m_type.Eq(CBotTypPointer) && bInit == 2 ) - { - CBotVarClass* instance = GivPointer(); - if ( instance == NULL ) - { - instance = new CBotVarClass(NULL, m_type); -// instance->SetClass(((CBotVarPointer*)this)->m_pClass); - SetPointer(instance); - } - instance->SetInit(1); - } - - if ( m_type.Eq(CBotTypClass) || m_type.Eq(CBotTypIntrinsic) ) - { - CBotVar* p = ((CBotVarClass*)this)->m_pVar; - while( p != NULL ) - { - p->SetInit( bInit ); - p->m_pMyThis = (CBotVarClass*)this; - p = p->GivNext(); - } - } + m_binit = bInit; + if ( bInit == 2 ) m_binit = IS_DEF; // cas spcial + + if ( m_type.Eq(CBotTypPointer) && bInit == 2 ) + { + CBotVarClass* instance = GivPointer(); + if ( instance == NULL ) + { + instance = new CBotVarClass(NULL, m_type); +// instance->SetClass(((CBotVarPointer*)this)->m_pClass); + SetPointer(instance); + } + instance->SetInit(1); + } + + if ( m_type.Eq(CBotTypClass) || m_type.Eq(CBotTypIntrinsic) ) + { + CBotVar* p = ((CBotVarClass*)this)->m_pVar; + while( p != NULL ) + { + p->SetInit( bInit ); + p->m_pMyThis = (CBotVarClass*)this; + p = p->GivNext(); + } + } } CBotString CBotVar::GivName() { - return m_token->GivString(); + return m_token->GivString(); } void CBotVar::SetName(const char* name) { - m_token->SetString(name); + m_token->SetString(name); } CBotToken* CBotVar::GivToken() { - return m_token; + return m_token; } CBotVar* CBotVar::GivItem(const char* name) { - ASM_TRAP(); - return NULL; + ASM_TRAP(); + return NULL; } CBotVar* CBotVar::GivItemRef(int nIdent) { - ASM_TRAP(); - return NULL; + ASM_TRAP(); + return NULL; } CBotVar* CBotVar::GivItemList() { - ASM_TRAP(); - return NULL; + ASM_TRAP(); + return NULL; } CBotVar* CBotVar::GivItem(int row, bool bGrow) { - ASM_TRAP(); - return NULL; + ASM_TRAP(); + return NULL; } // dit si une variable appartient une classe donne bool CBotVar::IsElemOfClass(const char* name) { - CBotClass* pc = NULL; + CBotClass* pc = NULL; - if ( m_type.Eq(CBotTypPointer) ) - { - pc = ((CBotVarPointer*)this)->m_pClass; - } - if ( m_type.Eq(CBotTypClass) ) - { - pc = ((CBotVarClass*)this)->m_pClass; - } + if ( m_type.Eq(CBotTypPointer) ) + { + pc = ((CBotVarPointer*)this)->m_pClass; + } + if ( m_type.Eq(CBotTypClass) ) + { + pc = ((CBotVarClass*)this)->m_pClass; + } - while ( pc != NULL ) - { - if ( pc->GivName() == name ) return true; - pc = pc->GivParent(); - } + while ( pc != NULL ) + { + if ( pc->GivName() == name ) return true; + pc = pc->GivParent(); + } - return false; + return false; } CBotVar* CBotVar::GivStaticVar() { - // rend le pointeur la variable si elle est statique - if ( m_bStatic == 0 || m_pMyThis == NULL ) return this; + // rend le pointeur la variable si elle est statique + if ( m_bStatic == 0 || m_pMyThis == NULL ) return this; - CBotClass* pClass = m_pMyThis->GivClass(); - return pClass->GivItem( m_token->GivString() ); + CBotClass* pClass = m_pMyThis->GivClass(); + return pClass->GivItem( m_token->GivString() ); } CBotVar* CBotVar::GivNext() { - return m_next; + return m_next; } void CBotVar::AddNext(CBotVar* pVar) { - CBotVar* p = this; - while (p->m_next != NULL) p = p->m_next; + CBotVar* p = this; + while (p->m_next != NULL) p = p->m_next; - p->m_next = pVar; + p->m_next = pVar; } void CBotVar::SetVal(CBotVar* var) { - switch (/*var->*/GivType()) - { - case CBotTypBoolean: - SetValInt(var->GivValInt()); - break; - case CBotTypInt: - SetValInt(var->GivValInt(), ((CBotVarInt*)var)->m_defnum); - break; - case CBotTypFloat: - SetValFloat(var->GivValFloat()); - break; - case CBotTypString: - SetValString(var->GivValString()); - break; - case CBotTypPointer: - case CBotTypNullPointer: - case CBotTypArrayPointer: - SetPointer(var->GivPointer()); - break; - case CBotTypClass: - { - delete ((CBotVarClass*)this)->m_pVar; - ((CBotVarClass*)this)->m_pVar = NULL; - Copy(var, false); - } - break; - default: - ASM_TRAP(); - } - - m_binit = var->m_binit; // copie l'tat nan s'il y a + switch (/*var->*/GivType()) + { + case CBotTypBoolean: + SetValInt(var->GivValInt()); + break; + case CBotTypInt: + SetValInt(var->GivValInt(), ((CBotVarInt*)var)->m_defnum); + break; + case CBotTypFloat: + SetValFloat(var->GivValFloat()); + break; + case CBotTypString: + SetValString(var->GivValString()); + break; + case CBotTypPointer: + case CBotTypNullPointer: + case CBotTypArrayPointer: + SetPointer(var->GivPointer()); + break; + case CBotTypClass: + { + delete ((CBotVarClass*)this)->m_pVar; + ((CBotVarClass*)this)->m_pVar = NULL; + Copy(var, false); + } + break; + default: + ASM_TRAP(); + } + + m_binit = var->m_binit; // copie l'tat nan s'il y a } void CBotVar::SetStatic(bool bStatic) { - m_bStatic = bStatic; + m_bStatic = bStatic; } void CBotVar::SetPrivate(int mPrivate) { - m_mPrivate = mPrivate; + m_mPrivate = mPrivate; } bool CBotVar::IsStatic() { - return m_bStatic; + return m_bStatic; } bool CBotVar::IsPrivate(int mode) { - return m_mPrivate >= mode; + return m_mPrivate >= mode; } int CBotVar::GivPrivate() { - return m_mPrivate; + return m_mPrivate; } void CBotVar::SetPointer(CBotVar* pVarClass) { - ASM_TRAP(); + ASM_TRAP(); } CBotVarClass* CBotVar::GivPointer() { - ASM_TRAP(); - return NULL; + ASM_TRAP(); + return NULL; } // toutes ces fonctions doivent tre dfinies dans les classes filles @@ -663,174 +663,174 @@ CBotVarClass* CBotVar::GivPointer() int CBotVar::GivValInt() { - ASM_TRAP(); - return 0; + ASM_TRAP(); + return 0; } float CBotVar::GivValFloat() { - ASM_TRAP(); - return 0; + ASM_TRAP(); + return 0; } void CBotVar::SetValInt(int c, const char* s) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::SetValFloat(float c) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Mul(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Power(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } int CBotVar::Div(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return 0; + ASM_TRAP(); + return 0; } int CBotVar::Modulo(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return 0; + ASM_TRAP(); + return 0; } void CBotVar::Add(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Sub(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } bool CBotVar::Lo(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return false; + ASM_TRAP(); + return false; } bool CBotVar::Hi(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return false; + ASM_TRAP(); + return false; } bool CBotVar::Ls(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return false; + ASM_TRAP(); + return false; } bool CBotVar::Hs(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return false; + ASM_TRAP(); + return false; } bool CBotVar::Eq(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return false; + ASM_TRAP(); + return false; } bool CBotVar::Ne(CBotVar* left, CBotVar* right) { - ASM_TRAP(); - return false; + ASM_TRAP(); + return false; } void CBotVar::And(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Or(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::XOr(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::ASR(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::SR(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::SL(CBotVar* left, CBotVar* right) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Neg() { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Not() { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Inc() { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Dec() { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::Copy(CBotVar* pSrc, bool bName) { - ASM_TRAP(); + ASM_TRAP(); } void CBotVar::SetValString(const char* p) { - ASM_TRAP(); + ASM_TRAP(); } CBotString CBotVar::GivValString() { - ASM_TRAP(); - return CBotString(); + ASM_TRAP(); + return CBotString(); } void CBotVar::SetClass(CBotClass* pClass) { - ASM_TRAP(); + ASM_TRAP(); } CBotClass* CBotVar::GivClass() { - ASM_TRAP(); - return NULL; + ASM_TRAP(); + return NULL; } /* void CBotVar::SetIndirection(CBotVar* pVar) { - // nop, uniquement pour CBotVarPointer::SetIndirection + // nop, uniquement pour CBotVarPointer::SetIndirection } */ @@ -839,19 +839,19 @@ void CBotVar::SetIndirection(CBotVar* pVar) // copie une variable dans une autre void CBotVarInt::Copy(CBotVar* pSrc, bool bName) { - CBotVarInt* p = (CBotVarInt*)pSrc; + CBotVarInt* p = (CBotVarInt*)pSrc; - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; - m_pMyThis = NULL; - m_pUserPtr = p->m_pUserPtr; + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; + m_pMyThis = NULL; + m_pUserPtr = p->m_pUserPtr; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; - m_defnum = p->m_defnum; + m_defnum = p->m_defnum; } @@ -859,189 +859,189 @@ void CBotVarInt::Copy(CBotVar* pSrc, bool bName) void CBotVarInt::SetValInt(int val, const char* defnum) { - m_val = val; - m_binit = true; - m_defnum = defnum; + m_val = val; + m_binit = true; + m_defnum = defnum; } void CBotVarInt::SetValFloat(float val) { - m_val = (int)val; - m_binit = true; + m_val = (int)val; + m_binit = true; } int CBotVarInt::GivValInt() { - return m_val; + return m_val; } float CBotVarInt::GivValFloat() { - return (float)m_val; + return (float)m_val; } CBotString CBotVarInt::GivValString() { - if ( !m_defnum.IsEmpty() ) return m_defnum; + if ( !m_defnum.IsEmpty() ) return m_defnum; - CBotString res; + CBotString res; - if ( !m_binit ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } + if ( !m_binit ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } - char buffer[300]; - sprintf(buffer, "%d", m_val); - res = buffer; + char buffer[300]; + sprintf(buffer, "%d", m_val); + res = buffer; - return res; + return res; } void CBotVarInt::Mul(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() * right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() * right->GivValInt(); + m_binit = true; } void CBotVarInt::Power(CBotVar* left, CBotVar* right) { - m_val = (int) pow( (double) left->GivValInt() , (double) right->GivValInt() ); - m_binit = true; + m_val = (int) pow( (double) left->GivValInt() , (double) right->GivValInt() ); + m_binit = true; } int CBotVarInt::Div(CBotVar* left, CBotVar* right) { - int r = right->GivValInt(); - if ( r != 0 ) - { - m_val = left->GivValInt() / r; - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); + int r = right->GivValInt(); + if ( r != 0 ) + { + m_val = left->GivValInt() / r; + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); } int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) { - int r = right->GivValInt(); - if ( r != 0 ) - { - m_val = left->GivValInt() % r; - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); + int r = right->GivValInt(); + if ( r != 0 ) + { + m_val = left->GivValInt() % r; + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); } void CBotVarInt::Add(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() + right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() + right->GivValInt(); + m_binit = true; } void CBotVarInt::Sub(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() - right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() - right->GivValInt(); + m_binit = true; } void CBotVarInt::XOr(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() ^ right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() ^ right->GivValInt(); + m_binit = true; } void CBotVarInt::And(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() & right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() & right->GivValInt(); + m_binit = true; } void CBotVarInt::Or(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() | right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() | right->GivValInt(); + m_binit = true; } void CBotVarInt::SL(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() << right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() << right->GivValInt(); + m_binit = true; } void CBotVarInt::ASR(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() >> right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() >> right->GivValInt(); + m_binit = true; } void CBotVarInt::SR(CBotVar* left, CBotVar* right) { - int source = left->GivValInt(); - int shift = right->GivValInt(); - if (shift>=1) source &= 0x7fffffff; - m_val = source >> shift; - m_binit = true; + int source = left->GivValInt(); + int shift = right->GivValInt(); + if (shift>=1) source &= 0x7fffffff; + m_val = source >> shift; + m_binit = true; } void CBotVarInt::Neg() { - m_val = -m_val; + m_val = -m_val; } void CBotVarInt::Not() { - m_val = ~m_val; + m_val = ~m_val; } void CBotVarInt::Inc() { - m_val++; - m_defnum.Empty(); + m_val++; + m_defnum.Empty(); } void CBotVarInt::Dec() { - m_val--; - m_defnum.Empty(); + m_val--; + m_defnum.Empty(); } bool CBotVarInt::Lo(CBotVar* left, CBotVar* right) { - return left->GivValInt() < right->GivValInt(); + return left->GivValInt() < right->GivValInt(); } bool CBotVarInt::Hi(CBotVar* left, CBotVar* right) { - return left->GivValInt() > right->GivValInt(); + return left->GivValInt() > right->GivValInt(); } bool CBotVarInt::Ls(CBotVar* left, CBotVar* right) { - return left->GivValInt() <= right->GivValInt(); + return left->GivValInt() <= right->GivValInt(); } bool CBotVarInt::Hs(CBotVar* left, CBotVar* right) { - return left->GivValInt() >= right->GivValInt(); + return left->GivValInt() >= right->GivValInt(); } bool CBotVarInt::Eq(CBotVar* left, CBotVar* right) { - return left->GivValInt() == right->GivValInt(); + return left->GivValInt() == right->GivValInt(); } bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) { - return left->GivValInt() != right->GivValInt(); + return left->GivValInt() != right->GivValInt(); } @@ -1050,19 +1050,19 @@ bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) // copie une variable dans une autre void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) { - CBotVarFloat* p = (CBotVarFloat*)pSrc; + CBotVarFloat* p = (CBotVarFloat*)pSrc; - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; } @@ -1070,139 +1070,139 @@ void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) void CBotVarFloat::SetValInt(int val, const char* s) { - m_val = (float)val; - m_binit = true; + m_val = (float)val; + m_binit = true; } void CBotVarFloat::SetValFloat(float val) { - m_val = val; - m_binit = true; + m_val = val; + m_binit = true; } int CBotVarFloat::GivValInt() { - return (int)m_val; + return (int)m_val; } float CBotVarFloat::GivValFloat() { - return m_val; + return m_val; } CBotString CBotVarFloat::GivValString() { - CBotString res; + CBotString res; - if ( !m_binit ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } + if ( !m_binit ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } - char buffer[300]; - sprintf(buffer, "%.2f", m_val); - res = buffer; + char buffer[300]; + sprintf(buffer, "%.2f", m_val); + res = buffer; - return res; + return res; } void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) { - m_val = left->GivValFloat() * right->GivValFloat(); - m_binit = true; + m_val = left->GivValFloat() * right->GivValFloat(); + m_binit = true; } void CBotVarFloat::Power(CBotVar* left, CBotVar* right) { - m_val = (float)pow( left->GivValFloat() , right->GivValFloat() ); - m_binit = true; + m_val = (float)pow( left->GivValFloat() , right->GivValFloat() ); + m_binit = true; } int CBotVarFloat::Div(CBotVar* left, CBotVar* right) { - float r = right->GivValFloat(); - if ( r != 0 ) - { - m_val = left->GivValFloat() / r; - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); + float r = right->GivValFloat(); + if ( r != 0 ) + { + m_val = left->GivValFloat() / r; + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); } int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) { - float r = right->GivValFloat(); - if ( r != 0 ) - { - m_val = (float)fmod( left->GivValFloat() , r ); - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); + float r = right->GivValFloat(); + if ( r != 0 ) + { + m_val = (float)fmod( left->GivValFloat() , r ); + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); } void CBotVarFloat::Add(CBotVar* left, CBotVar* right) { - m_val = left->GivValFloat() + right->GivValFloat(); - m_binit = true; + m_val = left->GivValFloat() + right->GivValFloat(); + m_binit = true; } void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) { - m_val = left->GivValFloat() - right->GivValFloat(); - m_binit = true; + m_val = left->GivValFloat() - right->GivValFloat(); + m_binit = true; } void CBotVarFloat::Neg() { - m_val = -m_val; + m_val = -m_val; } void CBotVarFloat::Inc() { - m_val++; + m_val++; } void CBotVarFloat::Dec() { - m_val--; + m_val--; } bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) { - return left->GivValFloat() < right->GivValFloat(); + return left->GivValFloat() < right->GivValFloat(); } bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) { - return left->GivValFloat() > right->GivValFloat(); + return left->GivValFloat() > right->GivValFloat(); } bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) { - return left->GivValFloat() <= right->GivValFloat(); + return left->GivValFloat() <= right->GivValFloat(); } bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) { - return left->GivValFloat() >= right->GivValFloat(); + return left->GivValFloat() >= right->GivValFloat(); } bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) { - return left->GivValFloat() == right->GivValFloat(); + return left->GivValFloat() == right->GivValFloat(); } bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) { - return left->GivValFloat() != right->GivValFloat(); + return left->GivValFloat() != right->GivValFloat(); } @@ -1211,19 +1211,19 @@ bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) // copie une variable dans une autre void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) { - CBotVarBoolean* p = (CBotVarBoolean*)pSrc; + CBotVarBoolean* p = (CBotVarBoolean*)pSrc; - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; } @@ -1231,77 +1231,77 @@ void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) void CBotVarBoolean::SetValInt(int val, const char* s) { - m_val = (bool)val; - m_binit = true; + m_val = (bool)val; + m_binit = true; } void CBotVarBoolean::SetValFloat(float val) { - m_val = (bool)val; - m_binit = true; + m_val = (bool)val; + m_binit = true; } int CBotVarBoolean::GivValInt() { - return m_val; + return m_val; } float CBotVarBoolean::GivValFloat() { - return (float)m_val; + return (float)m_val; } CBotString CBotVarBoolean::GivValString() { - CBotString ret; + CBotString ret; - CBotString res; + CBotString res; - if ( !m_binit ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } + if ( !m_binit ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } - ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); - return ret; + ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); + return ret; } void CBotVarBoolean::And(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() && right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() && right->GivValInt(); + m_binit = true; } void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() || right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() || right->GivValInt(); + m_binit = true; } void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) { - m_val = left->GivValInt() ^ right->GivValInt(); - m_binit = true; + m_val = left->GivValInt() ^ right->GivValInt(); + m_binit = true; } void CBotVarBoolean::Not() { - m_val = m_val ? false : true ; + m_val = m_val ? false : true ; } bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) { - return left->GivValInt() == right->GivValInt(); + return left->GivValInt() == right->GivValInt(); } bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) { - return left->GivValInt() != right->GivValInt(); + return left->GivValInt() != right->GivValInt(); } ////////////////////////////////////////////////////////////////////////////////////// @@ -1309,82 +1309,82 @@ bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) // copie une variable dans une autre void CBotVarString::Copy(CBotVar* pSrc, bool bName) { - CBotVarString* p = (CBotVarString*)pSrc; + CBotVarString* p = (CBotVarString*)pSrc; - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; } void CBotVarString::SetValString(const char* p) { - m_val = p; - m_binit = true; + m_val = p; + m_binit = true; } CBotString CBotVarString::GivValString() { - if ( !m_binit ) - { - CBotString res; - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - CBotString res; - res.LoadString(TX_NAN); - return res; - } + if ( !m_binit ) + { + CBotString res; + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + CBotString res; + res.LoadString(TX_NAN); + return res; + } - return m_val; + return m_val; } void CBotVarString::Add(CBotVar* left, CBotVar* right) { - m_val = left->GivValString() + right->GivValString(); - m_binit = true; + m_val = left->GivValString() + right->GivValString(); + m_binit = true; } bool CBotVarString::Eq(CBotVar* left, CBotVar* right) { - return (left->GivValString() == right->GivValString()); + return (left->GivValString() == right->GivValString()); } bool CBotVarString::Ne(CBotVar* left, CBotVar* right) { - return (left->GivValString() != right->GivValString()); + return (left->GivValString() != right->GivValString()); } bool CBotVarString::Lo(CBotVar* left, CBotVar* right) { - return (left->GivValString() == right->GivValString()); + return (left->GivValString() == right->GivValString()); } bool CBotVarString::Hi(CBotVar* left, CBotVar* right) { - return (left->GivValString() == right->GivValString()); + return (left->GivValString() == right->GivValString()); } bool CBotVarString::Ls(CBotVar* left, CBotVar* right) { - return (left->GivValString() == right->GivValString()); + return (left->GivValString() == right->GivValString()); } bool CBotVarString::Hs(CBotVar* left, CBotVar* right) { - return (left->GivValString() == right->GivValString()); + return (left->GivValString() == right->GivValString()); } @@ -1393,177 +1393,177 @@ bool CBotVarString::Hs(CBotVar* left, CBotVar* right) // copie une variable dans une autre void CBotVarClass::Copy(CBotVar* pSrc, bool bName) { - pSrc = pSrc->GivPointer(); // si source donn par un pointeur + pSrc = pSrc->GivPointer(); // si source donn par un pointeur - if ( pSrc->GivType() != CBotTypClass ) - ASM_TRAP(); + if ( pSrc->GivType() != CBotTypClass ) + ASM_TRAP(); - CBotVarClass* p = (CBotVarClass*)pSrc; + CBotVarClass* p = (CBotVarClass*)pSrc; - if (bName) *m_token = *p->m_token; + if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_pClass = p->m_pClass; - if ( p->m_pParent ) - { - ASM_TRAP(); "que faire du pParent"; - } + m_type = p->m_type; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_pClass = p->m_pClass; + if ( p->m_pParent ) + { + ASM_TRAP(); "que faire du pParent"; + } -// m_next = NULL; - m_pUserPtr = p->m_pUserPtr; - m_pMyThis = NULL;//p->m_pMyThis; - m_ItemIdent = p->m_ItemIdent; +// m_next = NULL; + m_pUserPtr = p->m_pUserPtr; + m_pMyThis = NULL;//p->m_pMyThis; + m_ItemIdent = p->m_ItemIdent; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; - delete m_pVar; - m_pVar = NULL; + delete m_pVar; + m_pVar = NULL; - CBotVar* pv = p->m_pVar; - while( pv != NULL ) - { - CBotVar* pn = CBotVar::Create(pv); - pn->Copy( pv ); - if ( m_pVar == NULL ) m_pVar = pn; - else m_pVar->AddNext(pn); + CBotVar* pv = p->m_pVar; + while( pv != NULL ) + { + CBotVar* pn = CBotVar::Create(pv); + pn->Copy( pv ); + if ( m_pVar == NULL ) m_pVar = pn; + else m_pVar->AddNext(pn); - pv = pv->GivNext(); - } + pv = pv->GivNext(); + } } void CBotVarClass::SetItemList(CBotVar* pVar) { - delete m_pVar; - m_pVar = pVar; // remplace le pointeur existant + delete m_pVar; + m_pVar = pVar; // remplace le pointeur existant } void CBotVarClass::SetIdent(long n) { - m_ItemIdent = n; + m_ItemIdent = n; } void CBotVarClass::SetClass(CBotClass* pClass)//, int &nIdent) { - m_type.m_pClass = pClass; + m_type.m_pClass = pClass; - if ( m_pClass == pClass ) return; + if ( m_pClass == pClass ) return; - m_pClass = pClass; + m_pClass = pClass; - // initialise les variables associes cette classe - delete m_pVar; - m_pVar = NULL; + // initialise les variables associes cette classe + delete m_pVar; + m_pVar = NULL; - if (pClass == NULL) return; + if (pClass == NULL) return; - CBotVar* pv = pClass->GivVar(); // premier de la liste - while ( pv != NULL ) - { - // cherche les dimensions max du tableau - CBotInstr* p = pv->m_LimExpr; // les diffrentes formules - if ( p != NULL ) - { - CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante - int n = 0; - int max[100]; + CBotVar* pv = pClass->GivVar(); // premier de la liste + while ( pv != NULL ) + { + // cherche les dimensions max du tableau + CBotInstr* p = pv->m_LimExpr; // les diffrentes formules + if ( p != NULL ) + { + CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante + int n = 0; + int max[100]; - while (p != NULL) - { - while( pile->IsOk() && !p->Execute(pile) ) ; // calcul de la taille sans interruptions - CBotVar* v = pile->GivVar(); // rsultat - max[n] = v->GivValInt(); // valeur - n++; - p = p->GivNext3(); - } - while (n<100) max[n++] = 0; + while (p != NULL) + { + while( pile->IsOk() && !p->Execute(pile) ) ; // calcul de la taille sans interruptions + CBotVar* v = pile->GivVar(); // rsultat + max[n] = v->GivValInt(); // valeur + n++; + p = p->GivNext3(); + } + while (n<100) max[n++] = 0; - pv->m_type.SetArray( max ); // mmorise les limitations - pile->Delete(); - } + pv->m_type.SetArray( max ); // mmorise les limitations + pile->Delete(); + } - CBotVar* pn = CBotVar::Create( pv ); // une copie - pn->SetStatic(pv->IsStatic()); - pn->SetPrivate(pv->GivPrivate()); + CBotVar* pn = CBotVar::Create( pv ); // une copie + pn->SetStatic(pv->IsStatic()); + pn->SetPrivate(pv->GivPrivate()); - if ( pv->m_InitExpr != NULL ) // expression pour l'initialisation ? - { -#if STACKMEM - CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante + if ( pv->m_InitExpr != NULL ) // expression pour l'initialisation ? + { +#if STACKMEM + CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante - while(pile->IsOk() && !pv->m_InitExpr->Execute(pile, pn)); // value l'expression sans timer + while(pile->IsOk() && !pv->m_InitExpr->Execute(pile, pn)); // value l'expression sans timer - pile->Delete(); + pile->Delete(); #else - CBotStack* pile = new CBotStack(NULL); // une pile indpendante - while(!pv->m_InitExpr->Execute(pile)); // value l'expression sans timer - pn->SetVal( pile->GivVar() ) ; - delete pile; + CBotStack* pile = new CBotStack(NULL); // une pile indpendante + while(!pv->m_InitExpr->Execute(pile)); // value l'expression sans timer + pn->SetVal( pile->GivVar() ) ; + delete pile; #endif - } + } -// pn->SetUniqNum(CBotVar::NextUniqNum()); // numrote les lments - pn->SetUniqNum(pv->GivUniqNum()); //++nIdent - pn->m_pMyThis = this; +// pn->SetUniqNum(CBotVar::NextUniqNum()); // numrote les lments + pn->SetUniqNum(pv->GivUniqNum()); //++nIdent + pn->m_pMyThis = this; - if ( m_pVar == NULL) m_pVar = pn; - else m_pVar->AddNext( pn ); - pv = pv->GivNext(); - } + if ( m_pVar == NULL) m_pVar = pn; + else m_pVar->AddNext( pn ); + pv = pv->GivNext(); + } } CBotClass* CBotVarClass::GivClass() { - return m_pClass; + return m_pClass; } void CBotVarClass::Maj(void* pUser, bool bContinu) { -/* if (!bContinu && m_pMyThis != NULL) - m_pMyThis->Maj(pUser, true);*/ +/* if (!bContinu && m_pMyThis != NULL) + m_pMyThis->Maj(pUser, true);*/ - // une routine de mise jour existe-elle ? + // une routine de mise jour existe-elle ? - if ( m_pClass->m_rMaj == NULL ) return; + if ( m_pClass->m_rMaj == NULL ) return; - // rcupre le pointeur user selon la classe - // ou selon le paramtre pass au CBotProgram::Run() + // rcupre le pointeur user selon la classe + // ou selon le paramtre pass au CBotProgram::Run() - if ( m_pUserPtr != NULL) pUser = m_pUserPtr; - if ( pUser == OBJECTDELETED || - pUser == OBJECTCREATED ) return; - m_pClass->m_rMaj( this, pUser ); + if ( m_pUserPtr != NULL) pUser = m_pUserPtr; + if ( pUser == OBJECTDELETED || + pUser == OBJECTCREATED ) return; + m_pClass->m_rMaj( this, pUser ); } CBotVar* CBotVarClass::GivItem(const char* name) { - CBotVar* p = m_pVar; + CBotVar* p = m_pVar; - while ( p != NULL ) - { - if ( p->GivName() == name ) return p; - p = p->GivNext(); - } + while ( p != NULL ) + { + if ( p->GivName() == name ) return p; + p = p->GivNext(); + } - if ( m_pParent != NULL ) return m_pParent->GivItem(name); - return NULL; + if ( m_pParent != NULL ) return m_pParent->GivItem(name); + return NULL; } CBotVar* CBotVarClass::GivItemRef(int nIdent) { - CBotVar* p = m_pVar; + CBotVar* p = m_pVar; - while ( p != NULL ) - { - if ( p->GivUniqNum() == nIdent ) return p; - p = p->GivNext(); - } + while ( p != NULL ) + { + if ( p->GivUniqNum() == nIdent ) return p; + p = p->GivNext(); + } - if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); - return NULL; + if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); + return NULL; } // pour la gestion d'un tableau @@ -1571,145 +1571,145 @@ CBotVar* CBotVarClass::GivItemRef(int nIdent) CBotVar* CBotVarClass::GivItem(int n, bool bExtend) { - CBotVar* p = m_pVar; + CBotVar* p = m_pVar; - if ( n < 0 ) return NULL; - if ( n > MAXARRAYSIZE ) return NULL; + if ( n < 0 ) return NULL; + if ( n > MAXARRAYSIZE ) return NULL; - if ( m_type.GivLimite() >= 0 && n >= m_type.GivLimite() ) return NULL; + if ( m_type.GivLimite() >= 0 && n >= m_type.GivLimite() ) return NULL; - if ( p == NULL && bExtend ) - { - p = CBotVar::Create("", m_type.GivTypElem()); - m_pVar = p; - } + if ( p == NULL && bExtend ) + { + p = CBotVar::Create("", m_type.GivTypElem()); + m_pVar = p; + } - if ( n == 0 ) return p; + if ( n == 0 ) return p; - while ( n-- > 0 ) - { - if ( p->m_next == NULL ) - { - if ( bExtend ) p->m_next = CBotVar::Create("", m_type.GivTypElem()); - if ( p->m_next == NULL ) return NULL; - } - p = p->m_next; - } + while ( n-- > 0 ) + { + if ( p->m_next == NULL ) + { + if ( bExtend ) p->m_next = CBotVar::Create("", m_type.GivTypElem()); + if ( p->m_next == NULL ) return NULL; + } + p = p->m_next; + } - return p; + return p; } CBotVar* CBotVarClass::GivItemList() { - return m_pVar; + return m_pVar; } CBotString CBotVarClass::GivValString() { -// if ( m_Indirect != NULL) return m_Indirect->GivValString(); - - CBotString res; - - if ( m_pClass != NULL ) // pas utilis pour un array - { - res = m_pClass->GivName() + CBotString("( "); - - CBotVarClass* my = this; - while ( my != NULL ) - { - CBotVar* pv = my->m_pVar; - while ( pv != NULL ) - { - res += pv->GivName() + CBotString("="); - - if ( pv->IsStatic() ) - { - CBotVar* pvv = my->m_pClass->GivItem(pv->GivName()); - res += pvv->GivValString(); - } - else - { - res += pv->GivValString(); - } - pv = pv->GivNext(); - if ( pv != NULL ) res += ", "; - } - my = my->m_pParent; - if ( my != NULL ) - { - res += ") extends "; - res += my->m_pClass->GivName(); - res += " ("; - } - } - } - else - { - res = "( "; - - CBotVar* pv = m_pVar; - while ( pv != NULL ) - { - res += pv->GivValString(); - if ( pv->GivNext() != NULL ) res += ", "; - pv = pv->GivNext(); - } - } - - res += " )"; - return res; +// if ( m_Indirect != NULL) return m_Indirect->GivValString(); + + CBotString res; + + if ( m_pClass != NULL ) // pas utilis pour un array + { + res = m_pClass->GivName() + CBotString("( "); + + CBotVarClass* my = this; + while ( my != NULL ) + { + CBotVar* pv = my->m_pVar; + while ( pv != NULL ) + { + res += pv->GivName() + CBotString("="); + + if ( pv->IsStatic() ) + { + CBotVar* pvv = my->m_pClass->GivItem(pv->GivName()); + res += pvv->GivValString(); + } + else + { + res += pv->GivValString(); + } + pv = pv->GivNext(); + if ( pv != NULL ) res += ", "; + } + my = my->m_pParent; + if ( my != NULL ) + { + res += ") extends "; + res += my->m_pClass->GivName(); + res += " ("; + } + } + } + else + { + res = "( "; + + CBotVar* pv = m_pVar; + while ( pv != NULL ) + { + res += pv->GivValString(); + if ( pv->GivNext() != NULL ) res += ", "; + pv = pv->GivNext(); + } + } + + res += " )"; + return res; } void CBotVarClass::IncrementUse() { - m_CptUse++; + m_CptUse++; } void CBotVarClass::DecrementUse() { - m_CptUse--; - if ( m_CptUse == 0 ) - { - // s'il y en a un, appel le destructeur - // mais seulement si un constructeur avait t appel. - if ( m_bConstructor ) - { - m_CptUse++; // ne revient pas dans le destructeur + m_CptUse--; + if ( m_CptUse == 0 ) + { + // s'il y en a un, appel le destructeur + // mais seulement si un constructeur avait t appel. + if ( m_bConstructor ) + { + m_CptUse++; // ne revient pas dans le destructeur - // m_error est static dans le stack - // sauve la valeur pour la remettre ensuite - int err, start, end; - CBotStack* pile = NULL; - err = pile->GivError(start,end); // pile == NULL a ne derange pas !! + // m_error est static dans le stack + // sauve la valeur pour la remettre ensuite + int err, start, end; + CBotStack* pile = NULL; + err = pile->GivError(start,end); // pile == NULL a ne derange pas !! - pile = CBotStack::FirstStack(); // efface l'erreur - CBotVar* ppVars[1]; - ppVars[0] = NULL; + pile = CBotStack::FirstStack(); // efface l'erreur + CBotVar* ppVars[1]; + ppVars[0] = NULL; - CBotVar* pThis = CBotVar::Create("this", CBotTypNullPointer); - pThis->SetPointer(this); - CBotVar* pResult = NULL; + CBotVar* pThis = CBotVar::Create("this", CBotTypNullPointer); + pThis->SetPointer(this); + CBotVar* pResult = NULL; - CBotString nom = "~" + m_pClass->GivName(); - long ident = 0; + CBotString nom = "~" + m_pClass->GivName(); + long ident = 0; - while ( pile->IsOk() && !m_pClass->ExecuteMethode(ident, nom, pThis, ppVars, pResult, pile, NULL)) ; // attend la fin + while ( pile->IsOk() && !m_pClass->ExecuteMethode(ident, nom, pThis, ppVars, pResult, pile, NULL)) ; // attend la fin - pile->ResetError(err, start,end); + pile->ResetError(err, start,end); - pile->Delete(); - delete pThis; - m_CptUse--; - } + pile->Delete(); + delete pThis; + m_CptUse--; + } - delete this; // s'auto-dtruit !! - } + delete this; // s'auto-dtruit !! + } } CBotVarClass* CBotVarClass::GivPointer() { - return this; + return this; } @@ -1717,47 +1717,47 @@ CBotVarClass* CBotVarClass::GivPointer() CBotVarClass* CBotVarClass::Find(long id) { - CBotVarClass* p = m_ExClass; + CBotVarClass* p = m_ExClass; - while ( p != NULL ) - { - if ( p->m_ItemIdent == id ) return p; - p = p->m_ExNext; - } + while ( p != NULL ) + { + if ( p->m_ItemIdent == id ) return p; + p = p->m_ExNext; + } - return NULL; + return NULL; } bool CBotVarClass::Eq(CBotVar* left, CBotVar* right) { - CBotVar* l = left->GivItemList(); - CBotVar* r = right->GivItemList(); + CBotVar* l = left->GivItemList(); + CBotVar* r = right->GivItemList(); - while ( l != NULL && r != NULL ) - { - if ( l->Ne(l, r) ) return false; - l = l->GivNext(); - r = r->GivNext(); - } + while ( l != NULL && r != NULL ) + { + if ( l->Ne(l, r) ) return false; + l = l->GivNext(); + r = r->GivNext(); + } - // devrait toujours arriv simultanment au bout (mmes classes) - return l == r; + // devrait toujours arriv simultanment au bout (mmes classes) + return l == r; } bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) { - CBotVar* l = left->GivItemList(); - CBotVar* r = right->GivItemList(); + CBotVar* l = left->GivItemList(); + CBotVar* r = right->GivItemList(); - while ( l != NULL && r != NULL ) - { - if ( l->Ne(l, r) ) return true; - l = l->GivNext(); - r = r->GivNext(); - } + while ( l != NULL && r != NULL ) + { + if ( l->Ne(l, r) ) return true; + l = l->GivNext(); + r = r->GivNext(); + } - // devrait toujours arriv simultanment au bout (mmes classes) - return l != r; + // devrait toujours arriv simultanment au bout (mmes classes) + return l != r; } ///////////////////////////////////////////////////////////////////////////// @@ -1765,109 +1765,109 @@ bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type ) { - if ( !type.Eq(CBotTypArrayPointer) && - !type.Eq(CBotTypArrayBody)) ASM_TRAP(); + if ( !type.Eq(CBotTypArrayPointer) && + !type.Eq(CBotTypArrayBody)) ASM_TRAP(); - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; - m_type = type; - m_type.SetType(CBotTypArrayPointer); - m_binit = false; + m_type = type; + m_type.SetType(CBotTypArrayPointer); + m_binit = false; - m_pInstance = NULL; // la liste des lments du tableau + m_pInstance = NULL; // la liste des lments du tableau } CBotVarArray::~CBotVarArray() { - if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); // une rfrence en moins + if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); // une rfrence en moins } // copie une variable dans une autre void CBotVarArray::Copy(CBotVar* pSrc, bool bName) { - if ( pSrc->GivType() != CBotTypArrayPointer ) - ASM_TRAP(); + if ( pSrc->GivType() != CBotTypArrayPointer ) + ASM_TRAP(); - CBotVarArray* p = (CBotVarArray*)pSrc; + CBotVarArray* p = (CBotVarArray*)pSrc; - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; - m_pInstance = p->GivPointer(); + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; + m_pInstance = p->GivPointer(); - if ( m_pInstance != NULL ) - m_pInstance->IncrementUse(); // une rfrence en plus + if ( m_pInstance != NULL ) + m_pInstance->IncrementUse(); // une rfrence en plus - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; } void CBotVarArray::SetPointer(CBotVar* pVarClass) { - m_binit = true; // init, mme sur un pointeur null + m_binit = true; // init, mme sur un pointeur null - if ( m_pInstance == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter - // car le dcrment peut dtruire l'object + if ( m_pInstance == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter + // car le dcrment peut dtruire l'object - if ( pVarClass != NULL ) - { - if ( pVarClass->GivType() == CBotTypArrayPointer ) - pVarClass = pVarClass->GivPointer(); // le vrai pointeur l'objet + if ( pVarClass != NULL ) + { + if ( pVarClass->GivType() == CBotTypArrayPointer ) + pVarClass = pVarClass->GivPointer(); // le vrai pointeur l'objet - if ( !pVarClass->m_type.Eq(CBotTypClass) && - !pVarClass->m_type.Eq(CBotTypArrayBody)) - ASM_TRAP(); + if ( !pVarClass->m_type.Eq(CBotTypClass) && + !pVarClass->m_type.Eq(CBotTypArrayBody)) + ASM_TRAP(); - ((CBotVarClass*)pVarClass)->IncrementUse(); // une rfrence en plus - } + ((CBotVarClass*)pVarClass)->IncrementUse(); // une rfrence en plus + } - if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); - m_pInstance = (CBotVarClass*)pVarClass; + if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); + m_pInstance = (CBotVarClass*)pVarClass; } CBotVarClass* CBotVarArray::GivPointer() { - if ( m_pInstance == NULL ) return NULL; - return m_pInstance->GivPointer(); + if ( m_pInstance == NULL ) return NULL; + return m_pInstance->GivPointer(); } CBotVar* CBotVarArray::GivItem(int n, bool bExtend) { - if ( m_pInstance == NULL ) - { - if ( !bExtend ) return NULL; - // cre une instance pour le tableau + if ( m_pInstance == NULL ) + { + if ( !bExtend ) return NULL; + // cre une instance pour le tableau - CBotVarClass* instance = new CBotVarClass(NULL, m_type); - SetPointer( instance ); - } - return m_pInstance->GivItem(n, bExtend); + CBotVarClass* instance = new CBotVarClass(NULL, m_type); + SetPointer( instance ); + } + return m_pInstance->GivItem(n, bExtend); } CBotVar* CBotVarArray::GivItemList() { - if ( m_pInstance == NULL) return NULL; - return m_pInstance->GivItemList(); + if ( m_pInstance == NULL) return NULL; + return m_pInstance->GivItemList(); } CBotString CBotVarArray::GivValString() { - if ( m_pInstance == NULL ) return ( CBotString( "Null pointer" ) ) ; - return m_pInstance->GivValString(); + if ( m_pInstance == NULL ) return ( CBotString( "Null pointer" ) ) ; + return m_pInstance->GivValString(); } bool CBotVarArray::Save1State(FILE* pf) { - if ( !WriteType(pf, m_type) ) return false; - return SaveVar(pf, m_pInstance); // sauve l'instance qui gre le tableau + if ( !WriteType(pf, m_type) ) return false; + return SaveVar(pf, m_pInstance); // sauve l'instance qui gre le tableau } @@ -1876,204 +1876,204 @@ bool CBotVarArray::Save1State(FILE* pf) CBotVarPointer::CBotVarPointer(const CBotToken* name, CBotTypResult& type ) { - if ( !type.Eq(CBotTypPointer) && - !type.Eq(CBotTypNullPointer) && - !type.Eq(CBotTypClass) && // par commodit accepte Class et Intrinsic - !type.Eq(CBotTypIntrinsic) ) ASM_TRAP(); + if ( !type.Eq(CBotTypPointer) && + !type.Eq(CBotTypNullPointer) && + !type.Eq(CBotTypClass) && // par commodit accepte Class et Intrinsic + !type.Eq(CBotTypIntrinsic) ) ASM_TRAP(); - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; - m_type = type; - if ( !type.Eq(CBotTypNullPointer) ) - m_type.SetType(CBotTypPointer); // quoi qu'il en soit, c'est un pointeur - m_binit = false; - m_pClass = NULL; - m_pVarClass = NULL; // sera dfini par un SetPointer() + m_type = type; + if ( !type.Eq(CBotTypNullPointer) ) + m_type.SetType(CBotTypPointer); // quoi qu'il en soit, c'est un pointeur + m_binit = false; + m_pClass = NULL; + m_pVarClass = NULL; // sera dfini par un SetPointer() - SetClass(type.GivClass() ); + SetClass(type.GivClass() ); } CBotVarPointer::~CBotVarPointer() { - if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); // une rfrence en moins + if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); // une rfrence en moins } void CBotVarPointer::Maj(void* pUser, bool bContinu) { -/* if ( !bContinu && m_pMyThis != NULL ) - m_pMyThis->Maj(pUser, false);*/ +/* if ( !bContinu && m_pMyThis != NULL ) + m_pMyThis->Maj(pUser, false);*/ - if ( m_pVarClass != NULL) m_pVarClass->Maj(pUser, false); + if ( m_pVarClass != NULL) m_pVarClass->Maj(pUser, false); } CBotVar* CBotVarPointer::GivItem(const char* name) { - if ( m_pVarClass == NULL) // pas d'instance existant ? - return m_pClass->GivItem(name); // rend le pointeur dans la classe elle-mme + if ( m_pVarClass == NULL) // pas d'instance existant ? + return m_pClass->GivItem(name); // rend le pointeur dans la classe elle-mme - return m_pVarClass->GivItem(name); + return m_pVarClass->GivItem(name); } CBotVar* CBotVarPointer::GivItemRef(int nIdent) { - if ( m_pVarClass == NULL) // pas d'instance existant ? - return m_pClass->GivItemRef(nIdent);// rend le pointeur dans la classe elle-mme + if ( m_pVarClass == NULL) // pas d'instance existant ? + return m_pClass->GivItemRef(nIdent);// rend le pointeur dans la classe elle-mme - return m_pVarClass->GivItemRef(nIdent); + return m_pVarClass->GivItemRef(nIdent); } CBotVar* CBotVarPointer::GivItemList() { - if ( m_pVarClass == NULL) return NULL; - return m_pVarClass->GivItemList(); + if ( m_pVarClass == NULL) return NULL; + return m_pVarClass->GivItemList(); } CBotString CBotVarPointer::GivValString() { - CBotString s = "Pointer to "; - if ( m_pVarClass == NULL ) s = "Null pointer" ; - else s += m_pVarClass->GivValString(); - return s; + CBotString s = "Pointer to "; + if ( m_pVarClass == NULL ) s = "Null pointer" ; + else s += m_pVarClass->GivValString(); + return s; } void CBotVarPointer::ConstructorSet() { - if ( m_pVarClass != NULL) m_pVarClass->ConstructorSet(); + if ( m_pVarClass != NULL) m_pVarClass->ConstructorSet(); } // initialise le pointeur vers l'instance d'une classe void CBotVarPointer::SetPointer(CBotVar* pVarClass) { - m_binit = true; // init, mme sur un pointeur null + m_binit = true; // init, mme sur un pointeur null - if ( m_pVarClass == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter - // car le dcrment peut dtruire l'object + if ( m_pVarClass == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter + // car le dcrment peut dtruire l'object - if ( pVarClass != NULL ) - { - if ( pVarClass->GivType() == CBotTypPointer ) - pVarClass = pVarClass->GivPointer(); // le vrai pointeur l'objet + if ( pVarClass != NULL ) + { + if ( pVarClass->GivType() == CBotTypPointer ) + pVarClass = pVarClass->GivPointer(); // le vrai pointeur l'objet -// if ( pVarClass->GivType() != CBotTypClass ) - if ( !pVarClass->m_type.Eq(CBotTypClass) ) - ASM_TRAP(); +// if ( pVarClass->GivType() != CBotTypClass ) + if ( !pVarClass->m_type.Eq(CBotTypClass) ) + ASM_TRAP(); - ((CBotVarClass*)pVarClass)->IncrementUse(); // une rfrence en plus - m_pClass = ((CBotVarClass*)pVarClass)->m_pClass; - m_pUserPtr = pVarClass->m_pUserPtr; // pas vraiment indispensable - m_type = CBotTypResult(CBotTypPointer, m_pClass); // un pointeur de quel genre - } + ((CBotVarClass*)pVarClass)->IncrementUse(); // une rfrence en plus + m_pClass = ((CBotVarClass*)pVarClass)->m_pClass; + m_pUserPtr = pVarClass->m_pUserPtr; // pas vraiment indispensable + m_type = CBotTypResult(CBotTypPointer, m_pClass); // un pointeur de quel genre + } - if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); - m_pVarClass = (CBotVarClass*)pVarClass; + if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); + m_pVarClass = (CBotVarClass*)pVarClass; } CBotVarClass* CBotVarPointer::GivPointer() { - if ( m_pVarClass == NULL ) return NULL; - return m_pVarClass->GivPointer(); + if ( m_pVarClass == NULL ) return NULL; + return m_pVarClass->GivPointer(); } void CBotVarPointer::SetIdent(long n) { - if ( m_pVarClass == NULL ) return; - m_pVarClass->SetIdent( n ); + if ( m_pVarClass == NULL ) return; + m_pVarClass->SetIdent( n ); } long CBotVarPointer::GivIdent() { - if ( m_pVarClass == NULL ) return 0; - return m_pVarClass->m_ItemIdent; + if ( m_pVarClass == NULL ) return 0; + return m_pVarClass->m_ItemIdent; } void CBotVarPointer::SetClass(CBotClass* pClass) { -// int nIdent = 0; - m_type.m_pClass = m_pClass = pClass; - if ( m_pVarClass != NULL ) m_pVarClass->SetClass(pClass); //, nIdent); +// int nIdent = 0; + m_type.m_pClass = m_pClass = pClass; + if ( m_pVarClass != NULL ) m_pVarClass->SetClass(pClass); //, nIdent); } CBotClass* CBotVarPointer::GivClass() { - if ( m_pVarClass != NULL ) return m_pVarClass->GivClass(); + if ( m_pVarClass != NULL ) return m_pVarClass->GivClass(); - return m_pClass; + return m_pClass; } bool CBotVarPointer::Save1State(FILE* pf) { - if ( m_pClass ) - { - if (!WriteString(pf, m_pClass->GivName())) return false; // nom de la classe - } - else - { - if (!WriteString(pf, "")) return false; - } + if ( m_pClass ) + { + if (!WriteString(pf, m_pClass->GivName())) return false; // nom de la classe + } + else + { + if (!WriteString(pf, "")) return false; + } - if (!WriteLong(pf, GivIdent())) return false; // la rfrence unique + if (!WriteLong(pf, GivIdent())) return false; // la rfrence unique - // sauve aussi une copie de l'instance - return SaveVar(pf, GivPointer()); + // sauve aussi une copie de l'instance + return SaveVar(pf, GivPointer()); } // copie une variable dans une autre void CBotVarPointer::Copy(CBotVar* pSrc, bool bName) { - if ( pSrc->GivType() != CBotTypPointer && - pSrc->GivType() != CBotTypNullPointer) - ASM_TRAP(); + if ( pSrc->GivType() != CBotTypPointer && + pSrc->GivType() != CBotTypNullPointer) + ASM_TRAP(); - CBotVarPointer* p = (CBotVarPointer*)pSrc; + CBotVarPointer* p = (CBotVarPointer*)pSrc; - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; -// m_pVarClass = p->m_pVarClass; - m_pVarClass = p->GivPointer(); + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; +// m_pVarClass = p->m_pVarClass; + m_pVarClass = p->GivPointer(); - if ( m_pVarClass != NULL ) - m_pVarClass->IncrementUse(); // une rfrence en plus + if ( m_pVarClass != NULL ) + m_pVarClass->IncrementUse(); // une rfrence en plus - m_pClass = p->m_pClass; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; + m_pClass = p->m_pClass; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; + // garde le mme idendificateur (par dfaut) + if (m_ident == 0 ) m_ident = p->m_ident; } bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right) { - CBotVarClass* l = left->GivPointer(); - CBotVarClass* r = right->GivPointer(); + CBotVarClass* l = left->GivPointer(); + CBotVarClass* r = right->GivPointer(); - if ( l == r ) return true; - if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return true; - if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return true; - return false; + if ( l == r ) return true; + if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return true; + if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return true; + return false; } bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) { - CBotVarClass* l = left->GivPointer(); - CBotVarClass* r = right->GivPointer(); + CBotVarClass* l = left->GivPointer(); + CBotVarClass* r = right->GivPointer(); - if ( l == r ) return false; - if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return false; - if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return false; - return true; + if ( l == r ) return false; + if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return false; + if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return false; + return true; } @@ -2084,162 +2084,162 @@ bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) CBotTypResult::CBotTypResult(int type) { - m_type = type; - m_pNext = NULL; - m_pClass = NULL; - m_limite = -1; + m_type = type; + m_pNext = NULL; + m_pClass = NULL; + m_limite = -1; } CBotTypResult::CBotTypResult(int type, const char* name) { - m_type = type; - m_pNext = NULL; - m_pClass = NULL; - m_limite = -1; + m_type = type; + m_pNext = NULL; + m_pClass = NULL; + m_limite = -1; - if ( type == CBotTypPointer || - type == CBotTypClass || - type == CBotTypIntrinsic ) - { - m_pClass = CBotClass::Find(name); - if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; - } + if ( type == CBotTypPointer || + type == CBotTypClass || + type == CBotTypIntrinsic ) + { + m_pClass = CBotClass::Find(name); + if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; + } } CBotTypResult::CBotTypResult(int type, CBotClass* pClass) { - m_type = type; - m_pNext = NULL; - m_pClass = pClass; - m_limite = -1; + m_type = type; + m_pNext = NULL; + m_pClass = pClass; + m_limite = -1; - if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; + if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; } CBotTypResult::CBotTypResult(int type, CBotTypResult elem) { - m_type = type; - m_pNext = NULL; - m_pClass = NULL; - m_limite = -1; + m_type = type; + m_pNext = NULL; + m_pClass = NULL; + m_limite = -1; - if ( type == CBotTypArrayPointer || - type == CBotTypArrayBody ) - m_pNext = new CBotTypResult( elem ); + if ( type == CBotTypArrayPointer || + type == CBotTypArrayBody ) + m_pNext = new CBotTypResult( elem ); } CBotTypResult::CBotTypResult(const CBotTypResult& typ) { - m_type = typ.m_type; - m_pClass = typ.m_pClass; - m_pNext = NULL; - m_limite = typ.m_limite; + m_type = typ.m_type; + m_pClass = typ.m_pClass; + m_pNext = NULL; + m_limite = typ.m_limite; - if ( typ.m_pNext ) - m_pNext = new CBotTypResult( *typ.m_pNext ); + if ( typ.m_pNext ) + m_pNext = new CBotTypResult( *typ.m_pNext ); } CBotTypResult::CBotTypResult() { - m_type = 0; - m_limite = -1; - m_pNext = NULL; - m_pClass = NULL; + m_type = 0; + m_limite = -1; + m_pNext = NULL; + m_pClass = NULL; } CBotTypResult::~CBotTypResult() { - delete m_pNext; + delete m_pNext; } int CBotTypResult::GivType(int mode) const { -#ifdef _DEBUG - if ( m_type == CBotTypPointer || - m_type == CBotTypClass || - m_type == CBotTypIntrinsic ) +#ifdef _DEBUG + if ( m_type == CBotTypPointer || + m_type == CBotTypClass || + m_type == CBotTypIntrinsic ) - if ( m_pClass == NULL ) ASM_TRAP(); + if ( m_pClass == NULL ) ASM_TRAP(); - - if ( m_type == CBotTypArrayPointer ) - if ( m_pNext == NULL ) ASM_TRAP(); + + if ( m_type == CBotTypArrayPointer ) + if ( m_pNext == NULL ) ASM_TRAP(); #endif - if ( mode == 3 && m_type == CBotTypNullPointer ) return CBotTypPointer; - return m_type; + if ( mode == 3 && m_type == CBotTypNullPointer ) return CBotTypPointer; + return m_type; } void CBotTypResult::SetType(int n) { - m_type = n; + m_type = n; } CBotClass* CBotTypResult::GivClass() const { - return m_pClass; + return m_pClass; } CBotTypResult& CBotTypResult::GivTypElem() const { - return *m_pNext; + return *m_pNext; } int CBotTypResult::GivLimite() const { - return m_limite; + return m_limite; } void CBotTypResult::SetLimite(int n) { - m_limite = n; + m_limite = n; } void CBotTypResult::SetArray( int* max ) { - m_limite = *max; - if (m_limite < 1) m_limite = -1; + m_limite = *max; + if (m_limite < 1) m_limite = -1; - if ( m_pNext != NULL ) // dernire dimension ? - { - m_pNext->SetArray( max+1 ); - } + if ( m_pNext != NULL ) // dernire dimension ? + { + m_pNext->SetArray( max+1 ); + } } bool CBotTypResult::Compare(const CBotTypResult& typ) const { - if ( m_type != typ.m_type ) return false; + if ( m_type != typ.m_type ) return false; - if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); + if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); - if ( m_type == CBotTypPointer || - m_type == CBotTypClass || - m_type == CBotTypIntrinsic ) - { - return m_pClass == typ.m_pClass; - } + if ( m_type == CBotTypPointer || + m_type == CBotTypClass || + m_type == CBotTypIntrinsic ) + { + return m_pClass == typ.m_pClass; + } - return true; + return true; } bool CBotTypResult::Eq(int type) const { - return m_type == type; + return m_type == type; } CBotTypResult& - CBotTypResult::operator=(const CBotTypResult& src) -{ - m_type = src.m_type; - m_limite = src.m_limite; - m_pClass = src.m_pClass; - m_pNext = NULL; - if ( src.m_pNext != NULL ) - { - m_pNext = new CBotTypResult(*src.m_pNext); - } - return *this; + CBotTypResult::operator=(const CBotTypResult& src) +{ + m_type = src.m_type; + m_limite = src.m_limite; + m_pClass = src.m_pClass; + m_pNext = NULL; + if ( src.m_pNext != NULL ) + { + m_pNext = new CBotTypResult(*src.m_pNext); + } + return *this; } diff --git a/src/CBot/resource.h b/src/CBot/resource.h index f82fb9c..6bf48e1 100644 --- a/src/CBot/resource.h +++ b/src/CBot/resource.h @@ -13,8 +13,6 @@ // * // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/.//{{NO_DEPENDENCIES}} -// Microsoft Developer Studio generated include file. -// Used by CBot.rc #ifndef _RESOURCE_H_ #define _RESOURCE_H_ -- cgit v1.2.3-1-g7c22 From 8797569d33c4917eb8f8a1dc2341aac7b5815315 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 22 Jul 2012 22:05:12 +0200 Subject: Texture & mouse functions; refactoring & fixes - cleaned up and added documentation to engine.h - refactored CEngine interface and associated structs - added mouse handling functions in CApplication & CEngine - fixed bugs in projection matrix setting - changed texture loading & handling - added const-values in CDevice & CGLDevice - changed event management in CApplication - other minor changes & bugfixes --- src/app/app.cpp | 208 +++++--- src/app/app.h | 67 ++- src/app/main.cpp | 4 +- src/common/event.h | 7 +- src/common/key.h | 4 +- src/common/singleton.h | 10 +- src/graphics/common/camera.h | 2 +- src/graphics/common/color.h | 16 + src/graphics/common/device.h | 23 +- src/graphics/common/engine.cpp | 646 ++++++++++++++++++++++++- src/graphics/common/engine.h | 726 ++++++++++++++++++---------- src/graphics/common/light.h | 4 +- src/graphics/common/particle.h | 4 +- src/graphics/common/texture.h | 41 +- src/graphics/opengl/gldevice.cpp | 156 +++--- src/graphics/opengl/gldevice.h | 31 +- src/graphics/opengl/glengine.cpp | 21 - src/graphics/opengl/glengine.h | 32 -- src/graphics/opengl/test/model_test.cpp | 16 +- src/graphics/opengl/test/texture_test.cpp | 12 +- src/graphics/opengl/test/transform_test.cpp | 6 +- src/math/geometry.h | 20 +- 22 files changed, 1488 insertions(+), 568 deletions(-) delete mode 100644 src/graphics/opengl/glengine.cpp delete mode 100644 src/graphics/opengl/glengine.h diff --git a/src/app/app.cpp b/src/app/app.cpp index 6a71f64..4812102 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -22,6 +22,7 @@ #include "app/system.h" #include "common/logger.h" #include "common/iman.h" +#include "common/image.h" #include "graphics/opengl/gldevice.h" @@ -31,6 +32,9 @@ #include +template<> CApplication* CSingleton::mInstance = NULL; + + //! Interval of timer called to update joystick state const int JOYSTICK_TIMER_INTERVAL = 1000/30; @@ -70,14 +74,9 @@ struct ApplicationPrivate }; -CApplication* CApplication::m_appInstance = NULL; - CApplication::CApplication() { - assert(m_appInstance == NULL); - m_appInstance = this; - m_private = new ApplicationPrivate(); m_exitCode = 0; @@ -107,6 +106,8 @@ CApplication::CApplication() m_debugMode = false; m_setupMode = true; + m_dataPath = "./data"; + ResetKey(); } @@ -120,29 +121,49 @@ CApplication::~CApplication() delete m_iMan; m_iMan = NULL; - - m_appInstance = NULL; } -Error CApplication::ParseArguments(int argc, char *argv[]) +bool CApplication::ParseArguments(int argc, char *argv[]) { + bool waitDataDir = false; + for (int i = 1; i < argc; ++i) { std::string arg = argv[i]; + if (waitDataDir) + { + waitDataDir = false; + m_dataPath = arg; + } + if (arg == "-debug") { m_showStats = true; SetDebugMode(true); } - // TODO else {} report invalid argument + else if (arg == "-datadir") + { + waitDataDir = true; + } + else + { + m_exitCode = 1; + return false; + } } - return ERR_OK; + // Data dir not given? + if (waitDataDir) + return false; + + return true; } bool CApplication::Create() { + // TODO: verify that data directory exists + // Temporarily -- only in windowed mode m_private->deviceConfig.fullScreen = false; @@ -365,7 +386,7 @@ void CApplication::CloseJoystick() Uint32 JoystickTimerCallback(Uint32 interval, void *) { - CApplication *app = CApplication::GetInstance(); + CApplication *app = CApplication::GetInstancePointer(); if ((app == NULL) || (! app->GetJoystickEnabled())) return 0; // don't run the timer again @@ -434,43 +455,82 @@ int CApplication::Run() { m_active = true; - while (m_private->currentEvent.type != SDL_QUIT) + while (true) { // Use SDL_PeepEvents() if the app is active, so we can use idle time to // render the scene. Else, use SDL_PollEvent() to avoid eating CPU time. int count = 0; - if (m_active) - { - SDL_PumpEvents(); - count = SDL_PeepEvents(&m_private->currentEvent, 1, SDL_GETEVENT, SDL_ALLEVENTS); - } - else - { - SDL_PollEvent(&m_private->currentEvent); - } - // If received an event - if ((m_active && count > 0) || (!m_active)) + // To be sure no old event remains + m_private->currentEvent.type = SDL_NOEVENT; + + bool haveEvent = true; + while (haveEvent) { - ParseEvent(); + haveEvent = false; + + if (m_active) + { + SDL_PumpEvents(); + count = SDL_PeepEvents(&m_private->currentEvent, 1, SDL_GETEVENT, SDL_ALLEVENTS); + } + else + { + count = SDL_PollEvent(&m_private->currentEvent); + } + + // If received an event + if (count > 0) + { + haveEvent = true; + + Event event = ParseEvent(); + + if (event.type == EVENT_QUIT) + goto end; // exit the loop + + if (event.type != EVENT_NULL) + { + bool passOn = ProcessEvent(event); + + if (m_engine != NULL && passOn) + passOn = m_engine->ProcessEvent(event); + + if (passOn) + m_eventQueue->AddEvent(event); + } + } } - // Render a frame during idle time (no messages are waiting) + // Enter game update & frame rendering only if active if (m_active && m_ready) { Event event; while (m_eventQueue->GetEvent(event)) { if (event.type == EVENT_QUIT) - { goto end; // exit both loops + + bool passOn = true; + + // Skip system events (they have been processed earlier) + if (! event.systemEvent) + { + passOn = ProcessEvent(event); + + if (passOn && m_engine != NULL) + passOn = m_engine->ProcessEvent(event); } - //m_robotMain->EventProcess(event); + /*if (passOn && m_robotMain != NULL) + m_robotMain->ProcessEvent(event); */ } + // Update game and render a frame during idle time (no messages are waiting) + bool ok = Render(); + // If an error occurs, push quit event to the queue - if (! Render()) + if (! ok) { SDL_Event quitEvent; memset(&quitEvent, 0, sizeof(SDL_Event)); @@ -481,7 +541,6 @@ int CApplication::Run() } end: - //m_sound->StopMusic(); Destroy(); return m_exitCode; @@ -501,23 +560,34 @@ PressState TranslatePressState(unsigned char state) return STATE_RELEASED; } -/** Conversion of the position of the mouse to the following coordinates: - -x: 0=left, 1=right +/** Conversion of the position of the mouse from window coords to interface coords: + - x: 0=left, 1=right + - y: 0=down, 1=up */ +Math::Point CApplication::WindowToInterfaceCoords(Math::IntPoint pos) +{ + return Math::Point( (float)pos.x / (float)m_private->deviceConfig.width, + 1.0f - (float)pos.y / (float)m_private->deviceConfig.height); +} -y: 0=down, 1=up -*/ -Math::Point CApplication::WindowToInterfaceCoords(int x, int y) +Math::IntPoint CApplication::InterfaceToWindowCoords(Math::Point pos) { - return Math::Point((float)x / (float)m_private->deviceConfig.width, - 1.0f - (float)y / (float)m_private->deviceConfig.height); + return Math::IntPoint((int)(pos.x * m_private->deviceConfig.width), + (int)((1.0f - pos.y) * m_private->deviceConfig.height)); } -void CApplication::ParseEvent() +/** The SDL event parsed is stored internally. + If event is not available or is not understood, returned event is of type EVENT_NULL. */ +Event CApplication::ParseEvent() { Event event; - if ( (m_private->currentEvent.type == SDL_KEYDOWN) || + event.systemEvent = true; + + if (m_private->currentEvent.type == SDL_QUIT) + { + event.type = EVENT_QUIT; + } + else if ( (m_private->currentEvent.type == SDL_KEYDOWN) || (m_private->currentEvent.type == SDL_KEYUP) ) { if (m_private->currentEvent.type == SDL_KEYDOWN) @@ -540,16 +610,15 @@ void CApplication::ParseEvent() event.mouseButton.button = m_private->currentEvent.button.button; event.mouseButton.state = TranslatePressState(m_private->currentEvent.button.state); - event.mouseButton.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); + event.mouseButton.pos = WindowToInterfaceCoords(Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y)); } else if (m_private->currentEvent.type == SDL_MOUSEMOTION) { event.type = EVENT_MOUSE_MOVE; event.mouseMove.state = TranslatePressState(m_private->currentEvent.button.state); - event.mouseMove.pos = WindowToInterfaceCoords(m_private->currentEvent.button.x, m_private->currentEvent.button.y); + event.mouseMove.pos = WindowToInterfaceCoords(Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y)); } - // TODO: joystick state polling instead of getting events else if (m_private->currentEvent.type == SDL_JOYAXISMOTION) { event.type = EVENT_JOY_AXIS; @@ -569,16 +638,13 @@ void CApplication::ParseEvent() event.joyButton.state = TranslatePressState(m_private->currentEvent.jbutton.state); } - - if (m_robotMain != NULL && event.type != EVENT_NULL) - { - //m_robotMain->EventProcess(event); - } - - ProcessEvent(event); + return event; } -void CApplication::ProcessEvent(Event event) +/** Processes incoming events. It is the first function called after an event is captures. + Function returns \c true if the event is to be passed on to other processing functions + or \c false if not. */ +bool CApplication::ProcessEvent(const Event &event) { CLogger *l = GetLogger(); // Print the events in debug mode to test the code @@ -621,8 +687,12 @@ void CApplication::ProcessEvent(Event event) break; } } + + // By default, pass on all events + return true; } +/** Renders the frame and swaps buffers as necessary. Returns \c false on error. */ bool CApplication::Render() { bool result = m_engine->Render(); @@ -709,14 +779,39 @@ int CApplication::GetKey(int keyRank, int option) return 0; } -void CApplication::SetMousePos(Math::Point pos) +void CApplication::SetGrabInput(bool grab) { - // TODO + SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF); } -void CApplication::SetMouseType(Gfx::MouseType type) +bool CApplication::GetGrabInput() { - // TODO + int result = SDL_WM_GrabInput(SDL_GRAB_QUERY); + return result == SDL_GRAB_ON; +} + +void CApplication::SetSystemMouseVisible(bool visible) +{ + SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE); +} + +bool CApplication::GetSystemMouseVisibile() +{ + int result = SDL_ShowCursor(SDL_QUERY); + return result == SDL_ENABLE; +} + + +void CApplication::SetSystemMousePos(Math::Point pos) +{ + Math::IntPoint windowPos = InterfaceToWindowCoords(pos); + SDL_WarpMouse(windowPos.x, windowPos.y); + m_systemMousePos = pos; +} + +Math::Point CApplication::GetSystemMousePos() +{ + return m_systemMousePos; } void CApplication::SetJoystickEnabled(bool enable) @@ -766,3 +861,8 @@ void CApplication::OutputText(long x, long y, char* str) { // TODO } + +std::string CApplication::GetDataFilePath(const std::string& dirName, const std::string& fileName) +{ + return m_dataPath + "/" + dirName + "/" + fileName; +} diff --git a/src/app/app.h b/src/app/app.h index ed2bd9a..576ed62 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -21,6 +21,7 @@ #include "common/misc.h" +#include "common/singleton.h" #include "graphics/common/device.h" #include "graphics/common/engine.h" @@ -33,8 +34,8 @@ class CEvent; class CRobotMain; class CSound; -struct ApplicationPrivate; +struct ApplicationPrivate; /** * \class CApplication @@ -67,7 +68,7 @@ struct ApplicationPrivate; * The events are further handled in CRobotMain class. * */ -class CApplication +class CApplication : public CSingleton { public: //! Constructor (can only be called once!) @@ -75,13 +76,9 @@ public: //! Destructor ~CApplication(); - //! Returns the only CApplication instance - static CApplication* GetInstance() - { return m_appInstance; } - public: //! Parses commandline arguments - Error ParseArguments(int argc, char *argv[]); + bool ParseArguments(int argc, char *argv[]); //! Initializes the application bool Create(); //! Main event loop @@ -117,20 +114,31 @@ public: void SetKey(int keyRank, int option, int key); int GetKey(int keyRank, int option); - void SetMouseType(Gfx::MouseType type); - void SetMousePos(Math::Point pos); + //! Sets the grab mode for input (keyboard & mouse) + void SetGrabInput(bool grab); + //! Returns the grab mode + bool GetGrabInput(); + + //! Sets the visiblity of system mouse cursor + void SetSystemMouseVisible(bool visible); + //! Returns the visiblity of system mouse cursor + bool GetSystemMouseVisibile(); - //? void SetNiceMouse(bool nice); - //? bool GetNiceMouse(); - //? bool GetNiceMouseCap(); + //! Sets the position of system mouse cursor (in interface coords) + void SetSystemMousePos(Math::Point pos); + //! Returns the position of system mouse cursor (in interface coords) + Math::Point GetSystemMousePos(); bool WriteScreenShot(char *filename, int width, int height); + //! Returns the full path to a file in data directory + std::string GetDataFilePath(const std::string &dirName, const std::string &fileName); + protected: - //! Processes an SDL event to Event struct - void ParseEvent(); + //! Processes the captured SDL event to Event struct + Event ParseEvent(); //! Handles some incoming events - void ProcessEvent(Event event); + bool ProcessEvent(const Event &event); //! Renders the image in window bool Render(); @@ -140,16 +148,9 @@ protected: void CloseJoystick(); //! Converts window coords to interface coords - Math::Point WindowToInterfaceCoords(int x, int y); - - //HRESULT ConfirmDevice( DDCAPS* pddDriverCaps, D3DDEVICEDESC7* pd3dDeviceDesc ); - //HRESULT Initialize3DEnvironment(); - //HRESULT Change3DEnvironment(); - //HRESULT CreateZBuffer(GUID* pDeviceGUID); - //HRESULT Render3DEnvironment(); - //VOID Cleanup3DEnvironment(); - //VOID DeleteDeviceObjects(); - //VOID DisplayFrameworkError( HRESULT, DWORD ); + Math::Point WindowToInterfaceCoords(Math::IntPoint pos); + //! Converts the interface coords to window coords + Math::IntPoint InterfaceToWindowCoords(Math::Point pos); void InitText(); void DrawSuppl(); @@ -157,8 +158,6 @@ protected: void OutputText(long x, long y, char* str); protected: - //! The only instance of CApplication - static CApplication* m_appInstance; //! Instance manager CInstanceManager* m_iMan; //! Private (SDL-dependent data) @@ -185,21 +184,16 @@ protected: bool m_debugMode; bool m_setupMode; + //! Whether joystick is enabled bool m_joystickEnabled; + //! Text set as window title std::string m_windowTitle; - //? long m_vidMemTotal; - //? bool m_appUseZBuffer; - //? bool m_appUseStereo; - //? bool m_audioState; - //? bool m_audioTrack; - //? bool m_niceMouse; - int m_keyState; Math::Vector m_axeKey; Math::Vector m_axeJoy; - Math::Point m_mousePos; + Math::Point m_systemMousePos; long m_mouseWheel; //! Current state of joystick axes; may be updated from another thread @@ -209,5 +203,8 @@ protected: float m_time; long m_key[50][2]; + + //! Path to directory with data files + std::string m_dataPath; }; diff --git a/src/app/main.cpp b/src/app/main.cpp index f857037..9eea6e4 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -69,10 +69,10 @@ int main(int argc, char *argv[]) CApplication app; // single instance of the application - Error err = app.ParseArguments(argc, argv); - if (err != ERR_OK) + if (! app.ParseArguments(argc, argv)) { SystemDialog(SDT_ERROR, "COLOBOT", "Invalid commandline arguments!\n"); + return app.GetExitCode(); } int code = 0; diff --git a/src/common/event.h b/src/common/event.h index 70b110d..6ce1a79 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -36,6 +36,7 @@ enum EventType // TODO: document the meaning of each value + //! Invalid event / no event EVENT_NULL = 0, //! Event sent on user or system quit request @@ -638,6 +639,9 @@ struct Event //! Type of event (EVENT_*) EventType type; + //! If true, the event was produced by system (SDL); else, it has come from user interface + bool systemEvent; + //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP KeyEventData key; //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP @@ -657,7 +661,8 @@ struct Event //? short keyState; // state of the keyboard (KS_ *) //? float rTime; // relative time - Event(EventType aType = EVENT_NULL) : type(aType) {} + Event(EventType aType = EVENT_NULL) + : type(aType), systemEvent(false) {} }; diff --git a/src/common/key.h b/src/common/key.h index 0ed6b54..de31c09 100644 --- a/src/common/key.h +++ b/src/common/key.h @@ -27,8 +27,8 @@ // Key symbol defined as concatenation to SDLK_... // If need arises, it can be changed to custom function or anything else -#define KEY(x) SDLK_ # x +#define KEY(x) SDLK_ ## x // Key modifier defined as concatenation to KMOD_... // If need arises, it can be changed to custom function or anything else -#define KEY_MOD(x) KMOD_ # x +#define KEY_MOD(x) KMOD_ ## x diff --git a/src/common/singleton.h b/src/common/singleton.h index ee01b24..4df7878 100644 --- a/src/common/singleton.h +++ b/src/common/singleton.h @@ -29,21 +29,21 @@ template class CSingleton public: static T& GetInstance() { - aserrt(mInstance); + assert(mInstance != NULL); return *mInstance; } - static T& GetInstancePointer() { - aserrt(mInstance); + static T* GetInstancePointer() { + assert(mInstance != NULL); return mInstance; } static bool IsCreated() { - return mInstance != NULL; + return mInstance != NULL; } CSingleton() { - assert(!mInstance); + assert(mInstance == NULL); mInstance = static_cast(this); } diff --git a/src/graphics/common/camera.h b/src/graphics/common/camera.h index be52061..76077bf 100644 --- a/src/graphics/common/camera.h +++ b/src/graphics/common/camera.h @@ -141,7 +141,7 @@ class CCamera { void SetCameraInvertY(bool bInvert); float RetMotorTurn(); - Gfx::MouseType RetMouseDef(Math::Point pos); + Gfx::EngineMouseType RetMouseDef(Math::Point pos); protected: bool EventMouseMove(const Event &event); diff --git a/src/graphics/common/color.h b/src/graphics/common/color.h index 68421c7..907a3b9 100644 --- a/src/graphics/common/color.h +++ b/src/graphics/common/color.h @@ -36,12 +36,23 @@ struct Color Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f) : r(aR), g(aG), b(aB), a(aA) {} + inline Gfx::Color Inverse() const + { + return Gfx::Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a); + } + //! Returns the struct cast to \c float* array; use with care! inline float* Array() { return (float*)this; } + //! Returns the struct cast to const float* array; use with care! + inline const float* Array() const + { + return (const float*)this; + } + //! Returns a string (r, g, b, a) inline std::string ToString() const { @@ -50,6 +61,11 @@ struct Color s << "(" << r << ", " << g << ", " << b << ", " << a << ")"; return s.str(); } + + inline bool operator==(const Gfx::Color &other) const + { + return r == other.r && g == other.g && b == other.b && a == other.a; + } }; /** diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index ceaf67a..d79d253 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -90,6 +90,7 @@ enum RenderState RENDER_STATE_DEPTH_TEST, RENDER_STATE_DEPTH_WRITE, RENDER_STATE_ALPHA_TEST, + RENDER_STATE_CULLING, RENDER_STATE_DITHERING }; @@ -292,14 +293,14 @@ public: virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix) = 0; //! Sets the current material - virtual void SetMaterial(Gfx::Material &material) = 0; + virtual void SetMaterial(const Gfx::Material &material) = 0; //! Returns the current material virtual const Gfx::Material& GetMaterial() = 0; //! Returns the maximum number of lights available virtual int GetMaxLightCount() = 0; //! Sets the light at given index - virtual void SetLight(int index, Gfx::Light &light) = 0; + virtual void SetLight(int index, const Gfx::Light &light) = 0; //! Returns the current light at given index virtual const Gfx::Light& GetLight(int index) = 0; //! Enables/disables the light at given index @@ -308,18 +309,18 @@ public: virtual bool GetLightEnabled(int index) = 0; //! Creates a texture from image; the image can be safely removed after that - virtual Gfx::Texture* CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) = 0; + virtual Gfx::Texture CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) = 0; //! Deletes a given texture, freeing it from video memory - virtual void DestroyTexture(Gfx::Texture *texture) = 0; + virtual void DestroyTexture(const Gfx::Texture &texture) = 0; //! Deletes all textures created so far virtual void DestroyAllTextures() = 0; //! Returns the maximum number of multitexture stages virtual int GetMaxTextureCount() = 0; //! Sets the (multi)texture at given index - virtual void SetTexture(int index, Gfx::Texture *texture) = 0; + virtual void SetTexture(int index, const Gfx::Texture &texture) = 0; //! Returns the (multi)texture at given index - virtual Gfx::Texture* GetTexture(int index) = 0; + virtual Gfx::Texture GetTexture(int index) = 0; //! Enables/disables the given texture stage virtual void SetTextureEnabled(int index, bool enabled) = 0; //! Returns the current enable state of given texture stage @@ -331,7 +332,7 @@ public: virtual Gfx::TextureParams GetTextureParams(int index) = 0; //! Sets the texture factor to the given color value - virtual void SetTextureFactor(Gfx::Color &color) = 0; + virtual void SetTextureFactor(const Gfx::Color &color) = 0; //! Returns the current texture factor virtual Gfx::Color GetTextureFactor() = 0; @@ -343,7 +344,7 @@ public: virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexTex2 *vertices, int vertexCount) = 0; //! Tests whether a sphere intersects the 6 clipping planes of projection volume - virtual int ComputeSphereVisibility(Math::Vector center, float radius) = 0; + virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius) = 0; //! Enables/disables the given render state virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; @@ -371,17 +372,17 @@ public: virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) = 0; //! Sets the clear color - virtual void SetClearColor(Gfx::Color color) = 0; + virtual void SetClearColor(const Gfx::Color &color) = 0; //! Returns the current clear color virtual Gfx::Color GetClearColor() = 0; //! Sets the global ambient color - virtual void SetGlobalAmbient(Gfx::Color color) = 0; + virtual void SetGlobalAmbient(const Gfx::Color &color) = 0; //! Returns the global ambient color virtual Gfx::Color GetGlobalAmbient() = 0; //! Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) - virtual void SetFogParams(Gfx::FogMode mode, Gfx::Color color, float start, float end, float density) = 0; + virtual void SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, float start, float end, float density) = 0; //! Returns the current fog parameters: mode, color, start distance, end distance and density (for exp models) virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) = 0; diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index 0b92224..c474402 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -19,9 +19,24 @@ #include "graphics/common/engine.h" +#include "app/app.h" +#include "common/iman.h" +#include "common/image.h" +#include "common/key.h" #include "graphics/common/device.h" #include "math/geometry.h" +// Initial size of various vectors +const int OBJECT_PREALLOCATE_COUNT = 1200; +const int SHADOW_PREALLOCATE_COUNT = 500; +const int GROUNDSPOT_PREALLOCATE_COUNT = 100; + +const int LEVEL1_PREALLOCATE_COUNT = 50; +const int LEVEL2_PREALLOCATE_COUNT = 100; +const int LEVEL3_PREALLOCATE_COUNT = 5; +const int LEVEL4_PREALLOCATE_COUNT = 10; +const int LEVEL5_PREALLOCATE_COUNT = 100; +const int LEVEL5_VERTEX_PREALLOCATE_COUNT = 200; Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) @@ -32,7 +47,117 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_wasInit = false; - // TODO + m_iMan = iMan; + m_iMan->AddInstance(CLASS_ENGINE, this); + m_app = app; + + /*m_light = new Gfx::CLight(m_iMan, this); + m_text = new Gfx::CText(m_iMan, this); + m_particle = new Gfx::CParticle(m_iMan, this); + m_water = new Gfx::CWater(m_iMan, this); + m_cloud = new Gfx::CCloud(m_iMan, this); + m_lightning = new Gfx::CLightning(m_iMan, this); + m_planet = new Gfx::CPlanet(m_iMan, this);*/ + m_sound = NULL; + m_terrain = NULL; + + m_dim.x = 640; + m_dim.y = 480; + m_lastDim = m_dim; + m_focus = 0.75f; + m_baseTime = 0; + m_lastTime = 0; + m_absTime = 0.0f; + m_rankView = 0; + + m_ambientColor[0] = Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); + m_ambientColor[1] = Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); + m_fogColor[0] = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f); + m_fogColor[1] = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f); + m_deepView[0] = 1000.0f; + m_deepView[1] = 1000.0f; + m_fogStart[0] = 0.75f; + m_fogStart[1] = 0.75f; + m_waterAddColor = Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f); + + m_pause = false; + m_render = true; + m_movieLock = false; + m_shadowVisible = true; + m_groundSpotVisible = true; + m_dirty = true; + m_fog = true; + m_speed = 1.0f; + m_secondTexNum = 0; + m_eyeDirH = 0.0f; + m_eyeDirV = 0.0f; + m_backgroundName = ""; // no background image + m_backgroundColorUp = 0; + m_backgroundColorDown = 0; + m_backgroundCloudUp = 0; + m_backgroundCloudDown = 0; + m_backgroundFull = false; + m_backgroundQuarter = false; + m_overFront = true; + m_overColor = 0; + m_overMode = ENG_RSTATE_TCOLOR_BLACK; + m_frontsizeName = ""; // no front image + m_hiliteRank[0] = -1; // empty list + m_eyePt = Math::Vector(0.0f, 0.0f, 0.0f); + m_lookatPt = Math::Vector(0.0f, 0.0f, 1.0f); + m_drawWorld = true; + m_drawFront = false; + m_limitLOD[0] = 100.0f; + m_limitLOD[1] = 200.0f; + m_particuleDensity = 1.0f; + m_clippingDistance = 1.0f; + m_lastClippingDistance = m_clippingDistance; + m_objectDetail = 1.0f; + m_lastObjectDetail = m_objectDetail; + m_terrainVision = 1000.0f; + m_gadgetQuantity = 1.0f; + m_textureQuality = 1; + m_totoMode = true; + m_lensMode = true; + m_waterMode = true; + m_skyMode = true; + m_backForce = true; + m_planetMode = true; + m_lightMode = true; + m_editIndentMode = true; + m_editIndentValue = 4; + m_tracePrecision = 1.0f; + + m_alphaMode = 1; + + m_forceStateColor = true; + m_stateColor = false; + + m_blackSrcBlend[0] = 0; + m_blackDestBlend[0] = 0; + m_whiteSrcBlend[0] = 0; + m_whiteDestBlend[0] = 0; + m_diffuseSrcBlend[0] = 0; + m_diffuseDestBlend[0] = 0; + m_alphaSrcBlend[0] = 0; + m_alphaDestBlend[0] = 0; + + m_updateGeometry = false; + + m_mousePos = Math::Point(0.5f, 0.5f); + m_mouseType = Gfx::ENG_MOUSE_NORM; + m_mouseVisible = false; + + m_texPath = "textures/"; + m_defaultTexParams.format = Gfx::TEX_IMG_RGBA; + m_defaultTexParams.mipmap = true; + m_defaultTexParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; + m_defaultTexParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; + + m_objectTree.reserve(LEVEL1_PREALLOCATE_COUNT); + m_objects.reserve(OBJECT_PREALLOCATE_COUNT); + m_shadow.reserve(SHADOW_PREALLOCATE_COUNT); + m_groundSpot.reserve(GROUNDSPOT_PREALLOCATE_COUNT); } Gfx::CEngine::~CEngine() @@ -41,7 +166,29 @@ Gfx::CEngine::~CEngine() m_app = NULL; m_device = NULL; - // TODO + /*delete m_light; + m_light = NULL; + + delete m_text; + m_text = NULL; + + delete m_particle; + m_particle = NULL; + + delete m_water; + m_water = NULL; + + delete m_cloud; + m_cloud = NULL; + + delete m_lightning; + m_lightning = NULL; + + delete m_planet; + m_planet = NULL;*/ + + m_sound = NULL; + m_terrain = NULL; } bool Gfx::CEngine::GetWasInit() @@ -64,7 +211,9 @@ bool Gfx::CEngine::Create() { m_wasInit = true; - // TODO + m_matWorldInterface.LoadIdentity(); + m_matViewInterface.LoadIdentity(); + Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); return true; } @@ -90,39 +239,502 @@ bool Gfx::CEngine::AfterDeviceSetInit() { m_device->SetClearColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)); - // TODO + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); + + Gfx::TextureCreateParams params; + params.format = Gfx::TEX_IMG_RGB; + params.minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + params.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + params.mipmap = false; + CreateTexture("mouse.png", params); return true; } +bool Gfx::CEngine::ProcessEvent(const Event &event) +{ + if (event.type == EVENT_MOUSE_MOVE) + { + m_mousePos = event.mouseMove.pos; + } + else if (event.type == EVENT_KEY_DOWN) + { + // !! Debug, to be removed later !! + + if (event.key.key == KEY(F1)) + { + m_mouseVisible = !m_mouseVisible; + m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile()); + } + else if (event.key.key == KEY(F2)) + { + int index = (int)m_mouseType; + m_mouseType = (Gfx::EngineMouseType)( (index + 1) % Gfx::ENG_MOUSE_COUNT ); + } + } + + // By default, pass on all events + return true; +} + +void Gfx::CEngine::SetTexture(const std::string &name, int stage) +{ + std::map::iterator it = m_texNameMap.find(name); + if (it != m_texNameMap.end()) + m_device->SetTexture(stage, (*it).second); + + // TODO if not present... +} + +void Gfx::CEngine::SetMaterial(const Gfx::Material &mat) +{ + m_device->SetMaterial(mat); +} + +void Gfx::CEngine::SetState(int state, Gfx::Color color) +{ + if ( state == m_lastState && color == m_lastColor ) + return; + + m_lastState = state; + m_lastColor = color; + + if ( m_alphaMode != 1 && (state & Gfx::ENG_RSTATE_ALPHA) ) + { + state &= ~Gfx::ENG_RSTATE_ALPHA; + + if (m_alphaMode == 2) + state |= Gfx::ENG_RSTATE_TTEXTURE_BLACK; + } + + if (state & Gfx::ENG_RSTATE_TTEXTURE_BLACK) // The transparent black texture? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + + m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); + + m_device->SetTextureFactor(color); + + Gfx::TextureParams params; + params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; + params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; + // TODO: params.alphaOperation = Gfx::TEX_MIX_OPER_DISABLED; + m_device->SetTextureParams(0, params); + } + else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + + m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); + + m_device->SetTextureFactor(color.Inverse()); + + Gfx::TextureParams params; + params.colorOperation = Gfx::TEX_MIX_OPER_ADD; + params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; + params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; + // TODO: params.alphaOperation = Gfx::TEX_MIX_OPER_DISABLED; + m_device->SetTextureParams(0, params); + } + // TODO other modes + else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color? + { + /* + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_blackSrcBlend[1]); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_blackDestBlend[1]); + + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color? + { + /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_whiteSrcBlend[1]); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_whiteDestBlend[1]); + + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, ~color); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent? + { + /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]); + + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + else if (state & Gfx::ENG_RSTATE_ALPHA) // image with alpha channel? + { + /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128)); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]); + + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/ + } + else // normal ? + { + /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); + m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); + + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + + if (state & Gfx::ENG_RSTATE_FOG) + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true); + + + bool second = m_groundSpotVisible || m_dirty; + + if ( !m_groundSpotVisible && (state & Gfx::ENG_RSTATE_SECOND) != 0 ) second = false; + if ( !m_dirty && (state & Gfx::ENG_RSTATE_SECOND) == 0 ) second = false; + + if ( (state & ENG_RSTATE_DUAL_BLACK) && second ) + { + /*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ + } + else if ( (state & ENG_RSTATE_DUAL_WHITE) && second ) + { + /*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ + } + else + { + /*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + + if (state & Gfx::ENG_RSTATE_WRAP) + { + /*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/ + } + else if (state & Gfx::ENG_RSTATE_CLAMP) + { + /*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ + } + else + { + /*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); + m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ + } + + if (state & Gfx::ENG_RSTATE_2FACE) + { + m_device->SetRenderState(Gfx::RENDER_STATE_CULLING, false); + } + else + { + m_device->SetRenderState(Gfx::RENDER_STATE_CULLING, true); + m_device->SetCullMode(Gfx::CULL_CCW); + } + + if (state & Gfx::ENG_RSTATE_LIGHT) + m_device->SetGlobalAmbient(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); + else + m_device->SetGlobalAmbient(m_ambientColor[m_rankView]); +} + bool Gfx::CEngine::Render() { + m_statisticTriangle = 0; + m_device->BeginScene(); - Math::Matrix world; - world.LoadIdentity(); - m_device->SetTransform(Gfx::TRANSFORM_WORLD, world); + SetUp3DView(); + + if (! Draw3DScene() ) + return false; + + SetUpInterfaceView(); + + if (! DrawInterface() ) + return false; + + m_device->EndScene(); + + return true; +} - Math::Matrix view; - view.LoadIdentity(); - m_device->SetTransform(Gfx::TRANSFORM_VIEW, view); +void Gfx::CEngine::SetUp3DView() +{ + // TODO +} - Math::Matrix proj; - Math::LoadOrthoProjectionMatrix(proj, -10.0f, 10.0f, -10.0f, 10.0f); - m_device->SetTransform(Gfx::TRANSFORM_PROJECTION, proj); +bool Gfx::CEngine::Draw3DScene() +{ + // TODO + return true; +} +void Gfx::CEngine::SetUpInterfaceView() +{ + m_device->SetTransform(Gfx::TRANSFORM_WORLD, m_matWorldInterface); + m_device->SetTransform(Gfx::TRANSFORM_VIEW, m_matViewInterface); + m_device->SetTransform(Gfx::TRANSFORM_PROJECTION, m_matProjInterface); +} + +bool Gfx::CEngine::DrawInterface() +{ Gfx::VertexCol vertices[3] = { - Gfx::VertexCol(Math::Vector(-2.0f, -1.0f, 0.0f), Gfx::Color(1.0f, 0.0f, 0.0f)), - Gfx::VertexCol(Math::Vector( 2.0f, -1.0f, 0.0f), Gfx::Color(0.0f, 1.0f, 0.0f)), - Gfx::VertexCol(Math::Vector( 0.0f, 1.5f, 0.0f), Gfx::Color(0.0f, 0.0f, 1.0f)) + Gfx::VertexCol(Math::Vector( 0.25f, 0.25f, 0.0f), Gfx::Color(1.0f, 0.0f, 0.0f)), + Gfx::VertexCol(Math::Vector( 0.75f, 0.25f, 0.0f), Gfx::Color(0.0f, 1.0f, 0.0f)), + Gfx::VertexCol(Math::Vector( 0.5f, 0.75f, 0.0f), Gfx::Color(0.0f, 0.0f, 1.0f)) }; m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, vertices, 3); - m_device->EndScene(); + DrawMouse(); return true; } +void Gfx::CEngine::DrawMouse() +{ + struct EngineMouse + { + Gfx::EngineMouseType type; + int icon1; + int icon2; + int iconShadow; + Gfx::EngineRenderState mode1; + Gfx::EngineRenderState mode2; + Math::Point hotPoint; + + EngineMouse(Gfx::EngineMouseType = Gfx::ENG_MOUSE_NORM, + int icon1 = -1, int icon2 = -1, int iconShadow = -1, + Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL, + Gfx::EngineRenderState mode2 = ENG_RSTATE_NORMAL, + Math::Point hotPoint = Math::Point()) + { + this->type = type; + this->icon1 = icon1; + this->icon2 = icon2; + this->iconShadow = iconShadow; + this->mode1 = mode1; + this->mode2 = mode2; + this->hotPoint = hotPoint; + } + }; + + static const EngineMouse MICE[Gfx::ENG_MOUSE_COUNT] = + { + EngineMouse(Gfx::ENG_MOUSE_NORM, 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f)), + EngineMouse(Gfx::ENG_MOUSE_WAIT, 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f)), + EngineMouse(Gfx::ENG_MOUSE_HAND, 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f)), + EngineMouse(Gfx::ENG_MOUSE_NO, 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f)), + EngineMouse(Gfx::ENG_MOUSE_EDIT, 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f)), + EngineMouse(Gfx::ENG_MOUSE_CROSS, 10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f)), + EngineMouse(Gfx::ENG_MOUSE_MOVEV, 12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f)), + EngineMouse(Gfx::ENG_MOUSE_MOVEH, 14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f)), + EngineMouse(Gfx::ENG_MOUSE_MOVED, 16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)), + EngineMouse(Gfx::ENG_MOUSE_MOVEI, 18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)), + EngineMouse(Gfx::ENG_MOUSE_MOVE, 20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f)), + EngineMouse(Gfx::ENG_MOUSE_TARGET, 22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f)), + EngineMouse(Gfx::ENG_MOUSE_SCROLLL, 24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f)), + EngineMouse(Gfx::ENG_MOUSE_SCROLLR, 26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f)), + EngineMouse(Gfx::ENG_MOUSE_SCROLLU, 28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f)), + EngineMouse(Gfx::ENG_MOUSE_SCROLLD, 30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f)) + }; + + static const Math::Point MOUSE_SIZE(0.05f, 0.05f); + + if (! m_mouseVisible) + return; + + if (m_app->GetSystemMouseVisibile()) + return; + + + Gfx::Material material; + material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f); + material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f); + + SetMaterial(material); + SetTexture("mouse.png"); + + for (int i = 0; i < Gfx::ENG_MOUSE_COUNT; ++i) + { + if (m_mouseType != MICE[i].type) + continue; + + Math::Point pos; + pos.x = m_mousePos.x - (MICE[i].hotPoint.x * MOUSE_SIZE.x) / 32.0f; + pos.y = m_mousePos.y - ((32.0f - MICE[i].hotPoint.y) * MOUSE_SIZE.y) / 32.0f; + + Math::Point shadowPos; + shadowPos.x = pos.x+(4.0f/640.0f); + shadowPos.y = pos.y-(3.0f/480.0f); + + // FIXME: doesn't work yet + + SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE); + DrawMouseSprite(shadowPos, MOUSE_SIZE, MICE[i].iconShadow); + + SetState(MICE[i].mode1); + DrawMouseSprite(pos, MOUSE_SIZE, MICE[i].icon1); + + SetState(MICE[i].mode2); + DrawMouseSprite(pos, MOUSE_SIZE, MICE[i].icon2); + + break; + } +} + +void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) +{ + if (icon == -1) + return; + + Math::Point p1 = pos; + Math::Point p2 = p1 + size; + + float u1 = (32.0f / 256.0f) * (icon % 8); + float v1 = (32.0f / 256.0f) * (icon / 8); + float u2 = (32.0f / 256.0f) + u1; + float v2 = (32.0f / 256.0f) + v1; + + float dp = 0.5f / 256.0f; + u1 += dp; + v1 += dp; + u2 -= dp; + v2 -= dp; + + Math::Vector normal(0.0f, 0.0f, -1.0f); + + Gfx::Vertex vertex[4] = + { + Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), normal, Math::Point(u1, v2)), + Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)), + Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), normal, Math::Point(u2, v2)), + Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), normal, Math::Point(u2, v1)) + }; + + m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, vertex, 4); + AddStatisticTriangle(2); +} + +Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams ¶ms) +{ + CImage img; + if (! img.Load(m_app->GetDataFilePath(m_texPath, texName))) + { + std::stringstream str; + str << "Couldn't load texture '" << texName << "': " << img.GetError(); + m_error = str.str(); + return Gfx::Texture(); // invalid texture + } + + Gfx::Texture result = m_device->CreateTexture(&img, params); + + if (! result.valid) + { + std::stringstream str; + str << "Couldn't load texture '" << texName << "': " << m_device->GetError(); + m_error = str.str(); + return result; + } + m_texNameMap[texName] = result; + m_revTexNameMap[result] = texName; + + return result; +} + +Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName) +{ + return CreateTexture(texName, m_defaultTexParams); +} + +void Gfx::CEngine::DestroyTexture(const std::string &texName) +{ + std::map::iterator it = m_texNameMap.find(texName); + if (it == m_texNameMap.end()) + return; + + std::map::iterator revIt = m_revTexNameMap.find((*it).second); + + m_device->DestroyTexture((*it).second); + + m_revTexNameMap.erase(revIt); + m_texNameMap.erase(it); +} + +void Gfx::CEngine::SetMouseVisible(bool visible) +{ + m_mouseVisible = visible; +} + +bool Gfx::CEngine::GetMouseVisible() +{ + return m_mouseVisible; +} + +void Gfx::CEngine::SetMousePos(Math::Point pos) +{ + m_mousePos = pos; +} + +Math::Point Gfx::CEngine::GetMousePos() +{ + return m_mousePos; +} + +void Gfx::CEngine::SetMouseType(Gfx::EngineMouseType type) +{ + m_mouseType = type; +} + +Gfx::EngineMouseType Gfx::CEngine::GetMouseType() +{ + return m_mouseType; +} + +void Gfx::CEngine::AddStatisticTriangle(int count) +{ + m_statisticTriangle += count; +} diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index 735d72a..b1510fd 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -20,8 +20,10 @@ #pragma once +#include "common/event.h" #include "graphics/common/color.h" #include "graphics/common/material.h" +#include "graphics/common/texture.h" #include "graphics/common/vertex.h" #include "math/intpoint.h" #include "math/matrix.h" @@ -30,6 +32,8 @@ #include +#include +#include class CApplication; @@ -51,75 +55,317 @@ class CPlanet; class CTerrain; -//const int MAXOBJECT = 1200; -//const int MAXSHADOW = 500; -//const int MAXGROUNDSPOT = 100; +/** + \enum EngineTriangleType + \brief Type of triangles drawn for engine objects */ +enum EngineTriangleType +{ + //! Triangles + ENG_TRIANGLE_TYPE_6T = 1, + //! Surfaces + ENG_TRIANGLE_TYPE_6S = 2 +}; +/** + \struct EngineTriangle + \brief A triangle drawn by the graphics engine */ +struct EngineTriangle +{ + //! Triangle vertices + Gfx::VertexTex2 triangle[3]; + //! Material + Gfx::Material material; + //! Render state (TODO: ?) + int state; + //! 1st texture + Gfx::Texture tex1; + //! 2nd texture + Gfx::Texture tex2; + + EngineTriangle() + { + state = 0; + } +}; -enum ObjectType +/** + \enum EngineObjectType + \brief Class of graphics engine object */ +enum EngineObjectType { //! Object doesn't exist - OBJTYPE_NULL = 0, + ENG_OBJTYPE_NULL = 0, //! Terrain - OBJTYPE_TERRAIN = 1, + ENG_OBJTYPE_TERRAIN = 1, //! Fixed object - OBJTYPE_FIX = 2, + ENG_OBJTYPE_FIX = 2, //! Moving object - OBJTYPE_VEHICULE = 3, + ENG_OBJTYPE_VEHICULE = 3, //! Part of a moving object - OBJTYPE_DESCENDANT = 4, + ENG_OBJTYPE_DESCENDANT = 4, //! Fixed object type quartz - OBJTYPE_QUARTZ = 5, + ENG_OBJTYPE_QUARTZ = 5, //! Fixed object type metal - OBJTYPE_METAL = 6 + ENG_OBJTYPE_METAL = 6 +}; + +/** + \struct EngineObject + \brief Object drawn by the graphics engine */ +struct EngineObject +{ + //! If true, the object is drawn + bool visible; + //! If true, object is behind the 2D interface + bool drawWorld; + //! If true, the shape is before the 2D interface + bool drawFront; + //! Number of triangles + int totalTriangles; + //! Type of object + Gfx::EngineObjectType type; + //! Transformation matrix + Math::Matrix transform; + //! Distance view - origin (TODO: ?) + float distance; + //! Bounding box min (origin 0,0,0 always included) + Math::Vector bboxMin; + //! bounding box max (origin 0,0,0 always included) + Math::Vector bboxMax; + //! Radius of the sphere at the origin + float radius; + //! Rank of the associated shadow + int shadowRank; + //! Transparency of the object [0, 1] + float transparency; + + EngineObject() + { + visible = false; + drawWorld = false; + drawFront = false; + totalTriangles = 0; + distance = 0.0f; + radius = 0.0f; + shadowRank = 0; + transparency = 0.0f; + } +}; + +struct EngineObjLevel1; +struct EngineObjLevel2; +struct EngineObjLevel3; +struct EngineObjLevel4; +struct EngineObjLevel5; + +/** + \struct EngineObjLevel5 + \brief Tier 5 of object tree */ +struct EngineObjLevel5 +{ + Gfx::Material material; + int state; + Gfx::EngineTriangleType type; + std::vector vertices; + + EngineObjLevel5(); +}; + +/** + \struct EngineObjLevel4 + \brief Tier 4 of object tree */ +struct EngineObjLevel4 +{ + int reserved; + std::vector up; + Gfx::EngineObjLevel3* down; + + EngineObjLevel4(); +}; + +/** + \struct EngineObjLevel3 + \brief Tier 3 of object tree */ +struct EngineObjLevel3 +{ + float min; + float max; + std::vector up; + Gfx::EngineObjLevel2* down; + + EngineObjLevel3(); +}; + +/** + \struct EngineObjLevel2 + \brief Tier 2 of object tree */ +struct EngineObjLevel2 +{ + int objRank; + std::vector up; + Gfx::EngineObjLevel1* down; + + EngineObjLevel2(); +}; + +/** + \struct EngineObjLevel1 + \brief Tier 1 of object tree */ +struct EngineObjLevel1 +{ + Gfx::Texture tex1; + Gfx::Texture tex2; + std::vector up; + + EngineObjLevel1(); +}; + +/** + \struct EngineShadowType + \brief Type of shadow drawn by the graphics engine */ +enum EngineShadowType +{ + //! Normal shadow + ENG_SHADOW_NORM = 0, + //! TODO: ? + ENG_SHADOW_WORM = 1 +}; + +/** + \struct EngineShadow + \brief Shadow drawn by the graphics engine */ +struct EngineShadow +{ + //! If true, shadow is invisible (object being carried for example) + bool hide; + //! Rank of the associated object + int objRank; + //! Type of shadow + Gfx::EngineShadowType type; + //! Position of the shadow + Math::Vector pos; + //! Normal to the terrain + Math::Vector normal; + //! Angle of the shadow + float angle; + //! Radius of the shadow + float radius; + //! Intensity of the shadow + float intensity; + //! Height from the ground + float height; + + EngineShadow() + { + hide = false; + objRank = 0; + angle = radius = intensity = height = 0.0f; + } }; -enum TriangleType +/** + \struct EngineGroundSpot + \brief A spot (large shadow) drawn on the ground by the graphics engine */ +struct EngineGroundSpot { - //! triangles - TRIANGLE_TYPE_6T = 1, - //! surfaces - TRIANGLE_TYPE_6S = 2 + //! Color of the shadow + Gfx::Color color; + //! Min altitude + float min; + //! Max altitude + float max; + //! Transition area + float smooth; + //! Position for the shadow + Math::Vector pos; + //! Radius of the shadow + float radius; + //! Position of the shadow drawn + Math::Vector drawPos; + //! Radius of the shadow drawn + float drawRadius; + + EngineGroundSpot() + { + min = max = smooth = radius = drawRadius = 0.0f; + } }; -enum Mapping +/** + \enum EngineGroundMarkPhase + \brief Phase of life of an EngineGroundMark */ +enum EngineGroundMarkPhase { - MAPPING_X = 1, - MAPPING_Y = 2, - MAPPING_Z = 3, - MAPPING_1X = 4, - MAPPING_1Y = 5, - MAPPING_1Z = 6 + //! Increase + ENG_GR_MARK_PHASE_INC = 1, + //! Fixed + ENG_GR_MARK_PHASE_FIX = 2, + //! Decrease + ENG_GR_MARK_PHASE_DEC = 2 }; -enum MouseType +/** + \struct EngineGroundMark + \brief A mark on ground drawn by the graphics engine */ +struct EngineGroundMark { - MOUSE_HIDE = 0, // no mouse - MOUSE_NORM = 1, - MOUSE_WAIT = 2, - MOUSE_EDIT = 3, - MOUSE_HAND = 4, - MOUSE_CROSS = 5, - MOUSE_SHOW = 6, - MOUSE_NO = 7, - MOUSE_MOVE = 8, // + - MOUSE_MOVEH = 9, // - - MOUSE_MOVEV = 10, // | - MOUSE_MOVED = 11, // / - MOUSE_MOVEI = 12, // \ // - MOUSE_SCROLLL = 13, // << - MOUSE_SCROLLR = 14, // >> - MOUSE_SCROLLU = 15, // ^ - MOUSE_SCROLLD = 16, // v - MOUSE_TARGET = 17 + //! If true, draw mark + bool draw; + //! Phase of life + Gfx::EngineGroundMarkPhase phase; + //! Times for 3 life phases + float delay[3]; + //! Fixed time + float fix; + //! Position for marks + Math::Vector pos; + //! Radius of marks + float radius; + //! Color intensity + float intensity; + //! Draw position for marks + Math::Vector drawPos; + //! Radius for marks + float drawRadius; + //! Draw intensity for marks + float drawIntensity; + //! X dimension of table + int dx; + //! Y dimension of table + int dy; + //! Pointer to the table + char* table; + + EngineGroundMark() + { + draw = false; + delay[0] = delay[1] = delay[2] = 0.0f; + fix = radius = intensity = drawRadius = drawIntensity = 0.0f; + dx = dy = 0; + table = NULL; + } }; -enum ShadowType +/** + \enum EngineTextureMapping + \brief Type of texture mapping + */ +enum EngineTextureMapping { - SHADOW_NORM = 0, - SHADOW_WORM = 1 + ENG_TEX_MAPPING_X = 1, + ENG_TEX_MAPPING_Y = 2, + ENG_TEX_MAPPING_Z = 3, + ENG_TEX_MAPPING_1X = 4, + ENG_TEX_MAPPING_1Y = 5, + ENG_TEX_MAPPING_1Z = 6 }; +/** + \enum EngineRenderState + \brief Render state of graphics engine + + States are used for settings certain modes, for instance texturing and blending. + The enum is a bitmask and some of the states can be OR'd together. */ enum EngineRenderState { //! Normal opaque materials @@ -162,130 +408,70 @@ enum EngineRenderState ENG_RSTATE_TCOLOR_WHITE = (1<<17) }; - -struct Triangle -{ - Gfx::VertexTex2 triangle[3]; - Gfx::Material material; - int state; - char texName1[20]; - char texName2[20]; -}; - - -struct ObjLevel6 -{ - int totalPossible; - int totalUsed; - Gfx::Material material; - int state; - Gfx::TriangleType type; - Gfx::VertexTex2 vertex[1]; -}; - -struct ObjLevel5 -{ - int totalPossible; - int totalUsed; - int reserve; - Gfx::ObjLevel6* table[1]; -}; - -struct ObjLevel4 -{ - int totalPossible; - int totalUsed; - float min, max; - Gfx::ObjLevel5* table[1]; -}; - -struct ObjLevel3 -{ - int totalPossible; - int totalUsed; - int objRank; - Gfx::ObjLevel4* table[1]; -}; - -struct ObjLevel2 -{ - int totalPossible; - int totalUsed; - char texName1[20]; - char texName2[20]; - Gfx::ObjLevel3* table[1]; -}; - -struct ObjLevel1 +/** + \enum EngineMouseType + \brief Type of mouse cursor displayed in-game */ +enum EngineMouseType { - int totalPossible; - int totalUsed; - Gfx::ObjLevel2* table[1]; + //! Normal cursor (arrow) + ENG_MOUSE_NORM = 0, + //! Busy + ENG_MOUSE_WAIT = 1, + //! Edit (I-beam) + ENG_MOUSE_EDIT = 2, + //! Hand + ENG_MOUSE_HAND = 3, + //! Small cross + ENG_MOUSE_CROSS = 4, + //! TODO: ? + ENG_MOUSE_SHOW = 5, + //! Crossed out sign + ENG_MOUSE_NO = 6, + //! Resize + ENG_MOUSE_MOVE = 7, + //! Resize horizontally + ENG_MOUSE_MOVEH = 8, + //! Resize vertically + ENG_MOUSE_MOVEV = 9, + //! Resize diagonally bottom-left to top-right + ENG_MOUSE_MOVED = 10, + //! Resize diagonally top-left to bottom-right + ENG_MOUSE_MOVEI = 11, + //! Scroll to the left + ENG_MOUSE_SCROLLL = 12, + //! Scroll to the right + ENG_MOUSE_SCROLLR = 13, + //! Scroll up + ENG_MOUSE_SCROLLU = 14, + //! Scroll down + ENG_MOUSE_SCROLLD = 15, + //! Larger crosshair + ENG_MOUSE_TARGET = 16, + + //! Number of items in enum + ENG_MOUSE_COUNT }; +/** + \class CEngine + \brief The graphics engine -struct Object -{ - bool used; // true -> object exists - bool visible; // true -> visible object - bool drawWorld; // true -> shape behind the interface - bool drawFront; // true -> shape before the interface - int totalTriangle; // number of triangles used - Gfx::ObjectType type; // type of the object (TYPE*) - Math::Matrix transform; // transformation matrix - float distance; // distance point of view - original - Math::Vector bboxMin; // bounding box of the object - Math::Vector bboxMax; // (the origin 0, 0, 0 is always included) - float radius; // radius of the sphere at the origin - int shadowRank; // rank of the associated shadow - float transparency; // transparency of the object (0 .. 1) -}; - -struct Shadow -{ - bool used; // true -> object exists - bool hide; // true -> invisible shadow (object carried by ex.) - int objRank; // rank of the object - Gfx::ShadowType type; // type of shadow - Math::Vector pos; // position for the shadow - Math::Vector normal; // normal terrain - float angle; // angle of the shadow - float radius; // radius of the shadow - float intensity; // intensity of the shadow - float height; // height from the ground -}; - -struct GroundSpot -{ - bool used; // true -> object exists - Gfx::Color color; // color of the shadow - float min, max; // altitudes min / max - float smooth; // transition area - Math::Vector pos; // position for the shadow - float radius; // radius of the shadow - Math::Vector drawPos; // drawn to position the shade - float drawRadius; // radius of the shadow drawn -}; + This is the main class for graphics engine. It is responsible for drawing the 3D scene, + setting various render states, and facilitating the drawing of 2D interface. -struct GroundMark -{ - bool used; // true -> object exists - bool draw; // true -> drawn mark - int phase; // 1 = increase, 2 = fixed, 3 = decrease - float delay[3]; // time for 3 phases - float fix; // fixed time - Math::Vector pos; // position for marks - float radius; // radius of marks - float intensity; // color intensity - Math::Vector drawPos; // drawn in position marks - float drawRadius; // radius marks drawn - float drawIntensity; // current drawn - int dx, dy; // dimensions table - char* table; // pointer to the table -}; + It uses a lower-level CDevice object which is implementation-independent core engine. + \section Objecs Engine objects + The 3D scene is composed of objects which are basically collections of triangles forming + a surface or simply independent triangles in space. Objects are stored in the engine + as a tree structure which is composed of 5 tiers (EngineObjLevel1, EngineObjLevel2 and so on). + Each tier stores some data about object triangle, like textures or materials used. + Additional information on objects stored are in EngineObject structure. + Each object is uniquely identified by its rank. + ... + */ class CEngine { public: @@ -304,12 +490,12 @@ public: bool AfterDeviceSetInit(); + void SetTerrain(Gfx::CTerrain* terrain); - bool Render(); - + bool ProcessEvent(const Event &event); + bool Render(); - void SetTerrain(Gfx::CTerrain* terrain); bool WriteProfile(); @@ -329,7 +515,7 @@ public: int DeleteDeviceObjects(); int RestoreSurfaces(); int FrameMove(float rTime); - void StepSimul(float rTime); + void StepSimulation(float rTime); int FinalCleanup(); void AddStatisticTriangle(int nb); int GetStatisticTriangle(); @@ -337,16 +523,10 @@ public: bool GetHilite(Math::Point &p1, Math::Point &p2); bool GetSpriteCoord(int &x, int &y); void SetInfoText(int line, char* text); - char * GetInfoText(int line); - //LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + char* GetInfoText(int line); void FirstExecuteAdapt(bool first); - //int GetVidMemTotal(); - //bool IsVideo8MB(); - //bool IsVideo32MB(); - bool EnumDevices(char *bufDevices, int lenDevices, char *bufModes, int lenModes, int &totalDevices, int &selectDevices, int &totalModes, int &selectModes); bool GetFullScreen(); - bool ChangeDevice(char *device, char *mode, bool full); Math::Matrix* GetMatView(); Math::Matrix* GetMatLeftView(); @@ -363,27 +543,43 @@ public: bool DeleteObject(int objRank); bool SetDrawWorld(int objRank, bool draw); bool SetDrawFront(int objRank, bool draw); - bool AddTriangle(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max, bool globalUpdate); - bool AddSurface(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max, bool globalUpdate); - bool AddQuick(int objRank, Gfx::ObjLevel6* buffer, char* texName1, char* texName2, float min, float max, bool globalUpdate); - Gfx::ObjLevel6* SearchTriangle(int objRank, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max); + + bool AddTriangle(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, + int state, std::string texName1, std::string texName2, + float min, float max, bool globalUpdate); + bool AddSurface(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, + int state, std::string texName1, std::string texName2, + float min, float max, bool globalUpdate); + bool AddQuick(int objRank, Gfx::EngineObjLevel5* buffer, + std::string texName1, std::string texName2, + float min, float max, bool globalUpdate); + Gfx::EngineObjLevel5* SearchTriangle(int objRank, const Gfx::Material &mat, + int state, std::string texName1, std::string texName2, + float min, float max); + void ChangeLOD(); bool ChangeSecondTexture(int objRank, char* texName2); int GetTotalTriangles(int objRank); - int GetTriangles(int objRank, float min, float max, Gfx::Triangle* buffer, int size, float percent); + int GetTriangles(int objRank, float min, float max, Gfx::EngineTriangle* buffer, int size, float percent); bool GetBBox(int objRank, Math::Vector &min, Math::Vector &max); - bool ChangeTextureMapping(int objRank, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max, Gfx::Mapping mode, float au, float bu, float av, float bv); - bool TrackTextureMapping(int objRank, const Gfx::Material &mat, int state, char* texName1, char* texName2, float min, float max, Gfx::Mapping mode, float pos, float factor, float tl, float ts, float tt); + bool ChangeTextureMapping(int objRank, const Gfx::Material &mat, int state, + const std::string &texName1, const std::string &texName2, + float min, float max, Gfx::EngineTextureMapping mode, + float au, float bu, float av, float bv); + bool TrackTextureMapping(int objRank, const Gfx::Material &mat, int state, + const std::string &texName1, const std::string &texName2, + float min, float max, Gfx::EngineTextureMapping mode, + float pos, float factor, float tl, float ts, float tt); bool SetObjectTransform(int objRank, const Math::Matrix &transform); bool GetObjectTransform(int objRank, Math::Matrix &transform); - bool SetObjectType(int objRank, Gfx::ObjectType type); - Gfx::ObjectType GetObjectType(int objRank); + bool SetObjectType(int objRank, Gfx::EngineObjectType type); + Gfx::EngineObjectType GetObjectType(int objRank); bool SetObjectTransparency(int objRank, float value); bool ShadowCreate(int objRank); void ShadowDelete(int objRank); bool SetObjectShadowHide(int objRank, bool hide); - bool SetObjectShadowType(int objRank, Gfx::ShadowType type); + bool SetObjectShadowType(int objRank, Gfx::EngineShadowType type); bool SetObjectShadowPos(int objRank, const Math::Vector &pos); bool SetObjectShadowNormal(int objRank, const Math::Vector &n); bool SetObjectShadowAngle(int objRank, float angle); @@ -401,16 +597,23 @@ public: bool SetObjectGroundSpotMinMax(int rank, float min, float max); bool SetObjectGroundSpotSmooth(int rank, float smooth); - int GroundMarkCreate(Math::Vector pos, float radius, float delay1, float delay2, float delay3, int dx, int dy, char* table); + int GroundMarkCreate(Math::Vector pos, float radius, + float delay1, float delay2, float delay3, + int dx, int dy, char* table); bool GroundMarkDelete(int rank); void Update(); - void SetViewParams(const Math::Vector &vEyePt, const Math::Vector &vLookatPt, const Math::Vector &vUpVec, float fEyeDistance); + void SetViewParams(const Math::Vector &eyePt, const Math::Vector &lookatPt, + const Math::Vector &upVec, float eyeDistance); + + Gfx::Texture CreateTexture(const std::string &texName, + const Gfx::TextureCreateParams ¶ms); + Gfx::Texture CreateTexture(const std::string &texName); + void DestroyTexture(const std::string &texName); - bool FreeTexture(char* name); - bool LoadTexture(char* name, int stage=0); - bool LoadAllTexture(); + bool LoadTexture(const std::string &name, int stage = 0); + bool LoadAllTextures(); void SetLimitLOD(int rank, float limit); float GetLimitLOD(int rank, bool last=false); @@ -436,30 +639,34 @@ public: void SetDrawWorld(bool draw); void SetDrawFront(bool draw); - void SetAmbiantColor(const Gfx::Color &color, int rank=0); - Gfx::Color GetAmbiantColor(int rank=0); + void SetAmbientColor(const Gfx::Color &color, int rank = 0); + Gfx::Color GetAmbientColor(int rank = 0); void SetWaterAddColor(const Gfx::Color &color); Gfx::Color GetWaterAddColor(); - void SetFogColor(const Gfx::Color &color, int rank=0); - Gfx::Color GetFogColor(int rank=0); + void SetFogColor(const Gfx::Color &color, int rank = 0); + Gfx::Color GetFogColor(int rank = 0); - void SetDeepView(float length, int rank=0, bool ref=false); - float GetDeepView(int rank=0); + void SetDeepView(float length, int rank = 0, bool ref=false); + float GetDeepView(int rank = 0); - void SetFogStart(float start, int rank=0); - float GetFogStart(int rank=0); + void SetFogStart(float start, int rank = 0); + float GetFogStart(int rank = 0); - void SetBackground(char *name, Gfx::Color up=Gfx::Color(), Gfx::Color down=Gfx::Color(), Gfx::Color cloudUp=Gfx::Color(), Gfx::Color cloudDown=Gfx::Color(), bool full=false, bool quarter=false); - void GetBackground(char *name, Gfx::Color &up, Gfx::Color &down, Gfx::Color &cloudUp, Gfx::Color &cloudDown, bool &full, bool &quarter); + void SetBackground(const std::string &name, Gfx::Color up = Gfx::Color(), Gfx::Color down = Gfx::Color(), + Gfx::Color cloudUp = Gfx::Color(), Gfx::Color cloudDown = Gfx::Color(), + bool full = false, bool quarter = false); + void GetBackground(const std::string &name, Gfx::Color &up, Gfx::Color &down, + Gfx::Color &cloudUp, Gfx::Color &cloudDown, + bool &full, bool &quarter); void SetFrontsizeName(char *name); void SetOverFront(bool front); - void SetOverColor(const Gfx::Color &color=Gfx::Color(), int mode=ENG_RSTATE_TCOLOR_BLACK); + void SetOverColor(const Gfx::Color &color = Gfx::Color(), int mode = ENG_RSTATE_TCOLOR_BLACK); - void SetParticuleDensity(float value); - float GetParticuleDensity(); - float ParticuleAdapt(float factor); + void SetParticleDensity(float value); + float GetParticleDensity(); + float ParticleAdapt(float factor); void SetClippingDistance(float value); float GetClippingDistance(); @@ -482,8 +689,8 @@ public: void SetWaterMode(bool present); bool GetWaterMode(); - void SetBlitzMode(bool present); - bool GetBlitzMode(); + void SetLightingMode(bool present); + bool GetLightingMode(); void SetSkyMode(bool present); bool GetSkyMode(); @@ -535,24 +742,24 @@ public: bool IsVisiblePoint(const Math::Vector &pos); int DetectObject(Math::Point mouse); - void SetState(int state, Gfx::Color color=Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); - void SetTexture(char *name, int stage=0); + void SetState(int state, Gfx::Color color = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); + void SetTexture(const std::string &name, int stage = 0); void SetMaterial(const Gfx::Material &mat); - void MoveMousePos(Math::Point pos); - void SetMousePos(Math::Point pos); - Math::Point GetMousePos(); - void SetMouseType(Gfx::MouseType type); - Gfx::MouseType GetMouseType(); - void SetMouseHide(bool hide); - bool GetMouseHide(); - void SetNiceMouse(bool nice); - bool GetNiceMouse(); - bool GetNiceMouseCap(); + void SetMouseVisible(bool show); + bool GetMouseVisible(); + void SetMousePos(Math::Point pos); + Math::Point GetMousePos(); + void SetMouseType(Gfx::EngineMouseType type); + Gfx::EngineMouseType GetMouseType(); CText* GetText(); - bool ChangeColor(char *name, Gfx::Color colorRef1, Gfx::Color colorNew1, Gfx::Color colorRef2, Gfx::Color colorNew2, float tolerance1, float tolerance2, Math::Point ts, Math::Point ti, Math::Point *pExclu=0, float shift=0.0f, bool hSV=false); + bool ChangeColor(char *name, Gfx::Color colorRef1, Gfx::Color colorNew1, + Gfx::Color colorRef2, Gfx::Color colorNew2, + float tolerance1, float tolerance2, + Math::Point ts, Math::Point ti, + Math::Point *pExclu=0, float shift=0.0f, bool hSV=false); bool OpenImage(char *name); bool CopyImage(); bool LoadImage(); @@ -566,26 +773,14 @@ public: //bool CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC); protected: - void MemSpace1(Gfx::ObjLevel1 *&p, int nb); - void MemSpace2(Gfx::ObjLevel2 *&p, int nb); - void MemSpace3(Gfx::ObjLevel3 *&p, int nb); - void MemSpace4(Gfx::ObjLevel4 *&p, int nb); - void MemSpace5(Gfx::ObjLevel5 *&p, int nb); - void MemSpace6(Gfx::ObjLevel6 *&p, int nb); - Gfx::ObjLevel2* AddLevel1(Gfx::ObjLevel1 *&p1, char* texName1, char* texName2); - Gfx::ObjLevel3* AddLevel2(Gfx::ObjLevel2 *&p2, int objRank); - Gfx::ObjLevel4* AddLevel3(Gfx::ObjLevel3 *&p3, float min, float max); - Gfx::ObjLevel5* AddLevel4(Gfx::ObjLevel4 *&p4, int reserve); - Gfx::ObjLevel6* AddLevel5(Gfx::ObjLevel5 *&p5, Gfx::TriangleType type, const Gfx::Material &mat, int state, int nb); + void SetUp3DView(); + bool Draw3DScene(); - bool IsVisible(int objRank); - bool DetectBBox(int objRank, Math::Point mouse); - bool DetectTriangle(Math::Point mouse, Gfx::VertexTex2 *triangle, int objRank, float &dist); - bool TransformPoint(Math::Vector &p2D, int objRank, Math::Vector p3D); - void ComputeDistance(); - void UpdateGeometry(); - void RenderGroundSpot(); + void SetUpInterfaceView(); + bool DrawInterface(); + + void DrawGroundSpot(); void DrawShadow(); void DrawBackground(); void DrawBackgroundGradient(Gfx::Color up, Gfx::Color down); @@ -594,14 +789,29 @@ protected: void DrawPlanet(); void DrawFrontsize(); void DrawOverColor(); - bool GetBBox2D(int objRank, Math::Point &min, Math::Point &max); void DrawHilite(); void DrawMouse(); - void DrawSprite(Math::Point pos, Math::Point dim, int icon); + void DrawMouseSprite(Math::Point pos, Math::Point dim, int icon); + + /* + Gfx::ObjLevel2* AddLevel1(Gfx::ObjLevel1 *&p1, char* texName1, char* texName2); + Gfx::ObjLevel3* AddLevel2(Gfx::ObjLevel2 *&p2, int objRank); + Gfx::ObjLevel4* AddLevel3(Gfx::ObjLevel3 *&p3, float min, float max); + Gfx::ObjLevel5* AddLevel4(Gfx::ObjLevel4 *&p4, int reserve); + Gfx::ObjLevel6* AddLevel5(Gfx::ObjLevel5 *&p5, Gfx::TriangleType type, const Gfx::Material &mat, int state, int nb);*/ + + bool IsVisible(int objRank); + bool DetectBBox(int objRank, Math::Point mouse); + bool GetBBox2D(int objRank, Math::Point &min, Math::Point &max); + bool DetectTriangle(Math::Point mouse, Gfx::VertexTex2 *triangle, int objRank, float &dist); + bool TransformPoint(Math::Vector &p2D, int objRank, Math::Vector p3D); + void ComputeDistance(); + void UpdateGeometry(); protected: CInstanceManager* m_iMan; CApplication* m_app; + CSound* m_sound; Gfx::CDevice* m_device; Gfx::CText* m_text; Gfx::CLight* m_light; @@ -611,7 +821,6 @@ protected: Gfx::CLightning* m_lightning; Gfx::CPlanet* m_planet; Gfx::CTerrain* m_terrain; - CSound* m_sound; bool m_wasInit; std::string m_error; @@ -644,21 +853,21 @@ protected: bool m_render; bool m_movieLock; - Math::IntPoint m_dim; - Math::IntPoint m_lastDim; - Gfx::ObjLevel1* m_objectPointer; - int m_objectParamTotal; - Gfx::Object* m_objectParam; - int m_shadowTotal; - Gfx::Shadow* m_shadow; - Gfx::GroundSpot* m_groundSpot; - Gfx::GroundMark m_groundMark; - Math::Vector m_eyePt; - Math::Vector m_lookatPt; + Math::IntPoint m_dim; + Math::IntPoint m_lastDim; + + std::vector m_objectTree; + std::vector m_objects; + std::vector m_shadow; + std::vector m_groundSpot; + Gfx::EngineGroundMark m_groundMark; + + Math::Vector m_eyePt; + Math::Vector m_lookatPt; float m_eyeDirH; float m_eyeDirV; int m_rankView; - Gfx::Color m_ambiantColor[2]; + Gfx::Color m_ambientColor[2]; Gfx::Color m_backColor[2]; Gfx::Color m_fogColor[2]; float m_deepView[2]; @@ -666,7 +875,7 @@ protected: Gfx::Color m_waterAddColor; int m_statisticTriangle; bool m_updateGeometry; - char m_infoText[10][200]; + //char m_infoText[10][200]; int m_alphaMode; bool m_stateColor; bool m_forceStateColor; @@ -676,7 +885,7 @@ protected: bool m_fog; bool m_firstGroundSpot; int m_secondTexNum; - char m_backgroundName[50]; + std::string m_backgroundName; Gfx::Color m_backgroundColorUp; Gfx::Color m_backgroundColorDown; Gfx::Color m_backgroundCloudUp; @@ -686,7 +895,7 @@ protected: bool m_overFront; Gfx::Color m_overColor; int m_overMode; - char m_frontsizeName[50]; + std::string m_frontsizeName; bool m_drawWorld; bool m_drawFront; float m_limitLOD[2]; @@ -719,10 +928,15 @@ protected: char m_lastTexture[2][50]; Gfx::Material m_lastMaterial; - Math::Point m_mousePos; - Gfx::MouseType m_mouseType; - bool m_mouseHide; - bool m_niceMouse; + std::string m_texPath; + Gfx::TextureCreateParams m_defaultTexParams; + + std::map m_texNameMap; + std::map m_revTexNameMap; + + Gfx::EngineMouseType m_mouseType; + Math::Point m_mousePos; + bool m_mouseVisible; //LPDIRECTDRAWSURFACE7 m_imageSurface; //DDSURFACEDESC2 m_imageDDSD; diff --git a/src/graphics/common/light.h b/src/graphics/common/light.h index f85d8da..46f1bfa 100644 --- a/src/graphics/common/light.h +++ b/src/graphics/common/light.h @@ -114,9 +114,9 @@ struct SceneLight bool enabled; //! Type of all objects included - Gfx::ObjectType includeType; + Gfx::EngineObjectType includeType; //! Type of all objects excluded - Gfx::ObjectType excludeType; + Gfx::EngineObjectType excludeType; //! Configuration of the light Gfx::Light light; diff --git a/src/graphics/common/particle.h b/src/graphics/common/particle.h index ec87648..bd9741f 100644 --- a/src/graphics/common/particle.h +++ b/src/graphics/common/particle.h @@ -265,7 +265,7 @@ public: void FlushParticle(); void FlushParticle(int sheet); int CreateParticle(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreateFrag(Math::Vector pos, Math::Vector speed, Triangle *triangle, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreateFrag(Math::Vector pos, Math::Vector speed, Gfx::EngineTriangle *triangle, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); int CreatePart(Math::Vector pos, Math::Vector speed, ParticleType type, float duration=1.0f, float mass=0.0f, float weight=0.0f, float windSensitivity=1.0f, int sheet=0); int CreateRay(Math::Vector pos, Math::Vector goal, ParticleType type, Math::Point dim, float duration=1.0f, int sheet=0); int CreateTrack(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float length=10.0f, float width=1.0f); @@ -318,7 +318,7 @@ protected: CSound* m_sound; Gfx::Particle m_particule[MAXPARTICULE*MAXPARTITYPE]; - Gfx::Triangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 + Gfx::EngineTriangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 Track m_track[MAXTRACK]; int m_wheelTraceTotal; int m_wheelTraceIndex; diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index fa374ac..83986ea 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -166,9 +166,48 @@ struct Texture bool valid; //! Id of the texture in graphics engine unsigned int id; + //! Width of texture + int width; + //! Height of texture + int height; + //! Whether the texture has alpha channel + bool alpha; Texture() - { valid = false; id = 0; } + { + valid = false; + id = 0; + width = height = 0; + alpha = false; + } + + //! Comparator for use in texture maps and sets + inline bool operator<(const Gfx::Texture &other) const + { + // Invalid textures are always "less than" every other texture + + if ( (!valid) && (!other.valid) ) + return false; + + if (!valid) + return true; + + if (!other.valid) + return false; + + return id < other.id; + } + + //! Comparator + inline bool operator==(const Gfx::Texture &other) const + { + if (valid != other.valid) + return false; + if ( (!valid) && (!other.valid) ) + return true; + + return id == other.id; + } }; }; // namespace Gfx diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 19a9cff..3d63bf2 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -30,21 +30,6 @@ #include -namespace Gfx { - -struct GLDevicePrivate -{ - void (APIENTRY* glMultiTexCoord2fARB)(GLenum target, GLfloat s, GLfloat t); - void (APIENTRY* glActiveTextureARB)(GLenum texture); - - GLDevicePrivate() - { - glMultiTexCoord2fARB = NULL; - glActiveTextureARB = NULL; - } -}; - -}; // namespace Gfx void Gfx::GLDeviceConfig::LoadDefault() @@ -65,7 +50,6 @@ void Gfx::GLDeviceConfig::LoadDefault() Gfx::CGLDevice::CGLDevice() { - m_private = new Gfx::GLDevicePrivate(); m_wasInit = false; m_texturing = false; } @@ -73,8 +57,6 @@ Gfx::CGLDevice::CGLDevice() Gfx::CGLDevice::~CGLDevice() { - delete m_private; - m_private = NULL; } bool Gfx::CGLDevice::GetWasInit() @@ -108,15 +90,6 @@ bool Gfx::CGLDevice::Create() return false; } - /*m_private->glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC) SDL_GL_GetProcAddress("glMultiTexCoord2fARB"); - m_private->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) SDL_GL_GetProcAddress("glActiveTextureARB"); - - if ((m_private->glMultiTexCoord2fARB == NULL) || (m_private->glActiveTextureARB == NULL)) - { - m_error = "Could not load extension functions, even though they seem supported"; - return false; - }*/ - m_wasInit = true; // This is mostly done in all modern hardware by default @@ -141,7 +114,7 @@ bool Gfx::CGLDevice::Create() int maxTextures = 0; glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures); - m_textures = std::vector (maxTextures, (Gfx::Texture*)(NULL)); + m_textures = std::vector (maxTextures, Gfx::Texture()); m_texturesEnabled = std::vector (maxTextures, false); m_texturesParams = std::vector(maxTextures, Gfx::TextureParams()); @@ -150,9 +123,6 @@ bool Gfx::CGLDevice::Create() void Gfx::CGLDevice::Destroy() { - /*m_private->glMultiTexCoord2fARB = NULL; - m_private->glActiveTextureARB = NULL;*/ - // Delete the remaining textures // Should not be strictly necessary, but just in case DestroyAllTextures(); @@ -174,8 +144,7 @@ void Gfx::CGLDevice::BeginScene() glMatrixMode(GL_PROJECTION); glLoadMatrixf(m_projectionMat.Array()); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(m_modelviewMat.Array()); + UpdateModelviewMatrix(); } void Gfx::CGLDevice::EndScene() @@ -193,16 +162,12 @@ void Gfx::CGLDevice::SetTransform(Gfx::TransformType type, const Math::Matrix &m if (type == Gfx::TRANSFORM_WORLD) { m_worldMat = matrix; - m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(m_modelviewMat.Array()); + UpdateModelviewMatrix(); } else if (type == Gfx::TRANSFORM_VIEW) { m_viewMat = matrix; - m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(m_modelviewMat.Array()); + UpdateModelviewMatrix(); } else if (type == Gfx::TRANSFORM_PROJECTION) { @@ -235,16 +200,12 @@ void Gfx::CGLDevice::MultiplyTransform(Gfx::TransformType type, const Math::Matr if (type == Gfx::TRANSFORM_WORLD) { m_worldMat = Math::MultiplyMatrices(m_worldMat, matrix); - m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(m_modelviewMat.Array()); + UpdateModelviewMatrix(); } else if (type == Gfx::TRANSFORM_VIEW) { m_viewMat = Math::MultiplyMatrices(m_viewMat, matrix); - m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(m_modelviewMat.Array()); + UpdateModelviewMatrix(); } else if (type == Gfx::TRANSFORM_PROJECTION) { @@ -258,7 +219,17 @@ void Gfx::CGLDevice::MultiplyTransform(Gfx::TransformType type, const Math::Matr } } -void Gfx::CGLDevice::SetMaterial(Gfx::Material &material) +void Gfx::CGLDevice::UpdateModelviewMatrix() +{ + m_modelviewMat = Math::MultiplyMatrices(m_viewMat, m_worldMat); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glScalef(1.0f, 1.0f, -1.0f); + glMultMatrixf(m_modelviewMat.Array()); +} + +void Gfx::CGLDevice::SetMaterial(const Gfx::Material &material) { m_material = material; @@ -277,7 +248,7 @@ int Gfx::CGLDevice::GetMaxLightCount() return m_lights.size(); } -void Gfx::CGLDevice::SetLight(int index, Gfx::Light &light) +void Gfx::CGLDevice::SetLight(int index, const Gfx::Light &light) { assert(index >= 0); assert(index < (int)m_lights.size()); @@ -285,9 +256,9 @@ void Gfx::CGLDevice::SetLight(int index, Gfx::Light &light) m_lights[index] = light; // Indexing from GL_LIGHT0 should always work - glLightfv(GL_LIGHT0 + index, GL_AMBIENT, light.ambient.Array()); - glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, light.diffuse.Array()); - glLightfv(GL_LIGHT0 + index, GL_SPECULAR, light.specular.Array()); + glLightfv(GL_LIGHT0 + index, GL_AMBIENT, const_cast(light.ambient.Array())); + glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, const_cast(light.diffuse.Array())); + glLightfv(GL_LIGHT0 + index, GL_SPECULAR, const_cast(light.specular.Array())); GLfloat position[4] = { light.position.x, light.position.y, light.position.z, 0.0f }; if (light.type == LIGHT_DIRECTIONAL) @@ -334,17 +305,23 @@ bool Gfx::CGLDevice::GetLightEnabled(int index) return m_lightsEnabled[index]; } -/** If image is invalid, returns NULL. +/** If image is invalid, returns invalid texture. Otherwise, returns pointer to new Gfx::Texture struct. This struct must not be deleted in other way than through DeleteTexture() */ -Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) +Gfx::Texture Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) { + Gfx::Texture result; + ImageData *data = image->GetData(); if (data == NULL) - return NULL; + { + m_error = "Invalid texture data"; + return result; // invalid texture + } - Gfx::Texture *result = new Gfx::Texture(); - result->valid = true; + result.valid = true; + result.width = data->surface->w; + result.height = data->surface->h; // Use & enable 1st texture stage glActiveTextureARB(GL_TEXTURE0_ARB); @@ -352,8 +329,8 @@ Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCre glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glGenTextures(1, &result->id); - glBindTexture(GL_TEXTURE_2D, result->id); + glGenTextures(1, &result.id); + glBindTexture(GL_TEXTURE_2D, result.id); // Set params @@ -395,13 +372,25 @@ Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCre GLenum sourceFormat = 0; if (params.format == Gfx::TEX_IMG_RGB) + { sourceFormat = GL_RGB; + result.alpha = false; + } else if (params.format == Gfx::TEX_IMG_BGR) + { sourceFormat = GL_BGR; + result.alpha = false; + } else if (params.format == Gfx::TEX_IMG_RGBA) + { sourceFormat = GL_RGBA; + result.alpha = true; + } else if (params.format == Gfx::TEX_IMG_BGRA) + { sourceFormat = GL_BGRA; + result.alpha = true; + } else assert(false); @@ -410,10 +399,10 @@ Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCre // Restore the previous state of 1st stage - if (m_textures[0] == NULL) - glBindTexture(GL_TEXTURE_2D, 0); + if (m_textures[0].valid) + glBindTexture(GL_TEXTURE_2D, m_textures[0].id); else - glBindTexture(GL_TEXTURE_2D, m_textures[0]->id); + glBindTexture(GL_TEXTURE_2D, 0); if ( (! m_texturing) || (! m_texturesEnabled[0]) ) glDisable(GL_TEXTURE_2D); @@ -421,9 +410,9 @@ Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCre return result; } -void Gfx::CGLDevice::DestroyTexture(Gfx::Texture *texture) +void Gfx::CGLDevice::DestroyTexture(const Gfx::Texture &texture) { - std::set::iterator it = m_allTextures.find(texture); + std::set::iterator it = m_allTextures.find(texture); if (it != m_allTextures.end()) m_allTextures.erase(it); @@ -431,21 +420,18 @@ void Gfx::CGLDevice::DestroyTexture(Gfx::Texture *texture) for (int index = 0; index < (int)m_textures.size(); ++index) { if (m_textures[index] == texture) - SetTexture(index, NULL); + SetTexture(index, Gfx::Texture()); // set to invalid texture } - glDeleteTextures(1, &texture->id); + glDeleteTextures(1, &texture.id); } void Gfx::CGLDevice::DestroyAllTextures() { - std::set allCopy = m_allTextures; - std::set::iterator it; + std::set allCopy = m_allTextures; + std::set::iterator it; for (it = allCopy.begin(); it != allCopy.end(); ++it) - { DestroyTexture(*it); - delete *it; - } } int Gfx::CGLDevice::GetMaxTextureCount() @@ -454,10 +440,10 @@ int Gfx::CGLDevice::GetMaxTextureCount() } /** - If \a texture is \c NULL or invalid, unbinds the given texture. + If \a texture is invalid, unbinds the given texture. If valid, binds the texture and enables the given texture stage. The setting is remembered, even if texturing is disabled at the moment. */ -void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) +void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) { assert(index >= 0); assert(index < (int)m_textures.size()); @@ -466,15 +452,15 @@ void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) glActiveTextureARB(GL_TEXTURE0_ARB + index); glEnable(GL_TEXTURE_2D); - if ((texture == NULL) || (! texture->valid)) + m_textures[index] = texture; // remember the change + + if (! texture.valid) { glBindTexture(GL_TEXTURE_2D, 0); // unbind texture - m_textures[index] = NULL; // remember the changes } else { - glBindTexture(GL_TEXTURE_2D, texture->id); // bind the texture - m_textures[index] = texture; // remember the changes + glBindTexture(GL_TEXTURE_2D, texture.id); // bind the texture SetTextureParams(index, m_texturesParams[index]); // texture params need to be re-set for the new texture } @@ -484,8 +470,8 @@ void Gfx::CGLDevice::SetTexture(int index, Gfx::Texture *texture) } /** - Returns the previously assigned texture or \c NULL if the given stage is not enabled. */ -Gfx::Texture* Gfx::CGLDevice::GetTexture(int index) + Returns the previously assigned texture or invalid texture if the given stage is not enabled. */ +Gfx::Texture Gfx::CGLDevice::GetTexture(int index) { assert(index >= 0); assert(index < (int)m_textures.size()); @@ -528,14 +514,14 @@ void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶m m_texturesParams[index] = params; // Don't actually do anything if texture not set - if (m_textures[index] == NULL) + if (! m_textures[index].valid) return; // Enable the given stage glActiveTextureARB(GL_TEXTURE0_ARB + index); glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, m_textures[index]->id); + glBindTexture(GL_TEXTURE_2D, m_textures[index].id); // Selection of operation and arguments glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); @@ -639,7 +625,7 @@ Gfx::TextureParams Gfx::CGLDevice::GetTextureParams(int index) return m_texturesParams[index]; } -void Gfx::CGLDevice::SetTextureFactor(Gfx::Color &color) +void Gfx::CGLDevice::SetTextureFactor(const Gfx::Color &color) { // Needs to be set for all texture stages for (int index = 0; index < (int)m_textures.size(); ++index) @@ -749,7 +735,7 @@ bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float */ // TODO: testing -int Gfx::CGLDevice::ComputeSphereVisibility(Math::Vector center, float radius) +int Gfx::CGLDevice::ComputeSphereVisibility(const Math::Vector ¢er, float radius) { Math::Matrix m; m.LoadIdentity(); @@ -847,6 +833,7 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; case Gfx::RENDER_STATE_ALPHA_TEST: flag = GL_ALPHA_TEST; break; + case Gfx::RENDER_STATE_CULLING: flag = GL_CULL_FACE; break; case Gfx::RENDER_STATE_DITHERING: flag = GL_DITHER; break; default: assert(false); break; } @@ -872,6 +859,7 @@ bool Gfx::CGLDevice::GetRenderState(Gfx::RenderState state) case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; case Gfx::RENDER_STATE_ALPHA_TEST: flag = GL_ALPHA_TEST; break; + case Gfx::RENDER_STATE_CULLING: flag = GL_CULL_FACE; break; case Gfx::RENDER_STATE_DITHERING: flag = GL_DITHER; break; default: assert(false); break; } @@ -1011,7 +999,7 @@ void Gfx::CGLDevice::GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstB dstBlend = TranslateGLBlendFunc(dstFlag); } -void Gfx::CGLDevice::SetClearColor(Gfx::Color color) +void Gfx::CGLDevice::SetClearColor(const Gfx::Color &color) { glClearColor(color.r, color.g, color.b, color.a); } @@ -1023,7 +1011,7 @@ Gfx::Color Gfx::CGLDevice::GetClearColor() return Gfx::Color(color[0], color[1], color[2], color[3]); } -void Gfx::CGLDevice::SetGlobalAmbient(Gfx::Color color) +void Gfx::CGLDevice::SetGlobalAmbient(const Gfx::Color &color) { glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color.Array()); } @@ -1035,7 +1023,7 @@ Gfx::Color Gfx::CGLDevice::GetGlobalAmbient() return Gfx::Color(color[0], color[1], color[2], color[3]); } -void Gfx::CGLDevice::SetFogParams(Gfx::FogMode mode, Gfx::Color color, float start, float end, float density) +void Gfx::CGLDevice::SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, float start, float end, float density) { if (mode == Gfx::FOG_LINEAR) glFogi(GL_FOG_MODE, GL_LINEAR); else if (mode == Gfx::FOG_EXP) glFogi(GL_FOG_MODE, GL_EXP); diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 56ea2c0..792ea4b 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -88,36 +88,36 @@ public: virtual const Math::Matrix& GetTransform(Gfx::TransformType type); virtual void MultiplyTransform(Gfx::TransformType type, const Math::Matrix &matrix); - virtual void SetMaterial(Gfx::Material &material); + virtual void SetMaterial(const Gfx::Material &material); virtual const Gfx::Material& GetMaterial(); virtual int GetMaxLightCount(); - virtual void SetLight(int index, Gfx::Light &light); + virtual void SetLight(int index, const Gfx::Light &light); virtual const Gfx::Light& GetLight(int index); virtual void SetLightEnabled(int index, bool enabled); virtual bool GetLightEnabled(int index); - virtual Gfx::Texture* CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms); - virtual void DestroyTexture(Gfx::Texture *texture); + virtual Gfx::Texture CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms); + virtual void DestroyTexture(const Gfx::Texture &texture); virtual void DestroyAllTextures(); virtual int GetMaxTextureCount(); - virtual void SetTexture(int index, Gfx::Texture *texture); - virtual Gfx::Texture* GetTexture(int index); + virtual void SetTexture(int index, const Gfx::Texture &texture); + virtual Gfx::Texture GetTexture(int index); virtual void SetTextureEnabled(int index, bool enabled); virtual bool GetTextureEnabled(int index); virtual void SetTextureParams(int index, const Gfx::TextureParams ¶ms); virtual Gfx::TextureParams GetTextureParams(int index); - virtual void SetTextureFactor(Gfx::Color &color); + virtual void SetTextureFactor(const Gfx::Color &color); virtual Gfx::Color GetTextureFactor(); virtual void DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount); virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount); virtual void DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount); - virtual int ComputeSphereVisibility(Math::Vector center, float radius); + virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius); virtual void SetRenderState(Gfx::RenderState state, bool enabled); virtual bool GetRenderState(Gfx::RenderState state); @@ -134,13 +134,13 @@ public: virtual void SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend); virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend); - virtual void SetClearColor(Gfx::Color color); + virtual void SetClearColor(const Gfx::Color &color); virtual Gfx::Color GetClearColor(); - virtual void SetGlobalAmbient(Gfx::Color color); + virtual void SetGlobalAmbient(const Gfx::Color &color); virtual Gfx::Color GetGlobalAmbient(); - virtual void SetFogParams(Gfx::FogMode mode, Gfx::Color color, float start, float end, float density); + virtual void SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, float start, float end, float density); virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density); virtual void SetCullMode(Gfx::CullMode mode); @@ -153,8 +153,9 @@ public: virtual Gfx::FillMode GetFillMode(); private: - //! Private, OpenGL-specific data - GLDevicePrivate* m_private; + void UpdateModelviewMatrix(); + +private: //! Was initialized? bool m_wasInit; //! Last encountered error @@ -180,14 +181,14 @@ private: //! Whether texturing is enabled in general bool m_texturing; //! Current textures; \c NULL value means unassigned - std::vector m_textures; + std::vector m_textures; //! Current texture stages enable status std::vector m_texturesEnabled; //! Current texture params std::vector m_texturesParams; //! Set of all created textures - std::set m_allTextures; + std::set m_allTextures; }; }; // namespace Gfx diff --git a/src/graphics/opengl/glengine.cpp b/src/graphics/opengl/glengine.cpp deleted file mode 100644 index 9aab348..0000000 --- a/src/graphics/opengl/glengine.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// glengine.h - -#include "graphics/opengl/glengine.h" - -// TODO \ No newline at end of file diff --git a/src/graphics/opengl/glengine.h b/src/graphics/opengl/glengine.h deleted file mode 100644 index fa67bfe..0000000 --- a/src/graphics/opengl/glengine.h +++ /dev/null @@ -1,32 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// glengine.h - -#pragma once - - -#include "graphics/common/engine.h" - -namespace Gfx -{ - -class CGLEngine : public Gfx::CEngine -{ - // TODO -}; - -}; diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index 88404b7..3aad1b6 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -34,15 +34,15 @@ Math::Vector ROTATION; const int FRAME_DELAY = 5000; -std::map TEXS; +std::map TEXS; SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; -Gfx::Texture* GetTexture(const std::string &name) +Gfx::Texture GetTexture(const std::string &name) { - std::map::iterator it = TEXS.find(name); + std::map::iterator it = TEXS.find(name); if (it == TEXS.end()) - return NULL; + return Gfx::Texture(); return (*it).second; } @@ -52,10 +52,10 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) if (name.empty()) return; - if (GetTexture(name) != NULL) - return; + Gfx::Texture tex = GetTexture(name); - Gfx::Texture *tex = NULL; + if (tex.valid) + return; CImage img; if (! img.Load(std::string("tex/") + name)) @@ -114,7 +114,7 @@ void Render(Gfx::CGLDevice *device, Gfx::CModelFile *modelFile) device->BeginScene(); Math::Matrix persp; - Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (800.0f) / (600.0f), 0.1f, 100.0f); device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); Math::Matrix id; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 03847fc..66ed770 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -40,8 +40,8 @@ void Init(Gfx::CGLDevice *device) tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; - Gfx::Texture* tex1 = device->CreateTexture(&img1, tex1CreateParams); - Gfx::Texture* tex2 = device->CreateTexture(&img2, tex2CreateParams); + Gfx::Texture tex1 = device->CreateTexture(&img1, tex1CreateParams); + Gfx::Texture tex2 = device->CreateTexture(&img2, tex2CreateParams); device->SetTexture(0, tex1); device->SetTexture(1, tex2); @@ -75,13 +75,13 @@ void Render(Gfx::CGLDevice *device) static Gfx::VertexTex2 quad[] = { - Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), - Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), - Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), - Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), }; Math::Matrix t; diff --git a/src/graphics/opengl/test/transform_test.cpp b/src/graphics/opengl/test/transform_test.cpp index 5982c4e..4decaa0 100644 --- a/src/graphics/opengl/test/transform_test.cpp +++ b/src/graphics/opengl/test/transform_test.cpp @@ -38,13 +38,13 @@ void Init(Gfx::CGLDevice *device) device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); device->SetShadeModel(Gfx::SHADE_SMOOTH); } - +#include void Render(Gfx::CGLDevice *device) { device->BeginScene(); Math::Matrix persp; - Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (800.0f) / (600.0f), 0.1f, 100.0f); device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); @@ -71,6 +71,8 @@ void Render(Gfx::CGLDevice *device) Gfx::VertexCol line[2] = { Gfx::VertexCol() }; + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + for (int x = -40; x <= 40; ++x) { line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); diff --git a/src/math/geometry.h b/src/math/geometry.h index 8954655..61d1868 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -284,26 +284,24 @@ inline void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from, //! Loads a perspective projection matrix /** \a fov field of view in radians - \a aspect aspect ratio (height / width) + \a aspect aspect ratio (width / height) \a nearPlane distance to near cut plane \a farPlane distance to far cut plane */ -inline void LoadProjectionMatrix(Math::Matrix &mat, float fov = 1.570795f, float aspect = 1.0f, +inline void LoadProjectionMatrix(Math::Matrix &mat, float fov = Math::PI / 2.0f, float aspect = 1.0f, float nearPlane = 1.0f, float farPlane = 1000.0f) { assert(fabs(farPlane - nearPlane) >= 0.01f); assert(fabs(sin(fov / 2)) >= 0.01f); - float w = aspect * (cosf(fov / 2) / sinf(fov / 2)); - float h = 1.0f * (cosf(fov / 2) / sinf(fov / 2)); - float q = farPlane / (farPlane - nearPlane); + float f = cosf(fov / 2.0f) / sinf(fov / 2.0f); mat.LoadZero(); - /* (1,1) */ mat.m[0 ] = w; - /* (2,2) */ mat.m[5 ] = h; - /* (3,3) */ mat.m[10] = q; - /* (4,3) */ mat.m[11] = 1.0f; - /* (3,4) */ mat.m[14] = -q * nearPlane; + /* (1,1) */ mat.m[0 ] = f / aspect; + /* (2,2) */ mat.m[5 ] = f; + /* (3,3) */ mat.m[10] = (nearPlane + farPlane) / (nearPlane - farPlane); + /* (4,3) */ mat.m[11] = -1.0f; + /* (3,4) */ mat.m[14] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane); } //! Loads an othogonal projection matrix @@ -320,7 +318,7 @@ inline void LoadOrthoProjectionMatrix(Math::Matrix &mat, float left, float right /* (3,3) */ mat.m[10] = -2.0f / (zFar - zNear); /* (1,4) */ mat.m[12] = - (right + left) / (right - left); - /* (2,4) */ mat.m[12] = - (top + bottom) / (top - bottom); + /* (2,4) */ mat.m[13] = - (top + bottom) / (top - bottom); /* (3,4) */ mat.m[14] = - (zFar + zNear) / (zFar - zNear); } -- cgit v1.2.3-1-g7c22 From 9d592045317ca66be415e60ba4c2db90b5d7ad3e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 23 Jul 2012 21:41:27 +0200 Subject: Cursor drawing - fixed cursor drawing in CEngine - changed event loop to generate more events --- src/CMakeLists.txt | 1 - src/app/app.cpp | 15 +- src/graphics/common/device.h | 6 +- src/graphics/common/engine.cpp | 402 +++++++++++++--------------- src/graphics/common/engine.h | 37 +++ src/graphics/common/test/CMakeLists.txt | 2 +- src/graphics/opengl/gldevice.cpp | 30 ++- src/graphics/opengl/gldevice.h | 6 +- src/graphics/opengl/test/transform_test.cpp | 4 +- src/math/point.h | 6 + src/math/vector.h | 6 + 11 files changed, 266 insertions(+), 249 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9430da6..be4219f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,7 +58,6 @@ graphics/common/terrain.cpp graphics/common/text.cpp graphics/common/water.cpp graphics/opengl/gldevice.cpp -graphics/opengl/glengine.cpp # object/auto/auto.cpp # object/auto/autobase.cpp # object/auto/autoconvert.cpp diff --git a/src/app/app.cpp b/src/app/app.cpp index 4812102..e626695 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -457,27 +457,24 @@ int CApplication::Run() while (true) { - // Use SDL_PeepEvents() if the app is active, so we can use idle time to - // render the scene. Else, use SDL_PollEvent() to avoid eating CPU time. - int count = 0; - // To be sure no old event remains m_private->currentEvent.type = SDL_NOEVENT; + if (m_active) + SDL_PumpEvents(); + bool haveEvent = true; while (haveEvent) { haveEvent = false; + int count = 0; + // Use SDL_PeepEvents() if the app is active, so we can use idle time to + // render the scene. Else, use SDL_PollEvent() to avoid eating CPU time. if (m_active) - { - SDL_PumpEvents(); count = SDL_PeepEvents(&m_private->currentEvent, 1, SDL_GETEVENT, SDL_ALLEVENTS); - } else - { count = SDL_PollEvent(&m_private->currentEvent); - } // If received an event if (count > 0) diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index d79d253..410596d 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -337,11 +337,11 @@ public: virtual Gfx::Color GetTextureFactor() = 0; //! Renders primitive composed of vertices with single texture - virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::Vertex *vertices, int vertexCount) = 0; + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices , int vertexCount) = 0; //! Renders primitive composed of vertices with color information and single texture - virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount) = 0; + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices , int vertexCount) = 0; //! Renders primitive composed of vertices with multitexturing (2 textures) - virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexTex2 *vertices, int vertexCount) = 0; + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount) = 0; //! Tests whether a sphere intersects the 6 clipping planes of projection volume virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius) = 0; diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index c474402..d8c4f4e 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -23,6 +23,7 @@ #include "common/iman.h" #include "common/image.h" #include "common/key.h" +#include "common/logger.h" #include "graphics/common/device.h" #include "math/geometry.h" @@ -144,6 +145,24 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_updateGeometry = false; + m_mice[Gfx::ENG_MOUSE_NORM] = Gfx::EngineMouse( 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f)); + m_mice[Gfx::ENG_MOUSE_WAIT] = Gfx::EngineMouse( 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f)); + m_mice[Gfx::ENG_MOUSE_HAND] = Gfx::EngineMouse( 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f)); + m_mice[Gfx::ENG_MOUSE_NO] = Gfx::EngineMouse( 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f)); + m_mice[Gfx::ENG_MOUSE_EDIT] = Gfx::EngineMouse( 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f)); + m_mice[Gfx::ENG_MOUSE_CROSS] = Gfx::EngineMouse(10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f)); + m_mice[Gfx::ENG_MOUSE_MOVEV] = Gfx::EngineMouse(12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f)); + m_mice[Gfx::ENG_MOUSE_MOVEH] = Gfx::EngineMouse(14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f)); + m_mice[Gfx::ENG_MOUSE_MOVED] = Gfx::EngineMouse(16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_MOVEI] = Gfx::EngineMouse(18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_MOVE] = Gfx::EngineMouse(20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f)); + m_mice[Gfx::ENG_MOUSE_TARGET] = Gfx::EngineMouse(22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLL] = Gfx::EngineMouse(24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLR] = Gfx::EngineMouse(26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLU] = Gfx::EngineMouse(28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLD] = Gfx::EngineMouse(30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f)); + + m_mouseSize = Math::Point(0.04f, 0.04f * (800.0f / 600.0f)); m_mousePos = Math::Point(0.5f, 0.5f); m_mouseType = Gfx::ENG_MOUSE_NORM; m_mouseVisible = false; @@ -251,30 +270,50 @@ bool Gfx::CEngine::AfterDeviceSetInit() return true; } -bool Gfx::CEngine::ProcessEvent(const Event &event) +Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams ¶ms) { - if (event.type == EVENT_MOUSE_MOVE) + CImage img; + if (! img.Load(m_app->GetDataFilePath(m_texPath, texName))) { - m_mousePos = event.mouseMove.pos; + std::stringstream str; + str << "Couldn't load texture '" << texName << "': " << img.GetError(); + m_error = str.str(); + return Gfx::Texture(); // invalid texture } - else if (event.type == EVENT_KEY_DOWN) - { - // !! Debug, to be removed later !! - if (event.key.key == KEY(F1)) - { - m_mouseVisible = !m_mouseVisible; - m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile()); - } - else if (event.key.key == KEY(F2)) - { - int index = (int)m_mouseType; - m_mouseType = (Gfx::EngineMouseType)( (index + 1) % Gfx::ENG_MOUSE_COUNT ); - } + Gfx::Texture result = m_device->CreateTexture(&img, params); + + if (! result.valid) + { + std::stringstream str; + str << "Couldn't load texture '" << texName << "': " << m_device->GetError(); + m_error = str.str(); + return result; } - // By default, pass on all events - return true; + m_texNameMap[texName] = result; + m_revTexNameMap[result] = texName; + + return result; +} + +Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName) +{ + return CreateTexture(texName, m_defaultTexParams); +} + +void Gfx::CEngine::DestroyTexture(const std::string &texName) +{ + std::map::iterator it = m_texNameMap.find(texName); + if (it == m_texNameMap.end()) + return; + + std::map::iterator revIt = m_revTexNameMap.find((*it).second); + + m_device->DestroyTexture((*it).second); + + m_revTexNameMap.erase(revIt); + m_texNameMap.erase(it); } void Gfx::CEngine::SetTexture(const std::string &name, int stage) @@ -307,22 +346,25 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) state |= Gfx::ENG_RSTATE_TTEXTURE_BLACK; } + // TODO other modes & thorough testing + if (state & Gfx::ENG_RSTATE_TTEXTURE_BLACK) // The transparent black texture? { m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); - + m_device->SetTextureEnabled(0, true); m_device->SetTextureFactor(color); Gfx::TextureParams params; params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; - // TODO: params.alphaOperation = Gfx::TEX_MIX_OPER_DISABLED; + params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; m_device->SetTextureParams(0, params); } else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture? @@ -331,88 +373,93 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); - + m_device->SetTextureEnabled(0, true); m_device->SetTextureFactor(color.Inverse()); Gfx::TextureParams params; params.colorOperation = Gfx::TEX_MIX_OPER_ADD; params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; - // TODO: params.alphaOperation = Gfx::TEX_MIX_OPER_DISABLED; + params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; m_device->SetTextureParams(0, params); } - // TODO other modes else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color? { - /* - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_blackSrcBlend[1]); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_blackDestBlend[1]); - - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); + + m_device->SetTextureFactor(color); + m_device->SetTextureEnabled(0, true); + m_device->SetTextureParams(0, Gfx::TextureParams()); } else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color? { - /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_whiteSrcBlend[1]); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_whiteDestBlend[1]); - - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, ~color); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); + + m_device->SetTextureFactor(color.Inverse()); + m_device->SetTextureEnabled(0, true); + m_device->SetTextureParams(0, Gfx::TextureParams()); } else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent? { - /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]); - - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + /*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]); + m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]); + + m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); + m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ } else if (state & Gfx::ENG_RSTATE_ALPHA) // image with alpha channel? { - /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128)); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]); - - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/ + /*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER); + m_device->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128)); + m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]); + m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]); + + m_device->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); + m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); + m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/ } else // normal ? { - /*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); - m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); - - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); - m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, true); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, false); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetTextureEnabled(0, true); + m_device->SetTextureParams(0, Gfx::TextureParams()); + + /*m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ } if (state & Gfx::ENG_RSTATE_FOG) @@ -426,40 +473,39 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) if ( (state & ENG_RSTATE_DUAL_BLACK) && second ) { - /*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ + /*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); + m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); + m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ } else if ( (state & ENG_RSTATE_DUAL_WHITE) && second ) { - /*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ + /*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD); + m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); + m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); + m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ } else { - /*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + m_device->SetTextureEnabled(1, false); } if (state & Gfx::ENG_RSTATE_WRAP) { - /*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/ + /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP); + m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/ } else if (state & Gfx::ENG_RSTATE_CLAMP) { - /*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ + /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); + m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ } else { - /*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); - m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ + /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); + m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ } if (state & Gfx::ENG_RSTATE_2FACE) @@ -478,10 +524,39 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetGlobalAmbient(m_ambientColor[m_rankView]); } +bool Gfx::CEngine::ProcessEvent(const Event &event) +{ + if (event.type == EVENT_MOUSE_MOVE) + { + m_mousePos = event.mouseMove.pos; + } + else if (event.type == EVENT_KEY_DOWN) + { + // !! Debug, to be removed later !! + + if (event.key.key == KEY(F1)) + { + m_mouseVisible = !m_mouseVisible; + m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile()); + } + else if (event.key.key == KEY(F2)) + { + int index = static_cast(m_mouseType); + m_mouseType = static_cast( (index + 1) % Gfx::ENG_MOUSE_COUNT ); + } + } + + // By default, pass on all events + return true; +} + bool Gfx::CEngine::Render() { m_statisticTriangle = 0; + m_lastState = -1; + SetState(Gfx::ENG_RSTATE_NORMAL); + m_device->BeginScene(); SetUp3DView(); @@ -535,61 +610,12 @@ bool Gfx::CEngine::DrawInterface() void Gfx::CEngine::DrawMouse() { - struct EngineMouse - { - Gfx::EngineMouseType type; - int icon1; - int icon2; - int iconShadow; - Gfx::EngineRenderState mode1; - Gfx::EngineRenderState mode2; - Math::Point hotPoint; - - EngineMouse(Gfx::EngineMouseType = Gfx::ENG_MOUSE_NORM, - int icon1 = -1, int icon2 = -1, int iconShadow = -1, - Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL, - Gfx::EngineRenderState mode2 = ENG_RSTATE_NORMAL, - Math::Point hotPoint = Math::Point()) - { - this->type = type; - this->icon1 = icon1; - this->icon2 = icon2; - this->iconShadow = iconShadow; - this->mode1 = mode1; - this->mode2 = mode2; - this->hotPoint = hotPoint; - } - }; - - static const EngineMouse MICE[Gfx::ENG_MOUSE_COUNT] = - { - EngineMouse(Gfx::ENG_MOUSE_NORM, 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f)), - EngineMouse(Gfx::ENG_MOUSE_WAIT, 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f)), - EngineMouse(Gfx::ENG_MOUSE_HAND, 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f)), - EngineMouse(Gfx::ENG_MOUSE_NO, 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f)), - EngineMouse(Gfx::ENG_MOUSE_EDIT, 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f)), - EngineMouse(Gfx::ENG_MOUSE_CROSS, 10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f)), - EngineMouse(Gfx::ENG_MOUSE_MOVEV, 12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f)), - EngineMouse(Gfx::ENG_MOUSE_MOVEH, 14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f)), - EngineMouse(Gfx::ENG_MOUSE_MOVED, 16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)), - EngineMouse(Gfx::ENG_MOUSE_MOVEI, 18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)), - EngineMouse(Gfx::ENG_MOUSE_MOVE, 20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f)), - EngineMouse(Gfx::ENG_MOUSE_TARGET, 22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f)), - EngineMouse(Gfx::ENG_MOUSE_SCROLLL, 24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f)), - EngineMouse(Gfx::ENG_MOUSE_SCROLLR, 26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f)), - EngineMouse(Gfx::ENG_MOUSE_SCROLLU, 28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f)), - EngineMouse(Gfx::ENG_MOUSE_SCROLLD, 30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f)) - }; - - static const Math::Point MOUSE_SIZE(0.05f, 0.05f); - if (! m_mouseVisible) return; if (m_app->GetSystemMouseVisibile()) return; - Gfx::Material material; material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f); material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f); @@ -597,32 +623,24 @@ void Gfx::CEngine::DrawMouse() SetMaterial(material); SetTexture("mouse.png"); - for (int i = 0; i < Gfx::ENG_MOUSE_COUNT; ++i) - { - if (m_mouseType != MICE[i].type) - continue; - - Math::Point pos; - pos.x = m_mousePos.x - (MICE[i].hotPoint.x * MOUSE_SIZE.x) / 32.0f; - pos.y = m_mousePos.y - ((32.0f - MICE[i].hotPoint.y) * MOUSE_SIZE.y) / 32.0f; + int index = static_cast(m_mouseType); - Math::Point shadowPos; - shadowPos.x = pos.x+(4.0f/640.0f); - shadowPos.y = pos.y-(3.0f/480.0f); + Math::Point pos = m_mousePos; + pos.x = m_mousePos.x - (m_mice[index].hotPoint.x * m_mouseSize.x) / 32.0f; + pos.y = m_mousePos.y - ((32.0f - m_mice[index].hotPoint.y) * m_mouseSize.y) / 32.0f; - // FIXME: doesn't work yet + Math::Point shadowPos; + shadowPos.x = pos.x + (4.0f/800.0f); + shadowPos.y = pos.y - (3.0f/600.0f); - SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE); - DrawMouseSprite(shadowPos, MOUSE_SIZE, MICE[i].iconShadow); + SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE); + DrawMouseSprite(shadowPos, m_mouseSize, m_mice[index].iconShadow); - SetState(MICE[i].mode1); - DrawMouseSprite(pos, MOUSE_SIZE, MICE[i].icon1); + SetState(m_mice[index].mode1); + DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon1); - SetState(MICE[i].mode2); - DrawMouseSprite(pos, MOUSE_SIZE, MICE[i].icon2); - - break; - } + SetState(m_mice[index].mode2); + DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon2); } void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) @@ -635,8 +653,8 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) float u1 = (32.0f / 256.0f) * (icon % 8); float v1 = (32.0f / 256.0f) * (icon / 8); - float u2 = (32.0f / 256.0f) + u1; - float v2 = (32.0f / 256.0f) + v1; + float u2 = u1 + (32.0f / 256.0f); + float v2 = v1 + (32.0f / 256.0f); float dp = 0.5f / 256.0f; u1 += dp; @@ -649,8 +667,8 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) Gfx::Vertex vertex[4] = { Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), normal, Math::Point(u1, v2)), - Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)), Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), normal, Math::Point(u2, v2)), + Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)), Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), normal, Math::Point(u2, v1)) }; @@ -658,52 +676,6 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) AddStatisticTriangle(2); } -Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams ¶ms) -{ - CImage img; - if (! img.Load(m_app->GetDataFilePath(m_texPath, texName))) - { - std::stringstream str; - str << "Couldn't load texture '" << texName << "': " << img.GetError(); - m_error = str.str(); - return Gfx::Texture(); // invalid texture - } - - Gfx::Texture result = m_device->CreateTexture(&img, params); - - if (! result.valid) - { - std::stringstream str; - str << "Couldn't load texture '" << texName << "': " << m_device->GetError(); - m_error = str.str(); - return result; - } - - m_texNameMap[texName] = result; - m_revTexNameMap[result] = texName; - - return result; -} - -Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName) -{ - return CreateTexture(texName, m_defaultTexParams); -} - -void Gfx::CEngine::DestroyTexture(const std::string &texName) -{ - std::map::iterator it = m_texNameMap.find(texName); - if (it == m_texNameMap.end()) - return; - - std::map::iterator revIt = m_revTexNameMap.find((*it).second); - - m_device->DestroyTexture((*it).second); - - m_revTexNameMap.erase(revIt); - m_texNameMap.erase(it); -} - void Gfx::CEngine::SetMouseVisible(bool visible) { m_mouseVisible = visible; diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index b1510fd..de4a115 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -360,6 +360,7 @@ enum EngineTextureMapping ENG_TEX_MAPPING_1Z = 6 }; + /** \enum EngineRenderState \brief Render state of graphics engine @@ -408,6 +409,7 @@ enum EngineRenderState ENG_RSTATE_TCOLOR_WHITE = (1<<17) }; + /** \enum EngineMouseType \brief Type of mouse cursor displayed in-game */ @@ -452,6 +454,39 @@ enum EngineMouseType ENG_MOUSE_COUNT }; +/** + \struct EngineMouse + \brief Information about mouse cursor */ +struct EngineMouse +{ + //! Index of texture element for 1st image + int icon1; + //! Index of texture element for 2nd image + int icon2; + //! Shadow texture part + int iconShadow; + //! Mode to render 1st image in + Gfx::EngineRenderState mode1; + //! Mode to render 2nd image in + Gfx::EngineRenderState mode2; + //! Hot point + Math::Point hotPoint; + + EngineMouse(int icon1 = -1, int icon2 = -1, int iconShadow = -1, + Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL, + Gfx::EngineRenderState mode2 = Gfx::ENG_RSTATE_NORMAL, + Math::Point hotPoint = Math::Point()) + { + this->icon1 = icon1; + this->icon2 = icon2; + this->iconShadow = iconShadow; + this->mode1 = mode1; + this->mode2 = mode2; + this->hotPoint = hotPoint; + } +}; + + /** \class CEngine \brief The graphics engine @@ -934,6 +969,8 @@ protected: std::map m_texNameMap; std::map m_revTexNameMap; + Gfx::EngineMouse m_mice[Gfx::ENG_MOUSE_COUNT]; + Math::Point m_mouseSize; Gfx::EngineMouseType m_mouseType; Math::Point m_mousePos; bool m_mouseVisible; diff --git a/src/graphics/common/test/CMakeLists.txt b/src/graphics/common/test/CMakeLists.txt index 7274169..bd83773 100644 --- a/src/graphics/common/test/CMakeLists.txt +++ b/src/graphics/common/test/CMakeLists.txt @@ -4,4 +4,4 @@ set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") include_directories(. ../../..) -add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp) +add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/logger.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp) diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 3d63bf2..5f684e6 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -658,7 +658,7 @@ Gfx::Color Gfx::CGLDevice::GetTextureFactor() return Gfx::Color(color[0], color[1], color[2], color[3]); } -void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount) +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Vertex *vertices, int vertexCount) { if (type == Gfx::PRIMITIVE_LINES) glBegin(GL_LINES); @@ -671,15 +671,15 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, in for (int i = 0; i < vertexCount; ++i) { - glNormal3fv((GLfloat*)vertices[i].normal.Array()); - glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array()); - glVertex3fv((GLfloat*)vertices[i].coord.Array()); + glNormal3fv(const_cast(vertices[i].normal.Array())); + glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast(vertices[i].texCoord.Array())); + glVertex3fv(const_cast(vertices[i].coord.Array())); } glEnd(); } -void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount) +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices, int vertexCount) { if (type == Gfx::PRIMITIVE_LINES) glBegin(GL_LINES); @@ -690,16 +690,16 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vert for (int i = 0; i < vertexCount; ++i) { - glColor4fv((GLfloat*)vertices[i].color.Array()); - glSecondaryColor3fv((GLfloat*)vertices[i].specular.Array()); - glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array()); - glVertex3fv((GLfloat*)vertices[i].coord.Array()); + glColor4fv(const_cast(vertices[i].color.Array())); + glSecondaryColor3fv(const_cast(vertices[i].specular.Array())); + glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast(vertices[i].texCoord.Array())); + glVertex3fv(const_cast(vertices[i].coord.Array())); } glEnd(); } -void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount) +void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const VertexTex2 *vertices, int vertexCount) { if (type == Gfx::PRIMITIVE_LINES) glBegin(GL_LINES); @@ -708,12 +708,14 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) glBegin(GL_TRIANGLE_STRIP); + glColor3f(1.0f, 1.0f, 1.0f); + for (int i = 0; i < vertexCount; ++i) { - glNormal3fv((GLfloat*) vertices[i].normal.Array()); - glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y); - glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y); - glVertex3fv((GLfloat*) vertices[i].coord.Array()); + glNormal3fv(const_cast(vertices[i].normal.Array())); + glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast(vertices[i].texCoord.Array())); + glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, const_cast(vertices[i].texCoord.Array())); + glVertex3fv(const_cast(vertices[i].coord.Array())); } glEnd(); diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 792ea4b..b6bfc6d 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -113,9 +113,9 @@ public: virtual void SetTextureFactor(const Gfx::Color &color); virtual Gfx::Color GetTextureFactor(); - virtual void DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount); - virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount); - virtual void DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount); + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices, int vertexCount); + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices, int vertexCount); + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount); virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius); diff --git a/src/graphics/opengl/test/transform_test.cpp b/src/graphics/opengl/test/transform_test.cpp index 4decaa0..83819b8 100644 --- a/src/graphics/opengl/test/transform_test.cpp +++ b/src/graphics/opengl/test/transform_test.cpp @@ -38,7 +38,7 @@ void Init(Gfx::CGLDevice *device) device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); device->SetShadeModel(Gfx::SHADE_SMOOTH); } -#include + void Render(Gfx::CGLDevice *device) { device->BeginScene(); @@ -71,8 +71,6 @@ void Render(Gfx::CGLDevice *device) Gfx::VertexCol line[2] = { Gfx::VertexCol() }; - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - for (int x = -40; x <= 40; ++x) { line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); diff --git a/src/math/point.h b/src/math/point.h index 80f80f4..740b275 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -74,6 +74,12 @@ struct Point return (float*)this; } + //! Returns the struct cast to const float* array; use with care! + inline const float* Array() const + { + return (const float*)this; + } + //! Returns the distance from (0,0) to the point (x,y) inline float Length() { diff --git a/src/math/vector.h b/src/math/vector.h index 65fc2f9..148d648 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -79,6 +79,12 @@ struct Vector return (float*)this; } + //! Returns the struct cast to const float* array; use with care! + inline const float* Array() const + { + return (const float*)this; + } + //! Returns the vector length inline float Length() const { -- cgit v1.2.3-1-g7c22 From 42963b341f3fbc055c494a0dc0c97d395fa69562 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 25 Jul 2012 00:27:01 +0200 Subject: Refactoring of texture code - refactored texture structs & functions - added note about OpenGL extensions - removed device.cpp as unnecessary - minor changes in CEngine --- src/app/app.cpp | 8 - src/graphics/common/device.cpp | 33 ---- src/graphics/common/device.h | 19 ++- src/graphics/common/engine.cpp | 90 +++++----- src/graphics/common/engine.h | 2 +- src/graphics/common/texture.h | 82 +++++---- src/graphics/opengl/gldevice.cpp | 273 +++++++++++++++--------------- src/graphics/opengl/gldevice.h | 9 +- src/graphics/opengl/test/model_test.cpp | 11 -- src/graphics/opengl/test/texture_test.cpp | 37 ++-- 10 files changed, 274 insertions(+), 290 deletions(-) delete mode 100644 src/graphics/common/device.cpp diff --git a/src/app/app.cpp b/src/app/app.cpp index e626695..c778a63 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -170,14 +170,6 @@ bool CApplication::Create() // Create the 3D engine m_engine = new Gfx::CEngine(m_iMan, this); - // Initialize the app's custom scene stuff, but before initializing the graphics device - if (! m_engine->BeforeCreateInit()) - { - SystemDialog(SDT_ERROR, "COLOBOT - Error", std::string("Error in CEngine::BeforeCreateInit() :\n") + - std::string(m_engine->GetError()) ); - m_exitCode = 1; - return false; - } /* // Create the sound instance. m_sound = new CSound(m_iMan); diff --git a/src/graphics/common/device.cpp b/src/graphics/common/device.cpp deleted file mode 100644 index fcd4318..0000000 --- a/src/graphics/common/device.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// device.cpp - - -#include "graphics/common/device.h" - - -void Gfx::DeviceConfig::LoadDefault() -{ - width = 800; - height = 600; - bpp = 32; - fullScreen = false; - resizeable = false; - doubleBuf = true; - noFrame = false; -} diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h index 410596d..7aa4ce3 100644 --- a/src/graphics/common/device.h +++ b/src/graphics/common/device.h @@ -62,7 +62,16 @@ struct DeviceConfig DeviceConfig() { LoadDefault(); } //! Loads the default values - void LoadDefault(); + inline void LoadDefault() + { + width = 800; + height = 600; + bpp = 32; + fullScreen = false; + resizeable = false; + doubleBuf = true; + noFrame = false; + } }; @@ -326,10 +335,10 @@ public: //! Returns the current enable state of given texture stage virtual bool GetTextureEnabled(int index) = 0; - //! Sets the current params of texture with given index - virtual void SetTextureParams(int index, const Gfx::TextureParams ¶ms) = 0; - //! Returns the current params of texture with given index - virtual Gfx::TextureParams GetTextureParams(int index) = 0; + //! Sets the params for texture stage with given index + virtual void SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms) = 0; + //! Returns the current params of texture stage with given index + virtual Gfx::TextureStageParams GetTextureStageParams(int index) = 0; //! Sets the texture factor to the given color value virtual void SetTextureFactor(const Gfx::Color &color) = 0; diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index d8c4f4e..dd1d23f 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -52,13 +52,13 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_iMan->AddInstance(CLASS_ENGINE, this); m_app = app; - /*m_light = new Gfx::CLight(m_iMan, this); - m_text = new Gfx::CText(m_iMan, this); - m_particle = new Gfx::CParticle(m_iMan, this); - m_water = new Gfx::CWater(m_iMan, this); - m_cloud = new Gfx::CCloud(m_iMan, this); - m_lightning = new Gfx::CLightning(m_iMan, this); - m_planet = new Gfx::CPlanet(m_iMan, this);*/ + m_light = NULL; + m_text = NULL; + m_particle = NULL; + m_water = NULL; + m_cloud = NULL; + m_lightning = NULL; + m_planet = NULL; m_sound = NULL; m_terrain = NULL; @@ -185,27 +185,6 @@ Gfx::CEngine::~CEngine() m_app = NULL; m_device = NULL; - /*delete m_light; - m_light = NULL; - - delete m_text; - m_text = NULL; - - delete m_particle; - m_particle = NULL; - - delete m_water; - m_water = NULL; - - delete m_cloud; - m_cloud = NULL; - - delete m_lightning; - m_lightning = NULL; - - delete m_planet; - m_planet = NULL;*/ - m_sound = NULL; m_terrain = NULL; } @@ -220,16 +199,18 @@ std::string Gfx::CEngine::GetError() return m_error; } -bool Gfx::CEngine::BeforeCreateInit() -{ - // TODO - return true; -} - bool Gfx::CEngine::Create() { m_wasInit = true; + /*m_light = new Gfx::CLight(m_iMan, this); + m_text = new Gfx::CText(m_iMan, this); + m_particle = new Gfx::CParticle(m_iMan, this); + m_water = new Gfx::CWater(m_iMan, this); + m_cloud = new Gfx::CCloud(m_iMan, this); + m_lightning = new Gfx::CLightning(m_iMan, this); + m_planet = new Gfx::CPlanet(m_iMan, this);*/ + m_matWorldInterface.LoadIdentity(); m_matViewInterface.LoadIdentity(); Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); @@ -241,6 +222,27 @@ void Gfx::CEngine::Destroy() { // TODO + /*delete m_light; + m_light = NULL; + + delete m_text; + m_text = NULL; + + delete m_particle; + m_particle = NULL; + + delete m_water; + m_water = NULL; + + delete m_cloud; + m_cloud = NULL; + + delete m_lightning; + m_lightning = NULL; + + delete m_planet; + m_planet = NULL;*/ + m_wasInit = false; } @@ -265,7 +267,7 @@ bool Gfx::CEngine::AfterDeviceSetInit() params.minFilter = Gfx::TEX_MIN_FILTER_NEAREST; params.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; params.mipmap = false; - CreateTexture("mouse.png", params); + m_miceTexture = CreateTexture("mouse.png", params); return true; } @@ -360,12 +362,12 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureEnabled(0, true); m_device->SetTextureFactor(color); - Gfx::TextureParams params; + Gfx::TextureStageParams params; params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - m_device->SetTextureParams(0, params); + m_device->SetTextureStageParams(0, params); } else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture? { @@ -379,12 +381,12 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureEnabled(0, true); m_device->SetTextureFactor(color.Inverse()); - Gfx::TextureParams params; + Gfx::TextureStageParams params; params.colorOperation = Gfx::TEX_MIX_OPER_ADD; params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - m_device->SetTextureParams(0, params); + m_device->SetTextureStageParams(0, params); } else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color? { @@ -398,7 +400,7 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureFactor(color); m_device->SetTextureEnabled(0, true); - m_device->SetTextureParams(0, Gfx::TextureParams()); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); } else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color? { @@ -412,7 +414,7 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetTextureFactor(color.Inverse()); m_device->SetTextureEnabled(0, true); - m_device->SetTextureParams(0, Gfx::TextureParams()); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); } else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent? { @@ -454,7 +456,7 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color) m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); m_device->SetTextureEnabled(0, true); - m_device->SetTextureParams(0, Gfx::TextureParams()); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); /*m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); @@ -620,8 +622,8 @@ void Gfx::CEngine::DrawMouse() material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f); material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f); - SetMaterial(material); - SetTexture("mouse.png"); + m_device->SetMaterial(material); + m_device->SetTexture(0, m_miceTexture); int index = static_cast(m_mouseType); diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index de4a115..0413816 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -516,7 +516,6 @@ public: bool GetWasInit(); std::string GetError(); - bool BeforeCreateInit(); bool Create(); void Destroy(); @@ -970,6 +969,7 @@ protected: std::map m_revTexNameMap; Gfx::EngineMouse m_mice[Gfx::ENG_MOUSE_COUNT]; + Gfx::Texture m_miceTexture; Math::Point m_mouseSize; Gfx::EngineMouseType m_mouseType; Math::Point m_mousePos; diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index 83986ea..787c2bf 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -25,15 +25,21 @@ namespace Gfx { \brief Format of image data */ enum TexImgFormat { + //! Try to determine automatically (may not work) + TEX_IMG_AUTO, + //! RGB triplet, 3 bytes TEX_IMG_RGB, + //! BGR triplet, 3 bytes TEX_IMG_BGR, + //! RGBA triplet, 4 bytes TEX_IMG_RGBA, + //! BGRA triplet, 4 bytes TEX_IMG_BGRA }; /** \enum TexMinFilter - \brief Minification texture filter + \brief Texture minification filter Corresponds to OpenGL modes but should translate to DirectX too. */ enum TexMinFilter @@ -48,7 +54,7 @@ enum TexMinFilter /** \enum TexMagFilter - \brief Magnification texture filter */ + \brief Texture magnification filter */ enum TexMagFilter { TEX_MAG_FILTER_NEAREST, @@ -66,30 +72,42 @@ enum TexWrapMode /** \enum TexMixOperation - \brief Multitexture mixing operation - */ + \brief Multitexture mixing operation */ enum TexMixOperation { + //! Default operation on default params (modulate on computed & texture) + TEX_MIX_OPER_DEFAULT, + //! = Arg1 + TEX_MIX_OPER_REPLACE, + //! = Arg1 * Arg2 TEX_MIX_OPER_MODULATE, - TEX_MIX_OPER_ADD + //! = Arg1 + Arg2 + TEX_MIX_OPER_ADD, + //! = Arg1 - Arg2 + TEX_MIX_OPER_SUBTRACT }; /** \enum TexMixArgument - \brief Multitexture mixing argument - */ + \brief Multitexture mixing argument */ enum TexMixArgument { - TEX_MIX_ARG_CURRENT, + //! Color from current texture TEX_MIX_ARG_TEXTURE, - TEX_MIX_ARG_DIFFUSE, + //! Color computed by previous texture unit (current in DirectX; previous in OpenGL) + TEX_MIX_ARG_COMPUTED_COLOR, + //! (Source) color of textured fragment (diffuse in DirectX; primary color in OpenGL) + TEX_MIX_ARG_SRC_COLOR, + //! Constant color (texture factor in DirectX; texture env color in OpenGL) TEX_MIX_ARG_FACTOR }; /** \struct TextureCreateParams \brief Parameters for texture creation - */ + + These params define how particular texture is created and later displayed. + They must be specified at texture creation time and cannot be changed later. */ struct TextureCreateParams { //! Whether to generate mipmaps @@ -100,10 +118,6 @@ struct TextureCreateParams Gfx::TexMinFilter minFilter; //! Magnification filter Gfx::TexMagFilter magFilter; - //! Wrap S coord mode - Gfx::TexWrapMode wrapS; - //! Wrap T coord mode - Gfx::TexWrapMode wrapT; //! Constructor; calls LoadDefault() TextureCreateParams() @@ -117,17 +131,16 @@ struct TextureCreateParams minFilter = Gfx::TEX_MIN_FILTER_NEAREST; magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - - wrapS = Gfx::TEX_WRAP_REPEAT; - wrapT = Gfx::TEX_WRAP_REPEAT; } }; /** - \struct TextureParams - \brief Parameters for texture creation - */ -struct TextureParams + \struct TextureStageParams + \brief Parameters for a texture unit + + These params define the behavior of texturing units (stages). + They can be changed freely and are feature of graphics engine, not any particular texture. */ +struct TextureStageParams { //! Mixing operation done on color values Gfx::TexMixOperation colorOperation; @@ -141,30 +154,41 @@ struct TextureParams Gfx::TexMixArgument alphaArg1; //! 2nd argument of alpha operations Gfx::TexMixArgument alphaArg2; + //! Wrap mode for 1st tex coord + Gfx::TexWrapMode wrapS; + //! Wrap mode for 2nd tex coord + Gfx::TexWrapMode wrapT; //! Constructor; calls LoadDefault() - TextureParams() + TextureStageParams() { LoadDefault(); } //! Loads the default values inline void LoadDefault() { - colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + alphaArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + wrapS = wrapT = Gfx::TEX_WRAP_REPEAT; } }; -/** \struct Texture*/ +/** + \struct Texture + \brief Info about a texture + + Identifies (through id) a texture created in graphics engine. + Also contains some additional data. */ struct Texture { - //! Whether the texture was loaded + //! Whether the texture (ID) is valid bool valid; - //! Id of the texture in graphics engine + //! ID of the texture in graphics engine unsigned int id; //! Width of texture int width; diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 5f684e6..a3668da 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -20,6 +20,7 @@ #include "graphics/opengl/gldevice.h" #include "math/geometry.h" +// Should define prototypes of used extensions as OpenGL functions #define GL_GLEXT_PROTOTYPES #include @@ -71,24 +72,9 @@ std::string Gfx::CGLDevice::GetError() bool Gfx::CGLDevice::Create() { - /* First check for extensions - These should be available in standard OpenGL 1.3 - But every distribution is different - So we're loading them dynamically through SDL_GL_GetProcAddress() */ - - std::string extensions = std::string( (char*) glGetString(GL_EXTENSIONS)); - - if (extensions.find("GL_ARB_multitexture") == std::string::npos) - { - m_error = "Required extension GL_ARB_multitexture not supported"; - return false; - } - - if (extensions.find("GL_EXT_texture_env_combine") == std::string::npos) - { - m_error = "Required extension GL_EXT_texture_env_combine not supported"; - return false; - } + /* NOTE: extension testing is not done here as the assumed version of OpenGL to be used (1.4+) + must already have the required extensions. The used extensions are listed here for reference: + GL_ARB_multitexture, GL_EXT_texture_env_combine, GL_EXT_secondary_color */ m_wasInit = true; @@ -112,11 +98,11 @@ bool Gfx::CGLDevice::Create() m_lightsEnabled = std::vector (GL_MAX_LIGHTS, false); int maxTextures = 0; - glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextures); + glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTextures); - m_textures = std::vector (maxTextures, Gfx::Texture()); + m_currentTextures = std::vector (maxTextures, Gfx::Texture()); m_texturesEnabled = std::vector (maxTextures, false); - m_texturesParams = std::vector(maxTextures, Gfx::TextureParams()); + m_textureStageParams = std::vector(maxTextures, Gfx::TextureStageParams()); return true; } @@ -130,9 +116,9 @@ void Gfx::CGLDevice::Destroy() m_lights.clear(); m_lightsEnabled.clear(); - m_textures.clear(); + m_currentTextures.clear(); m_texturesEnabled.clear(); - m_texturesParams.clear(); + m_textureStageParams.clear(); m_wasInit = false; } @@ -324,7 +310,7 @@ Gfx::Texture Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCrea result.height = data->surface->h; // Use & enable 1st texture stage - glActiveTextureARB(GL_TEXTURE0_ARB); + glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); @@ -352,19 +338,6 @@ Gfx::Texture Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCrea glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magF); - if (params.wrapS == Gfx::TEX_WRAP_CLAMP) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - else if (params.wrapS == Gfx::TEX_WRAP_REPEAT) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - else assert(false); - - if (params.wrapT == Gfx::TEX_WRAP_CLAMP) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - else if (params.wrapT == Gfx::TEX_WRAP_REPEAT) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - else assert(false); - - if (params.mipmap) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); else @@ -399,8 +372,8 @@ Gfx::Texture Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCrea // Restore the previous state of 1st stage - if (m_textures[0].valid) - glBindTexture(GL_TEXTURE_2D, m_textures[0].id); + if (m_currentTextures[0].valid) + glBindTexture(GL_TEXTURE_2D, m_currentTextures[0].id); else glBindTexture(GL_TEXTURE_2D, 0); @@ -417,9 +390,9 @@ void Gfx::CGLDevice::DestroyTexture(const Gfx::Texture &texture) m_allTextures.erase(it); // Unbind the texture if in use anywhere - for (int index = 0; index < (int)m_textures.size(); ++index) + for (int index = 0; index < (int)m_currentTextures.size(); ++index) { - if (m_textures[index] == texture) + if (m_currentTextures[index] == texture) SetTexture(index, Gfx::Texture()); // set to invalid texture } @@ -436,7 +409,7 @@ void Gfx::CGLDevice::DestroyAllTextures() int Gfx::CGLDevice::GetMaxTextureCount() { - return m_textures.size(); + return m_currentTextures.size(); } /** @@ -446,13 +419,13 @@ int Gfx::CGLDevice::GetMaxTextureCount() void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) { assert(index >= 0); - assert(index < (int)m_textures.size()); + assert(index < (int)m_currentTextures.size()); // Enable the given texture stage - glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTexture(GL_TEXTURE0 + index); glEnable(GL_TEXTURE_2D); - m_textures[index] = texture; // remember the change + m_currentTextures[index] = texture; // remember the change if (! texture.valid) { @@ -460,8 +433,8 @@ void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) } else { - glBindTexture(GL_TEXTURE_2D, texture.id); // bind the texture - SetTextureParams(index, m_texturesParams[index]); // texture params need to be re-set for the new texture + glBindTexture(GL_TEXTURE_2D, texture.id); // bind the texture + SetTextureStageParams(index, m_textureStageParams[index]); // texture stage params need to be re-set for the new texture } // Disable the stage if it is set so @@ -474,19 +447,19 @@ void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) Gfx::Texture Gfx::CGLDevice::GetTexture(int index) { assert(index >= 0); - assert(index < (int)m_textures.size()); + assert(index < (int)m_currentTextures.size()); - return m_textures[index]; + return m_currentTextures[index]; } void Gfx::CGLDevice::SetTextureEnabled(int index, bool enabled) { assert(index >= 0); - assert(index < (int)m_textures.size()); + assert(index < (int)m_currentTextures.size()); m_texturesEnabled[index] = enabled; - glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTexture(GL_TEXTURE0 + index); if (enabled) glEnable(GL_TEXTURE_2D); else @@ -496,7 +469,7 @@ void Gfx::CGLDevice::SetTextureEnabled(int index, bool enabled) bool Gfx::CGLDevice::GetTextureEnabled(int index) { assert(index >= 0); - assert(index < (int)m_textures.size()); + assert(index < (int)m_currentTextures.size()); return m_texturesEnabled[index]; } @@ -505,111 +478,137 @@ bool Gfx::CGLDevice::GetTextureEnabled(int index) Sets the texture parameters for the given texture stage. If the given texture was not set (bound) yet, nothing happens. The settings are remembered, even if texturing is disabled at the moment. */ -void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶ms) +void Gfx::CGLDevice::SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms) { assert(index >= 0); - assert(index < (int)m_textures.size()); + assert(index < (int)m_currentTextures.size()); // Remember the settings - m_texturesParams[index] = params; + m_textureStageParams[index] = params; // Don't actually do anything if texture not set - if (! m_textures[index].valid) + if (! m_currentTextures[index].valid) return; // Enable the given stage - glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTexture(GL_TEXTURE0 + index); glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, m_textures[index].id); + glBindTexture(GL_TEXTURE_2D, m_currentTextures[index].id); + + // To save some trouble + if ( (params.colorOperation == Gfx::TEX_MIX_OPER_DEFAULT) && + (params.alphaOperation == Gfx::TEX_MIX_OPER_DEFAULT) ) + { + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + goto after_tex_operations; + } - // Selection of operation and arguments glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + // Only these modes of getting color & alpha are used + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); + // Color operation - if (params.colorOperation == Gfx::TEX_MIX_OPER_MODULATE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB_ARB, GL_MODULATE); + + if (params.colorOperation == Gfx::TEX_MIX_OPER_DEFAULT) + { + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); + goto after_tex_color; + } + else if (params.colorOperation == Gfx::TEX_MIX_OPER_REPLACE) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); + else if (params.colorOperation == Gfx::TEX_MIX_OPER_MODULATE) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); else if (params.colorOperation == Gfx::TEX_MIX_OPER_ADD) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB_ARB, GL_ADD); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_ADD); + else if (params.colorOperation == Gfx::TEX_MIX_OPER_SUBTRACT) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_SUBTRACT); else assert(false); // Color arg1 - if (params.colorArg1 == Gfx::TEX_MIX_ARG_CURRENT) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS); // that's right - stupid D3D enum values - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR); - } - else if (params.colorArg1 == Gfx::TEX_MIX_ARG_TEXTURE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR); - } - else if (params.colorArg1 == Gfx::TEX_MIX_ARG_DIFFUSE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR); // here as well - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR); - } + if (params.colorArg1 == Gfx::TEX_MIX_ARG_TEXTURE) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); + else if (params.colorArg1 == Gfx::TEX_MIX_ARG_COMPUTED_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS); + else if (params.colorArg1 == Gfx::TEX_MIX_ARG_SRC_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); + else if (params.colorArg1 == Gfx::TEX_MIX_ARG_FACTOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT); else assert(false); // Color arg2 - if (params.colorArg2 == Gfx::TEX_MIX_ARG_CURRENT) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR); - } - else if (params.colorArg2 == Gfx::TEX_MIX_ARG_TEXTURE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR); - } - else if (params.colorArg2 == Gfx::TEX_MIX_ARG_DIFFUSE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR); - } + if (params.colorArg2 == Gfx::TEX_MIX_ARG_TEXTURE) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE); + else if (params.colorArg2 == Gfx::TEX_MIX_ARG_COMPUTED_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); + else if (params.colorArg2 == Gfx::TEX_MIX_ARG_SRC_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR); + else if (params.colorArg2 == Gfx::TEX_MIX_ARG_FACTOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT); else assert(false); + +after_tex_color: + // Alpha operation - if (params.alphaOperation == Gfx::TEX_MIX_OPER_MODULATE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA_ARB, GL_MODULATE); + if (params.alphaOperation == Gfx::TEX_MIX_OPER_DEFAULT) + { + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE); + goto after_tex_operations; + } + else if (params.colorOperation == Gfx::TEX_MIX_OPER_REPLACE) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); + else if (params.alphaOperation == Gfx::TEX_MIX_OPER_MODULATE) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); else if (params.alphaOperation == Gfx::TEX_MIX_OPER_ADD) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA_ARB, GL_ADD); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_ADD); + else if (params.alphaOperation == Gfx::TEX_MIX_OPER_SUBTRACT) + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_SUBTRACT); else assert(false); // Alpha arg1 - if (params.alphaArg1 == Gfx::TEX_MIX_ARG_CURRENT) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA); - } - else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_TEXTURE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA); - } - else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_DIFFUSE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - } + if (params.alphaArg1 == Gfx::TEX_MIX_ARG_TEXTURE) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); + else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_COMPUTED_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS); + else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_SRC_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); + else if (params.alphaArg1 == Gfx::TEX_MIX_ARG_FACTOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_CONSTANT); else assert(false); // Alpha arg2 - if (params.alphaArg2 == Gfx::TEX_MIX_ARG_CURRENT) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA); - } - else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_TEXTURE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA); - } - else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_DIFFUSE) - { - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA); - } + if (params.alphaArg2 == Gfx::TEX_MIX_ARG_TEXTURE) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE); + else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_COMPUTED_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PREVIOUS); + else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_SRC_COLOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR); + else if (params.alphaArg2 == Gfx::TEX_MIX_ARG_FACTOR) + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_CONSTANT); + else assert(false); + + +after_tex_operations: + + if (params.wrapS == Gfx::TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + else if (params.wrapS == Gfx::TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + else assert(false); + + if (params.wrapT == Gfx::TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + else if (params.wrapT == Gfx::TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); else assert(false); // Disable the stage if it is set so @@ -617,21 +616,21 @@ void Gfx::CGLDevice::SetTextureParams(int index, const Gfx::TextureParams ¶m glDisable(GL_TEXTURE_2D); } -Gfx::TextureParams Gfx::CGLDevice::GetTextureParams(int index) +Gfx::TextureStageParams Gfx::CGLDevice::GetTextureStageParams(int index) { assert(index >= 0); - assert(index < (int)m_textures.size()); + assert(index < (int)m_currentTextures.size()); - return m_texturesParams[index]; + return m_textureStageParams[index]; } void Gfx::CGLDevice::SetTextureFactor(const Gfx::Color &color) { // Needs to be set for all texture stages - for (int index = 0; index < (int)m_textures.size(); ++index) + for (int index = 0; index < (int)m_currentTextures.size(); ++index) { // Activate stage - glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTexture(GL_TEXTURE0 + index); glEnable(GL_TEXTURE_2D); glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color.Array()); @@ -645,7 +644,7 @@ void Gfx::CGLDevice::SetTextureFactor(const Gfx::Color &color) Gfx::Color Gfx::CGLDevice::GetTextureFactor() { // Get from 1st stage (should be the same for all stages) - glActiveTextureARB(GL_TEXTURE0_ARB); + glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); GLfloat color[4] = { 0.0f }; @@ -672,7 +671,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Vertex *vertic for (int i = 0; i < vertexCount; ++i) { glNormal3fv(const_cast(vertices[i].normal.Array())); - glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast(vertices[i].texCoord.Array())); + glMultiTexCoord2fv(GL_TEXTURE0, const_cast(vertices[i].texCoord.Array())); glVertex3fv(const_cast(vertices[i].coord.Array())); } @@ -692,7 +691,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol { glColor4fv(const_cast(vertices[i].color.Array())); glSecondaryColor3fv(const_cast(vertices[i].specular.Array())); - glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast(vertices[i].texCoord.Array())); + glMultiTexCoord2fv(GL_TEXTURE0, const_cast(vertices[i].texCoord.Array())); glVertex3fv(const_cast(vertices[i].coord.Array())); } @@ -713,8 +712,8 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const VertexTex2 *ve for (int i = 0; i < vertexCount; ++i) { glNormal3fv(const_cast(vertices[i].normal.Array())); - glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast(vertices[i].texCoord.Array())); - glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, const_cast(vertices[i].texCoord.Array())); + glMultiTexCoord2fv(GL_TEXTURE0, const_cast(vertices[i].texCoord.Array())); + glMultiTexCoord2fv(GL_TEXTURE1, const_cast(vertices[i].texCoord.Array())); glVertex3fv(const_cast(vertices[i].coord.Array())); } @@ -814,9 +813,9 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) m_texturing = enabled; // Enable/disable stages with new setting - for (int index = 0; index < (int)m_textures.size(); ++index) + for (int index = 0; index < (int)m_currentTextures.size(); ++index) { - glActiveTextureARB(GL_TEXTURE0_ARB + index); + glActiveTexture(GL_TEXTURE0 + index); if (m_texturing && m_texturesEnabled[index]) glEnable(GL_TEXTURE_2D); else diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index b6bfc6d..0f4d0c9 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -107,8 +107,8 @@ public: virtual void SetTextureEnabled(int index, bool enabled); virtual bool GetTextureEnabled(int index); - virtual void SetTextureParams(int index, const Gfx::TextureParams ¶ms); - virtual Gfx::TextureParams GetTextureParams(int index); + virtual void SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms); + virtual Gfx::TextureStageParams GetTextureStageParams(int index); virtual void SetTextureFactor(const Gfx::Color &color); virtual Gfx::Color GetTextureFactor(); @@ -153,6 +153,7 @@ public: virtual Gfx::FillMode GetFillMode(); private: + //! Updates internal modelview matrix void UpdateModelviewMatrix(); private: @@ -181,11 +182,11 @@ private: //! Whether texturing is enabled in general bool m_texturing; //! Current textures; \c NULL value means unassigned - std::vector m_textures; + std::vector m_currentTextures; //! Current texture stages enable status std::vector m_texturesEnabled; //! Current texture params - std::vector m_texturesParams; + std::vector m_textureStageParams; //! Set of all created textures std::set m_allTextures; diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index 3aad1b6..3e3d100 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -73,7 +73,6 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) texCreateParams.format = Gfx::TEX_IMG_BGRA; texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; - texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; tex = device->CreateTexture(&img, texCreateParams); } @@ -139,16 +138,6 @@ void Render(Gfx::CGLDevice *device, Gfx::CModelFile *modelFile) device->SetTextureEnabled(0, true); device->SetTextureEnabled(1, true); - Gfx::TextureParams tex1Params; - tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(0, tex1Params); - - Gfx::TextureParams tex2Params; - tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(1, tex2Params); - device->SetMaterial(triangles[i].material); tri[0] = triangles[i].p1; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 66ed770..aa9817e 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -10,9 +10,14 @@ void Init(Gfx::CGLDevice *device) { - device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); device->SetShadeModel(Gfx::SHADE_SMOOTH); + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); + device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + CImage img1; if (! img1.Load("tex1.png")) { @@ -31,32 +36,18 @@ void Init(Gfx::CGLDevice *device) tex1CreateParams.format = Gfx::TEX_IMG_RGBA; tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; - tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; Gfx::TextureCreateParams tex2CreateParams; tex2CreateParams.mipmap = true; tex2CreateParams.format = Gfx::TEX_IMG_RGBA; tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; Gfx::Texture tex1 = device->CreateTexture(&img1, tex1CreateParams); Gfx::Texture tex2 = device->CreateTexture(&img2, tex2CreateParams); device->SetTexture(0, tex1); device->SetTexture(1, tex2); - - Gfx::TextureParams tex1Params; - tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(0, tex1Params); - - Gfx::TextureParams tex2Params; - tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(1, tex2Params); - - device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); } void Render(Gfx::CGLDevice *device) @@ -84,6 +75,16 @@ void Render(Gfx::CGLDevice *device) Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), }; + Gfx::TextureStageParams tex1StageParams; + tex1StageParams.colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + tex1StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(0, tex1StageParams); + + Gfx::TextureStageParams tex2StageParams; + tex2StageParams.colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + tex2StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(1, tex2StageParams); + Math::Matrix t; Math::LoadTranslationMatrix(t, Math::Vector(-4.0f, 4.0f, 0.0f)); device->SetTransform(Gfx::TRANSFORM_VIEW, t); @@ -101,12 +102,12 @@ void Render(Gfx::CGLDevice *device) device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); - device->SetTextureEnabled(0, true); - device->SetTextureEnabled(1, true); - Math::LoadTranslationMatrix(t, Math::Vector( 0.0f, -4.0f, 0.0f)); device->SetTransform(Gfx::TRANSFORM_VIEW, t); + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); device->EndScene(); -- cgit v1.2.3-1-g7c22 From 9cd28e26e90ecbce239991e054293f748895fb98 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 25 Jul 2012 00:28:56 +0200 Subject: Fix in CMakeLists.txt --- src/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index be4219f..06f5990 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,7 +46,6 @@ common/stringutils.cpp graphics/common/camera.cpp graphics/common/cloud.cpp graphics/common/color.cpp -graphics/common/device.cpp graphics/common/engine.cpp graphics/common/light.cpp graphics/common/lightning.cpp -- cgit v1.2.3-1-g7c22 From bc1c9b5284c10d8aafc04bcd6c3597b3626a782f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 25 Jul 2012 20:27:13 +0200 Subject: Fixed bug in texturing --- src/graphics/opengl/gldevice.cpp | 20 ++++++++++---------- src/graphics/opengl/test/CMakeLists.txt | 3 --- src/graphics/opengl/test/texture_test.cpp | 10 ++++++++++ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index a3668da..fc6e9b6 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -516,19 +516,19 @@ void Gfx::CGLDevice::SetTextureStageParams(int index, const Gfx::TextureStagePar if (params.colorOperation == Gfx::TEX_MIX_OPER_DEFAULT) { + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); goto after_tex_color; } else if (params.colorOperation == Gfx::TEX_MIX_OPER_REPLACE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); else if (params.colorOperation == Gfx::TEX_MIX_OPER_MODULATE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); else if (params.colorOperation == Gfx::TEX_MIX_OPER_ADD) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_ADD); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD); else if (params.colorOperation == Gfx::TEX_MIX_OPER_SUBTRACT) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_SUBTRACT); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT); else assert(false); // Color arg1 @@ -559,19 +559,19 @@ after_tex_color: // Alpha operation if (params.alphaOperation == Gfx::TEX_MIX_OPER_DEFAULT) { - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS); glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE); goto after_tex_operations; } else if (params.colorOperation == Gfx::TEX_MIX_OPER_REPLACE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); else if (params.alphaOperation == Gfx::TEX_MIX_OPER_MODULATE) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE); else if (params.alphaOperation == Gfx::TEX_MIX_OPER_ADD) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_ADD); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD); else if (params.alphaOperation == Gfx::TEX_MIX_OPER_SUBTRACT) - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_SUBTRACT); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_SUBTRACT); else assert(false); // Alpha arg1 diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 58c4714..f49fbac 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -30,7 +30,6 @@ configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common set(TEXTURE_SOURCES ../gldevice.cpp -../../common/device.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp @@ -38,7 +37,6 @@ texture_test.cpp set(MODEL_SOURCES ../gldevice.cpp -../../common/device.cpp ../../common/modelfile.cpp ../../../common/logger.cpp ../../../common/image.cpp @@ -50,7 +48,6 @@ model_test.cpp set(TRANSFORM_SOURCES ../gldevice.cpp -../../common/device.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index aa9817e..c3c568b 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -108,6 +108,16 @@ void Render(Gfx::CGLDevice *device) device->SetTextureEnabled(0, true); device->SetTextureEnabled(1, true); + tex1StageParams.colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + tex1StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(0, tex1StageParams); + + tex2StageParams.colorOperation = Gfx::TEX_MIX_OPER_ADD; + tex2StageParams.colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; + tex2StageParams.colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + tex2StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(1, tex2StageParams); + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); device->EndScene(); -- cgit v1.2.3-1-g7c22 From 6b846aa62924df0dea4af78c6333f0bd141bda78 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 00:18:02 +0200 Subject: Compile fixes Fixed some compiling issues --- src/CBot/CBot.h | 2 +- src/graphics/common/engine.h | 2 +- src/graphics/common/particle.h | 2 +- src/sound/sound.h | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index d618131..3b7d7e8 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -32,7 +32,7 @@ // fix for MSVC instruction __asm int 3 (setting a trap) -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(__GNUC__) #define ASM_TRAP() asm("int $3"); #else #define ASM_TRAP() __asm int 3; diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index 6d2937c..b36ccbc 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -481,7 +481,7 @@ public: void SetLightMode(bool present); bool RetLightMode(); - void SetEditIndentMode(bool auto); + void SetEditIndentMode(bool indentAuto); bool RetEditIndentMode(); void SetEditIndentValue(int value); diff --git a/src/graphics/common/particle.h b/src/graphics/common/particle.h index e430e2a..b72075f 100644 --- a/src/graphics/common/particle.h +++ b/src/graphics/common/particle.h @@ -305,7 +305,7 @@ protected: void DrawParticuleWheel(int i); CObject* SearchObjectGun(Math::Vector old, Math::Vector pos, ParticuleType type, CObject *father); CObject* SearchObjectRay(Math::Vector pos, Math::Vector goal, ParticuleType type, CObject *father); - void Play(Snd::Sound sound, Math::Vector pos, float amplitude); + void Play(Sound sound, Math::Vector pos, float amplitude); bool TrackMove(int i, Math::Vector pos, float progress); void TrackDraw(int i, ParticuleType type); diff --git a/src/sound/sound.h b/src/sound/sound.h index 598ffe3..2eb92a0 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -28,6 +28,8 @@ #include +#include + /*! * Maximum possible audio volume */ -- cgit v1.2.3-1-g7c22 From d1fe0d2dcc30a76055d6423805f8e0108b4b64be Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 19:05:09 +0200 Subject: Lighting - fixed problems with lighting - added light_test --- src/graphics/common/light.h | 94 +++++-- src/graphics/common/material.h | 3 + src/graphics/opengl/gldevice.cpp | 107 ++++++-- src/graphics/opengl/gldevice.h | 4 + src/graphics/opengl/test/CMakeLists.txt | 12 + src/graphics/opengl/test/light_test.cpp | 437 ++++++++++++++++++++++++++++++++ 6 files changed, 615 insertions(+), 42 deletions(-) create mode 100644 src/graphics/opengl/test/light_test.cpp diff --git a/src/graphics/common/light.h b/src/graphics/common/light.h index 46f1bfa..e1d51ab 100644 --- a/src/graphics/common/light.h +++ b/src/graphics/common/light.h @@ -41,8 +41,6 @@ enum LightType * \brief Light * * This structure was created as analog to DirectX's D3DLIGHT. - * - * It contains analogous fields as the D3DLIGHT struct. */ struct Light { @@ -54,29 +52,40 @@ struct Light Gfx::Color diffuse; //! Color of specular light Gfx::Color specular; - //! Position in world space + //! Position in world space (for point & spot lights) Math::Vector position; - //! Direction in world space + //! Direction in world space (for directional & spot lights) Math::Vector direction; - //! Cutoff range - float range; - //! Falloff - float falloff; - //! Inner angle of spotlight cone - float theta; - //! Outer angle of spotlight cone - float phi; - //! Constant attenuation + //! Constant attenuation factor float attenuation0; - //! Linear attenuation + //! Linear attenuation factor float attenuation1; - //! Quadratic attenuation + //! Quadratic attenuation factor float attenuation2; + //! Angle of spotlight cone (0-90 degrees) + float spotAngle; + //! Intensity of spotlight (0 = uniform; 128 = most intense) + float spotIntensity; + //! Constructor; calls LoadDefault() Light() + { + LoadDefault(); + } + + //! Loads default values + void LoadDefault() { type = LIGHT_POINT; - range = falloff = theta = phi = attenuation0 = attenuation1 = attenuation2 = 0.0f; + ambient = Gfx::Color(0.4f, 0.4f, 0.4f); + diffuse = Gfx::Color(0.8f, 0.8f, 0.8f); + specular = Gfx::Color(1.0f, 1.0f, 1.0f); + position = Math::Vector(0.0f, 0.0f, 0.0f); + direction = Math::Vector(0.0f, 0.0f, 1.0f); + attenuation0 = 1.0f; + attenuation1 = attenuation2 = 0.0f; + spotAngle = 90.0f; + spotIntensity = 0.0f; } }; @@ -101,12 +110,12 @@ struct LightProgression }; /** - * \struct SceneLight + * \struct DynamicLight * \brief Dynamic light in 3D scene * * TODO documentation */ -struct SceneLight +struct DynamicLight { //! true -> light exists bool used; @@ -128,6 +137,53 @@ struct SceneLight Gfx::LightProgression colorBlue; }; -// TODO CLight +/** + \class CLight + \brief Manager for dynamic lights in 3D scene + */ +class CLight +{ +public: + CLight(CInstanceManager *iMan, Gfx::CEngine* engine); + virtual ~CLight(); + + void SetDevice(Gfx::CDevice* device); + + void FlushLight(); + int CreateLight(); + bool DeleteLight(int lightRank); + bool SetLight(int lightRank, const Gfx::Light &light); + bool GetLight(int lightRank, Gfx::Light &light); + bool LightEnable(int lightRank, bool enable); + + bool SetLightIncludeType(int lightRank, Gfx::EngineObjectType type); + bool SetLightExcludeType(int lightRank, Gfx::EngineObjectType type); + + bool SetLightPos(int lightRank, const Math::Vector &pos); + Math::Vector GetLightPos(int lightRank); + + bool SetLightDir(int lightRank, const Math::Vector &dir); + Math::Vector GetLightDir(int lightRank); + + bool SetLightIntensitySpeed(int lightRank, float speed); + bool SetLightIntensity(int lightRank, float value); + float GetLightIntensity(int lightRank); + void AdaptLightColor(const Gfx::Color &color, float factor); + + bool SetLightColorSpeed(int lightRank, float speed); + bool SetLightColor(int lightRank, const Gfx::Color &color); + Gfx::Color GetLightColor(int lightRank); + + void FrameLight(float rTime); + void LightUpdate(); + void LightUpdate(Gfx::EngineObjectType type); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CDevice* m_device; + float m_time; + std::vector m_dynLights; +}; }; // namespace Gfx diff --git a/src/graphics/common/material.h b/src/graphics/common/material.h index f71923f..c828d90 100644 --- a/src/graphics/common/material.h +++ b/src/graphics/common/material.h @@ -19,6 +19,9 @@ #pragma once +#include "graphics/common/color.h" + + namespace Gfx { /** diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index fc6e9b6..d31d007 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -52,6 +52,7 @@ void Gfx::GLDeviceConfig::LoadDefault() Gfx::CGLDevice::CGLDevice() { m_wasInit = false; + m_lighting = false; m_texturing = false; } @@ -86,6 +87,9 @@ bool Gfx::CGLDevice::Create() // To use separate specular color in drawing primitives glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); + // To avoid problems with scaling & lighting + glEnable(GL_RESCALE_NORMAL); + // Set just to be sure glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glMatrixMode(GL_PROJECTION); @@ -100,9 +104,9 @@ bool Gfx::CGLDevice::Create() int maxTextures = 0; glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTextures); - m_currentTextures = std::vector (maxTextures, Gfx::Texture()); - m_texturesEnabled = std::vector (maxTextures, false); - m_textureStageParams = std::vector(maxTextures, Gfx::TextureStageParams()); + m_currentTextures = std::vector (maxTextures, Gfx::Texture()); + m_texturesEnabled = std::vector (maxTextures, false); + m_textureStageParams = std::vector(maxTextures, Gfx::TextureStageParams()); return true; } @@ -213,15 +217,21 @@ void Gfx::CGLDevice::UpdateModelviewMatrix() glLoadIdentity(); glScalef(1.0f, 1.0f, -1.0f); glMultMatrixf(m_modelviewMat.Array()); + + if (m_lighting) + { + for (int index = 0; index < (int)m_lights.size(); ++index) + UpdateLightPosition(index); + } } void Gfx::CGLDevice::SetMaterial(const Gfx::Material &material) { m_material = material; - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_material.ambient.Array()); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_material.diffuse.Array()); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_material.specular.Array()); + glMaterialfv(GL_FRONT, GL_AMBIENT, m_material.ambient.Array()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, m_material.diffuse.Array()); + glMaterialfv(GL_FRONT, GL_SPECULAR, m_material.specular.Array()); } const Gfx::Material& Gfx::CGLDevice::GetMaterial() @@ -246,23 +256,56 @@ void Gfx::CGLDevice::SetLight(int index, const Gfx::Light &light) glLightfv(GL_LIGHT0 + index, GL_DIFFUSE, const_cast(light.diffuse.Array())); glLightfv(GL_LIGHT0 + index, GL_SPECULAR, const_cast(light.specular.Array())); - GLfloat position[4] = { light.position.x, light.position.y, light.position.z, 0.0f }; - if (light.type == LIGHT_DIRECTIONAL) - position[3] = 0.0f; + glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, light.attenuation0); + glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, light.attenuation1); + glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, light.attenuation2); + + if (light.type == Gfx::LIGHT_SPOT) + { + glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, light.spotAngle); + glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, light.spotIntensity); + } else - position[3] = 1.0f; - glLightfv(GL_LIGHT0 + index, GL_POSITION, position); + { + glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, 180.0f); + } - GLfloat direction[4] = { light.direction.x, light.direction.y, light.direction.z, 0.0f }; - glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, direction); + UpdateLightPosition(index); +} - glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, light.range); +void Gfx::CGLDevice::UpdateLightPosition(int index) +{ + assert(index >= 0); + assert(index < (int)m_lights.size()); - // TODO: falloff?, phi?, theta? + if ((! m_lighting) || (! m_lightsEnabled[index])) + return; - glLightf(GL_LIGHT0 + index, GL_CONSTANT_ATTENUATION, light.attenuation0); - glLightf(GL_LIGHT0 + index, GL_LINEAR_ATTENUATION, light.attenuation1); - glLightf(GL_LIGHT0 + index, GL_QUADRATIC_ATTENUATION, light.attenuation2); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + glLoadIdentity(); + glScalef(1.0f, 1.0f, -1.0f); + glMultMatrixf(m_viewMat.Array()); + + if (m_lights[index].type == LIGHT_DIRECTIONAL) + { + GLfloat position[4] = { m_lights[index].direction.x, m_lights[index].direction.y, m_lights[index].direction.z, 0.0f }; + glLightfv(GL_LIGHT0 + index, GL_POSITION, position); + } + else + { + GLfloat position[4] = { m_lights[index].position.x, m_lights[index].position.y, m_lights[index].position.z, 1.0f }; + glLightfv(GL_LIGHT0 + index, GL_POSITION, position); + } + + if (m_lights[index].type == Gfx::LIGHT_SPOT) + { + GLfloat direction[4] = { m_lights[index].direction.x, m_lights[index].direction.y, m_lights[index].direction.z, 0.0f }; + glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, direction); + } + + glPopMatrix(); } const Gfx::Light& Gfx::CGLDevice::GetLight(int index) @@ -803,12 +846,29 @@ int Gfx::CGLDevice::ComputeSphereVisibility(const Math::Vector ¢er, float ra void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) { - if (state == RENDER_STATE_DEPTH_WRITE) + if (state == Gfx::RENDER_STATE_DEPTH_WRITE) { glDepthMask(enabled ? GL_TRUE : GL_FALSE); return; } - else if (state == RENDER_STATE_TEXTURING) + else if (state == Gfx::RENDER_STATE_LIGHTING) + { + m_lighting = enabled; + + if (enabled) + glEnable(GL_LIGHTING); + else + glDisable(GL_LIGHTING); + + if (enabled) + { + for (int index = 0; index < (int)m_lights.size(); ++index) + UpdateLightPosition(index); + } + + return; + } + else if (state == Gfx::RENDER_STATE_TEXTURING) { m_texturing = enabled; @@ -829,7 +889,6 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) switch (state) { - case Gfx::RENDER_STATE_LIGHTING: flag = GL_LIGHTING; break; case Gfx::RENDER_STATE_BLENDING: flag = GL_BLEND; break; case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; @@ -847,7 +906,10 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) bool Gfx::CGLDevice::GetRenderState(Gfx::RenderState state) { - if (state == RENDER_STATE_TEXTURING) + if (state == Gfx::RENDER_STATE_LIGHTING) + return m_lighting; + + if (state == Gfx::RENDER_STATE_TEXTURING) return m_texturing; GLenum flag = 0; @@ -855,7 +917,6 @@ bool Gfx::CGLDevice::GetRenderState(Gfx::RenderState state) switch (state) { case Gfx::RENDER_STATE_DEPTH_WRITE: flag = GL_DEPTH_WRITEMASK; break; - case Gfx::RENDER_STATE_LIGHTING: flag = GL_DEPTH_WRITEMASK; break; case Gfx::RENDER_STATE_BLENDING: flag = GL_BLEND; break; case Gfx::RENDER_STATE_FOG: flag = GL_FOG; break; case Gfx::RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 0f4d0c9..b779123 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -155,6 +155,8 @@ public: private: //! Updates internal modelview matrix void UpdateModelviewMatrix(); + //! Updates position for given light based on transformation matrices + void UpdateLightPosition(int index); private: //! Was initialized? @@ -174,6 +176,8 @@ private: //! The current material Gfx::Material m_material; + //! Whether lighting is enabled + bool m_lighting; //! Current lights std::vector m_lights; //! Current lights enable status diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index f49fbac..793e858 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -55,6 +55,15 @@ set(TRANSFORM_SOURCES transform_test.cpp ) +set(LIGHT_SOURCES +../gldevice.cpp +../../../common/logger.cpp +../../../common/image.cpp +../../../common/iman.cpp +../../../app/system.cpp +light_test.cpp +) + include_directories(../../../ ${CMAKE_CURRENT_BINARY_DIR}) set(LIBS @@ -73,3 +82,6 @@ target_link_libraries(model_test ${LIBS}) add_executable(transform_test ${TRANSFORM_SOURCES}) target_link_libraries(transform_test ${LIBS}) + +add_executable(light_test ${LIGHT_SOURCES}) +target_link_libraries(light_test ${LIBS}) \ No newline at end of file diff --git a/src/graphics/opengl/test/light_test.cpp b/src/graphics/opengl/test/light_test.cpp new file mode 100644 index 0000000..80fa911 --- /dev/null +++ b/src/graphics/opengl/test/light_test.cpp @@ -0,0 +1,437 @@ +#include "app/system.h" +#include "common/logger.h" +#include "common/image.h" +#include "common/iman.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include +#include + +enum KeySlots +{ + K_Forward, + K_Back, + K_Left, + K_Right, + K_Up, + K_Down, + K_Count +}; +bool KEYMAP[K_Count] = { false }; + +Math::Point MOUSE_POS_BASE; + +Math::Vector TRANSLATION(0.0f, 2.0f, 0.0f); +Math::Vector ROTATION, ROTATION_BASE; + +float CUBE_ORBIT = 0.0f; + +const int FRAME_DELAY = 5000; + +SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; + +void Init(Gfx::CGLDevice *device) +{ + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); + device->SetShadeModel(Gfx::SHADE_SMOOTH); +} + +void Render(Gfx::CGLDevice *device) +{ + device->BeginScene(); + + /* Unlit part of scene */ + + device->SetRenderState(Gfx::RENDER_STATE_LIGHTING, false); + device->SetRenderState(Gfx::RENDER_STATE_CULLING, false); // Double-sided drawing + + Math::Matrix persp; + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (800.0f) / (600.0f), 0.1f, 100.0f); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); + + + Math::Matrix viewMat; + Math::Matrix mat; + + viewMat.LoadIdentity(); + + Math::LoadRotationXMatrix(mat, -ROTATION.x); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadRotationYMatrix(mat, -ROTATION.y); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadTranslationMatrix(mat, -TRANSLATION); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + device->SetTransform(Gfx::TRANSFORM_VIEW, viewMat); + + Math::Matrix worldMat; + worldMat.LoadIdentity(); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + Gfx::VertexCol line[2] = { Gfx::VertexCol() }; + + for (int x = -40; x <= 40; ++x) + { + line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[0].coord.z = -40; + line[0].coord.x = x; + line[1].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[1].coord.z = 40; + line[1].coord.x = x; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + for (int z = -40; z <= 40; ++z) + { + line[0].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[0].coord.z = z; + line[0].coord.x = -40; + line[1].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[1].coord.z = z; + line[1].coord.x = 40; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + + Gfx::VertexCol quad[6] = { Gfx::VertexCol() }; + + quad[0].coord = Math::Vector(-1.0f, -1.0f, 0.0f); + quad[1].coord = Math::Vector( 1.0f, -1.0f, 0.0f); + quad[2].coord = Math::Vector(-1.0f, 1.0f, 0.0f); + quad[3].coord = Math::Vector( 1.0f, 1.0f, 0.0f); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 1.0f, 0.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(40.0f, 2.0f, 40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(0.0f, 1.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(-40.0f, 2.0f, -40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 0.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(10.0f, 4.5f, 5.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + + /* Moving lit cube */ + device->SetRenderState(Gfx::RENDER_STATE_LIGHTING, true); + device->SetRenderState(Gfx::RENDER_STATE_CULLING, true); // Culling (CCW faces) + + device->SetGlobalAmbient(Gfx::Color(0.4f, 0.4f, 0.4f)); + + Gfx::Light light1; + light1.type = Gfx::LIGHT_POINT; + light1.position = Math::Vector(10.0f, 4.5f, 5.0f); + light1.ambient = Gfx::Color(0.2f, 0.2f, 0.2f); + light1.diffuse = Gfx::Color(1.0f, 0.1f, 0.1f); + light1.specular = Gfx::Color(0.0f, 0.0f, 0.0f); + device->SetLight(0, light1); + device->SetLightEnabled(0, true); + + /*Gfx::Light light2; + device->SetLight(1, light2); + device->SetLightEnabled(1, true);*/ + + Gfx::Material material; + material.ambient = Gfx::Color(0.3f, 0.3f, 0.3f); + material.diffuse = Gfx::Color(0.8f, 0.7f, 0.6f); + material.specular = Gfx::Color(0.0f, 0.0f, 0.0f); + device->SetMaterial(material); + + const Gfx::Vertex cube[6][4] = + { + { + // Front + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)) + }, + + { + // Back + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)), + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)) + }, + + { + // Top + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)) + }, + + { + // Bottom + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)) + }, + + { + // Left + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, 1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, 1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, -1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)) + }, + + { + // Right + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, -1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, 1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, -1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, 1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)) + } + }; + + Math::Matrix cubeTrans; + Math::LoadTranslationMatrix(cubeTrans, Math::Vector(10.0f, 2.0f, 5.0f)); + Math::Matrix cubeRot; + Math::LoadRotationMatrix(cubeRot, Math::Vector(0.0f, 1.0f, 0.0f), CUBE_ORBIT); + Math::Matrix cubeRotInv; + Math::LoadRotationMatrix(cubeRotInv, Math::Vector(0.0f, 1.0f, 0.0f), -CUBE_ORBIT); + Math::Matrix cubeTransRad; + Math::LoadTranslationMatrix(cubeTransRad, Math::Vector(0.0f, 0.0f, 6.0f)); + worldMat = Math::MultiplyMatrices(cubeTransRad, cubeRotInv); + worldMat = Math::MultiplyMatrices(cubeRot, worldMat); + worldMat = Math::MultiplyMatrices(cubeTrans, worldMat); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + for (int i = 0; i < 6; ++i) + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, cube[i], 4); + + device->EndScene(); +} + +void Update() +{ + const float TRANS_SPEED = 6.0f; // units / sec + + GetCurrentTimeStamp(CURR_TIME); + float timeDiff = TimeStampDiff(PREV_TIME, CURR_TIME, STU_SEC); + CopyTimeStamp(PREV_TIME, CURR_TIME); + + CUBE_ORBIT += timeDiff * (Math::PI / 4.0f); + + Math::Vector incTrans; + + if (KEYMAP[K_Forward]) + incTrans.z = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Back]) + incTrans.z = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Right]) + incTrans.x = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Left]) + incTrans.x = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Up]) + incTrans.y = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Down]) + incTrans.y = -TRANS_SPEED * timeDiff; + + Math::Point rotTrans = Math::RotatePoint(-ROTATION.y, Math::Point(incTrans.x, incTrans.z)); + incTrans.x = rotTrans.x; + incTrans.z = rotTrans.y; + TRANSLATION += incTrans; +} + +void KeyboardDown(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = true; + break; + case SDLK_s: + KEYMAP[K_Back] = true; + break; + case SDLK_d: + KEYMAP[K_Right] = true; + break; + case SDLK_a: + KEYMAP[K_Left] = true; + break; + case SDLK_z: + KEYMAP[K_Down] = true; + break; + case SDLK_x: + KEYMAP[K_Up] = true; + break; + default: + break; + } +} + +void KeyboardUp(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = false; + break; + case SDLK_s: + KEYMAP[K_Back] = false; + break; + case SDLK_d: + KEYMAP[K_Right] = false; + break; + case SDLK_a: + KEYMAP[K_Left] = false; + break; + case SDLK_z: + KEYMAP[K_Down] = false; + break; + case SDLK_x: + KEYMAP[K_Up] = false; + break; + default: + break; + } +} + +void MouseMove(int x, int y) +{ + Math::Point currentPos((float)x, (float)y); + + static bool first = true; + if (first || (x < 10) || (y < 10) || (x > 790) || (y > 590)) + { + SDL_WarpMouse(400, 300); + MOUSE_POS_BASE.x = 400; + MOUSE_POS_BASE.y = 300; + ROTATION_BASE = ROTATION; + first = false; + return; + } + + ROTATION.y = ROTATION_BASE.y + ((float) (x - MOUSE_POS_BASE.x) / 800.0f) * Math::PI; + ROTATION.x = ROTATION_BASE.x + ((float) (y - MOUSE_POS_BASE.y) / 600.0f) * Math::PI; +} + +int main(int argc, char *argv[]) +{ + CLogger logger; + + PREV_TIME = CreateTimeStamp(); + CURR_TIME = CreateTimeStamp(); + + GetCurrentTimeStamp(PREV_TIME); + GetCurrentTimeStamp(CURR_TIME); + + CInstanceManager iMan; + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Light Test", "Light Test"); + + //SDL_WM_GrabInput(SDL_GRAB_ON); + SDL_ShowCursor(SDL_DISABLE); + + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device); + + bool done = false; + while (! done) + { + Render(device); + Update(); + + SDL_GL_SwapBuffers(); + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + break; + done = true; + } + else if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_q) + { + done = true; + break; + } + else + KeyboardDown(event.key.keysym.sym); + } + else if (event.type == SDL_KEYUP) + KeyboardUp(event.key.keysym.sym); + else if (event.type == SDL_MOUSEMOTION) + MouseMove(event.motion.x, event.motion.y); + } + + usleep(FRAME_DELAY); + } + + //SDL_WM_GrabInput(SDL_GRAB_OFF); + SDL_ShowCursor(SDL_ENABLE); + + device->Destroy(); + delete device; + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + DestroyTimeStamp(PREV_TIME); + DestroyTimeStamp(CURR_TIME); + + return 0; +} -- cgit v1.2.3-1-g7c22 From 045f17a274c0cd41aebd34d5759f7fe791b680e4 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 21:35:04 +0200 Subject: Dynamic light manager - rewrote old CLight as CLightManager --- src/graphics/common/engine.cpp | 23 ++- src/graphics/common/engine.h | 26 +-- src/graphics/common/light.cpp | 396 +++++++++++++++++++++++++++++++++++- src/graphics/common/light.h | 125 ++++++++---- src/graphics/opengl/test/README.txt | 1 + 5 files changed, 517 insertions(+), 54 deletions(-) diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp index dd1d23f..e6dcfc7 100644 --- a/src/graphics/common/engine.cpp +++ b/src/graphics/common/engine.cpp @@ -52,7 +52,7 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_iMan->AddInstance(CLASS_ENGINE, this); m_app = app; - m_light = NULL; + m_lightMan = NULL; m_text = NULL; m_particle = NULL; m_water = NULL; @@ -203,7 +203,7 @@ bool Gfx::CEngine::Create() { m_wasInit = true; - /*m_light = new Gfx::CLight(m_iMan, this); + /*m_lightMan = new Gfx::CLight(m_iMan, this); m_text = new Gfx::CText(m_iMan, this); m_particle = new Gfx::CParticle(m_iMan, this); m_water = new Gfx::CWater(m_iMan, this); @@ -222,8 +222,8 @@ void Gfx::CEngine::Destroy() { // TODO - /*delete m_light; - m_light = NULL; + /*delete m_lightMan; + m_lightMan = NULL; delete m_text; m_text = NULL; @@ -678,6 +678,21 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) AddStatisticTriangle(2); } +bool Gfx::CEngine::GetPause() +{ + return m_pause; +} + +Math::Vector Gfx::CEngine::GetLookatPt() +{ + return m_lookatPt; +} + +Math::Vector Gfx::CEngine::GetEyePt() +{ + return m_eyePt; +} + void Gfx::CEngine::SetMouseVisible(bool visible) { m_mouseVisible = visible; diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h index 0413816..9b6cc1f 100644 --- a/src/graphics/common/engine.h +++ b/src/graphics/common/engine.h @@ -45,7 +45,7 @@ class CSound; namespace Gfx { class CDevice; -class CLight; +class CLightManager; class CText; class CParticle; class CWater; @@ -843,18 +843,18 @@ protected: void UpdateGeometry(); protected: - CInstanceManager* m_iMan; - CApplication* m_app; - CSound* m_sound; - Gfx::CDevice* m_device; - Gfx::CText* m_text; - Gfx::CLight* m_light; - Gfx::CParticle* m_particle; - Gfx::CWater* m_water; - Gfx::CCloud* m_cloud; - Gfx::CLightning* m_lightning; - Gfx::CPlanet* m_planet; - Gfx::CTerrain* m_terrain; + CInstanceManager* m_iMan; + CApplication* m_app; + CSound* m_sound; + Gfx::CDevice* m_device; + Gfx::CText* m_text; + Gfx::CLightManager* m_lightMan; + Gfx::CParticle* m_particle; + Gfx::CWater* m_water; + Gfx::CCloud* m_cloud; + Gfx::CLightning* m_lightning; + Gfx::CPlanet* m_planet; + Gfx::CTerrain* m_terrain; bool m_wasInit; std::string m_error; diff --git a/src/graphics/common/light.cpp b/src/graphics/common/light.cpp index d938256..3ca890c 100644 --- a/src/graphics/common/light.cpp +++ b/src/graphics/common/light.cpp @@ -19,4 +19,398 @@ #include "graphics/common/light.h" -// TODO implementation \ No newline at end of file +#include "common/iman.h" +#include "graphics/common/device.h" +#include "math/geometry.h" + +#include + + +void Gfx::LightProgression::Init(float value) +{ + starting = value; + ending = value; + current = value; + progress = 0.0f; + speed = 100.0f; +} + +void Gfx::LightProgression::Update(float rTime) +{ + if (speed < 100.0f) + { + if (progress < 1.0f) + { + progress += speed * rTime; + if (progress > 1.0f) + progress = 1.0f; + } + + current = starting + progress * (ending - starting); + } + else + { + current = ending; + } +} + +void Gfx::LightProgression::SetTarget(float value) +{ + starting = current; + ending = value; + progress = 0.0f; +} + + +Gfx::DynamicLight::DynamicLight() +{ + used = enabled = false; +} + + + +Gfx::CLightManager::CLightManager(CInstanceManager* iMan, Gfx::CEngine* engine) +{ + m_iMan = iMan; + m_iMan->AddInstance(CLASS_LIGHT, this); + + m_device = NULL; + m_engine = engine; + + m_time = 0.0f; +} + +Gfx::CLightManager::~CLightManager() +{ + m_iMan->DeleteInstance(CLASS_LIGHT, this); + + m_iMan = NULL; + m_device = NULL; + m_engine = NULL; +} + +void Gfx::CLightManager::SetDevice(Gfx::CDevice* device) +{ + m_device = device; + + m_dynLights = std::vector(m_device->GetMaxLightCount(), Gfx::DynamicLight()); +} + +void Gfx::CLightManager::FlushLights() +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + m_dynLights[i].used = false; + m_device->SetLightEnabled(i, false); + } +} + +/** Returns the index of light created or -1 if all lights are used. */ +int Gfx::CLightManager::CreateLight() +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (m_dynLights[i].used) continue; + + m_dynLights[i] = Gfx::DynamicLight(); + + m_dynLights[i].used = true; + m_dynLights[i].enabled = true; + + m_dynLights[i].includeType = Gfx::ENG_OBJTYPE_NULL; + m_dynLights[i].excludeType = Gfx::ENG_OBJTYPE_NULL; + + m_dynLights[i].light.type = Gfx::LIGHT_DIRECTIONAL; + m_dynLights[i].light.diffuse = Gfx::Color(0.5f, 0.5f, 0.5f); + m_dynLights[i].light.position = Math::Vector(-100.0f, 100.0f, -100.0f); + m_dynLights[i].light.direction = Math::Vector( 1.0f, -1.0f, 1.0f); + + m_dynLights[i].intensity.Init(1.0f); // maximum + m_dynLights[i].colorRed.Init(0.5f); + m_dynLights[i].colorGreen.Init(0.5f); + m_dynLights[i].colorBlue.Init(0.5f); // gray + + return i; + } + + return -1; +} + +bool Gfx::CLightManager::DeleteLight(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].used = false; + m_device->SetLightEnabled(lightRank, false); + + return true; +} + +// Specifies a light. + +bool Gfx::CLightManager::SetLight(int lightRank, const Gfx::Light &light) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].light = light; + + m_dynLights[lightRank].colorRed.Init(m_dynLights[lightRank].light.diffuse.r); + m_dynLights[lightRank].colorGreen.Init(m_dynLights[lightRank].light.diffuse.g); + m_dynLights[lightRank].colorBlue.Init(m_dynLights[lightRank].light.diffuse.b); + + return true; +} + +bool Gfx::CLightManager::GetLight(int lightRank, Gfx::Light &light) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + light = m_dynLights[lightRank].light; + return true; +} + +bool Gfx::CLightManager::SetLightEnabled(int lightRank, bool enabled) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].enabled = enabled; + return true; +} + +bool Gfx::CLightManager::SetLightIncludeType(int lightRank, Gfx::EngineObjectType type) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].includeType = type; + return true; +} + +bool Gfx::CLightManager::SetLightExcludeType(int lightRank, Gfx::EngineObjectType type) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].excludeType = type; + return true; +} + +bool Gfx::CLightManager::SetLightPos(int lightRank, const Math::Vector &pos) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].light.position = pos; + return true; +} + +Math::Vector Gfx::CLightManager::GetLightPos(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return Math::Vector(0.0f, 0.0f, 0.0f); + + return m_dynLights[lightRank].light.position; +} + +bool Gfx::CLightManager::SetLightDir(int lightRank, const Math::Vector &dir) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].light.direction = dir; + return true; +} + +Math::Vector Gfx::CLightManager::GetLightDir(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return Math::Vector(0.0f, 0.0f, 0.0f); + + return m_dynLights[lightRank].light.direction; +} + +bool Gfx::CLightManager::SetLightIntensitySpeed(int lightRank, float speed) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].intensity.speed = speed; + return true; +} + +bool Gfx::CLightManager::SetLightIntensity(int lightRank, float value) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].intensity.SetTarget(value); + return true; +} + +float Gfx::CLightManager::GetLightIntensity(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return 0.0f; + + return m_dynLights[lightRank].intensity.current; +} + + +bool Gfx::CLightManager::SetLightColorSpeed(int lightRank, float speed) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].colorRed.speed = speed; + m_dynLights[lightRank].colorGreen.speed = speed; + m_dynLights[lightRank].colorBlue.speed = speed; + return true; +} + +bool Gfx::CLightManager::SetLightColor(int lightRank, const Gfx::Color &color) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].colorRed.SetTarget(color.r); + m_dynLights[lightRank].colorGreen.SetTarget(color.g); + m_dynLights[lightRank].colorBlue.SetTarget(color.b); + return true; +} + +Gfx::Color Gfx::CLightManager::GetLightColor(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); + + Gfx::Color color; + color.r = m_dynLights[lightRank].colorRed.current; + color.g = m_dynLights[lightRank].colorGreen.current; + color.b = m_dynLights[lightRank].colorBlue.current; + return color; +} + +void Gfx::CLightManager::AdaptLightColor(const Gfx::Color &color, float factor) +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + + Gfx::Color value; + value.r = m_dynLights[i].colorRed.current; + value.g = m_dynLights[i].colorGreen.current; + value.b = m_dynLights[i].colorBlue.current; + + value.r += color.r * factor; + value.g += color.g * factor; + value.b += color.b * factor; + + m_dynLights[i].colorRed.Init(value.r); + m_dynLights[i].colorGreen.Init(value.g); + m_dynLights[i].colorBlue.Init(value.b); + } + + UpdateLights(); +} + +void Gfx::CLightManager::UpdateProgression(float rTime) +{ + if (m_engine->GetPause()) + return; + + m_time += rTime; + + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + + m_dynLights[i].intensity.Update(rTime); + m_dynLights[i].colorRed.Update(rTime); + m_dynLights[i].colorGreen.Update(rTime); + m_dynLights[i].colorBlue.Update(rTime); + + if (m_dynLights[i].includeType == Gfx::ENG_OBJTYPE_QUARTZ) + { + m_dynLights[i].light.direction.x = sinf(1.0f * (m_time + i*Math::PI*0.5f)); + m_dynLights[i].light.direction.z = cosf(1.1f * (m_time + i*Math::PI*0.5f)); + m_dynLights[i].light.direction.y = -1.0f + 0.5f * cosf((m_time + i*Math::PI*0.5f)*2.7f); + } + + if (m_dynLights[i].includeType == Gfx::ENG_OBJTYPE_METAL) + { + Math::Vector dir = m_engine->GetEyePt() - m_engine->GetLookatPt(); + float angle = Math::RotateAngle(dir.x, dir.z); + angle += Math::PI * 0.5f * i; + m_dynLights[i].light.direction.x = sinf(2.0f * angle); + m_dynLights[i].light.direction.z = cosf(2.0f * angle); + } + } +} + + +void Gfx::CLightManager::UpdateLights() +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + + bool enabled = m_dynLights[i].enabled; + if (m_dynLights[i].intensity.current == 0.0f) + enabled = false; + + if (enabled) + { + float value = m_dynLights[i].colorRed.current * m_dynLights[i].intensity.current; + m_dynLights[i].light.diffuse.r = value; + + value = m_dynLights[i].colorGreen.current * m_dynLights[i].intensity.current; + m_dynLights[i].light.diffuse.g = value; + + value = m_dynLights[i].colorBlue.current * m_dynLights[i].intensity.current; + m_dynLights[i].light.diffuse.b = value; + + m_device->SetLight(i, m_dynLights[i].light); + m_device->SetLightEnabled(i, enabled); + } + else + { + m_dynLights[i].light.diffuse.r = 0.0f; + m_dynLights[i].light.diffuse.g = 0.0f; + m_dynLights[i].light.diffuse.b = 0.0f; + + m_device->SetLightEnabled(i, enabled); + } + } +} + +void Gfx::CLightManager::UpdateLightsEnableState(Gfx::EngineObjectType type) +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + if (! m_dynLights[i].enabled) + continue; + if (m_dynLights[i].intensity.current == 0.0f) + continue; + + if (m_dynLights[i].includeType != Gfx::ENG_OBJTYPE_NULL) + { + bool enabled = (m_dynLights[i].includeType == type); + m_device->SetLightEnabled(i, enabled); + } + + if (m_dynLights[i].excludeType != Gfx::ENG_OBJTYPE_NULL) + { + bool enabled = (m_dynLights[i].excludeType != type); + m_device->SetLightEnabled(i, enabled); + } + } +} diff --git a/src/graphics/common/light.h b/src/graphics/common/light.h index e1d51ab..93e8c1b 100644 --- a/src/graphics/common/light.h +++ b/src/graphics/common/light.h @@ -27,8 +27,9 @@ namespace Gfx { -/** \enum LightType - * \brief Type of light */ +/** + \enum LightType + \brief Type of light in 3D scene */ enum LightType { LIGHT_POINT, @@ -37,11 +38,10 @@ enum LightType }; /** - * \struct Light - * \brief Light - * - * This structure was created as analog to DirectX's D3DLIGHT. - */ + \struct Light + \brief Properties of light in 3D scene + + This structure was created as analog to DirectX's D3DLIGHT. */ struct Light { //! Type of light source @@ -90,99 +90,152 @@ struct Light }; /** - * \struct LightProgression - * \brief Describes the progression of light parameters change - * - * TODO documentation - */ + \struct LightProgression + \brief Describes the progression of light parameters change */ struct LightProgression { + //! Starting value float starting; + //! Ending (destination) value float ending; + //! Current value float current; + //! Progress from start to end float progress; + //! Speed of progression float speed; LightProgression() { starting = ending = current = progress = speed = 0.0f; } + + //! Initializes the progression + void Init(float value); + + //! Updates the progression + void Update(float rTime); + + //! Sets the new end value (starting is set to current) + void SetTarget(float value); }; /** - * \struct DynamicLight - * \brief Dynamic light in 3D scene - * - * TODO documentation - */ + \struct DynamicLight + \brief Dynamic light in 3D scene + + It is an extension over standard light properties. Added are dynamic progressions for light + colors and intensity and types of objects included/excluded in lighting. */ struct DynamicLight { - //! true -> light exists + //! Whether the light is used bool used; - //! true -> light turned on + //! Whether the light is turned on bool enabled; - //! Type of all objects included - Gfx::EngineObjectType includeType; - //! Type of all objects excluded - Gfx::EngineObjectType excludeType; - //! Configuration of the light Gfx::Light light; - //! intensity (0 .. 1) + //! Progression of intensity [0, 1] Gfx::LightProgression intensity; + //! Progression of red diffuse color Gfx::LightProgression colorRed; + //! Progression of green diffuse color Gfx::LightProgression colorGreen; + //! Progression of blue diffuse color Gfx::LightProgression colorBlue; + + //! Type of objects included in lighting with this light; if Gfx::ENG_OBJTYPE_NULL is used, it is ignored + Gfx::EngineObjectType includeType; + //! Type of objects excluded from lighting with this light; if Gfx::ENG_OBJTYPE_NULL is used, it is ignored + Gfx::EngineObjectType excludeType; + + DynamicLight(); }; /** - \class CLight + \class CLightManager \brief Manager for dynamic lights in 3D scene + + (Old CLight class) + + The class is responsible for managing dynamic lights (struct Gfx::DynamicLight) used in 3D scene. + The dynamic lights are created, updated and deleted through the class' interface. + + Number of available lights depends on graphics device used. Class allocates vector + for the total number of lights, but only some are used. */ -class CLight +class CLightManager { public: - CLight(CInstanceManager *iMan, Gfx::CEngine* engine); - virtual ~CLight(); + //! Constructor + CLightManager(CInstanceManager *iMan, Gfx::CEngine* engine); + //! Destructor + virtual ~CLightManager(); + //! Sets the device to be used void SetDevice(Gfx::CDevice* device); - void FlushLight(); + //! Clears and disables all lights + void FlushLights(); + //! Creates a new dynamic light and returns its index (lightRank) int CreateLight(); + //! Deletes and disables the given dynamic light bool DeleteLight(int lightRank); + //! Sets the light parameters for dynamic light bool SetLight(int lightRank, const Gfx::Light &light); + //! Returns the light parameters for given dynamic light bool GetLight(int lightRank, Gfx::Light &light); - bool LightEnable(int lightRank, bool enable); + //! Enables/disables the given dynamic light + bool SetLightEnabled(int lightRank, bool enable); + //! Sets what objects are included in given dynamic light bool SetLightIncludeType(int lightRank, Gfx::EngineObjectType type); + //! Sets what objects are excluded from given dynamic light bool SetLightExcludeType(int lightRank, Gfx::EngineObjectType type); + //! Sets the position of dynamic light bool SetLightPos(int lightRank, const Math::Vector &pos); + //! Returns the position of dynamic light Math::Vector GetLightPos(int lightRank); + //! Sets the direction of dynamic light bool SetLightDir(int lightRank, const Math::Vector &dir); + //! Returns the direction of dynamic light Math::Vector GetLightDir(int lightRank); - bool SetLightIntensitySpeed(int lightRank, float speed); + //! Sets the destination intensity for dynamic light's intensity progression bool SetLightIntensity(int lightRank, float value); + //! Returns the current light intensity float GetLightIntensity(int lightRank); + //! Sets the rate of change for dynamic light intensity + bool SetLightIntensitySpeed(int lightRank, float speed); + + //! Adjusts the color of all dynamic lights void AdaptLightColor(const Gfx::Color &color, float factor); - bool SetLightColorSpeed(int lightRank, float speed); + //! Sets the destination color for dynamic light's color progression bool SetLightColor(int lightRank, const Gfx::Color &color); + //! Returns current light color Gfx::Color GetLightColor(int lightRank); + //! Sets the rate of change for dynamic light colors (RGB) + bool SetLightColorSpeed(int lightRank, float speed); - void FrameLight(float rTime); - void LightUpdate(); - void LightUpdate(Gfx::EngineObjectType type); + //! Updates progression of dynamic lights + void UpdateProgression(float rTime); + //! Updates (recalculates) all dynamic lights + void UpdateLights(); + //! Enables or disables dynamic lights affecting the given object type + void UpdateLightsEnableState(Gfx::EngineObjectType type); protected: CInstanceManager* m_iMan; CEngine* m_engine; CDevice* m_device; + + //! Current time float m_time; + //! List of dynamic lights std::vector m_dynLights; }; diff --git a/src/graphics/opengl/test/README.txt b/src/graphics/opengl/test/README.txt index fe6f1d7..c618415 100644 --- a/src/graphics/opengl/test/README.txt +++ b/src/graphics/opengl/test/README.txt @@ -6,3 +6,4 @@ Test programs for OpenGL engine: requires ./tex folder (or symlink) with Colobot textures viewer is controlled from keyboard - the bindings can be found in code - transform_test -> simple "walk around" test for world & view transformations + - light test -> test for lighting -- cgit v1.2.3-1-g7c22 From 4ddcd9f810fa588ccf90442f7b4e5ddf385e85f2 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 22:26:19 +0200 Subject: Change of paths in src/graphics - moved abstract core to src/graphics/core - moved proper graphics engine to src/graphics/engine --- src/CMakeLists.txt | 26 +- src/app/app.h | 4 +- src/graphics/common/README.txt | 5 - src/graphics/common/camera.cpp | 23 - src/graphics/common/camera.h | 270 -------- src/graphics/common/cloud.cpp | 23 - src/graphics/common/cloud.h | 94 --- src/graphics/common/color.cpp | 103 --- src/graphics/common/color.h | 98 --- src/graphics/common/device.h | 414 ------------ src/graphics/common/engine.cpp | 729 -------------------- src/graphics/common/engine.h | 985 ---------------------------- src/graphics/common/light.cpp | 416 ------------ src/graphics/common/light.h | 242 ------- src/graphics/common/lightning.cpp | 23 - src/graphics/common/lightning.h | 85 --- src/graphics/common/material.h | 47 -- src/graphics/common/modelfile.cpp | 841 ------------------------ src/graphics/common/modelfile.h | 120 ---- src/graphics/common/particle.cpp | 23 - src/graphics/common/particle.h | 337 ---------- src/graphics/common/planet.cpp | 23 - src/graphics/common/planet.h | 80 --- src/graphics/common/pyro.cpp | 23 - src/graphics/common/pyro.h | 175 ----- src/graphics/common/terrain.cpp | 23 - src/graphics/common/terrain.h | 211 ------ src/graphics/common/test/CMakeLists.txt | 7 - src/graphics/common/test/modelfile_test.cpp | 48 -- src/graphics/common/text.cpp | 23 - src/graphics/common/text.h | 113 ---- src/graphics/common/texture.h | 237 ------- src/graphics/common/vertex.h | 141 ---- src/graphics/common/water.cpp | 23 - src/graphics/common/water.h | 134 ---- src/graphics/core/README.txt | 6 + src/graphics/core/color.cpp | 103 +++ src/graphics/core/color.h | 98 +++ src/graphics/core/device.h | 414 ++++++++++++ src/graphics/core/light.h | 91 +++ src/graphics/core/material.h | 47 ++ src/graphics/core/texture.h | 237 +++++++ src/graphics/core/vertex.h | 141 ++++ src/graphics/engine/README.txt | 8 + src/graphics/engine/camera.cpp | 23 + src/graphics/engine/camera.h | 270 ++++++++ src/graphics/engine/cloud.cpp | 23 + src/graphics/engine/cloud.h | 94 +++ src/graphics/engine/engine.cpp | 729 ++++++++++++++++++++ src/graphics/engine/engine.h | 985 ++++++++++++++++++++++++++++ src/graphics/engine/lightman.cpp | 416 ++++++++++++ src/graphics/engine/lightman.h | 181 +++++ src/graphics/engine/lightning.cpp | 23 + src/graphics/engine/lightning.h | 85 +++ src/graphics/engine/modelfile.cpp | 841 ++++++++++++++++++++++++ src/graphics/engine/modelfile.h | 120 ++++ src/graphics/engine/particle.cpp | 23 + src/graphics/engine/particle.h | 337 ++++++++++ src/graphics/engine/planet.cpp | 23 + src/graphics/engine/planet.h | 80 +++ src/graphics/engine/pyro.cpp | 23 + src/graphics/engine/pyro.h | 175 +++++ src/graphics/engine/terrain.cpp | 23 + src/graphics/engine/terrain.h | 211 ++++++ src/graphics/engine/test/CMakeLists.txt | 7 + src/graphics/engine/test/modelfile_test.cpp | 48 ++ src/graphics/engine/text.cpp | 23 + src/graphics/engine/text.h | 113 ++++ src/graphics/engine/water.cpp | 23 + src/graphics/engine/water.h | 134 ++++ src/graphics/opengl/README.txt | 4 +- src/graphics/opengl/gldevice.h | 2 +- src/graphics/opengl/test/CMakeLists.txt | 4 +- src/graphics/opengl/test/model_test.cpp | 2 +- 74 files changed, 6199 insertions(+), 6160 deletions(-) delete mode 100644 src/graphics/common/README.txt delete mode 100644 src/graphics/common/camera.cpp delete mode 100644 src/graphics/common/camera.h delete mode 100644 src/graphics/common/cloud.cpp delete mode 100644 src/graphics/common/cloud.h delete mode 100644 src/graphics/common/color.cpp delete mode 100644 src/graphics/common/color.h delete mode 100644 src/graphics/common/device.h delete mode 100644 src/graphics/common/engine.cpp delete mode 100644 src/graphics/common/engine.h delete mode 100644 src/graphics/common/light.cpp delete mode 100644 src/graphics/common/light.h delete mode 100644 src/graphics/common/lightning.cpp delete mode 100644 src/graphics/common/lightning.h delete mode 100644 src/graphics/common/material.h delete mode 100644 src/graphics/common/modelfile.cpp delete mode 100644 src/graphics/common/modelfile.h delete mode 100644 src/graphics/common/particle.cpp delete mode 100644 src/graphics/common/particle.h delete mode 100644 src/graphics/common/planet.cpp delete mode 100644 src/graphics/common/planet.h delete mode 100644 src/graphics/common/pyro.cpp delete mode 100644 src/graphics/common/pyro.h delete mode 100644 src/graphics/common/terrain.cpp delete mode 100644 src/graphics/common/terrain.h delete mode 100644 src/graphics/common/test/CMakeLists.txt delete mode 100644 src/graphics/common/test/modelfile_test.cpp delete mode 100644 src/graphics/common/text.cpp delete mode 100644 src/graphics/common/text.h delete mode 100644 src/graphics/common/texture.h delete mode 100644 src/graphics/common/vertex.h delete mode 100644 src/graphics/common/water.cpp delete mode 100644 src/graphics/common/water.h create mode 100644 src/graphics/core/README.txt create mode 100644 src/graphics/core/color.cpp create mode 100644 src/graphics/core/color.h create mode 100644 src/graphics/core/device.h create mode 100644 src/graphics/core/light.h create mode 100644 src/graphics/core/material.h create mode 100644 src/graphics/core/texture.h create mode 100644 src/graphics/core/vertex.h create mode 100644 src/graphics/engine/README.txt create mode 100644 src/graphics/engine/camera.cpp create mode 100644 src/graphics/engine/camera.h create mode 100644 src/graphics/engine/cloud.cpp create mode 100644 src/graphics/engine/cloud.h create mode 100644 src/graphics/engine/engine.cpp create mode 100644 src/graphics/engine/engine.h create mode 100644 src/graphics/engine/lightman.cpp create mode 100644 src/graphics/engine/lightman.h create mode 100644 src/graphics/engine/lightning.cpp create mode 100644 src/graphics/engine/lightning.h create mode 100644 src/graphics/engine/modelfile.cpp create mode 100644 src/graphics/engine/modelfile.h create mode 100644 src/graphics/engine/particle.cpp create mode 100644 src/graphics/engine/particle.h create mode 100644 src/graphics/engine/planet.cpp create mode 100644 src/graphics/engine/planet.h create mode 100644 src/graphics/engine/pyro.cpp create mode 100644 src/graphics/engine/pyro.h create mode 100644 src/graphics/engine/terrain.cpp create mode 100644 src/graphics/engine/terrain.h create mode 100644 src/graphics/engine/test/CMakeLists.txt create mode 100644 src/graphics/engine/test/modelfile_test.cpp create mode 100644 src/graphics/engine/text.cpp create mode 100644 src/graphics/engine/text.h create mode 100644 src/graphics/engine/water.cpp create mode 100644 src/graphics/engine/water.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 06f5990..76a10e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,19 +43,19 @@ common/iman.cpp # common/profile.cpp # common/restext.cpp common/stringutils.cpp -graphics/common/camera.cpp -graphics/common/cloud.cpp -graphics/common/color.cpp -graphics/common/engine.cpp -graphics/common/light.cpp -graphics/common/lightning.cpp -graphics/common/modelfile.cpp -graphics/common/particle.cpp -graphics/common/planet.cpp -graphics/common/pyro.cpp -graphics/common/terrain.cpp -graphics/common/text.cpp -graphics/common/water.cpp +graphics/core/color.cpp +graphics/engine/camera.cpp +graphics/engine/cloud.cpp +graphics/engine/engine.cpp +graphics/engine/lightman.cpp +graphics/engine/lightning.cpp +graphics/engine/modelfile.cpp +graphics/engine/particle.cpp +graphics/engine/planet.cpp +graphics/engine/pyro.cpp +graphics/engine/terrain.cpp +graphics/engine/text.cpp +graphics/engine/water.cpp graphics/opengl/gldevice.cpp # object/auto/auto.cpp # object/auto/autobase.cpp diff --git a/src/app/app.h b/src/app/app.h index 576ed62..956eab8 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -22,8 +22,8 @@ #include "common/misc.h" #include "common/singleton.h" -#include "graphics/common/device.h" -#include "graphics/common/engine.h" +#include "graphics/core/device.h" +#include "graphics/engine/engine.h" #include #include diff --git a/src/graphics/common/README.txt b/src/graphics/common/README.txt deleted file mode 100644 index 495a453..0000000 --- a/src/graphics/common/README.txt +++ /dev/null @@ -1,5 +0,0 @@ -src/graphics/common - -Universal structs and classes used in graphics engine - -Concrete implementation in OpenGL is in graphics/opengl directory. diff --git a/src/graphics/common/camera.cpp b/src/graphics/common/camera.cpp deleted file mode 100644 index 9990d01..0000000 --- a/src/graphics/common/camera.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// camera.cpp - -#include "graphics/common/camera.h" - - -// TODO implementation diff --git a/src/graphics/common/camera.h b/src/graphics/common/camera.h deleted file mode 100644 index 76077bf..0000000 --- a/src/graphics/common/camera.h +++ /dev/null @@ -1,270 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// camera.h - -#pragma once - -#include "engine.h" -#include "common/event.h" - - -class CInstanceManager; -class CObject; - -namespace Gfx { - - -enum CameraType -{ - CAMERA_NULL = 0, // camera undefined - CAMERA_FREE = 1, // camera free (never in principle) - CAMERA_EDIT = 2, // camera while editing a program - CAMERA_ONBOARD = 3, // camera on board a robot - CAMERA_BACK = 4, // camera behind a robot - CAMERA_FIX = 5, // static camera following robot - CAMERA_EXPLO = 6, // camera steady after explosion - CAMERA_SCRIPT = 7, // camera during a film script - CAMERA_INFO = 8, // camera for displaying information - CAMERA_VISIT = 9, // visit instead of an error - CAMERA_DIALOG = 10, // camera for dialogue - CAMERA_PLANE = 11, // static camera height -}; - -enum CameraSmooth -{ - CS_NONE = 0, // sharp - CS_NORM = 1, // normal - CS_HARD = 2, // hard - CS_SPEC = 3, // special -}; - -enum CenteringPhase -{ - CP_NULL = 0, - CP_START = 1, - CP_WAIT = 2, - CP_STOP = 3, -}; - -enum CameraEffect -{ - CE_NULL = 0, // no effect - CE_TERRAFORM = 1, // digging in - CE_CRASH = 2, // Vehicle driving is severely - CE_EXPLO = 3, // explosion - CE_SHOT = 4, // not mortal shot - CE_VIBRATION = 5, // vibration during construction - CE_PET = 6, // spleen reactor -}; - -enum OverEffect -{ - OE_NULL = 0, // no effect - OE_BLOOD = 1, // flash red - OE_FADEINw = 2, // white -> nothing - OE_FADEOUTw = 3, // nothing -> white - OE_FADEOUTb = 4, // nothing -> blue - OE_BLITZ = 5, // lightning -}; - - - -class CCamera { - - public: - CCamera(CInstanceManager* iMan); - ~CCamera(); - - bool EventProcess(const Event &event); - - void Init(Math::Vector eye, Math::Vector lookat, float delay); - - void SetObject(CObject* object); - CObject* RetObject(); - - void SetType(CameraType type); - CameraType RetType(); - - void SetSmooth(CameraSmooth type); - CameraSmooth RetSmoth(); - - void SetDist(float dist); - float RetDist(); - - void SetFixDirection(float angle); - float RetFixDirection(); - - void SetRemotePan(float value); - float RetRemotePan(); - - void SetRemoteZoom(float value); - float RetRemoteZoom(); - - void StartVisit(Math::Vector goal, float dist); - void StopVisit(); - - void RetCamera(Math::Vector &eye, Math::Vector &lookat); - - bool StartCentering(CObject *object, float angleH, float angleV, float dist, float time); - bool StopCentering(CObject *object, float time); - void AbortCentering(); - - void FlushEffect(); - void StartEffect(CameraEffect effect, Math::Vector pos, float force); - - void FlushOver(); - void SetOverBaseColor(Gfx::Color color); - void StartOver(OverEffect effect, Math::Vector pos, float force); - - void FixCamera(); - void SetScriptEye(Math::Vector eye); - void SetScriptLookat(Math::Vector lookat); - - void SetEffect(bool bEnable); - void SetCameraScroll(bool bScroll); - void SetCameraInvertX(bool bInvert); - void SetCameraInvertY(bool bInvert); - - float RetMotorTurn(); - Gfx::EngineMouseType RetMouseDef(Math::Point pos); - -protected: - bool EventMouseMove(const Event &event); - void EventMouseWheel(int dir); - bool EventFrame(const Event &event); - bool EventFrameFree(const Event &event); - bool EventFrameEdit(const Event &event); - bool EventFrameDialog(const Event &event); - bool EventFrameBack(const Event &event); - bool EventFrameFix(const Event &event); - bool EventFrameExplo(const Event &event); - bool EventFrameOnBoard(const Event &event); - bool EventFrameInfo(const Event &event); - bool EventFrameVisit(const Event &event); - bool EventFrameScript(const Event &event); - - void SetViewTime(const Math::Vector &vEyePt, const Math::Vector &vLookatPt, float rTime); - bool IsCollision(Math::Vector &eye, Math::Vector lookat); - bool IsCollisionBack(Math::Vector &eye, Math::Vector lookat); - bool IsCollisionFix(Math::Vector &eye, Math::Vector lookat); - - Math::Vector ExcludeTerrain(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); - Math::Vector ExcludeObject(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); - - void SetViewParams(const Math::Vector &eye, const Math::Vector &lookat, const Math::Vector &up); - void EffectFrame(const Event &event); - void OverFrame(const Event &event); - -protected: - CInstanceManager* m_iMan; - Gfx::CEngine* m_engine; - CTerrain* m_terrain; - CWater* m_water; - - CameraType m_type; // the type of camera (CAMERA *) - CameraSmooth m_smooth; // type of smoothing - CObject* m_cameraObj; // object linked to the camera - - float m_eyeDistance; // distance between the eyes - float m_initDelay; // time of initial centering - - Math::Vector m_actualEye; // current eye - Math::Vector m_actualLookat; // aim current - Math::Vector m_finalEye; // final eye - Math::Vector m_finalLookat; // aim final - Math::Vector m_normEye; // normal eye - Math::Vector m_normLookat; // aim normal - float m_focus; - - bool m_bRightDown; - Math::Point m_rightPosInit; - Math::Point m_rightPosCenter; - Math::Point m_rightPosMove; - - Math::Vector m_eyePt; // CAMERA_FREE: eye - float m_directionH; // CAMERA_FREE: horizontal direction - float m_directionV; // CAMERA_FREE: vertical direction - float m_heightEye; // CAMERA_FREE: height above the ground - float m_heightLookat; // CAMERA_FREE: height above the ground - float m_speed; // CAMERA_FREE: speed of movement - - float m_backDist; // CAMERA_BACK: distance - float m_backMin; // CAMERA_BACK: distance minimal - float m_addDirectionH; // CAMERA_BACK: additional direction - float m_addDirectionV; // CAMERA_BACK: additional direction - bool m_bTransparency; - - float m_fixDist; // CAMERA_FIX: distance - float m_fixDirectionH; // CAMERA_FIX: direction - float m_fixDirectionV; // CAMERA_FIX: direction - - Math::Vector m_visitGoal; // CAMERA_VISIT: target position - float m_visitDist; // CAMERA_VISIT: distance - float m_visitTime; // CAMERA_VISIT: relative time - CameraType m_visitType; // CAMERA_VISIT: initial type - float m_visitDirectionH; // CAMERA_VISIT: direction - float m_visitDirectionV; // CAMERA_VISIT: direction - - float m_editHeight; // CAMERA_EDIT: height - - float m_remotePan; - float m_remoteZoom; - - Math::Point m_mousePos; - float m_mouseDirH; - float m_mouseDirV; - float m_mouseMarging; - - float m_motorTurn; - - CenteringPhase m_centeringPhase; - float m_centeringAngleH; - float m_centeringAngleV; - float m_centeringDist; - float m_centeringCurrentH; - float m_centeringCurrentV; - float m_centeringTime; - float m_centeringProgress; - - CameraEffect m_effectType; - Math::Vector m_effectPos; - float m_effectForce; - float m_effectProgress; - Math::Vector m_effectOffset; - - OverEffect m_overType; - float m_overForce; - float m_overTime; - Gfx::Color m_overColorBase; - Gfx::Color m_overColor; - int m_overMode; - float m_overFadeIn; - float m_overFadeOut; - - Math::Vector m_scriptEye; - Math::Vector m_scriptLookat; - - bool m_bEffect; // shocks if explosion? - bool m_bCameraScroll; // scroll in the edges? - bool m_bCameraInvertX; // X inversion in the edges? - bool m_bCameraInvertY; // Y inversion in the edges? - -}; - - -}; // namespace Gfx diff --git a/src/graphics/common/cloud.cpp b/src/graphics/common/cloud.cpp deleted file mode 100644 index 707f641..0000000 --- a/src/graphics/common/cloud.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// cloud.cpp - -#include "graphics/common/cloud.h" - - -// TODO implementation diff --git a/src/graphics/common/cloud.h b/src/graphics/common/cloud.h deleted file mode 100644 index 19b689f..0000000 --- a/src/graphics/common/cloud.h +++ /dev/null @@ -1,94 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// cloud.h - -#pragma once - -#include "common/event.h" -#include "graphics/common/color.h" -#include "math/point.h" -#include "math/vector.h" - - - -class CInstanceManager; - - -namespace Gfx { - -class CEngine; -class CTerrain; - -const short MAXCLOUDLINE = 100; - -struct CloudLine -{ - short x, y; // beginning - short len; // in length x - float px1, px2, pz; -}; - - -class CCloud -{ -public: - CCloud(CInstanceManager* iMan, CEngine* engine); - ~CCloud(); - - bool EventProcess(const Event &event); - void Flush(); - bool Create(const char *filename, Gfx::Color diffuse, Gfx::Color ambient, float level); - void Draw(); - - bool SetLevel(float level); - float RetLevel(); - - void SetEnable(bool bEnable); - bool RetEnable(); - -protected: - bool EventFrame(const Event &event); - void AdjustLevel(Math::Vector &pos, Math::Vector &eye, float deep, Math::Point &uv1, Math::Point &uv2); - bool CreateLine(int x, int y, int len); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CTerrain* m_terrain; - - char m_filename[100]; - float m_level; // overall level - Math::Point m_speed; // feedrate (wind) - Gfx::Color m_diffuse; // diffuse color - Gfx::Color m_ambient; // ambient color - float m_time; - float m_lastTest; - int m_subdiv; - - Math::Vector m_wind; // wind speed - int m_brick; // brick mosaic - float m_size; // size of a brick element - - int m_lineUsed; - CloudLine m_line[MAXCLOUDLINE]; - - bool m_bEnable; -}; - - -}; // namespace Gfx diff --git a/src/graphics/common/color.cpp b/src/graphics/common/color.cpp deleted file mode 100644 index c1b7337..0000000 --- a/src/graphics/common/color.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// color.cpp - -#include "graphics/common/color.h" - -#include "math/func.h" - - -Gfx::ColorHSV Gfx::RGB2HSV(Gfx::Color color) -{ - Gfx::ColorHSV result; - - float min = Math::Min(color.r, color.g, color.b); - float max = Math::Max(color.r, color.g, color.b); - - result.v = max; // intensity - - if ( max == 0.0f ) - { - result.s = 0.0f; // saturation - result.h = 0.0f; // undefined color! - } - else - { - float delta = max-min; - result.s = delta/max; // saturation - - if ( color.r == max ) // between yellow & magenta - { - result.h = (color.g-color.b)/delta; - } - else if ( color.g == max ) // between cyan & yellow - { - result.h = 2.0f+(color.b-color.r)/delta; - } - else // between magenta & cyan - { - result.h = 4.0f+(color.r-color.g)/delta; - } - - result.h *= 60.0f; // in degrees - if ( result.h < 0.0f ) result.h += 360.0f; - result.h /= 360.0f; // 0..1 - } - - return result; -} - -Gfx::Color Gfx::HSV2RGB(Gfx::ColorHSV color) -{ - Gfx::Color result; - - color.h = Math::Norm(color.h)*360.0f; - color.s = Math::Norm(color.s); - color.v = Math::Norm(color.v); - - if ( color.s == 0.0f ) // zero saturation? - { - result.r = color.v; - result.g = color.v; - result.b = color.v; // gray - } - else - { - if ( color.h == 360.0f ) color.h = 0.0f; - color.h /= 60.0f; - int i = (int)color.h; // integer part (0 .. 5) - float f = color.h-i; // fractional part - - float v = color.v; - float p = color.v*(1.0f-color.s); - float q = color.v*(1.0f-(color.s*f)); - float t = color.v*(1.0f-(color.s*(1.0f-f))); - - switch (i) - { - case 0: result.r=v; result.g=t; result.b=p; break; - case 1: result.r=q; result.g=v; result.b=p; break; - case 2: result.r=p; result.g=v; result.b=t; break; - case 3: result.r=p; result.g=q; result.b=v; break; - case 4: result.r=t; result.g=p; result.b=v; break; - case 5: result.r=v; result.g=p; result.b=q; break; - } - } - - return result; -} - diff --git a/src/graphics/common/color.h b/src/graphics/common/color.h deleted file mode 100644 index 907a3b9..0000000 --- a/src/graphics/common/color.h +++ /dev/null @@ -1,98 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// color.h - -#pragma once - - -#include - - -namespace Gfx { - -/** - \struct Color - \brief RGBA color */ -struct Color -{ - //! Red, green, blue and alpha components - float r, g, b, a; - - //! Constructor; default values are (0,0,0,0) = black - Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f) - : r(aR), g(aG), b(aB), a(aA) {} - - inline Gfx::Color Inverse() const - { - return Gfx::Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a); - } - - //! Returns the struct cast to \c float* array; use with care! - inline float* Array() - { - return (float*)this; - } - - //! Returns the struct cast to const float* array; use with care! - inline const float* Array() const - { - return (const float*)this; - } - - //! Returns a string (r, g, b, a) - inline std::string ToString() const - { - std::stringstream s; - s.precision(3); - s << "(" << r << ", " << g << ", " << b << ", " << a << ")"; - return s.str(); - } - - inline bool operator==(const Gfx::Color &other) const - { - return r == other.r && g == other.g && b == other.b && a == other.a; - } -}; - -/** - \struct ColorHSV - \brief HSV color */ -struct ColorHSV -{ - float h, s, v; - - ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f) - : h(aH), s(aS), v(aV) {} - - //! Returns a string "(h, s, v)" - inline std::string ToString() const - { - std::stringstream s; - s.precision(3); - s << "(" << h << ", " << s << ", " << v << ")"; - return s.str(); - } -}; - -//! Converts a RGB color to HSV color -Gfx::ColorHSV RGB2HSV(Gfx::Color color); - -//! Converts a HSV color to RGB color -Gfx::Color HSV2RGB(Gfx::ColorHSV color); - -}; // namespace Gfx - diff --git a/src/graphics/common/device.h b/src/graphics/common/device.h deleted file mode 100644 index 7aa4ce3..0000000 --- a/src/graphics/common/device.h +++ /dev/null @@ -1,414 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// device.h - -#pragma once - - -#include "graphics/common/color.h" -#include "graphics/common/light.h" -#include "graphics/common/material.h" -#include "graphics/common/texture.h" -#include "graphics/common/vertex.h" -#include "math/matrix.h" - -#include - - -class CImage; - - -namespace Gfx { - -/** - \struct DeviceConfig - \brief General config for graphics device - - These settings are common window options set by SDL. -*/ -struct DeviceConfig -{ - //! Screen width - int width; - //! Screen height - int height; - //! Bits per pixel - int bpp; - //! Full screen - bool fullScreen; - //! Resizeable window - bool resizeable; - //! Double buffering - bool doubleBuf; - //! No window frame (also set with full screen) - bool noFrame; - - //! Constructor calls LoadDefault() - DeviceConfig() { LoadDefault(); } - - //! Loads the default values - inline void LoadDefault() - { - width = 800; - height = 600; - bpp = 32; - fullScreen = false; - resizeable = false; - doubleBuf = true; - noFrame = false; - } -}; - - -/** - \enum TransformType - \brief Type of transformation in rendering pipeline - - These correspond to DirectX's three transformation matrices. */ -enum TransformType -{ - TRANSFORM_WORLD, - TRANSFORM_VIEW, - TRANSFORM_PROJECTION -}; - -/** - \enum RenderState - \brief Render states that can be enabled/disabled */ -enum RenderState -{ - RENDER_STATE_LIGHTING, - RENDER_STATE_TEXTURING, - RENDER_STATE_BLENDING, - RENDER_STATE_FOG, - RENDER_STATE_DEPTH_TEST, - RENDER_STATE_DEPTH_WRITE, - RENDER_STATE_ALPHA_TEST, - RENDER_STATE_CULLING, - RENDER_STATE_DITHERING -}; - -/** - \enum CompFunc - \brief Type of function used to compare values */ -enum CompFunc -{ - COMP_FUNC_NEVER, - COMP_FUNC_LESS, - COMP_FUNC_EQUAL, - COMP_FUNC_NOTEQUAL, - COMP_FUNC_LEQUAL, - COMP_FUNC_GREATER, - COMP_FUNC_GEQUAL, - COMP_FUNC_ALWAYS -}; - -/** - \enum BlendFunc - \brief Type of blending function */ -enum BlendFunc -{ - BLEND_ZERO, - BLEND_ONE, - BLEND_SRC_COLOR, - BLEND_INV_SRC_COLOR, - BLEND_DST_COLOR, - BLEND_INV_DST_COLOR, - BLEND_SRC_ALPHA, - BLEND_INV_SRC_ALPHA, - BLEND_DST_ALPHA, - BLEND_INV_DST_ALPHA, - BLEND_SRC_ALPHA_SATURATE -}; - -/** - \enum FogMode - \brief Type of fog calculation function */ -enum FogMode -{ - FOG_LINEAR, - FOG_EXP, - FOG_EXP2 -}; - -/** - \enum CullMode - \brief Culling mode for polygons */ -enum CullMode -{ - //! Cull clockwise side - CULL_CW, - //! Cull counter-clockwise side - CULL_CCW -}; - -/** - \enum ShadeModel - \brief Shade model used in rendering */ -enum ShadeModel -{ - SHADE_FLAT, - SHADE_SMOOTH -}; - -/** - \enum FillMode - \brief Polygon fill mode */ -enum FillMode -{ - //! Draw only points - FILL_POINT, - //! Draw only lines - FILL_LINES, - //! Draw full polygons - FILL_FILL -}; - -/** - \enum PrimitiveType - \brief Type of primitive to render - - Only these two types are used. */ -enum PrimitiveType -{ - PRIMITIVE_LINES, - PRIMITIVE_TRIANGLES, - PRIMITIVE_TRIANGLE_STRIP -}; - -/** - \enum IntersectPlane - \brief Intersection plane of projection volume - - These flags can be OR'd together. */ -enum IntersectPlane -{ - INTERSECT_PLANE_LEFT = 0x01, - INTERSECT_PLANE_RIGHT = 0x02, - INTERSECT_PLANE_TOP = 0x04, - INTERSECT_PLANE_BOTTOM = 0x08, - INTERSECT_PLANE_FRONT = 0x10, - INTERSECT_PLANE_BACK = 0x20, - INTERSECT_PLANE_ALL = INTERSECT_PLANE_LEFT | INTERSECT_PLANE_RIGHT | - INTERSECT_PLANE_TOP | INTERSECT_PLANE_BOTTOM | - INTERSECT_PLANE_FRONT | INTERSECT_PLANE_BACK -}; - -/* - -Notes for rewriting DirectX code: - ->> SetRenderState() translates to many functions depending on param - -D3DRENDERSTATE_ALPHABLENDENABLE -> SetRenderState() with RENDER_STATE_BLENDING -D3DRENDERSTATE_ALPHAFUNC -> SetAlphaTestFunc() func -D3DRENDERSTATE_ALPHAREF -> SetAlphaTestFunc() ref -D3DRENDERSTATE_ALPHATESTENABLE -> SetRenderState() with RENDER_STATE_ALPHA_TEST -D3DRENDERSTATE_AMBIENT -> SetGlobalAmbient() -D3DRENDERSTATE_CULLMODE -> SetCullMode() -D3DRENDERSTATE_DESTBLEND -> SetBlendFunc() dest blending func -D3DRENDERSTATE_DITHERENABLE -> SetRenderState() with RENDER_STATE_DITHERING -D3DRENDERSTATE_FILLMODE -> SetFillMode() -D3DRENDERSTATE_FOGCOLOR -> SetFogParams() -D3DRENDERSTATE_FOGENABLE -> SetRenderState() with RENDER_STATE_FOG -D3DRENDERSTATE_FOGEND -> SetFogParams() -D3DRENDERSTATE_FOGSTART -> SetFogParams() -D3DRENDERSTATE_FOGVERTEXMODE -> SetFogParams() fog model -D3DRENDERSTATE_LIGHTING -> SetRenderState() with RENDER_STATE_LIGHTING -D3DRENDERSTATE_SHADEMODE -> SetShadeModel() -D3DRENDERSTATE_SPECULARENABLE -> doesn't matter (always enabled) -D3DRENDERSTATE_SRCBLEND -> SetBlendFunc() src blending func -D3DRENDERSTATE_TEXTUREFACTOR -> SetTextureFactor() -D3DRENDERSTATE_ZBIAS -> SetDepthBias() -D3DRENDERSTATE_ZENABLE -> SetRenderState() with RENDER_STATE_DEPTH_TEST -D3DRENDERSTATE_ZFUNC -> SetDepthTestFunc() -D3DRENDERSTATE_ZWRITEENABLE -> SetRenderState() with RENDER_STATE_DEPTH_WRITE - - ->> SetTextureStageState() translates to SetTextureParams() or CreateTexture() for some params - -Params from enum in struct TextureCreateParams or TextureParams - D3DTSS_ADDRESS -> Gfx::TexWrapMode wrapS, wrapT - D3DTSS_ALPHAARG1 -> Gfx::TexMixArgument alphaArg1 - D3DTSS_ALPHAARG2 -> Gfx::TexMixArgument alphaArg2 - D3DTSS_ALPHAOP -> Gfx::TexMixOperation alphaOperation - D3DTSS_COLORARG1 -> Gfx::TexMixArgument colorArg1 - D3DTSS_COLORARG2 -> Gfx::TexMixArgument colorArg2 - D3DTSS_COLOROP -> Gfx::TexMixOperation colorOperation - D3DTSS_MAGFILTER -> Gfx::TexMagFilter magFilter - D3DTSS_MINFILTER -> Gfx::TexMinFilter minFilter - D3DTSS_TEXCOORDINDEX -> doesn't matter (texture coords are set explicitly by glMultiTexCoordARB*) - -Note that D3DTSS_ALPHAOP or D3DTSS_COLOROP set to D3DTOP_DISABLE must translate to disabling the whole texture stage. -In DirectX, you shouldn't mix enabling one and disabling the other. -Also, if previous stage is disabled in DirectX, the later ones are disabled, too. In OpenGL, that is not the case. - -*/ - -/** - \class CDevice - \brief Abstract interface of graphics device - - It is based on DIRECT3DDEVICE class from DirectX to make it easier to port existing code. - It encapsulates the general graphics device state and provides a common interface - to graphics-specific functions which will be used throughout the program, - both in CEngine class and in UI classes. Note that it doesn't contain all functions from DirectX, - only those that were used in old code. - - */ -class CDevice -{ -public: - virtual ~CDevice() {} - - //! Initializes the device, setting the initial state - virtual bool Create() = 0; - //! Destroys the device, releasing every acquired resource - virtual void Destroy() = 0; - - //! Returns whether the device has been initialized - virtual bool GetWasInit() = 0; - //! Returns the last encountered error - virtual std::string GetError() = 0; - - //! Begins drawing the 3D scene - virtual void BeginScene() = 0; - //! Ends drawing the 3D scene - virtual void EndScene() = 0; - - //! Clears the screen to blank - virtual void Clear() = 0; - - //! Sets the transform matrix of given type - virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0; - //! Returns the current transform matrix of given type - virtual const Math::Matrix& GetTransform(TransformType type) = 0; - //! Multiplies the current transform matrix of given type by given matrix - virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix) = 0; - - //! Sets the current material - virtual void SetMaterial(const Gfx::Material &material) = 0; - //! Returns the current material - virtual const Gfx::Material& GetMaterial() = 0; - - //! Returns the maximum number of lights available - virtual int GetMaxLightCount() = 0; - //! Sets the light at given index - virtual void SetLight(int index, const Gfx::Light &light) = 0; - //! Returns the current light at given index - virtual const Gfx::Light& GetLight(int index) = 0; - //! Enables/disables the light at given index - virtual void SetLightEnabled(int index, bool enabled) = 0; - //! Returns the current enable state of light at given index - virtual bool GetLightEnabled(int index) = 0; - - //! Creates a texture from image; the image can be safely removed after that - virtual Gfx::Texture CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) = 0; - //! Deletes a given texture, freeing it from video memory - virtual void DestroyTexture(const Gfx::Texture &texture) = 0; - //! Deletes all textures created so far - virtual void DestroyAllTextures() = 0; - - //! Returns the maximum number of multitexture stages - virtual int GetMaxTextureCount() = 0; - //! Sets the (multi)texture at given index - virtual void SetTexture(int index, const Gfx::Texture &texture) = 0; - //! Returns the (multi)texture at given index - virtual Gfx::Texture GetTexture(int index) = 0; - //! Enables/disables the given texture stage - virtual void SetTextureEnabled(int index, bool enabled) = 0; - //! Returns the current enable state of given texture stage - virtual bool GetTextureEnabled(int index) = 0; - - //! Sets the params for texture stage with given index - virtual void SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms) = 0; - //! Returns the current params of texture stage with given index - virtual Gfx::TextureStageParams GetTextureStageParams(int index) = 0; - - //! Sets the texture factor to the given color value - virtual void SetTextureFactor(const Gfx::Color &color) = 0; - //! Returns the current texture factor - virtual Gfx::Color GetTextureFactor() = 0; - - //! Renders primitive composed of vertices with single texture - virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices , int vertexCount) = 0; - //! Renders primitive composed of vertices with color information and single texture - virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices , int vertexCount) = 0; - //! Renders primitive composed of vertices with multitexturing (2 textures) - virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount) = 0; - - //! Tests whether a sphere intersects the 6 clipping planes of projection volume - virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius) = 0; - - //! Enables/disables the given render state - virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; - //! Returns the current setting of given render state - virtual bool GetRenderState(Gfx::RenderState state) = 0; - - //! Sets the function of depth test - virtual void SetDepthTestFunc(Gfx::CompFunc func) = 0; - //! Returns the current function of depth test - virtual Gfx::CompFunc GetDepthTestFunc() = 0; - - //! Sets the depth bias (constant value added to Z-coords) - virtual void SetDepthBias(float factor) = 0; - //! Returns the current depth bias - virtual float GetDepthBias() = 0; - - //! Sets the alpha test function and reference value - virtual void SetAlphaTestFunc(Gfx::CompFunc func, float refValue) = 0; - //! Returns the current alpha test function and reference value - virtual void GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue) = 0; - - //! Sets the blending functions for source and destination operations - virtual void SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend) = 0; - //! Returns the current blending functions for source and destination operations - virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) = 0; - - //! Sets the clear color - virtual void SetClearColor(const Gfx::Color &color) = 0; - //! Returns the current clear color - virtual Gfx::Color GetClearColor() = 0; - - //! Sets the global ambient color - virtual void SetGlobalAmbient(const Gfx::Color &color) = 0; - //! Returns the global ambient color - virtual Gfx::Color GetGlobalAmbient() = 0; - - //! Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) - virtual void SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, float start, float end, float density) = 0; - //! Returns the current fog parameters: mode, color, start distance, end distance and density (for exp models) - virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) = 0; - - //! Sets the current cull mode - virtual void SetCullMode(Gfx::CullMode mode) = 0; - //! Returns the current cull mode - virtual Gfx::CullMode GetCullMode() = 0; - - //! Sets the shade model - virtual void SetShadeModel(Gfx::ShadeModel model) = 0; - //! Returns the current shade model - virtual Gfx::ShadeModel GetShadeModel() = 0; - - //! Sets the current fill mode - virtual void SetFillMode(Gfx::FillMode mode) = 0; - //! Returns the current fill mode - virtual Gfx::FillMode GetFillMode() = 0; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/engine.cpp b/src/graphics/common/engine.cpp deleted file mode 100644 index e6dcfc7..0000000 --- a/src/graphics/common/engine.cpp +++ /dev/null @@ -1,729 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// engine.cpp - -#include "graphics/common/engine.h" - -#include "app/app.h" -#include "common/iman.h" -#include "common/image.h" -#include "common/key.h" -#include "common/logger.h" -#include "graphics/common/device.h" -#include "math/geometry.h" - -// Initial size of various vectors -const int OBJECT_PREALLOCATE_COUNT = 1200; -const int SHADOW_PREALLOCATE_COUNT = 500; -const int GROUNDSPOT_PREALLOCATE_COUNT = 100; - -const int LEVEL1_PREALLOCATE_COUNT = 50; -const int LEVEL2_PREALLOCATE_COUNT = 100; -const int LEVEL3_PREALLOCATE_COUNT = 5; -const int LEVEL4_PREALLOCATE_COUNT = 10; -const int LEVEL5_PREALLOCATE_COUNT = 100; -const int LEVEL5_VERTEX_PREALLOCATE_COUNT = 200; - - -Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) -{ - m_iMan = iMan; - m_app = app; - m_device = NULL; - - m_wasInit = false; - - m_iMan = iMan; - m_iMan->AddInstance(CLASS_ENGINE, this); - m_app = app; - - m_lightMan = NULL; - m_text = NULL; - m_particle = NULL; - m_water = NULL; - m_cloud = NULL; - m_lightning = NULL; - m_planet = NULL; - m_sound = NULL; - m_terrain = NULL; - - m_dim.x = 640; - m_dim.y = 480; - m_lastDim = m_dim; - m_focus = 0.75f; - m_baseTime = 0; - m_lastTime = 0; - m_absTime = 0.0f; - m_rankView = 0; - - m_ambientColor[0] = Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); - m_ambientColor[1] = Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); - m_fogColor[0] = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f); - m_fogColor[1] = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f); - m_deepView[0] = 1000.0f; - m_deepView[1] = 1000.0f; - m_fogStart[0] = 0.75f; - m_fogStart[1] = 0.75f; - m_waterAddColor = Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f); - - m_pause = false; - m_render = true; - m_movieLock = false; - m_shadowVisible = true; - m_groundSpotVisible = true; - m_dirty = true; - m_fog = true; - m_speed = 1.0f; - m_secondTexNum = 0; - m_eyeDirH = 0.0f; - m_eyeDirV = 0.0f; - m_backgroundName = ""; // no background image - m_backgroundColorUp = 0; - m_backgroundColorDown = 0; - m_backgroundCloudUp = 0; - m_backgroundCloudDown = 0; - m_backgroundFull = false; - m_backgroundQuarter = false; - m_overFront = true; - m_overColor = 0; - m_overMode = ENG_RSTATE_TCOLOR_BLACK; - m_frontsizeName = ""; // no front image - m_hiliteRank[0] = -1; // empty list - m_eyePt = Math::Vector(0.0f, 0.0f, 0.0f); - m_lookatPt = Math::Vector(0.0f, 0.0f, 1.0f); - m_drawWorld = true; - m_drawFront = false; - m_limitLOD[0] = 100.0f; - m_limitLOD[1] = 200.0f; - m_particuleDensity = 1.0f; - m_clippingDistance = 1.0f; - m_lastClippingDistance = m_clippingDistance; - m_objectDetail = 1.0f; - m_lastObjectDetail = m_objectDetail; - m_terrainVision = 1000.0f; - m_gadgetQuantity = 1.0f; - m_textureQuality = 1; - m_totoMode = true; - m_lensMode = true; - m_waterMode = true; - m_skyMode = true; - m_backForce = true; - m_planetMode = true; - m_lightMode = true; - m_editIndentMode = true; - m_editIndentValue = 4; - m_tracePrecision = 1.0f; - - m_alphaMode = 1; - - m_forceStateColor = true; - m_stateColor = false; - - m_blackSrcBlend[0] = 0; - m_blackDestBlend[0] = 0; - m_whiteSrcBlend[0] = 0; - m_whiteDestBlend[0] = 0; - m_diffuseSrcBlend[0] = 0; - m_diffuseDestBlend[0] = 0; - m_alphaSrcBlend[0] = 0; - m_alphaDestBlend[0] = 0; - - m_updateGeometry = false; - - m_mice[Gfx::ENG_MOUSE_NORM] = Gfx::EngineMouse( 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f)); - m_mice[Gfx::ENG_MOUSE_WAIT] = Gfx::EngineMouse( 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f)); - m_mice[Gfx::ENG_MOUSE_HAND] = Gfx::EngineMouse( 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f)); - m_mice[Gfx::ENG_MOUSE_NO] = Gfx::EngineMouse( 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f)); - m_mice[Gfx::ENG_MOUSE_EDIT] = Gfx::EngineMouse( 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f)); - m_mice[Gfx::ENG_MOUSE_CROSS] = Gfx::EngineMouse(10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f)); - m_mice[Gfx::ENG_MOUSE_MOVEV] = Gfx::EngineMouse(12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f)); - m_mice[Gfx::ENG_MOUSE_MOVEH] = Gfx::EngineMouse(14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f)); - m_mice[Gfx::ENG_MOUSE_MOVED] = Gfx::EngineMouse(16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)); - m_mice[Gfx::ENG_MOUSE_MOVEI] = Gfx::EngineMouse(18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)); - m_mice[Gfx::ENG_MOUSE_MOVE] = Gfx::EngineMouse(20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f)); - m_mice[Gfx::ENG_MOUSE_TARGET] = Gfx::EngineMouse(22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f)); - m_mice[Gfx::ENG_MOUSE_SCROLLL] = Gfx::EngineMouse(24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f)); - m_mice[Gfx::ENG_MOUSE_SCROLLR] = Gfx::EngineMouse(26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f)); - m_mice[Gfx::ENG_MOUSE_SCROLLU] = Gfx::EngineMouse(28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f)); - m_mice[Gfx::ENG_MOUSE_SCROLLD] = Gfx::EngineMouse(30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f)); - - m_mouseSize = Math::Point(0.04f, 0.04f * (800.0f / 600.0f)); - m_mousePos = Math::Point(0.5f, 0.5f); - m_mouseType = Gfx::ENG_MOUSE_NORM; - m_mouseVisible = false; - - m_texPath = "textures/"; - m_defaultTexParams.format = Gfx::TEX_IMG_RGBA; - m_defaultTexParams.mipmap = true; - m_defaultTexParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; - m_defaultTexParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; - - m_objectTree.reserve(LEVEL1_PREALLOCATE_COUNT); - m_objects.reserve(OBJECT_PREALLOCATE_COUNT); - m_shadow.reserve(SHADOW_PREALLOCATE_COUNT); - m_groundSpot.reserve(GROUNDSPOT_PREALLOCATE_COUNT); -} - -Gfx::CEngine::~CEngine() -{ - m_iMan = NULL; - m_app = NULL; - m_device = NULL; - - m_sound = NULL; - m_terrain = NULL; -} - -bool Gfx::CEngine::GetWasInit() -{ - return m_wasInit; -} - -std::string Gfx::CEngine::GetError() -{ - return m_error; -} - -bool Gfx::CEngine::Create() -{ - m_wasInit = true; - - /*m_lightMan = new Gfx::CLight(m_iMan, this); - m_text = new Gfx::CText(m_iMan, this); - m_particle = new Gfx::CParticle(m_iMan, this); - m_water = new Gfx::CWater(m_iMan, this); - m_cloud = new Gfx::CCloud(m_iMan, this); - m_lightning = new Gfx::CLightning(m_iMan, this); - m_planet = new Gfx::CPlanet(m_iMan, this);*/ - - m_matWorldInterface.LoadIdentity(); - m_matViewInterface.LoadIdentity(); - Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); - - return true; -} - -void Gfx::CEngine::Destroy() -{ - // TODO - - /*delete m_lightMan; - m_lightMan = NULL; - - delete m_text; - m_text = NULL; - - delete m_particle; - m_particle = NULL; - - delete m_water; - m_water = NULL; - - delete m_cloud; - m_cloud = NULL; - - delete m_lightning; - m_lightning = NULL; - - delete m_planet; - m_planet = NULL;*/ - - m_wasInit = false; -} - -void Gfx::CEngine::SetDevice(Gfx::CDevice *device) -{ - m_device = device; -} - -Gfx::CDevice* Gfx::CEngine::GetDevice() -{ - return m_device; -} - -bool Gfx::CEngine::AfterDeviceSetInit() -{ - m_device->SetClearColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)); - - m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); - - Gfx::TextureCreateParams params; - params.format = Gfx::TEX_IMG_RGB; - params.minFilter = Gfx::TEX_MIN_FILTER_NEAREST; - params.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - params.mipmap = false; - m_miceTexture = CreateTexture("mouse.png", params); - - return true; -} - -Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams ¶ms) -{ - CImage img; - if (! img.Load(m_app->GetDataFilePath(m_texPath, texName))) - { - std::stringstream str; - str << "Couldn't load texture '" << texName << "': " << img.GetError(); - m_error = str.str(); - return Gfx::Texture(); // invalid texture - } - - Gfx::Texture result = m_device->CreateTexture(&img, params); - - if (! result.valid) - { - std::stringstream str; - str << "Couldn't load texture '" << texName << "': " << m_device->GetError(); - m_error = str.str(); - return result; - } - - m_texNameMap[texName] = result; - m_revTexNameMap[result] = texName; - - return result; -} - -Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName) -{ - return CreateTexture(texName, m_defaultTexParams); -} - -void Gfx::CEngine::DestroyTexture(const std::string &texName) -{ - std::map::iterator it = m_texNameMap.find(texName); - if (it == m_texNameMap.end()) - return; - - std::map::iterator revIt = m_revTexNameMap.find((*it).second); - - m_device->DestroyTexture((*it).second); - - m_revTexNameMap.erase(revIt); - m_texNameMap.erase(it); -} - -void Gfx::CEngine::SetTexture(const std::string &name, int stage) -{ - std::map::iterator it = m_texNameMap.find(name); - if (it != m_texNameMap.end()) - m_device->SetTexture(stage, (*it).second); - - // TODO if not present... -} - -void Gfx::CEngine::SetMaterial(const Gfx::Material &mat) -{ - m_device->SetMaterial(mat); -} - -void Gfx::CEngine::SetState(int state, Gfx::Color color) -{ - if ( state == m_lastState && color == m_lastColor ) - return; - - m_lastState = state; - m_lastColor = color; - - if ( m_alphaMode != 1 && (state & Gfx::ENG_RSTATE_ALPHA) ) - { - state &= ~Gfx::ENG_RSTATE_ALPHA; - - if (m_alphaMode == 2) - state |= Gfx::ENG_RSTATE_TTEXTURE_BLACK; - } - - // TODO other modes & thorough testing - - if (state & Gfx::ENG_RSTATE_TTEXTURE_BLACK) // The transparent black texture? - { - m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); - m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); - m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); - m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); - m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); - - m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); - m_device->SetTextureEnabled(0, true); - m_device->SetTextureFactor(color); - - Gfx::TextureStageParams params; - params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; - params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; - params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - m_device->SetTextureStageParams(0, params); - } - else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture? - { - m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); - m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); - m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); - m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); - m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); - - m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); - m_device->SetTextureEnabled(0, true); - m_device->SetTextureFactor(color.Inverse()); - - Gfx::TextureStageParams params; - params.colorOperation = Gfx::TEX_MIX_OPER_ADD; - params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; - params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; - params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - m_device->SetTextureStageParams(0, params); - } - else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color? - { - m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); - m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); - m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); - m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); - m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); - - m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); - - m_device->SetTextureFactor(color); - m_device->SetTextureEnabled(0, true); - m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); - } - else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color? - { - m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); - m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); - m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); - m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); - m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); - - m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); - - m_device->SetTextureFactor(color.Inverse()); - m_device->SetTextureEnabled(0, true); - m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); - } - else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent? - { - /*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); - m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); - m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); - m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); - m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]); - m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]); - - m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); - m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ - } - else if (state & Gfx::ENG_RSTATE_ALPHA) // image with alpha channel? - { - /*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); - m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); - m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); - m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true); - m_device->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER); - m_device->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128)); - m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]); - m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]); - - m_device->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); - m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); - m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); - m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); - m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/ - } - else // normal ? - { - m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true); - m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, true); - m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, false); - m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); - m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); - - m_device->SetTextureEnabled(0, true); - m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); - - /*m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); - m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); - m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ - } - - if (state & Gfx::ENG_RSTATE_FOG) - m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true); - - - bool second = m_groundSpotVisible || m_dirty; - - if ( !m_groundSpotVisible && (state & Gfx::ENG_RSTATE_SECOND) != 0 ) second = false; - if ( !m_dirty && (state & Gfx::ENG_RSTATE_SECOND) == 0 ) second = false; - - if ( (state & ENG_RSTATE_DUAL_BLACK) && second ) - { - /*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); - m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); - m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); - m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ - } - else if ( (state & ENG_RSTATE_DUAL_WHITE) && second ) - { - /*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD); - m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); - m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); - m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); - m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ - } - else - { - m_device->SetTextureEnabled(1, false); - } - - if (state & Gfx::ENG_RSTATE_WRAP) - { - /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP); - m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/ - } - else if (state & Gfx::ENG_RSTATE_CLAMP) - { - /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); - m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ - } - else - { - /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); - m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ - } - - if (state & Gfx::ENG_RSTATE_2FACE) - { - m_device->SetRenderState(Gfx::RENDER_STATE_CULLING, false); - } - else - { - m_device->SetRenderState(Gfx::RENDER_STATE_CULLING, true); - m_device->SetCullMode(Gfx::CULL_CCW); - } - - if (state & Gfx::ENG_RSTATE_LIGHT) - m_device->SetGlobalAmbient(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); - else - m_device->SetGlobalAmbient(m_ambientColor[m_rankView]); -} - -bool Gfx::CEngine::ProcessEvent(const Event &event) -{ - if (event.type == EVENT_MOUSE_MOVE) - { - m_mousePos = event.mouseMove.pos; - } - else if (event.type == EVENT_KEY_DOWN) - { - // !! Debug, to be removed later !! - - if (event.key.key == KEY(F1)) - { - m_mouseVisible = !m_mouseVisible; - m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile()); - } - else if (event.key.key == KEY(F2)) - { - int index = static_cast(m_mouseType); - m_mouseType = static_cast( (index + 1) % Gfx::ENG_MOUSE_COUNT ); - } - } - - // By default, pass on all events - return true; -} - -bool Gfx::CEngine::Render() -{ - m_statisticTriangle = 0; - - m_lastState = -1; - SetState(Gfx::ENG_RSTATE_NORMAL); - - m_device->BeginScene(); - - SetUp3DView(); - - if (! Draw3DScene() ) - return false; - - SetUpInterfaceView(); - - if (! DrawInterface() ) - return false; - - m_device->EndScene(); - - return true; -} - -void Gfx::CEngine::SetUp3DView() -{ - // TODO -} - -bool Gfx::CEngine::Draw3DScene() -{ - // TODO - return true; -} - -void Gfx::CEngine::SetUpInterfaceView() -{ - m_device->SetTransform(Gfx::TRANSFORM_WORLD, m_matWorldInterface); - m_device->SetTransform(Gfx::TRANSFORM_VIEW, m_matViewInterface); - m_device->SetTransform(Gfx::TRANSFORM_PROJECTION, m_matProjInterface); -} - -bool Gfx::CEngine::DrawInterface() -{ - Gfx::VertexCol vertices[3] = - { - Gfx::VertexCol(Math::Vector( 0.25f, 0.25f, 0.0f), Gfx::Color(1.0f, 0.0f, 0.0f)), - Gfx::VertexCol(Math::Vector( 0.75f, 0.25f, 0.0f), Gfx::Color(0.0f, 1.0f, 0.0f)), - Gfx::VertexCol(Math::Vector( 0.5f, 0.75f, 0.0f), Gfx::Color(0.0f, 0.0f, 1.0f)) - }; - - m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, vertices, 3); - - DrawMouse(); - - return true; -} - -void Gfx::CEngine::DrawMouse() -{ - if (! m_mouseVisible) - return; - - if (m_app->GetSystemMouseVisibile()) - return; - - Gfx::Material material; - material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f); - material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f); - - m_device->SetMaterial(material); - m_device->SetTexture(0, m_miceTexture); - - int index = static_cast(m_mouseType); - - Math::Point pos = m_mousePos; - pos.x = m_mousePos.x - (m_mice[index].hotPoint.x * m_mouseSize.x) / 32.0f; - pos.y = m_mousePos.y - ((32.0f - m_mice[index].hotPoint.y) * m_mouseSize.y) / 32.0f; - - Math::Point shadowPos; - shadowPos.x = pos.x + (4.0f/800.0f); - shadowPos.y = pos.y - (3.0f/600.0f); - - SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE); - DrawMouseSprite(shadowPos, m_mouseSize, m_mice[index].iconShadow); - - SetState(m_mice[index].mode1); - DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon1); - - SetState(m_mice[index].mode2); - DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon2); -} - -void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) -{ - if (icon == -1) - return; - - Math::Point p1 = pos; - Math::Point p2 = p1 + size; - - float u1 = (32.0f / 256.0f) * (icon % 8); - float v1 = (32.0f / 256.0f) * (icon / 8); - float u2 = u1 + (32.0f / 256.0f); - float v2 = v1 + (32.0f / 256.0f); - - float dp = 0.5f / 256.0f; - u1 += dp; - v1 += dp; - u2 -= dp; - v2 -= dp; - - Math::Vector normal(0.0f, 0.0f, -1.0f); - - Gfx::Vertex vertex[4] = - { - Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), normal, Math::Point(u1, v2)), - Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), normal, Math::Point(u2, v2)), - Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)), - Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), normal, Math::Point(u2, v1)) - }; - - m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, vertex, 4); - AddStatisticTriangle(2); -} - -bool Gfx::CEngine::GetPause() -{ - return m_pause; -} - -Math::Vector Gfx::CEngine::GetLookatPt() -{ - return m_lookatPt; -} - -Math::Vector Gfx::CEngine::GetEyePt() -{ - return m_eyePt; -} - -void Gfx::CEngine::SetMouseVisible(bool visible) -{ - m_mouseVisible = visible; -} - -bool Gfx::CEngine::GetMouseVisible() -{ - return m_mouseVisible; -} - -void Gfx::CEngine::SetMousePos(Math::Point pos) -{ - m_mousePos = pos; -} - -Math::Point Gfx::CEngine::GetMousePos() -{ - return m_mousePos; -} - -void Gfx::CEngine::SetMouseType(Gfx::EngineMouseType type) -{ - m_mouseType = type; -} - -Gfx::EngineMouseType Gfx::CEngine::GetMouseType() -{ - return m_mouseType; -} - -void Gfx::CEngine::AddStatisticTriangle(int count) -{ - m_statisticTriangle += count; -} diff --git a/src/graphics/common/engine.h b/src/graphics/common/engine.h deleted file mode 100644 index 9b6cc1f..0000000 --- a/src/graphics/common/engine.h +++ /dev/null @@ -1,985 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// engine.h - -#pragma once - - -#include "common/event.h" -#include "graphics/common/color.h" -#include "graphics/common/material.h" -#include "graphics/common/texture.h" -#include "graphics/common/vertex.h" -#include "math/intpoint.h" -#include "math/matrix.h" -#include "math/point.h" -#include "math/vector.h" - - -#include -#include -#include - - -class CApplication; -class CInstanceManager; -class CObject; -class CSound; - - -namespace Gfx { - -class CDevice; -class CLightManager; -class CText; -class CParticle; -class CWater; -class CCloud; -class CLightning; -class CPlanet; -class CTerrain; - - -/** - \enum EngineTriangleType - \brief Type of triangles drawn for engine objects */ -enum EngineTriangleType -{ - //! Triangles - ENG_TRIANGLE_TYPE_6T = 1, - //! Surfaces - ENG_TRIANGLE_TYPE_6S = 2 -}; - -/** - \struct EngineTriangle - \brief A triangle drawn by the graphics engine */ -struct EngineTriangle -{ - //! Triangle vertices - Gfx::VertexTex2 triangle[3]; - //! Material - Gfx::Material material; - //! Render state (TODO: ?) - int state; - //! 1st texture - Gfx::Texture tex1; - //! 2nd texture - Gfx::Texture tex2; - - EngineTriangle() - { - state = 0; - } -}; - -/** - \enum EngineObjectType - \brief Class of graphics engine object */ -enum EngineObjectType -{ - //! Object doesn't exist - ENG_OBJTYPE_NULL = 0, - //! Terrain - ENG_OBJTYPE_TERRAIN = 1, - //! Fixed object - ENG_OBJTYPE_FIX = 2, - //! Moving object - ENG_OBJTYPE_VEHICULE = 3, - //! Part of a moving object - ENG_OBJTYPE_DESCENDANT = 4, - //! Fixed object type quartz - ENG_OBJTYPE_QUARTZ = 5, - //! Fixed object type metal - ENG_OBJTYPE_METAL = 6 -}; - -/** - \struct EngineObject - \brief Object drawn by the graphics engine */ -struct EngineObject -{ - //! If true, the object is drawn - bool visible; - //! If true, object is behind the 2D interface - bool drawWorld; - //! If true, the shape is before the 2D interface - bool drawFront; - //! Number of triangles - int totalTriangles; - //! Type of object - Gfx::EngineObjectType type; - //! Transformation matrix - Math::Matrix transform; - //! Distance view - origin (TODO: ?) - float distance; - //! Bounding box min (origin 0,0,0 always included) - Math::Vector bboxMin; - //! bounding box max (origin 0,0,0 always included) - Math::Vector bboxMax; - //! Radius of the sphere at the origin - float radius; - //! Rank of the associated shadow - int shadowRank; - //! Transparency of the object [0, 1] - float transparency; - - EngineObject() - { - visible = false; - drawWorld = false; - drawFront = false; - totalTriangles = 0; - distance = 0.0f; - radius = 0.0f; - shadowRank = 0; - transparency = 0.0f; - } -}; - -struct EngineObjLevel1; -struct EngineObjLevel2; -struct EngineObjLevel3; -struct EngineObjLevel4; -struct EngineObjLevel5; - -/** - \struct EngineObjLevel5 - \brief Tier 5 of object tree */ -struct EngineObjLevel5 -{ - Gfx::Material material; - int state; - Gfx::EngineTriangleType type; - std::vector vertices; - - EngineObjLevel5(); -}; - -/** - \struct EngineObjLevel4 - \brief Tier 4 of object tree */ -struct EngineObjLevel4 -{ - int reserved; - std::vector up; - Gfx::EngineObjLevel3* down; - - EngineObjLevel4(); -}; - -/** - \struct EngineObjLevel3 - \brief Tier 3 of object tree */ -struct EngineObjLevel3 -{ - float min; - float max; - std::vector up; - Gfx::EngineObjLevel2* down; - - EngineObjLevel3(); -}; - -/** - \struct EngineObjLevel2 - \brief Tier 2 of object tree */ -struct EngineObjLevel2 -{ - int objRank; - std::vector up; - Gfx::EngineObjLevel1* down; - - EngineObjLevel2(); -}; - -/** - \struct EngineObjLevel1 - \brief Tier 1 of object tree */ -struct EngineObjLevel1 -{ - Gfx::Texture tex1; - Gfx::Texture tex2; - std::vector up; - - EngineObjLevel1(); -}; - -/** - \struct EngineShadowType - \brief Type of shadow drawn by the graphics engine */ -enum EngineShadowType -{ - //! Normal shadow - ENG_SHADOW_NORM = 0, - //! TODO: ? - ENG_SHADOW_WORM = 1 -}; - -/** - \struct EngineShadow - \brief Shadow drawn by the graphics engine */ -struct EngineShadow -{ - //! If true, shadow is invisible (object being carried for example) - bool hide; - //! Rank of the associated object - int objRank; - //! Type of shadow - Gfx::EngineShadowType type; - //! Position of the shadow - Math::Vector pos; - //! Normal to the terrain - Math::Vector normal; - //! Angle of the shadow - float angle; - //! Radius of the shadow - float radius; - //! Intensity of the shadow - float intensity; - //! Height from the ground - float height; - - EngineShadow() - { - hide = false; - objRank = 0; - angle = radius = intensity = height = 0.0f; - } -}; - -/** - \struct EngineGroundSpot - \brief A spot (large shadow) drawn on the ground by the graphics engine */ -struct EngineGroundSpot -{ - //! Color of the shadow - Gfx::Color color; - //! Min altitude - float min; - //! Max altitude - float max; - //! Transition area - float smooth; - //! Position for the shadow - Math::Vector pos; - //! Radius of the shadow - float radius; - //! Position of the shadow drawn - Math::Vector drawPos; - //! Radius of the shadow drawn - float drawRadius; - - EngineGroundSpot() - { - min = max = smooth = radius = drawRadius = 0.0f; - } -}; - -/** - \enum EngineGroundMarkPhase - \brief Phase of life of an EngineGroundMark */ -enum EngineGroundMarkPhase -{ - //! Increase - ENG_GR_MARK_PHASE_INC = 1, - //! Fixed - ENG_GR_MARK_PHASE_FIX = 2, - //! Decrease - ENG_GR_MARK_PHASE_DEC = 2 -}; - -/** - \struct EngineGroundMark - \brief A mark on ground drawn by the graphics engine */ -struct EngineGroundMark -{ - //! If true, draw mark - bool draw; - //! Phase of life - Gfx::EngineGroundMarkPhase phase; - //! Times for 3 life phases - float delay[3]; - //! Fixed time - float fix; - //! Position for marks - Math::Vector pos; - //! Radius of marks - float radius; - //! Color intensity - float intensity; - //! Draw position for marks - Math::Vector drawPos; - //! Radius for marks - float drawRadius; - //! Draw intensity for marks - float drawIntensity; - //! X dimension of table - int dx; - //! Y dimension of table - int dy; - //! Pointer to the table - char* table; - - EngineGroundMark() - { - draw = false; - delay[0] = delay[1] = delay[2] = 0.0f; - fix = radius = intensity = drawRadius = drawIntensity = 0.0f; - dx = dy = 0; - table = NULL; - } -}; - -/** - \enum EngineTextureMapping - \brief Type of texture mapping - */ -enum EngineTextureMapping -{ - ENG_TEX_MAPPING_X = 1, - ENG_TEX_MAPPING_Y = 2, - ENG_TEX_MAPPING_Z = 3, - ENG_TEX_MAPPING_1X = 4, - ENG_TEX_MAPPING_1Y = 5, - ENG_TEX_MAPPING_1Z = 6 -}; - - -/** - \enum EngineRenderState - \brief Render state of graphics engine - - States are used for settings certain modes, for instance texturing and blending. - The enum is a bitmask and some of the states can be OR'd together. */ -enum EngineRenderState -{ - //! Normal opaque materials - ENG_RSTATE_NORMAL = 0, - //! The transparent texture (black = no) - ENG_RSTATE_TTEXTURE_BLACK = (1<<0), - //! The transparent texture (white = no) - ENG_RSTATE_TTEXTURE_WHITE = (1<<1), - //! The transparent diffuse color - ENG_RSTATE_TDIFFUSE = (1<<2), - //! Texture wrap - ENG_RSTATE_WRAP = (1<<3), - //! Texture borders with solid color - ENG_RSTATE_CLAMP = (1<<4), - //! Light texture (ambient max) - ENG_RSTATE_LIGHT = (1<<5), - //! Double black texturing - ENG_RSTATE_DUAL_BLACK = (1<<6), - //! Double white texturing - ENG_RSTATE_DUAL_WHITE = (1<<7), - //! Part 1 (no change in. MOD!) - ENG_RSTATE_PART1 = (1<<8), - //! Part 2 - ENG_RSTATE_PART2 = (1<<9), - //! Part 3 - ENG_RSTATE_PART3 = (1<<10), - //! Part 4 - ENG_RSTATE_PART4 = (1<<11), - //! Double-sided face - ENG_RSTATE_2FACE = (1<<12), - //! Image using alpha channel - ENG_RSTATE_ALPHA = (1<<13), - //! Always use 2nd floor texturing - ENG_RSTATE_SECOND = (1<<14), - //! Causes the fog - ENG_RSTATE_FOG = (1<<15), - //! The transparent color (black = no) - ENG_RSTATE_TCOLOR_BLACK = (1<<16), - //! The transparent color (white = no) - ENG_RSTATE_TCOLOR_WHITE = (1<<17) -}; - - -/** - \enum EngineMouseType - \brief Type of mouse cursor displayed in-game */ -enum EngineMouseType -{ - //! Normal cursor (arrow) - ENG_MOUSE_NORM = 0, - //! Busy - ENG_MOUSE_WAIT = 1, - //! Edit (I-beam) - ENG_MOUSE_EDIT = 2, - //! Hand - ENG_MOUSE_HAND = 3, - //! Small cross - ENG_MOUSE_CROSS = 4, - //! TODO: ? - ENG_MOUSE_SHOW = 5, - //! Crossed out sign - ENG_MOUSE_NO = 6, - //! Resize - ENG_MOUSE_MOVE = 7, - //! Resize horizontally - ENG_MOUSE_MOVEH = 8, - //! Resize vertically - ENG_MOUSE_MOVEV = 9, - //! Resize diagonally bottom-left to top-right - ENG_MOUSE_MOVED = 10, - //! Resize diagonally top-left to bottom-right - ENG_MOUSE_MOVEI = 11, - //! Scroll to the left - ENG_MOUSE_SCROLLL = 12, - //! Scroll to the right - ENG_MOUSE_SCROLLR = 13, - //! Scroll up - ENG_MOUSE_SCROLLU = 14, - //! Scroll down - ENG_MOUSE_SCROLLD = 15, - //! Larger crosshair - ENG_MOUSE_TARGET = 16, - - //! Number of items in enum - ENG_MOUSE_COUNT -}; - -/** - \struct EngineMouse - \brief Information about mouse cursor */ -struct EngineMouse -{ - //! Index of texture element for 1st image - int icon1; - //! Index of texture element for 2nd image - int icon2; - //! Shadow texture part - int iconShadow; - //! Mode to render 1st image in - Gfx::EngineRenderState mode1; - //! Mode to render 2nd image in - Gfx::EngineRenderState mode2; - //! Hot point - Math::Point hotPoint; - - EngineMouse(int icon1 = -1, int icon2 = -1, int iconShadow = -1, - Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL, - Gfx::EngineRenderState mode2 = Gfx::ENG_RSTATE_NORMAL, - Math::Point hotPoint = Math::Point()) - { - this->icon1 = icon1; - this->icon2 = icon2; - this->iconShadow = iconShadow; - this->mode1 = mode1; - this->mode2 = mode2; - this->hotPoint = hotPoint; - } -}; - - -/** - \class CEngine - \brief The graphics engine - - This is the main class for graphics engine. It is responsible for drawing the 3D scene, - setting various render states, and facilitating the drawing of 2D interface. - - It uses a lower-level CDevice object which is implementation-independent core engine. - - \section Objecs Engine objects - - The 3D scene is composed of objects which are basically collections of triangles forming - a surface or simply independent triangles in space. Objects are stored in the engine - as a tree structure which is composed of 5 tiers (EngineObjLevel1, EngineObjLevel2 and so on). - Each tier stores some data about object triangle, like textures or materials used. - Additional information on objects stored are in EngineObject structure. - Each object is uniquely identified by its rank. - - ... - */ -class CEngine -{ -public: - CEngine(CInstanceManager *iMan, CApplication *app); - ~CEngine(); - - bool GetWasInit(); - std::string GetError(); - - bool Create(); - void Destroy(); - - void SetDevice(Gfx::CDevice *device); - Gfx::CDevice* GetDevice(); - - bool AfterDeviceSetInit(); - - void SetTerrain(Gfx::CTerrain* terrain); - - bool ProcessEvent(const Event &event); - - bool Render(); - - - bool WriteProfile(); - - void SetPause(bool pause); - bool GetPause(); - - void SetMovieLock(bool lock); - bool GetMovieLock(); - - void SetShowStat(bool show); - bool GetShowStat(); - - void SetRenderEnable(bool enable); - - int OneTimeSceneInit(); - int InitDeviceObjects(); - int DeleteDeviceObjects(); - int RestoreSurfaces(); - int FrameMove(float rTime); - void StepSimulation(float rTime); - int FinalCleanup(); - void AddStatisticTriangle(int nb); - int GetStatisticTriangle(); - void SetHiliteRank(int *rankList); - bool GetHilite(Math::Point &p1, Math::Point &p2); - bool GetSpriteCoord(int &x, int &y); - void SetInfoText(int line, char* text); - char* GetInfoText(int line); - void FirstExecuteAdapt(bool first); - - bool GetFullScreen(); - - Math::Matrix* GetMatView(); - Math::Matrix* GetMatLeftView(); - Math::Matrix* GetMatRightView(); - - void TimeInit(); - void TimeEnterGel(); - void TimeExitGel(); - float TimeGet(); - - int GetRestCreate(); - int CreateObject(); - void FlushObject(); - bool DeleteObject(int objRank); - bool SetDrawWorld(int objRank, bool draw); - bool SetDrawFront(int objRank, bool draw); - - bool AddTriangle(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, - int state, std::string texName1, std::string texName2, - float min, float max, bool globalUpdate); - bool AddSurface(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, - int state, std::string texName1, std::string texName2, - float min, float max, bool globalUpdate); - bool AddQuick(int objRank, Gfx::EngineObjLevel5* buffer, - std::string texName1, std::string texName2, - float min, float max, bool globalUpdate); - Gfx::EngineObjLevel5* SearchTriangle(int objRank, const Gfx::Material &mat, - int state, std::string texName1, std::string texName2, - float min, float max); - - void ChangeLOD(); - bool ChangeSecondTexture(int objRank, char* texName2); - int GetTotalTriangles(int objRank); - int GetTriangles(int objRank, float min, float max, Gfx::EngineTriangle* buffer, int size, float percent); - bool GetBBox(int objRank, Math::Vector &min, Math::Vector &max); - bool ChangeTextureMapping(int objRank, const Gfx::Material &mat, int state, - const std::string &texName1, const std::string &texName2, - float min, float max, Gfx::EngineTextureMapping mode, - float au, float bu, float av, float bv); - bool TrackTextureMapping(int objRank, const Gfx::Material &mat, int state, - const std::string &texName1, const std::string &texName2, - float min, float max, Gfx::EngineTextureMapping mode, - float pos, float factor, float tl, float ts, float tt); - bool SetObjectTransform(int objRank, const Math::Matrix &transform); - bool GetObjectTransform(int objRank, Math::Matrix &transform); - bool SetObjectType(int objRank, Gfx::EngineObjectType type); - Gfx::EngineObjectType GetObjectType(int objRank); - bool SetObjectTransparency(int objRank, float value); - - bool ShadowCreate(int objRank); - void ShadowDelete(int objRank); - bool SetObjectShadowHide(int objRank, bool hide); - bool SetObjectShadowType(int objRank, Gfx::EngineShadowType type); - bool SetObjectShadowPos(int objRank, const Math::Vector &pos); - bool SetObjectShadowNormal(int objRank, const Math::Vector &n); - bool SetObjectShadowAngle(int objRank, float angle); - bool SetObjectShadowRadius(int objRank, float radius); - bool SetObjectShadowIntensity(int objRank, float intensity); - bool SetObjectShadowHeight(int objRank, float h); - float GetObjectShadowRadius(int objRank); - - void GroundSpotFlush(); - int GroundSpotCreate(); - void GroundSpotDelete(int rank); - bool SetObjectGroundSpotPos(int rank, const Math::Vector &pos); - bool SetObjectGroundSpotRadius(int rank, float radius); - bool SetObjectGroundSpotColor(int rank, const Gfx::Color &color); - bool SetObjectGroundSpotMinMax(int rank, float min, float max); - bool SetObjectGroundSpotSmooth(int rank, float smooth); - - int GroundMarkCreate(Math::Vector pos, float radius, - float delay1, float delay2, float delay3, - int dx, int dy, char* table); - bool GroundMarkDelete(int rank); - - void Update(); - - void SetViewParams(const Math::Vector &eyePt, const Math::Vector &lookatPt, - const Math::Vector &upVec, float eyeDistance); - - Gfx::Texture CreateTexture(const std::string &texName, - const Gfx::TextureCreateParams ¶ms); - Gfx::Texture CreateTexture(const std::string &texName); - void DestroyTexture(const std::string &texName); - - bool LoadTexture(const std::string &name, int stage = 0); - bool LoadAllTextures(); - - void SetLimitLOD(int rank, float limit); - float GetLimitLOD(int rank, bool last=false); - - void SetTerrainVision(float vision); - - void SetGroundSpot(bool mode); - bool GetGroundSpot(); - void SetShadow(bool mode); - bool GetShadow(); - void SetDirty(bool mode); - bool GetDirty(); - void SetFog(bool mode); - bool GetFog(); - bool GetStateColor(); - - void SetSecondTexture(int texNum); - int GetSecondTexture(); - - void SetRankView(int rank); - int GetRankView(); - - void SetDrawWorld(bool draw); - void SetDrawFront(bool draw); - - void SetAmbientColor(const Gfx::Color &color, int rank = 0); - Gfx::Color GetAmbientColor(int rank = 0); - - void SetWaterAddColor(const Gfx::Color &color); - Gfx::Color GetWaterAddColor(); - - void SetFogColor(const Gfx::Color &color, int rank = 0); - Gfx::Color GetFogColor(int rank = 0); - - void SetDeepView(float length, int rank = 0, bool ref=false); - float GetDeepView(int rank = 0); - - void SetFogStart(float start, int rank = 0); - float GetFogStart(int rank = 0); - - void SetBackground(const std::string &name, Gfx::Color up = Gfx::Color(), Gfx::Color down = Gfx::Color(), - Gfx::Color cloudUp = Gfx::Color(), Gfx::Color cloudDown = Gfx::Color(), - bool full = false, bool quarter = false); - void GetBackground(const std::string &name, Gfx::Color &up, Gfx::Color &down, - Gfx::Color &cloudUp, Gfx::Color &cloudDown, - bool &full, bool &quarter); - void SetFrontsizeName(char *name); - void SetOverFront(bool front); - void SetOverColor(const Gfx::Color &color = Gfx::Color(), int mode = ENG_RSTATE_TCOLOR_BLACK); - - void SetParticleDensity(float value); - float GetParticleDensity(); - float ParticleAdapt(float factor); - - void SetClippingDistance(float value); - float GetClippingDistance(); - - void SetObjectDetail(float value); - float GetObjectDetail(); - - void SetGadgetQuantity(float value); - float GetGadgetQuantity(); - - void SetTextureQuality(int value); - int GetTextureQuality(); - - void SetTotoMode(bool present); - bool GetTotoMode(); - - void SetLensMode(bool present); - bool GetLensMode(); - - void SetWaterMode(bool present); - bool GetWaterMode(); - - void SetLightingMode(bool present); - bool GetLightingMode(); - - void SetSkyMode(bool present); - bool GetSkyMode(); - - void SetBackForce(bool present); - bool GetBackForce(); - - void SetPlanetMode(bool present); - bool GetPlanetMode(); - - void SetLightMode(bool present); - bool GetLightMode(); - - void SetEditIndentMode(bool autoIndent); - bool GetEditIndentMode(); - - void SetEditIndentValue(int value); - int GetEditIndentValue(); - - void SetSpeed(float speed); - float GetSpeed(); - - void SetTracePrecision(float factor); - float GetTracePrecision(); - - void SetFocus(float focus); - float GetFocus(); - Math::Vector GetEyePt(); - Math::Vector GetLookatPt(); - float GetEyeDirH(); - float GetEyeDirV(); - Math::Point GetDim(); - void UpdateMatProj(); - - void ApplyChange(); - - void FlushPressKey(); - void ResetKey(); - void SetKey(int keyRank, int option, int key); - int GetKey(int keyRank, int option); - - void SetJoystick(bool enable); - bool GetJoystick(); - - void SetDebugMode(bool mode); - bool GetDebugMode(); - bool GetSetupMode(); - - bool IsVisiblePoint(const Math::Vector &pos); - - int DetectObject(Math::Point mouse); - void SetState(int state, Gfx::Color color = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); - void SetTexture(const std::string &name, int stage = 0); - void SetMaterial(const Gfx::Material &mat); - - void SetMouseVisible(bool show); - bool GetMouseVisible(); - void SetMousePos(Math::Point pos); - Math::Point GetMousePos(); - void SetMouseType(Gfx::EngineMouseType type); - Gfx::EngineMouseType GetMouseType(); - - CText* GetText(); - - bool ChangeColor(char *name, Gfx::Color colorRef1, Gfx::Color colorNew1, - Gfx::Color colorRef2, Gfx::Color colorNew2, - float tolerance1, float tolerance2, - Math::Point ts, Math::Point ti, - Math::Point *pExclu=0, float shift=0.0f, bool hSV=false); - bool OpenImage(char *name); - bool CopyImage(); - bool LoadImage(); - bool ScrollImage(int dx, int dy); - bool SetDot(int x, int y, Gfx::Color color); - bool CloseImage(); - bool WriteScreenShot(char *filename, int width, int height); - //bool GetRenderDC(HDC &hDC); - //bool ReleaseRenderDC(HDC &hDC); - //PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp); - //bool CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC); - -protected: - - void SetUp3DView(); - bool Draw3DScene(); - - void SetUpInterfaceView(); - bool DrawInterface(); - - void DrawGroundSpot(); - void DrawShadow(); - void DrawBackground(); - void DrawBackgroundGradient(Gfx::Color up, Gfx::Color down); - void DrawBackgroundImageQuarter(Math::Point p1, Math::Point p2, char *name); - void DrawBackgroundImage(); - void DrawPlanet(); - void DrawFrontsize(); - void DrawOverColor(); - void DrawHilite(); - void DrawMouse(); - void DrawMouseSprite(Math::Point pos, Math::Point dim, int icon); - - /* - Gfx::ObjLevel2* AddLevel1(Gfx::ObjLevel1 *&p1, char* texName1, char* texName2); - Gfx::ObjLevel3* AddLevel2(Gfx::ObjLevel2 *&p2, int objRank); - Gfx::ObjLevel4* AddLevel3(Gfx::ObjLevel3 *&p3, float min, float max); - Gfx::ObjLevel5* AddLevel4(Gfx::ObjLevel4 *&p4, int reserve); - Gfx::ObjLevel6* AddLevel5(Gfx::ObjLevel5 *&p5, Gfx::TriangleType type, const Gfx::Material &mat, int state, int nb);*/ - - bool IsVisible(int objRank); - bool DetectBBox(int objRank, Math::Point mouse); - bool GetBBox2D(int objRank, Math::Point &min, Math::Point &max); - bool DetectTriangle(Math::Point mouse, Gfx::VertexTex2 *triangle, int objRank, float &dist); - bool TransformPoint(Math::Vector &p2D, int objRank, Math::Vector p3D); - void ComputeDistance(); - void UpdateGeometry(); - -protected: - CInstanceManager* m_iMan; - CApplication* m_app; - CSound* m_sound; - Gfx::CDevice* m_device; - Gfx::CText* m_text; - Gfx::CLightManager* m_lightMan; - Gfx::CParticle* m_particle; - Gfx::CWater* m_water; - Gfx::CCloud* m_cloud; - Gfx::CLightning* m_lightning; - Gfx::CPlanet* m_planet; - Gfx::CTerrain* m_terrain; - - bool m_wasInit; - std::string m_error; - - int m_blackSrcBlend[2]; - int m_blackDestBlend[2]; - int m_whiteSrcBlend[2]; - int m_whiteDestBlend[2]; - int m_diffuseSrcBlend[2]; - int m_diffuseDestBlend[2]; - int m_alphaSrcBlend[2]; - int m_alphaDestBlend[2]; - - Math::Matrix m_matProj; - Math::Matrix m_matLeftView; - Math::Matrix m_matRightView; - Math::Matrix m_matView; - float m_focus; - - Math::Matrix m_matWorldInterface; - Math::Matrix m_matProjInterface; - Math::Matrix m_matViewInterface; - - long m_baseTime; - long m_stopTime; - float m_absTime; - float m_lastTime; - float m_speed; - bool m_pause; - bool m_render; - bool m_movieLock; - - Math::IntPoint m_dim; - Math::IntPoint m_lastDim; - - std::vector m_objectTree; - std::vector m_objects; - std::vector m_shadow; - std::vector m_groundSpot; - Gfx::EngineGroundMark m_groundMark; - - Math::Vector m_eyePt; - Math::Vector m_lookatPt; - float m_eyeDirH; - float m_eyeDirV; - int m_rankView; - Gfx::Color m_ambientColor[2]; - Gfx::Color m_backColor[2]; - Gfx::Color m_fogColor[2]; - float m_deepView[2]; - float m_fogStart[2]; - Gfx::Color m_waterAddColor; - int m_statisticTriangle; - bool m_updateGeometry; - //char m_infoText[10][200]; - int m_alphaMode; - bool m_stateColor; - bool m_forceStateColor; - bool m_groundSpotVisible; - bool m_shadowVisible; - bool m_dirty; - bool m_fog; - bool m_firstGroundSpot; - int m_secondTexNum; - std::string m_backgroundName; - Gfx::Color m_backgroundColorUp; - Gfx::Color m_backgroundColorDown; - Gfx::Color m_backgroundCloudUp; - Gfx::Color m_backgroundCloudDown; - bool m_backgroundFull; - bool m_backgroundQuarter; - bool m_overFront; - Gfx::Color m_overColor; - int m_overMode; - std::string m_frontsizeName; - bool m_drawWorld; - bool m_drawFront; - float m_limitLOD[2]; - float m_particuleDensity; - float m_clippingDistance; - float m_lastClippingDistance; - float m_objectDetail; - float m_lastObjectDetail; - float m_terrainVision; - float m_gadgetQuantity; - int m_textureQuality; - bool m_totoMode; - bool m_lensMode; - bool m_waterMode; - bool m_skyMode; - bool m_backForce; - bool m_planetMode; - bool m_lightMode; - bool m_editIndentMode; - int m_editIndentValue; - float m_tracePrecision; - - int m_hiliteRank[100]; - bool m_hilite; - Math::Point m_hiliteP1; - Math::Point m_hiliteP2; - - int m_lastState; - Gfx::Color m_lastColor; - char m_lastTexture[2][50]; - Gfx::Material m_lastMaterial; - - std::string m_texPath; - Gfx::TextureCreateParams m_defaultTexParams; - - std::map m_texNameMap; - std::map m_revTexNameMap; - - Gfx::EngineMouse m_mice[Gfx::ENG_MOUSE_COUNT]; - Gfx::Texture m_miceTexture; - Math::Point m_mouseSize; - Gfx::EngineMouseType m_mouseType; - Math::Point m_mousePos; - bool m_mouseVisible; - - //LPDIRECTDRAWSURFACE7 m_imageSurface; - //DDSURFACEDESC2 m_imageDDSD; - //WORD* m_imageCopy; - //int m_imageDX; - //int m_imageDY; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/light.cpp b/src/graphics/common/light.cpp deleted file mode 100644 index 3ca890c..0000000 --- a/src/graphics/common/light.cpp +++ /dev/null @@ -1,416 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// light.cpp - -#include "graphics/common/light.h" - -#include "common/iman.h" -#include "graphics/common/device.h" -#include "math/geometry.h" - -#include - - -void Gfx::LightProgression::Init(float value) -{ - starting = value; - ending = value; - current = value; - progress = 0.0f; - speed = 100.0f; -} - -void Gfx::LightProgression::Update(float rTime) -{ - if (speed < 100.0f) - { - if (progress < 1.0f) - { - progress += speed * rTime; - if (progress > 1.0f) - progress = 1.0f; - } - - current = starting + progress * (ending - starting); - } - else - { - current = ending; - } -} - -void Gfx::LightProgression::SetTarget(float value) -{ - starting = current; - ending = value; - progress = 0.0f; -} - - -Gfx::DynamicLight::DynamicLight() -{ - used = enabled = false; -} - - - -Gfx::CLightManager::CLightManager(CInstanceManager* iMan, Gfx::CEngine* engine) -{ - m_iMan = iMan; - m_iMan->AddInstance(CLASS_LIGHT, this); - - m_device = NULL; - m_engine = engine; - - m_time = 0.0f; -} - -Gfx::CLightManager::~CLightManager() -{ - m_iMan->DeleteInstance(CLASS_LIGHT, this); - - m_iMan = NULL; - m_device = NULL; - m_engine = NULL; -} - -void Gfx::CLightManager::SetDevice(Gfx::CDevice* device) -{ - m_device = device; - - m_dynLights = std::vector(m_device->GetMaxLightCount(), Gfx::DynamicLight()); -} - -void Gfx::CLightManager::FlushLights() -{ - for (int i = 0; i < static_cast( m_dynLights.size() ); i++) - { - m_dynLights[i].used = false; - m_device->SetLightEnabled(i, false); - } -} - -/** Returns the index of light created or -1 if all lights are used. */ -int Gfx::CLightManager::CreateLight() -{ - for (int i = 0; i < static_cast( m_dynLights.size() ); i++) - { - if (m_dynLights[i].used) continue; - - m_dynLights[i] = Gfx::DynamicLight(); - - m_dynLights[i].used = true; - m_dynLights[i].enabled = true; - - m_dynLights[i].includeType = Gfx::ENG_OBJTYPE_NULL; - m_dynLights[i].excludeType = Gfx::ENG_OBJTYPE_NULL; - - m_dynLights[i].light.type = Gfx::LIGHT_DIRECTIONAL; - m_dynLights[i].light.diffuse = Gfx::Color(0.5f, 0.5f, 0.5f); - m_dynLights[i].light.position = Math::Vector(-100.0f, 100.0f, -100.0f); - m_dynLights[i].light.direction = Math::Vector( 1.0f, -1.0f, 1.0f); - - m_dynLights[i].intensity.Init(1.0f); // maximum - m_dynLights[i].colorRed.Init(0.5f); - m_dynLights[i].colorGreen.Init(0.5f); - m_dynLights[i].colorBlue.Init(0.5f); // gray - - return i; - } - - return -1; -} - -bool Gfx::CLightManager::DeleteLight(int lightRank) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].used = false; - m_device->SetLightEnabled(lightRank, false); - - return true; -} - -// Specifies a light. - -bool Gfx::CLightManager::SetLight(int lightRank, const Gfx::Light &light) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].light = light; - - m_dynLights[lightRank].colorRed.Init(m_dynLights[lightRank].light.diffuse.r); - m_dynLights[lightRank].colorGreen.Init(m_dynLights[lightRank].light.diffuse.g); - m_dynLights[lightRank].colorBlue.Init(m_dynLights[lightRank].light.diffuse.b); - - return true; -} - -bool Gfx::CLightManager::GetLight(int lightRank, Gfx::Light &light) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - light = m_dynLights[lightRank].light; - return true; -} - -bool Gfx::CLightManager::SetLightEnabled(int lightRank, bool enabled) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].enabled = enabled; - return true; -} - -bool Gfx::CLightManager::SetLightIncludeType(int lightRank, Gfx::EngineObjectType type) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].includeType = type; - return true; -} - -bool Gfx::CLightManager::SetLightExcludeType(int lightRank, Gfx::EngineObjectType type) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].excludeType = type; - return true; -} - -bool Gfx::CLightManager::SetLightPos(int lightRank, const Math::Vector &pos) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].light.position = pos; - return true; -} - -Math::Vector Gfx::CLightManager::GetLightPos(int lightRank) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return Math::Vector(0.0f, 0.0f, 0.0f); - - return m_dynLights[lightRank].light.position; -} - -bool Gfx::CLightManager::SetLightDir(int lightRank, const Math::Vector &dir) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].light.direction = dir; - return true; -} - -Math::Vector Gfx::CLightManager::GetLightDir(int lightRank) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return Math::Vector(0.0f, 0.0f, 0.0f); - - return m_dynLights[lightRank].light.direction; -} - -bool Gfx::CLightManager::SetLightIntensitySpeed(int lightRank, float speed) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].intensity.speed = speed; - return true; -} - -bool Gfx::CLightManager::SetLightIntensity(int lightRank, float value) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].intensity.SetTarget(value); - return true; -} - -float Gfx::CLightManager::GetLightIntensity(int lightRank) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return 0.0f; - - return m_dynLights[lightRank].intensity.current; -} - - -bool Gfx::CLightManager::SetLightColorSpeed(int lightRank, float speed) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].colorRed.speed = speed; - m_dynLights[lightRank].colorGreen.speed = speed; - m_dynLights[lightRank].colorBlue.speed = speed; - return true; -} - -bool Gfx::CLightManager::SetLightColor(int lightRank, const Gfx::Color &color) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return false; - - m_dynLights[lightRank].colorRed.SetTarget(color.r); - m_dynLights[lightRank].colorGreen.SetTarget(color.g); - m_dynLights[lightRank].colorBlue.SetTarget(color.b); - return true; -} - -Gfx::Color Gfx::CLightManager::GetLightColor(int lightRank) -{ - if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) - return Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); - - Gfx::Color color; - color.r = m_dynLights[lightRank].colorRed.current; - color.g = m_dynLights[lightRank].colorGreen.current; - color.b = m_dynLights[lightRank].colorBlue.current; - return color; -} - -void Gfx::CLightManager::AdaptLightColor(const Gfx::Color &color, float factor) -{ - for (int i = 0; i < static_cast( m_dynLights.size() ); i++) - { - if (! m_dynLights[i].used) - continue; - - Gfx::Color value; - value.r = m_dynLights[i].colorRed.current; - value.g = m_dynLights[i].colorGreen.current; - value.b = m_dynLights[i].colorBlue.current; - - value.r += color.r * factor; - value.g += color.g * factor; - value.b += color.b * factor; - - m_dynLights[i].colorRed.Init(value.r); - m_dynLights[i].colorGreen.Init(value.g); - m_dynLights[i].colorBlue.Init(value.b); - } - - UpdateLights(); -} - -void Gfx::CLightManager::UpdateProgression(float rTime) -{ - if (m_engine->GetPause()) - return; - - m_time += rTime; - - for (int i = 0; i < static_cast( m_dynLights.size() ); i++) - { - if (! m_dynLights[i].used) - continue; - - m_dynLights[i].intensity.Update(rTime); - m_dynLights[i].colorRed.Update(rTime); - m_dynLights[i].colorGreen.Update(rTime); - m_dynLights[i].colorBlue.Update(rTime); - - if (m_dynLights[i].includeType == Gfx::ENG_OBJTYPE_QUARTZ) - { - m_dynLights[i].light.direction.x = sinf(1.0f * (m_time + i*Math::PI*0.5f)); - m_dynLights[i].light.direction.z = cosf(1.1f * (m_time + i*Math::PI*0.5f)); - m_dynLights[i].light.direction.y = -1.0f + 0.5f * cosf((m_time + i*Math::PI*0.5f)*2.7f); - } - - if (m_dynLights[i].includeType == Gfx::ENG_OBJTYPE_METAL) - { - Math::Vector dir = m_engine->GetEyePt() - m_engine->GetLookatPt(); - float angle = Math::RotateAngle(dir.x, dir.z); - angle += Math::PI * 0.5f * i; - m_dynLights[i].light.direction.x = sinf(2.0f * angle); - m_dynLights[i].light.direction.z = cosf(2.0f * angle); - } - } -} - - -void Gfx::CLightManager::UpdateLights() -{ - for (int i = 0; i < static_cast( m_dynLights.size() ); i++) - { - if (! m_dynLights[i].used) - continue; - - bool enabled = m_dynLights[i].enabled; - if (m_dynLights[i].intensity.current == 0.0f) - enabled = false; - - if (enabled) - { - float value = m_dynLights[i].colorRed.current * m_dynLights[i].intensity.current; - m_dynLights[i].light.diffuse.r = value; - - value = m_dynLights[i].colorGreen.current * m_dynLights[i].intensity.current; - m_dynLights[i].light.diffuse.g = value; - - value = m_dynLights[i].colorBlue.current * m_dynLights[i].intensity.current; - m_dynLights[i].light.diffuse.b = value; - - m_device->SetLight(i, m_dynLights[i].light); - m_device->SetLightEnabled(i, enabled); - } - else - { - m_dynLights[i].light.diffuse.r = 0.0f; - m_dynLights[i].light.diffuse.g = 0.0f; - m_dynLights[i].light.diffuse.b = 0.0f; - - m_device->SetLightEnabled(i, enabled); - } - } -} - -void Gfx::CLightManager::UpdateLightsEnableState(Gfx::EngineObjectType type) -{ - for (int i = 0; i < static_cast( m_dynLights.size() ); i++) - { - if (! m_dynLights[i].used) - continue; - if (! m_dynLights[i].enabled) - continue; - if (m_dynLights[i].intensity.current == 0.0f) - continue; - - if (m_dynLights[i].includeType != Gfx::ENG_OBJTYPE_NULL) - { - bool enabled = (m_dynLights[i].includeType == type); - m_device->SetLightEnabled(i, enabled); - } - - if (m_dynLights[i].excludeType != Gfx::ENG_OBJTYPE_NULL) - { - bool enabled = (m_dynLights[i].excludeType != type); - m_device->SetLightEnabled(i, enabled); - } - } -} diff --git a/src/graphics/common/light.h b/src/graphics/common/light.h deleted file mode 100644 index 93e8c1b..0000000 --- a/src/graphics/common/light.h +++ /dev/null @@ -1,242 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// light.h - -#pragma once - - -#include "graphics/common/engine.h" -#include "graphics/common/color.h" -#include "math/vector.h" - - -namespace Gfx { - -/** - \enum LightType - \brief Type of light in 3D scene */ -enum LightType -{ - LIGHT_POINT, - LIGHT_SPOT, - LIGHT_DIRECTIONAL -}; - -/** - \struct Light - \brief Properties of light in 3D scene - - This structure was created as analog to DirectX's D3DLIGHT. */ -struct Light -{ - //! Type of light source - Gfx::LightType type; - //! Color of ambient light - Gfx::Color ambient; - //! Color of diffuse light - Gfx::Color diffuse; - //! Color of specular light - Gfx::Color specular; - //! Position in world space (for point & spot lights) - Math::Vector position; - //! Direction in world space (for directional & spot lights) - Math::Vector direction; - //! Constant attenuation factor - float attenuation0; - //! Linear attenuation factor - float attenuation1; - //! Quadratic attenuation factor - float attenuation2; - //! Angle of spotlight cone (0-90 degrees) - float spotAngle; - //! Intensity of spotlight (0 = uniform; 128 = most intense) - float spotIntensity; - - //! Constructor; calls LoadDefault() - Light() - { - LoadDefault(); - } - - //! Loads default values - void LoadDefault() - { - type = LIGHT_POINT; - ambient = Gfx::Color(0.4f, 0.4f, 0.4f); - diffuse = Gfx::Color(0.8f, 0.8f, 0.8f); - specular = Gfx::Color(1.0f, 1.0f, 1.0f); - position = Math::Vector(0.0f, 0.0f, 0.0f); - direction = Math::Vector(0.0f, 0.0f, 1.0f); - attenuation0 = 1.0f; - attenuation1 = attenuation2 = 0.0f; - spotAngle = 90.0f; - spotIntensity = 0.0f; - } -}; - -/** - \struct LightProgression - \brief Describes the progression of light parameters change */ -struct LightProgression -{ - //! Starting value - float starting; - //! Ending (destination) value - float ending; - //! Current value - float current; - //! Progress from start to end - float progress; - //! Speed of progression - float speed; - - LightProgression() - { - starting = ending = current = progress = speed = 0.0f; - } - - //! Initializes the progression - void Init(float value); - - //! Updates the progression - void Update(float rTime); - - //! Sets the new end value (starting is set to current) - void SetTarget(float value); -}; - -/** - \struct DynamicLight - \brief Dynamic light in 3D scene - - It is an extension over standard light properties. Added are dynamic progressions for light - colors and intensity and types of objects included/excluded in lighting. */ -struct DynamicLight -{ - //! Whether the light is used - bool used; - //! Whether the light is turned on - bool enabled; - - //! Configuration of the light - Gfx::Light light; - - //! Progression of intensity [0, 1] - Gfx::LightProgression intensity; - //! Progression of red diffuse color - Gfx::LightProgression colorRed; - //! Progression of green diffuse color - Gfx::LightProgression colorGreen; - //! Progression of blue diffuse color - Gfx::LightProgression colorBlue; - - //! Type of objects included in lighting with this light; if Gfx::ENG_OBJTYPE_NULL is used, it is ignored - Gfx::EngineObjectType includeType; - //! Type of objects excluded from lighting with this light; if Gfx::ENG_OBJTYPE_NULL is used, it is ignored - Gfx::EngineObjectType excludeType; - - DynamicLight(); -}; - -/** - \class CLightManager - \brief Manager for dynamic lights in 3D scene - - (Old CLight class) - - The class is responsible for managing dynamic lights (struct Gfx::DynamicLight) used in 3D scene. - The dynamic lights are created, updated and deleted through the class' interface. - - Number of available lights depends on graphics device used. Class allocates vector - for the total number of lights, but only some are used. - */ -class CLightManager -{ -public: - //! Constructor - CLightManager(CInstanceManager *iMan, Gfx::CEngine* engine); - //! Destructor - virtual ~CLightManager(); - - //! Sets the device to be used - void SetDevice(Gfx::CDevice* device); - - //! Clears and disables all lights - void FlushLights(); - //! Creates a new dynamic light and returns its index (lightRank) - int CreateLight(); - //! Deletes and disables the given dynamic light - bool DeleteLight(int lightRank); - //! Sets the light parameters for dynamic light - bool SetLight(int lightRank, const Gfx::Light &light); - //! Returns the light parameters for given dynamic light - bool GetLight(int lightRank, Gfx::Light &light); - //! Enables/disables the given dynamic light - bool SetLightEnabled(int lightRank, bool enable); - - //! Sets what objects are included in given dynamic light - bool SetLightIncludeType(int lightRank, Gfx::EngineObjectType type); - //! Sets what objects are excluded from given dynamic light - bool SetLightExcludeType(int lightRank, Gfx::EngineObjectType type); - - //! Sets the position of dynamic light - bool SetLightPos(int lightRank, const Math::Vector &pos); - //! Returns the position of dynamic light - Math::Vector GetLightPos(int lightRank); - - //! Sets the direction of dynamic light - bool SetLightDir(int lightRank, const Math::Vector &dir); - //! Returns the direction of dynamic light - Math::Vector GetLightDir(int lightRank); - - //! Sets the destination intensity for dynamic light's intensity progression - bool SetLightIntensity(int lightRank, float value); - //! Returns the current light intensity - float GetLightIntensity(int lightRank); - //! Sets the rate of change for dynamic light intensity - bool SetLightIntensitySpeed(int lightRank, float speed); - - //! Adjusts the color of all dynamic lights - void AdaptLightColor(const Gfx::Color &color, float factor); - - //! Sets the destination color for dynamic light's color progression - bool SetLightColor(int lightRank, const Gfx::Color &color); - //! Returns current light color - Gfx::Color GetLightColor(int lightRank); - //! Sets the rate of change for dynamic light colors (RGB) - bool SetLightColorSpeed(int lightRank, float speed); - - //! Updates progression of dynamic lights - void UpdateProgression(float rTime); - //! Updates (recalculates) all dynamic lights - void UpdateLights(); - //! Enables or disables dynamic lights affecting the given object type - void UpdateLightsEnableState(Gfx::EngineObjectType type); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CDevice* m_device; - - //! Current time - float m_time; - //! List of dynamic lights - std::vector m_dynLights; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/lightning.cpp b/src/graphics/common/lightning.cpp deleted file mode 100644 index 076fcb4..0000000 --- a/src/graphics/common/lightning.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// lightning.cpp (aka blitz.cpp) - -#include "graphics/common/lightning.h" - - -// TODO implementation diff --git a/src/graphics/common/lightning.h b/src/graphics/common/lightning.h deleted file mode 100644 index 957344c..0000000 --- a/src/graphics/common/lightning.h +++ /dev/null @@ -1,85 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// lightning.h (aka blitz.h) - -#pragma once - -#include "common/misc.h" -#include "math/vector.h" - - -class CInstanceManager; -class CObject; -class CSound; - - -namespace Gfx { - -class CEngine; -class CTerrain; -class CCamera; - - -const float BLITZPARA = 200.0f; // radius of lightning protection -const short BLITZMAX = 50; - -enum BlitzPhase -{ - BPH_WAIT, - BPH_BLITZ, -}; - - -class CLightning -{ -public: - CLightning(CInstanceManager* iMan, CEngine* engine); - ~CLightning(); - - void Flush(); - bool EventProcess(const Event &event); - bool Create(float sleep, float delay, float magnetic); - bool GetStatus(float &sleep, float &delay, float &magnetic, float &progress); - bool SetStatus(float sleep, float delay, float magnetic, float progress); - void Draw(); - -protected: - bool EventFrame(const Event &event); - CObject* SearchObject(Math::Vector pos); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CTerrain* m_terrain; - CCamera* m_camera; - CSound* m_sound; - - bool m_bBlitzExist; - float m_sleep; - float m_delay; - float m_magnetic; - BlitzPhase m_phase; - float m_time; - float m_speed; - float m_progress; - Math::Vector m_pos; - Math::Point m_shift[BLITZMAX]; - float m_width[BLITZMAX]; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/material.h b/src/graphics/common/material.h deleted file mode 100644 index c828d90..0000000 --- a/src/graphics/common/material.h +++ /dev/null @@ -1,47 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// material.h - -#pragma once - - -#include "graphics/common/color.h" - - -namespace Gfx { - -/** - * \struct Material - * \brief Material of a surface - * - * This structure was created as analog to DirectX's D3DMATERIAL. - * - * It contains values of 3 material colors: diffuse, ambient and specular. - * In D3DMATERIAL there are other fields, but they are not used - * by the graphics engine. - */ -struct Material -{ - //! Diffuse color - Gfx::Color diffuse; - //! Ambient color - Gfx::Color ambient; - //! Specular color - Gfx::Color specular; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/modelfile.cpp b/src/graphics/common/modelfile.cpp deleted file mode 100644 index 22801e8..0000000 --- a/src/graphics/common/modelfile.cpp +++ /dev/null @@ -1,841 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// modelfile.cpp (aka modfile.cpp) - -#include "graphics/common/modelfile.h" - -#include "common/iman.h" -#include "common/ioutils.h" -#include "common/logger.h" -#include "common/stringutils.h" -#include "math/geometry.h" - -#include - -#include - - -//! How big the triangle vector is by default -const int TRIANGLE_PREALLOCATE_COUNT = 2000; - -/** - \struct ModelHeader - \brief Header info for model file - */ -struct ModelHeader -{ - //! Revision number - int revision; - //! Version number - int version; - //! Total number of vertices - int totalVertices; - //! Reserved area - int reserved[10]; - - ModelHeader() - { - memset(this, 0, sizeof(*this)); - } -}; - - -struct OldModelTriangle1 -{ - char used; - char selected; - Gfx::Vertex p1; - Gfx::Vertex p2; - Gfx::Vertex p3; - Gfx::Material material; - char texName[20]; - float min; - float max; - - OldModelTriangle1() - { - memset(this, 0, sizeof(*this)); - } -}; - -struct OldModelTriangle2 -{ - char used; - char selected; - Gfx::Vertex p1; - Gfx::Vertex p2; - Gfx::Vertex p3; - Gfx::Material material; - char texName[20]; - float min; - float max; - long state; - short reserved1; - short reserved2; - short reserved3; - short reserved4; - OldModelTriangle2() - { - memset(this, 0, sizeof(*this)); - } -}; - - -struct NewModelTriangle -{ - char used; - char selected; - Gfx::VertexTex2 p1; - Gfx::VertexTex2 p2; - Gfx::VertexTex2 p3; - Gfx::Material material; - char texName[20]; - float min; - float max; - long state; - short texNum2; - short reserved2; - short reserved3; - short reserved4; - - NewModelTriangle() - { - memset(this, 0, sizeof(*this)); - } -}; - - -Gfx::Vertex ReadBinaryVertex(std::istream &stream) -{ - Gfx::Vertex result; - - result.coord.x = IOUtils::ReadBinaryFloat(stream); - result.coord.y = IOUtils::ReadBinaryFloat(stream); - result.coord.z = IOUtils::ReadBinaryFloat(stream); - result.normal.x = IOUtils::ReadBinaryFloat(stream); - result.normal.y = IOUtils::ReadBinaryFloat(stream); - result.normal.z = IOUtils::ReadBinaryFloat(stream); - result.texCoord.x = IOUtils::ReadBinaryFloat(stream); - result.texCoord.y = IOUtils::ReadBinaryFloat(stream); - - return result; -} - -void WriteBinaryVertex(Gfx::Vertex vertex, std::ostream &stream) -{ - IOUtils::WriteBinaryFloat(vertex.coord.x, stream); - IOUtils::WriteBinaryFloat(vertex.coord.y, stream); - IOUtils::WriteBinaryFloat(vertex.coord.z, stream); - IOUtils::WriteBinaryFloat(vertex.normal.x, stream); - IOUtils::WriteBinaryFloat(vertex.normal.y, stream); - IOUtils::WriteBinaryFloat(vertex.normal.z, stream); - IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); - IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); -} - -Gfx::VertexTex2 ReadBinaryVertexTex2(std::istream &stream) -{ - Gfx::VertexTex2 result; - - result.coord.x = IOUtils::ReadBinaryFloat(stream); - result.coord.y = IOUtils::ReadBinaryFloat(stream); - result.coord.z = IOUtils::ReadBinaryFloat(stream); - result.normal.x = IOUtils::ReadBinaryFloat(stream); - result.normal.y = IOUtils::ReadBinaryFloat(stream); - result.normal.z = IOUtils::ReadBinaryFloat(stream); - result.texCoord.x = IOUtils::ReadBinaryFloat(stream); - result.texCoord.y = IOUtils::ReadBinaryFloat(stream); - result.texCoord2.x = IOUtils::ReadBinaryFloat(stream); - result.texCoord2.y = IOUtils::ReadBinaryFloat(stream); - - return result; -} - -void WriteBinaryVertexTex2(Gfx::VertexTex2 vertex, std::ostream &stream) -{ - IOUtils::WriteBinaryFloat(vertex.coord.x, stream); - IOUtils::WriteBinaryFloat(vertex.coord.y, stream); - IOUtils::WriteBinaryFloat(vertex.coord.z, stream); - IOUtils::WriteBinaryFloat(vertex.normal.x, stream); - IOUtils::WriteBinaryFloat(vertex.normal.y, stream); - IOUtils::WriteBinaryFloat(vertex.normal.z, stream); - IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); - IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); - IOUtils::WriteBinaryFloat(vertex.texCoord2.x, stream); - IOUtils::WriteBinaryFloat(vertex.texCoord2.y, stream); -} - -Gfx::Material ReadBinaryMaterial(std::istream &stream) -{ - Gfx::Material result; - - result.diffuse.r = IOUtils::ReadBinaryFloat(stream); - result.diffuse.g = IOUtils::ReadBinaryFloat(stream); - result.diffuse.b = IOUtils::ReadBinaryFloat(stream); - result.diffuse.a = IOUtils::ReadBinaryFloat(stream); - - result.ambient.r = IOUtils::ReadBinaryFloat(stream); - result.ambient.g = IOUtils::ReadBinaryFloat(stream); - result.ambient.b = IOUtils::ReadBinaryFloat(stream); - result.ambient.a = IOUtils::ReadBinaryFloat(stream); - - result.specular.r = IOUtils::ReadBinaryFloat(stream); - result.specular.g = IOUtils::ReadBinaryFloat(stream); - result.specular.b = IOUtils::ReadBinaryFloat(stream); - result.specular.a = IOUtils::ReadBinaryFloat(stream); - - /* emissive.r = */ IOUtils::ReadBinaryFloat(stream); - /* emissive.g = */ IOUtils::ReadBinaryFloat(stream); - /* emissive.b = */ IOUtils::ReadBinaryFloat(stream); - /* emissive.a = */ IOUtils::ReadBinaryFloat(stream); - - /* power = */ IOUtils::ReadBinaryFloat(stream); - - return result; -} - -void WriteBinaryMaterial(Gfx::Material material, std::ostream &stream) -{ - IOUtils::WriteBinaryFloat(material.diffuse.r, stream); - IOUtils::WriteBinaryFloat(material.diffuse.g, stream); - IOUtils::WriteBinaryFloat(material.diffuse.b, stream); - IOUtils::WriteBinaryFloat(material.diffuse.a, stream); - - IOUtils::WriteBinaryFloat(material.ambient.r, stream); - IOUtils::WriteBinaryFloat(material.ambient.g, stream); - IOUtils::WriteBinaryFloat(material.ambient.b, stream); - IOUtils::WriteBinaryFloat(material.ambient.a, stream); - - IOUtils::WriteBinaryFloat(material.specular.r, stream); - IOUtils::WriteBinaryFloat(material.specular.g, stream); - IOUtils::WriteBinaryFloat(material.specular.b, stream); - IOUtils::WriteBinaryFloat(material.specular.a, stream); - - /* emissive.r */ IOUtils::WriteBinaryFloat(0.0f, stream); - /* emissive.g */ IOUtils::WriteBinaryFloat(0.0f, stream); - /* emissive.b */ IOUtils::WriteBinaryFloat(0.0f, stream); - /* emissive.a */ IOUtils::WriteBinaryFloat(0.0f, stream); - - /* power */ IOUtils::WriteBinaryFloat(0.0f, stream); -} - -Gfx::ModelTriangle::ModelTriangle() -{ - min = 0.0f; - max = 0.0f; - state = 0L; -} - - -Gfx::CModelFile::CModelFile(CInstanceManager* iMan) -{ - m_iMan = iMan; - - m_engine = (CEngine*)m_iMan->SearchInstance(CLASS_ENGINE); - - m_triangles.reserve(TRIANGLE_PREALLOCATE_COUNT); -} - -Gfx::CModelFile::~CModelFile() -{ -} - -std::string Gfx::CModelFile::GetError() -{ - return m_error; -} - - -bool Gfx::CModelFile::ReadModel(const std::string &filename, bool edit, bool meta) -{ - m_triangles.clear(); - m_error = ""; - - std::ifstream stream; - stream.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); - if (! stream.good()) - { - m_error = std::string("Could not open file '") + filename + std::string("'"); - return false; - } - - return ReadModel(stream, edit, meta); -} - -bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) -{ - m_triangles.clear(); - m_error = ""; - - // FIXME: for now, reading models only from files, not metafile - - ModelHeader header; - - header.revision = IOUtils::ReadBinary<4, int>(stream); - header.version = IOUtils::ReadBinary<4, int>(stream); - header.totalVertices = IOUtils::ReadBinary<4, int>(stream); - for (int i = 0; i < 10; ++i) - header.reserved[i] = IOUtils::ReadBinary<4, int>(stream); - - - if (! stream.good()) - { - m_error = "Error reading model file header"; - return false; - } - - // Old model version #1 - if ( (header.revision == 1) && (header.version == 0) ) - { - for (int i = 0; i < header.totalVertices; ++i) - { - OldModelTriangle1 t; - t.used = IOUtils::ReadBinary<1, char>(stream); - t.selected = IOUtils::ReadBinary<1, char>(stream); - - t.p1 = ReadBinaryVertex(stream); - t.p2 = ReadBinaryVertex(stream); - t.p3 = ReadBinaryVertex(stream); - - t.material = ReadBinaryMaterial(stream); - stream.read(t.texName, 20); - t.min = IOUtils::ReadBinaryFloat(stream); - t.max = IOUtils::ReadBinaryFloat(stream); - - if (! stream.good()) - { - m_error = "Error reading model data"; - return false; - } - - Gfx::ModelTriangle triangle; - triangle.p1.FromVertex(t.p1); - triangle.p2.FromVertex(t.p2); - triangle.p3.FromVertex(t.p3); - - triangle.material = t.material; - triangle.tex1Name = std::string(t.texName); - triangle.min = t.min; - triangle.max = t.max; - - m_triangles.push_back(triangle); - } - } - else if ( header.revision == 1 && header.version == 1 ) - { - for (int i = 0; i < header.totalVertices; ++i) - { - OldModelTriangle2 t; - t.used = IOUtils::ReadBinary<1, char>(stream); - t.selected = IOUtils::ReadBinary<1, char>(stream); - - t.p1 = ReadBinaryVertex(stream); - t.p2 = ReadBinaryVertex(stream); - t.p3 = ReadBinaryVertex(stream); - - t.material = ReadBinaryMaterial(stream); - stream.read(t.texName, 20); - t.min = IOUtils::ReadBinaryFloat(stream); - t.max = IOUtils::ReadBinaryFloat(stream); - t.state = IOUtils::ReadBinary<4, long>(stream); - - t.reserved1 = IOUtils::ReadBinary<2, short>(stream); - t.reserved2 = IOUtils::ReadBinary<2, short>(stream); - t.reserved3 = IOUtils::ReadBinary<2, short>(stream); - t.reserved4 = IOUtils::ReadBinary<2, short>(stream); - - if (! stream.good()) - { - m_error = "Error reading model data"; - return false; - } - - Gfx::ModelTriangle triangle; - triangle.p1.FromVertex(t.p1); - triangle.p2.FromVertex(t.p2); - triangle.p3.FromVertex(t.p3); - - triangle.material = t.material; - triangle.tex1Name = std::string(t.texName); - triangle.min = t.min; - triangle.max = t.max; - triangle.state = t.state; - - m_triangles.push_back(triangle); - } - } - else - { - for (int i = 0; i < header.totalVertices; ++i) - { - NewModelTriangle t; - t.used = IOUtils::ReadBinary<1, char>(stream); - t.selected = IOUtils::ReadBinary<1, char>(stream); - - /* padding */ IOUtils::ReadBinary<2, unsigned int>(stream); - - t.p1 = ReadBinaryVertexTex2(stream); - t.p2 = ReadBinaryVertexTex2(stream); - t.p3 = ReadBinaryVertexTex2(stream); - - t.material = ReadBinaryMaterial(stream); - stream.read(t.texName, 20); - t.min = IOUtils::ReadBinaryFloat(stream); - t.max = IOUtils::ReadBinaryFloat(stream); - t.state = IOUtils::ReadBinary<4, long>(stream); - t.texNum2 = IOUtils::ReadBinary<2, short>(stream); - - t.reserved2 = IOUtils::ReadBinary<2, short>(stream); - t.reserved3 = IOUtils::ReadBinary<2, short>(stream); - t.reserved4 = IOUtils::ReadBinary<2, short>(stream); - - if (! stream.good()) - { - m_error = "Error reading model data"; - return false; - } - - Gfx::ModelTriangle triangle; - triangle.p1 = t.p1; - triangle.p2 = t.p2; - triangle.p3 = t.p3; - - triangle.material = t.material; - triangle.tex1Name = std::string(t.texName); - char tex2Name[20] = { 0 }; - triangle.min = t.min; - triangle.max = t.max; - triangle.state = t.state; - - if (t.texNum2 != 0) - sprintf(tex2Name, "dirty%.2d.tga", t.texNum2); // hardcoded as in the original code - - triangle.tex2Name = std::string(tex2Name); - - m_triangles.push_back(triangle); - } - } - - for (int i = 0; i < (int) m_triangles.size(); ++i) - { - m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "tga"); - - GetLogger()->Info("ModelTriangle %d\n", i+1); - std::string s1 = m_triangles[i].p1.ToString(); - GetLogger()->Info(" p1: %s\n", s1.c_str()); - std::string s2 = m_triangles[i].p2.ToString(); - GetLogger()->Info(" p2: %s\n", s2.c_str()); - std::string s3 = m_triangles[i].p3.ToString(); - GetLogger()->Info(" p3: %s\n", s3.c_str()); - - std::string d = m_triangles[i].material.diffuse.ToString(); - std::string a = m_triangles[i].material.ambient.ToString(); - std::string s = m_triangles[i].material.specular.ToString(); - GetLogger()->Info(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); - - GetLogger()->Info(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), m_triangles[i].tex2Name.c_str()); - GetLogger()->Info(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); - GetLogger()->Info(" state: %ld\n", m_triangles[i].state); - } - - /* - if (! edit) - { - float limit[2]; - limit[0] = m_engine->RetLimitLOD(0); // frontier AB as config - limit[1] = m_engine->RetLimitLOD(1); // frontier BC as config - - // Standard frontiers -> config. - for (int i = 0; i < m_triangles.size(); ++i) - { - if ( m_triangles[i].min == 0.0f && - m_triangles[i].max == 100.0f ) // resolution A ? - { - m_triangles[i].max = limit[0]; - } - else if ( m_triangles[i].min == 100.0f && - m_triangles[i].max == 200.0f ) // resolution B ? - { - m_triangles[i].min = limit[0]; - m_triangles[i].max = limit[1]; - } - else if ( m_triangles[i].min == 200.0f && - m_triangles[i].max == 1000000.0f ) // resolution C ? - { - m_triangles[i].min = limit[1]; - } - } - }*/ - - return true; -} - -bool Gfx::CModelFile::WriteModel(const std::string &filename) -{ - m_error = ""; - - std::ofstream stream; - stream.open(filename.c_str(), std::ios_base::out | std::ios_base::binary); - if (! stream.good()) - { - m_error = std::string("Could not open file '") + filename + std::string("'"); - return false; - } - - return WriteModel(stream); -} - -bool Gfx::CModelFile::WriteModel(std::ostream &stream) -{ - m_error = ""; - - if (m_triangles.size() == 0) - { - m_error = "Empty model"; - return false; - } - - ModelHeader header; - header.revision = 1; - header.version = 2; - header.totalVertices = m_triangles.size(); - - IOUtils::WriteBinary<4, int>(header.revision, stream); - IOUtils::WriteBinary<4, int>(header.version, stream); - IOUtils::WriteBinary<4, int>(header.totalVertices, stream); - for (int i = 0; i < 10; ++i) - IOUtils::WriteBinary<4, int>(header.reserved[i], stream); - - for (int i = 0; i < (int)m_triangles.size(); ++i) - { - NewModelTriangle t; - - t.used = true; - - t.p1 = m_triangles[i].p1; - t.p2 = m_triangles[i].p2; - t.p3 = m_triangles[i].p3; - - t.material = m_triangles[i].material; - strncpy(t.texName, m_triangles[i].tex1Name.c_str(), 20); - t.min = m_triangles[i].min; - t.max = m_triangles[i].max; - t.state = m_triangles[i].state; - int no = 0; - sscanf(m_triangles[i].tex2Name.c_str(), "dirty%d.tga", &no); // hardcoded as in the original code - t.texNum2 = no; - - - IOUtils::WriteBinary<1, char>(t.used, stream); - IOUtils::WriteBinary<1, char>(t.selected, stream); - - WriteBinaryVertexTex2(t.p1, stream); - WriteBinaryVertexTex2(t.p2, stream); - WriteBinaryVertexTex2(t.p3, stream); - - WriteBinaryMaterial(t.material, stream); - stream.write(t.texName, 20); - IOUtils::WriteBinaryFloat(t.min, stream); - IOUtils::WriteBinaryFloat(t.max, stream); - IOUtils::WriteBinary<4, long>(t.state, stream); - IOUtils::WriteBinary<2, short>(t.texNum2, stream); - - IOUtils::WriteBinary<2, short>(t.reserved2, stream); - IOUtils::WriteBinary<2, short>(t.reserved3, stream); - IOUtils::WriteBinary<2, short>(t.reserved4, stream); - } - - return true; -} - -bool Gfx::CModelFile::ReadDXF(const std::string &filename, float min, float max) -{ - m_triangles.clear(); - m_error = ""; - - std::ifstream stream; - stream.open(filename.c_str(), std::ios_base::in); - if (! stream.good()) - { - m_error = std::string("Couldn't open file '") + filename + std::string("'"); - return false; - } - - return ReadDXF(stream, min, max); -} - -bool Gfx::CModelFile::ReadDXF(std::istream &stream, float min, float max) -{ - m_triangles.clear(); - m_error = ""; - - if (! stream.good()) - { - m_error = "Invalid stream"; - return false; - } - - // Input state - bool waitNumVertex = false; - bool waitNumFace = false; - bool waitVertexX = false; - bool waitVertexY = false; - bool waitVertexZ = false; - bool waitFaceX = false; - bool waitFaceY = false; - bool waitFaceZ = false; - - // Vertex array - std::vector vertices; - vertices.reserve(TRIANGLE_PREALLOCATE_COUNT); - - // Number of vertices & faces of the primitive to be read - int vertexNum = 0, faceNum = 0; - // Vertex coords - Math::Vector coords; - // Indexes of face (triangle) points - int p1 = 0, p2 = 0, p3 = 0; - - // Input line - std::string line; - while (! stream.eof() ) - { - // Read line with command - std::getline(stream, line); - int command = StrUtils::FromString(line); - - // Read line with param - std::getline(stream, line); - - bool ok = true; - - - if (command == 66) - { - waitNumVertex = true; - } - - if ( command == 71 && waitNumVertex ) - { - waitNumVertex = false; - vertexNum = StrUtils::FromString(line, &ok); - waitNumFace = true; - } - - if ( command == 72 && waitNumFace ) - { - waitNumFace = false; - faceNum = StrUtils::FromString(line, &ok); - waitVertexX = true; - } - - if ( command == 10 && waitVertexX ) - { - waitVertexX = false; - coords.x = StrUtils::FromString(line, &ok); - waitVertexY = true; - } - - if ( command == 20 && waitVertexY ) - { - waitVertexY = false; - coords.y = StrUtils::FromString(line, &ok); - waitVertexZ = true; - } - - if ( command == 30 && waitVertexZ ) - { - waitVertexZ = false; - coords.z = StrUtils::FromString(line, &ok); - - vertexNum --; - if ( vertexNum >= 0 ) - { - Math::Vector p(coords.x, coords.z, coords.y); // permutation of Y and Z! - vertices.push_back(p); - waitVertexX = true; - } - else - { - waitFaceX = true; - } - } - - if ( command == 71 && waitFaceX ) - { - waitFaceX = false; - p1 = StrUtils::FromString(line, &ok); - if ( p1 < 0 ) p1 = -p1; - waitFaceY = true; - } - - if ( command == 72 && waitFaceY ) - { - waitFaceY = false; - p2 = StrUtils::FromString(line, &ok); - if ( p2 < 0 ) p2 = -p2; - waitFaceZ = true; - } - - if ( command == 73 && waitFaceZ ) - { - waitFaceZ = false; - p3 = StrUtils::FromString(line, &ok); - if ( p3 < 0 ) p3 = -p3; - - faceNum --; - if ( faceNum >= 0 ) - { - assert( (p1-1 >= 0) && (p1-1 < (int)vertices.size() ) ); - assert( (p2-1 >= 0) && (p2-1 < (int)vertices.size() ) ); - assert( (p3-1 >= 0) && (p3-1 < (int)vertices.size() ) ); - - CreateTriangle(vertices[p3-1], vertices[p2-1], vertices[p1-1], min, max); - waitFaceX = true; - } - } - - if (! ok) - { - m_error = "Error reading data"; - return false; - } - - } - - return true; -} - -bool Gfx::CModelFile::CreateEngineObject(int objRank, int addState) -{ - /*char texName1[20]; - char texName2[20]; - int texNum, i, state; - - for (int i = 0; i < m_trianglesUsed; i++) - { - if (! m_triangles[i].used) continue; - - state = m_triangles[i].state; - strcpy(texName1, m_triangles[i].texName); - texName2[0] = 0; - - if ( strcmp(texName1, "plant.tga") == 0 ) // ??? - { - state |= D3DSTATEALPHA; - } - - if ( m_triangles[i].texNum2 != 0 ) - { - if ( m_triangles[i].texNum2 == 1 ) - { - texNum = m_engine->RetSecondTexture(); - } - else - { - texNum = m_triangles[i].texNum2; - } - - if ( texNum >= 1 && texNum <= 10 ) - { - state |= D3DSTATEDUALb; - } - if ( texNum >= 11 && texNum <= 20 ) - { - state |= D3DSTATEDUALw; - } - sprintf(texName2, "dirty%.2d.tga", texNum); // ??? - } - - m_engine->AddTriangle(objRank, &m_triangles[i].p1, 3, - m_triangles[i].material, - state + addState, - texName1, texName2, - m_triangles[i].min, - m_triangles[i].max, false); - }*/ - return true; -} - -void Gfx::CModelFile::Mirror() -{ - for (int i = 0; i < (int)m_triangles.size(); i++) - { - Gfx::VertexTex2 t = m_triangles[i].p1; - m_triangles[i].p1 = m_triangles[i].p2; - m_triangles[i].p2 = t; - - m_triangles[i].p1.coord.z = -m_triangles[i].p1.coord.z; - m_triangles[i].p2.coord.z = -m_triangles[i].p2.coord.z; - m_triangles[i].p3.coord.z = -m_triangles[i].p3.coord.z; - - m_triangles[i].p1.normal.z = -m_triangles[i].p1.normal.z; - m_triangles[i].p2.normal.z = -m_triangles[i].p2.normal.z; - m_triangles[i].p3.normal.z = -m_triangles[i].p3.normal.z; - } -} - -std::vector& Gfx::CModelFile::GetTriangles() -{ - return m_triangles; -} - -int Gfx::CModelFile::GetTriangleCount() -{ - return m_triangles.size(); -} - -float Gfx::CModelFile::GetHeight(Math::Vector pos) -{ - float limit = 5.0f; - - for (int i = 0; i < (int)m_triangles.size(); i++) - { - if ( fabs(pos.x - m_triangles[i].p1.coord.x) < limit && - fabs(pos.z - m_triangles[i].p1.coord.z) < limit ) - return m_triangles[i].p1.coord.y; - - if ( fabs(pos.x - m_triangles[i].p2.coord.x) < limit && - fabs(pos.z - m_triangles[i].p2.coord.z) < limit ) - return m_triangles[i].p2.coord.y; - - if ( fabs(pos.x - m_triangles[i].p3.coord.x) < limit && - fabs(pos.z - m_triangles[i].p3.coord.z) < limit ) - return m_triangles[i].p3.coord.y; - } - - return 0.0f; -} - -void Gfx::CModelFile::CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max) -{ - Gfx::ModelTriangle triangle; - - Math::Vector n = Math::NormalToPlane(p3, p2, p1); - triangle.p1 = Gfx::VertexTex2(p1, n); - triangle.p2 = Gfx::VertexTex2(p2, n); - triangle.p3 = Gfx::VertexTex2(p3, n); - - triangle.material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f, 0.0f); - triangle.material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f, 0.0f); - - triangle.min = min; - triangle.max = max; - - m_triangles.push_back(triangle); -} diff --git a/src/graphics/common/modelfile.h b/src/graphics/common/modelfile.h deleted file mode 100644 index f8cb022..0000000 --- a/src/graphics/common/modelfile.h +++ /dev/null @@ -1,120 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// modelfile.h (aka modfile.h) - -#include "graphics/common/engine.h" -#include "graphics/common/vertex.h" -#include "graphics/common/material.h" -#include "math/vector.h" - -#include -#include -#include - - -class CInstanceManager; - - -namespace Gfx { - -/** - \struct ModelTriangle - \brief Triangle of a 3D model - */ -struct ModelTriangle -{ - //! 1st vertex - Gfx::VertexTex2 p1; - //! 2nd vertex - Gfx::VertexTex2 p2; - //! 3rd vertex - Gfx::VertexTex2 p3; - //! Material - Gfx::Material material; - //! Name of 1st texture - std::string tex1Name; - //! Name of 2nd texture - std::string tex2Name; - //! Min LOD threshold - float min; - //! Max LOD threshold - float max; - //! Rendering state to be set - long state; - - ModelTriangle(); -}; - - -/** - \class CModelFile - \brief Model file reader/writer - - Allows reading and writing model objects. Models are collections of ModelTriangle structs. */ -class CModelFile -{ -public: - CModelFile(CInstanceManager* iMan); - ~CModelFile(); - - //! Returns the last error encountered - std::string GetError(); - - //! Reads a binary Colobot model from file - bool ReadModel(const std::string &filename, bool edit = false, bool meta = true); - //! Reads a binary Colobot model from stream - bool ReadModel(std::istream &stream, bool edit = false, bool meta = true); - //! Writes the model to Colobot binary model file - bool WriteModel(const std::string &filename); - //! Writes the model to Colobot binary model file - bool WriteModel(std::ostream &stream); - - //! Reads a DXF model from file - bool ReadDXF(const std::string &filename, float min, float max); - //! Reads a DXF model from stream - bool ReadDXF(std::istream &stream, float min, float max); - - //! Returns the number of triangles in model - int GetTriangleCount(); - //! Returns the triangle vector - std::vector& GetTriangles(); - //! Returns the height of model -- closest point to X and Z coords of \a pos - float GetHeight(Math::Vector pos); - - //! Mirrors the model along the Z axis - void Mirror(); - - //! Creates an object in the graphics engine from the model - bool CreateEngineObject(int objRank, int addState = 0); - -protected: - //! Adds a triangle to the list - void CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max); - -protected: - CInstanceManager* m_iMan; - Gfx::CEngine* m_engine; - - //! Last error - std::string m_error; - - //! Model triangles - std::vector m_triangles; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/particle.cpp b/src/graphics/common/particle.cpp deleted file mode 100644 index 322c2d0..0000000 --- a/src/graphics/common/particle.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// particle.cpp (aka particule.cpp) - -#include "graphics/common/particle.h" - - -// TODO implementation diff --git a/src/graphics/common/particle.h b/src/graphics/common/particle.h deleted file mode 100644 index bd9741f..0000000 --- a/src/graphics/common/particle.h +++ /dev/null @@ -1,337 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// particle.h (aka particule.h) - -#pragma once - -#include "engine.h" -#include "sound/sound.h" - - -class CInstanceManager; -class CRobotMain; -class CObject; -class CSound; - - - -namespace Gfx { - -const short MAXPARTICULE = 500; -const short MAXPARTITYPE = 5; -const short MAXTRACK = 100; -const short MAXTRACKLEN = 10; -const short MAXPARTIFOG = 100; -const short MAXWHEELTRACE = 1000; - -const short SH_WORLD = 0; // particle in the world in the interface -const short SH_FRONT = 1; // particle in the world on the interface -const short SH_INTERFACE = 2; // particle in the interface -const short SH_MAX = 3; - -// type == 0 -> triangles -// type == 1 -> effect00 (black background) -// type == 2 -> effect01 (black background) -// type == 3 -> effect02 (black background) -// type == 4 -> text (white background) - - -enum ParticleType -{ - PARTIEXPLOT = 1, // technology explosion - PARTIEXPLOO = 2, // organic explosion - PARTIMOTOR = 3, // the engine exhaust gas - PARTIGLINT = 4, // reflection - PARTIBLITZ = 5, // lightning recharging battery - PARTICRASH = 6, // dust after fall - PARTIGAS = 7, // gas from the reactor - PARTIFIRE = 9, // fireball shrinks - PARTIFIREZ = 10, // fireball grows - PARTIBLUE = 11, // blue ball - PARTISELY = 12, // yellow selection - PARTISELR = 13, // red selection - PARTIGUN1 = 18, // a bullet (fireball) - PARTIGUN2 = 19, // bullet 2 (ant) - PARTIGUN3 = 20, // bullet 3 (spider) - PARTIGUN4 = 21, // bullet 4 (orgaball) - PARTIFRAG = 22, // triangular fragment - PARTIQUEUE = 23, // inflamed tail - PARTIORGANIC1 = 24, // organic ball mother - PARTIORGANIC2 = 25, // organic ball daughter - PARTISMOKE1 = 26, // black smoke - PARTISMOKE2 = 27, // black smoke - PARTISMOKE3 = 28, // black smoke - PARTISMOKE4 = 29, // black smoke - PARTIBLOOD = 30, // human blood - PARTIBLOODM = 31, // blood laying - PARTIVAPOR = 32, // steam - PARTIVIRUS1 = 33, // virus 1 - PARTIVIRUS2 = 34, // virus 2 - PARTIVIRUS3 = 35, // virus 3 - PARTIVIRUS4 = 36, // virus 4 - PARTIVIRUS5 = 37, // virus 5 - PARTIVIRUS6 = 38, // virus 6 - PARTIVIRUS7 = 39, // virus 7 - PARTIVIRUS8 = 40, // virus 8 - PARTIVIRUS9 = 41, // virus 9 - PARTIVIRUS10 = 42, // virus 10 - PARTIRAY1 = 43, // ray 1 (turn) - PARTIRAY2 = 44, // ray 2 (electric arc) - PARTIRAY3 = 45, // ray 3 - PARTIRAY4 = 46, // ray 4 - PARTIFLAME = 47, // flame - PARTIBUBBLE = 48, // bubble - PARTIFLIC = 49, // circles in the water - PARTIEJECT = 50, // ejection from the reactor - PARTISCRAPS = 51, // waste from the reactor - PARTITOTO = 52, // reactor of tot - PARTIERROR = 53, // toto says no - PARTIWARNING = 54, // foo says blah - PARTIINFO = 54, // toto says yes - PARTIQUARTZ = 55, // reflection crystal - PARTISPHERE0 = 56, // explosion sphere - PARTISPHERE1 = 57, // energy sphere - PARTISPHERE2 = 58, // analysis sphere - PARTISPHERE3 = 59, // shield sphere - PARTISPHERE4 = 60, // information sphere (emit) - PARTISPHERE5 = 61, // botanical sphere (gravity root) - PARTISPHERE6 = 62, // information sphere (receive) - PARTISPHERE7 = 63, // sphere - PARTISPHERE8 = 64, // sphere - PARTISPHERE9 = 65, // sphere - PARTIGUNDEL = 66, // bullet destroyed by shield - PARTIPART = 67, // object part - PARTITRACK1 = 68, // drag 1 - PARTITRACK2 = 69, // drag 2 - PARTITRACK3 = 70, // drag 3 - PARTITRACK4 = 71, // drag 4 - PARTITRACK5 = 72, // drag 5 - PARTITRACK6 = 73, // drag 6 - PARTITRACK7 = 74, // drag 7 - PARTITRACK8 = 75, // drag 8 - PARTITRACK9 = 76, // drag 9 - PARTITRACK10 = 77, // drag 10 - PARTITRACK11 = 78, // drag 11 - PARTITRACK12 = 79, // drag 12 - PARTITRACK13 = 80, // drag 13 - PARTITRACK14 = 81, // drag 14 - PARTITRACK15 = 82, // drag 15 - PARTITRACK16 = 83, // drag 16 - PARTITRACK17 = 84, // drag 17 - PARTITRACK18 = 85, // drag 18 - PARTITRACK19 = 86, // drag 19 - PARTITRACK20 = 87, // drag 20 - PARTIGLINTb = 88, // blue reflection - PARTIGLINTr = 89, // red reflection - PARTILENS1 = 90, // brilliance 1 (orange) - PARTILENS2 = 91, // brilliance 2 (yellow) - PARTILENS3 = 92, // brilliance 3 (red) - PARTILENS4 = 93, // brilliance 4 (violet) - PARTICONTROL = 94, // reflection on button - PARTISHOW = 95, // shows a place - PARTICHOC = 96, // shock wave - PARTIGFLAT = 97, // shows if the ground is flat - PARTIRECOVER = 98, // blue ball recycler - PARTIROOT = 100, // gravity root smoke - PARTIPLOUF0 = 101, // splash - PARTIPLOUF1 = 102, // splash - PARTIPLOUF2 = 103, // splash - PARTIPLOUF3 = 104, // splash - PARTIPLOUF4 = 105, // splash - PARTIDROP = 106, // drop - PARTIFOG0 = 107, // fog 0 - PARTIFOG1 = 108, // fog 1 - PARTIFOG2 = 109, // fog 2 - PARTIFOG3 = 110, // fog 3 - PARTIFOG4 = 111, // fog 4 - PARTIFOG5 = 112, // fog 5 - PARTIFOG6 = 113, // fog 6 - PARTIFOG7 = 114, // fog 7 - PARTIFOG8 = 115, // fog 8 - PARTIFOG9 = 116, // fog 9 - PARTILIMIT1 = 117, // shows the limits 1 - PARTILIMIT2 = 118, // shows the limits 2 - PARTILIMIT3 = 119, // shows the limits 3 - PARTILIMIT4 = 120, // shows the limits 4 - PARTIWATER = 121, // drop of water - PARTIEXPLOG1 = 122, // ball explosion 1 - PARTIEXPLOG2 = 123, // ball explosion 2 - PARTIBASE = 124, // gases of spaceship - PARTITRACE0 = 140, // trace - PARTITRACE1 = 141, // trace - PARTITRACE2 = 142, // trace - PARTITRACE3 = 143, // trace - PARTITRACE4 = 144, // trace - PARTITRACE5 = 145, // trace - PARTITRACE6 = 146, // trace - PARTITRACE7 = 147, // trace - PARTITRACE8 = 148, // trace - PARTITRACE9 = 149, // trace - PARTITRACE10 = 150, // trace - PARTITRACE11 = 151, // trace - PARTITRACE12 = 152, // trace - PARTITRACE13 = 153, // trace - PARTITRACE14 = 154, // trace - PARTITRACE15 = 155, // trace - PARTITRACE16 = 156, // trace - PARTITRACE17 = 157, // trace - PARTITRACE18 = 158, // trace - PARTITRACE19 = 159, // trace -}; - -enum ParticlePhase -{ - PARPHSTART = 0, - PARPHEND = 1, -}; - -struct Particle -{ - char bUsed; // TRUE -> particle used - char bRay; // TRUE -> ray with goal - unsigned short uniqueStamp; // unique mark - short sheet; // sheet (0..n) - ParticleType type; // type PARTI* - ParticlePhase phase; // phase PARPH* - float mass; // mass of the particle (in rebounding) - float weight; // weight of the particle (for noise) - float duration; // length of life - Math::Vector pos; // absolute position (relative if object links) - Math::Vector goal; // goal position (if bRay) - Math::Vector speed; // speed of displacement - float windSensitivity; - short bounce; // number of rebounds - Math::Point dim; // dimensions of the rectangle - float zoom; // zoom (0..1) - float angle; // angle of rotation - float intensity; // intensity - Math::Point texSup; // coordinated upper texture - Math::Point texInf; // coordinated lower texture - float time; // age of the particle (0..n) - float phaseTime; // age at the beginning of phase - float testTime; // time since last test - CObject* objLink; // father object (for example reactor) - CObject* objFather; // father object (for example reactor) - short objRank; // rank of the object, or -1 - short trackRank; // rank of the drag -}; - -struct Track -{ - char bUsed; // TRUE -> drag used - char bDrawParticle; - float step; // duration of not - float last; // increase last not memorized - float intensity; // intensity at starting (0..1) - float width; // tail width - int used; // number of positions in "pos" - int head; // head to write index - Math::Vector pos[MAXTRACKLEN]; - float len[MAXTRACKLEN]; -}; - -struct WheelTrace -{ - ParticleType type; // type PARTI* - Math::Vector pos[4]; // rectangle positions - float startTime; // beginning of life -}; - - - -class CParticle -{ -public: - CParticle(CInstanceManager* iMan, CEngine* engine); - ~CParticle(); - - void SetGLDevice(CDevice device); - - void FlushParticle(); - void FlushParticle(int sheet); - int CreateParticle(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreateFrag(Math::Vector pos, Math::Vector speed, Gfx::EngineTriangle *triangle, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreatePart(Math::Vector pos, Math::Vector speed, ParticleType type, float duration=1.0f, float mass=0.0f, float weight=0.0f, float windSensitivity=1.0f, int sheet=0); - int CreateRay(Math::Vector pos, Math::Vector goal, ParticleType type, Math::Point dim, float duration=1.0f, int sheet=0); - int CreateTrack(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float length=10.0f, float width=1.0f); - void CreateWheelTrace(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3, const Math::Vector &p4, ParticleType type); - void DeleteParticle(ParticleType type); - void DeleteParticle(int channel); - void SetObjectLink(int channel, CObject *object); - void SetObjectFather(int channel, CObject *object); - void SetPosition(int channel, Math::Vector pos); - void SetDimension(int channel, Math::Point dim); - void SetZoom(int channel, float zoom); - void SetAngle(int channel, float angle); - void SetIntensity(int channel, float intensity); - void SetParam(int channel, Math::Vector pos, Math::Point dim, float zoom, float angle, float intensity); - void SetPhase(int channel, ParticlePhase phase, float duration); - bool GetPosition(int channel, Math::Vector &pos); - - Gfx::Color RetFogColor(Math::Vector pos); - - void SetFrameUpdate(int sheet, bool bUpdate); - void FrameParticle(float rTime); - void DrawParticle(int sheet); - - bool WriteWheelTrace(char *filename, int width, int height, Math::Vector dl, Math::Vector ur); - -protected: - void DeleteRank(int rank); - bool CheckChannel(int &channel); - void DrawParticleTriangle(int i); - void DrawParticleNorm(int i); - void DrawParticleFlat(int i); - void DrawParticleFog(int i); - void DrawParticleRay(int i); - void DrawParticleSphere(int i); - void DrawParticleCylinder(int i); - void DrawParticleWheel(int i); - CObject* SearchObjectGun(Math::Vector old, Math::Vector pos, ParticleType type, CObject *father); - CObject* SearchObjectRay(Math::Vector pos, Math::Vector goal, ParticleType type, CObject *father); - void Play(Sound sound, Math::Vector pos, float amplitude); - bool TrackMove(int i, Math::Vector pos, float progress); - void TrackDraw(int i, ParticleType type); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CDevice* m_pDevice; - CRobotMain* m_main; - CTerrain* m_terrain; - CWater* m_water; - CSound* m_sound; - - Gfx::Particle m_particule[MAXPARTICULE*MAXPARTITYPE]; - Gfx::EngineTriangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 - Track m_track[MAXTRACK]; - int m_wheelTraceTotal; - int m_wheelTraceIndex; - WheelTrace m_wheelTrace[MAXWHEELTRACE]; - int m_totalInterface[MAXPARTITYPE][SH_MAX]; - bool m_bFrameUpdate[SH_MAX]; - int m_fogTotal; - int m_fog[MAXPARTIFOG]; - int m_uniqueStamp; - int m_exploGunCounter; - float m_lastTimeGunDel; - float m_absTime; -}; - - -}; // namespace Gfx diff --git a/src/graphics/common/planet.cpp b/src/graphics/common/planet.cpp deleted file mode 100644 index 4fa17a1..0000000 --- a/src/graphics/common/planet.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// planet.cpp - -#include "graphics/common/planet.h" - - -// TODO implementation diff --git a/src/graphics/common/planet.h b/src/graphics/common/planet.h deleted file mode 100644 index 264d05c..0000000 --- a/src/graphics/common/planet.h +++ /dev/null @@ -1,80 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// planet.h - -#pragma once - -#include "common/event.h" -#include "math/point.h" - - -class CInstanceManager; - - -namespace Gfx { - -class CEngine; - - -const short MAXPLANET = 10; - -struct Planet -{ - char bUsed; // TRUE -> planet exists - Math::Point start; // initial position in degrees - Math::Point angle; // current position in degrees - float dim; // dimensions (0..1) - float speed; // speed - float dir; // direction in the sky - char name[20]; // name of the texture - Math::Point uv1, uv2; // texture mapping - char bTGA; // texture .TGA -}; - - - - -class CPlanet { -public: - CPlanet(CInstanceManager* iMan, CEngine* engine); - ~CPlanet(); - - void Flush(); - bool EventProcess(const Event &event); - bool Create(int mode, Math::Point start, float dim, float speed, float dir, char *name, Math::Point uv1, Math::Point uv2); - bool PlanetExist(); - void LoadTexture(); - void Draw(); - void SetMode(int mode); - int RetMode(); - -protected: - bool EventFrame(const Event &event); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - - float m_time; - int m_mode; - Planet m_planet[2][MAXPLANET]; - bool m_bPlanetExist; -}; - - -}; // namespace Gfx diff --git a/src/graphics/common/pyro.cpp b/src/graphics/common/pyro.cpp deleted file mode 100644 index 6b5b1af..0000000 --- a/src/graphics/common/pyro.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// pyro.cpp - -#include "graphics/common/pyro.h" - - -// TODO implementation diff --git a/src/graphics/common/pyro.h b/src/graphics/common/pyro.h deleted file mode 100644 index fda74b3..0000000 --- a/src/graphics/common/pyro.h +++ /dev/null @@ -1,175 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// pyro.h - -#pragma once - -#include "common/misc.h" -#include "graphics/common/engine.h" -//#include "object/object.h" -// TEMPORARILY! -enum ObjectType {}; - - -class CInstanceManager; -class CObject; -class CDisplayText; -class CRobotMain; -class CSound; - - -namespace Gfx { - -class CEngine; -class CTerrain; -class CCamera; -class CParticle; -class CLight; - - -enum PyroType -{ - PT_NULL = 0, - PT_FRAGT = 1, // fragmentation of technical object - PT_FRAGO = 2, // fragmentation of organic object - PT_FRAGW = 4, // fragmentation of object under water - PT_EXPLOT = 5, // explosion of technical object - PT_EXPLOO = 6, // explosion of organic object - PT_EXPLOW = 8, // explosion of object under water - PT_SHOTT = 9, // hit technical object - PT_SHOTH = 10, // hit human - PT_SHOTM = 11, // hit queen - PT_SHOTW = 12, // hit under water - PT_EGG = 13, // break the egg - PT_BURNT = 14, // burning of technical object - PT_BURNO = 15, // burning of organic object - PT_SPIDER = 16, // spider explosion - PT_FALL = 17, // cargo falling - PT_WPCHECK = 18, // indicator reaches - PT_FLCREATE = 19, // flag create - PT_FLDELETE = 20, // flag destroy - PT_RESET = 21, // reset position of the object - PT_WIN = 22, // fireworks - PT_LOST = 23, // black smoke - PT_DEADG = 24, // shooting death - PT_DEADW = 25, // drowning death - PT_FINDING = 26, // object discovered -}; - - -struct PyroBurnPart -{ - int part; - Math::Vector initialPos; - Math::Vector finalPos; - Math::Vector initialAngle; - Math::Vector finalAngle; -}; - -struct PyroLightOper -{ - float progress; - float intensity; - Gfx::Color color; -}; - - - -class CPyro { -public: - CPyro(CInstanceManager* iMan); - ~CPyro(); - - void DeleteObject(bool bAll=false); - bool Create(PyroType type, CObject* pObj, float force=1.0f); - bool EventProcess(const Event &event); - Error IsEnded(); - void CutObjectLink(CObject* pObj); - -protected: - void DisplayError(PyroType type, CObject* pObj); - bool CreateLight(Math::Vector pos, float height); - void DeleteObject(bool bPrimary, bool bSecondary); - - void CreateTriangle(CObject* pObj, ObjectType oType, int part); - - void ExploStart(); - void ExploTerminate(); - - void BurnStart(); - void BurnAddPart(int part, Math::Vector pos, Math::Vector angle); - void BurnProgress(); - bool BurnIsKeepPart(int part); - void BurnTerminate(); - - void FallStart(); - CObject* FallSearchBeeExplo(); - void FallProgress(float rTime); - Error FallIsEnded(); - - void LightOperFlush(); - void LightOperAdd(float progress, float intensity, float r, float g, float b); - void LightOperFrame(float rTime); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CTerrain* m_terrain; - CCamera* m_camera; - CParticle* m_particule; - CLight* m_light; - CObject* m_object; - CDisplayText* m_displayText; - CRobotMain* m_main; - CSound* m_sound; - - Math::Vector m_pos; // center of the effect - Math::Vector m_posPower; // center of the battery - bool m_bPower; // battery exists? - PyroType m_type; - float m_force; - float m_size; - float m_progress; - float m_speed; - float m_time; - float m_lastParticule; - float m_lastParticuleSmoke; - int m_soundChannel; - - int m_lightRank; - int m_lightOperTotal; - PyroLightOper m_lightOper[10]; - float m_lightHeight; - - ObjectType m_burnType; - int m_burnPartTotal; - PyroBurnPart m_burnPart[10]; - int m_burnKeepPart[10]; - float m_burnFall; - - float m_fallFloor; - float m_fallSpeed; - float m_fallBulletTime; - bool m_bFallEnding; - - int m_crashSphereUsed; // number of spheres used - Math::Vector m_crashSpherePos[50]; - float m_crashSphereRadius[50]; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/terrain.cpp b/src/graphics/common/terrain.cpp deleted file mode 100644 index 9b61dfc..0000000 --- a/src/graphics/common/terrain.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// terrain.cpp - -#include "graphics/common/terrain.h" - - -// TODO implementation diff --git a/src/graphics/common/terrain.h b/src/graphics/common/terrain.h deleted file mode 100644 index fd9a1a6..0000000 --- a/src/graphics/common/terrain.h +++ /dev/null @@ -1,211 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// terrain.h - -#pragma once - -#include "graphics/common/engine.h" - - -class CInstanceManager; - - -namespace Gfx { - -class CEngine; -class CWater; - - -const short FLATLIMIT = (5.0f*Math::PI/180.0f); - - -enum TerrainRes -{ - TR_NULL = 0, - TR_STONE = 1, - TR_URANIUM = 2, - TR_POWER = 3, - TR_KEYa = 4, - TR_KEYb = 5, - TR_KEYc = 6, - TR_KEYd = 7, -}; - - -const short MAXBUILDINGLEVEL = 100; - -struct BuildingLevel -{ - Math::Vector center; - float factor; - float min; - float max; - float level; - float height; - float bboxMinX; - float bboxMaxX; - float bboxMinZ; - float bboxMaxZ; -}; - - -const short MAXMATTERRAIN = 100; - -struct TerrainMaterial -{ - short id; - char texName[20]; - float u,v; - float hardness; - char mat[4]; // up, right, down, left -}; - -struct DotLevel -{ - short id; - char mat[4]; // up, right, down, left -}; - - -const short MAXFLYINGLIMIT = 10; - -struct FlyingLimit -{ - Math::Vector center; - float extRadius; - float intRadius; - float maxHeight; -}; - - - -class CTerrain -{ -public: - CTerrain(CInstanceManager* iMan); - ~CTerrain(); - - bool Generate(int mosaic, int brickP2, float size, float vision, int depth, float hardness); - bool InitTextures(char* baseName, int* table, int dx, int dy); - void LevelFlush(); - bool LevelMaterial(int id, char* baseName, float u, float v, int up, int right, int down, int left, float hardness); - bool LevelInit(int id); - bool LevelGenerate(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius); - void FlushRelief(); - bool ReliefFromBMP(const char* filename, float scaleRelief, bool adjustBorder); - bool ReliefFromDXF(const char* filename, float scaleRelief); - bool ResFromBMP(const char* filename); - bool CreateObjects(bool bMultiRes); - bool Terraform(const Math::Vector &p1, const Math::Vector &p2, float height); - - void SetWind(Math::Vector speed); - Math::Vector RetWind(); - - float RetFineSlope(const Math::Vector &pos); - float RetCoarseSlope(const Math::Vector &pos); - bool GetNormal(Math::Vector &n, const Math::Vector &p); - float RetFloorLevel(const Math::Vector &p, bool bBrut=false, bool bWater=false); - float RetFloorHeight(const Math::Vector &p, bool bBrut=false, bool bWater=false); - bool MoveOnFloor(Math::Vector &p, bool bBrut=false, bool bWater=false); - bool ValidPosition(Math::Vector &p, float marging); - TerrainRes RetResource(const Math::Vector &p); - void LimitPos(Math::Vector &pos); - - void FlushBuildingLevel(); - bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor); - bool UpdateBuildingLevel(Math::Vector center); - bool DeleteBuildingLevel(Math::Vector center); - float RetBuildingFactor(const Math::Vector &p); - float RetHardness(const Math::Vector &p); - - int RetMosaic(); - int RetBrick(); - float RetSize(); - float RetScaleRelief(); - - void GroundFlat(Math::Vector pos); - float RetFlatZoneRadius(Math::Vector center, float max); - - void SetFlyingMaxHeight(float height); - float RetFlyingMaxHeight(); - void FlushFlyingLimit(); - bool AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight); - float RetFlyingLimit(Math::Vector pos, bool bNoLimit); - -protected: - bool ReliefAddDot(Math::Vector pos, float scaleRelief); - void AdjustRelief(); - Math::Vector RetVector(int x, int y); - Gfx::VertexTex2 RetVertex(int x, int y, int step); - bool CreateMosaic(int ox, int oy, int step, int objRank, const Gfx::Material &mat, float min, float max); - bool CreateSquare(bool bMultiRes, int x, int y); - - TerrainMaterial* LevelSearchMat(int id); - void LevelTextureName(int x, int y, char *name, Math::Point &uv); - float LevelRetHeight(int x, int y); - bool LevelGetDot(int x, int y, float min, float max, float slope); - int LevelTestMat(char *mat); - void LevelSetDot(int x, int y, int id, char *mat); - bool LevelIfDot(int x, int y, int id, char *mat); - bool LevelPutDot(int x, int y, int id); - void LevelOpenTable(); - void LevelCloseTable(); - - void AdjustBuildingLevel(Math::Vector &p); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CWater* m_water; - - int m_mosaic; // number of mosaics - int m_brick; // number of bricks per mosaics - float m_size; // size of an item in an brick - float m_vision; // vision before a change of resolution - float* m_relief; // table of the relief - int* m_texture; // table of textures - int* m_objRank; // table of rows of objects - bool m_bMultiText; - bool m_bLevelText; - float m_scaleMapping; // scale of the mapping - float m_scaleRelief; - int m_subdivMapping; - int m_depth; // number of different resolutions (1,2,3,4) - char m_texBaseName[20]; - char m_texBaseExt[10]; - float m_defHardness; - - TerrainMaterial m_levelMat[MAXMATTERRAIN+1]; - int m_levelMatTotal; - int m_levelMatMax; - int m_levelDotSize; - DotLevel* m_levelDot; - int m_levelID; - - int m_buildingUsed; - BuildingLevel m_buildingTable[MAXBUILDINGLEVEL]; - - unsigned char* m_resources; - Math::Vector m_wind; // wind speed - - float m_flyingMaxHeight; - int m_flyingLimitTotal; - FlyingLimit m_flyingLimit[MAXFLYINGLIMIT]; -}; - -}; // namespace Gfx diff --git a/src/graphics/common/test/CMakeLists.txt b/src/graphics/common/test/CMakeLists.txt deleted file mode 100644 index bd83773..0000000 --- a/src/graphics/common/test/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -set(CMAKE_BUILD_TYPE debug) -set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") - -include_directories(. ../../..) -add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/logger.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp) diff --git a/src/graphics/common/test/modelfile_test.cpp b/src/graphics/common/test/modelfile_test.cpp deleted file mode 100644 index cc44f98..0000000 --- a/src/graphics/common/test/modelfile_test.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "graphics/common/modelfile.h" -#include "common/iman.h" - -#include - - -int main(int argc, char *argv[]) -{ - if (argc != 4) - { - std::cerr << "Usage: " << argv[0] << " {mod|dxf} in_file out_file" << std::endl; - return 1; - } - - CInstanceManager iMan; - Gfx::CModelFile modfile(&iMan); - - std::string mode(argv[1]); - if (mode == "mod") - { - if (! modfile.ReadModel(argv[2], false, false) ) - { - std::cerr << "Read error: " << modfile.GetError() << std::endl; - return 2; - } - } - else if (mode == "dxf") - { - if (! modfile.ReadDXF(argv[2], false, false) ) - { - std::cerr << "Read error: " << modfile.GetError() << std::endl; - return 2; - } - } - else - { - std::cerr << "Usage: " << argv[0] << " {mod|dxf} in_file out_file" << std::endl; - return 1; - } - - if (! modfile.WriteModel(argv[3]) ) - { - std::cerr << "Write error: " << modfile.GetError() << std::endl; - return 3; - } - - return 0; -} diff --git a/src/graphics/common/text.cpp b/src/graphics/common/text.cpp deleted file mode 100644 index 0c5eb66..0000000 --- a/src/graphics/common/text.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// text.cpp - -#include "graphics/common/text.h" - - -// TODO implementation diff --git a/src/graphics/common/text.h b/src/graphics/common/text.h deleted file mode 100644 index f96dc61..0000000 --- a/src/graphics/common/text.h +++ /dev/null @@ -1,113 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// text.h - -#pragma once - -#include "graphics/common/engine.h" -#include "graphics/common/device.h" -#include "math/point.h" - - -class CInstanceManager; - - -namespace Gfx { - -const float SMALLFONT = 10.0f; -const float BIGFONT = 15.0f; - -const float NORMSTRETCH = 0.8f; - - - -enum FontType -{ - FONT_COLOBOT = 0, - FONT_COURIER = 1, - FONT_BUTTON = 2, -}; - -enum FontTitle -{ - TITLE_BIG = 0x04, - TITLE_NORM = 0x08, - TITLE_LITTLE = 0x0c, -}; - -enum FontColor -{ - COLOR_LINK = 0x10, - COLOR_TOKEN = 0x20, - COLOR_TYPE = 0x30, - COLOR_CONST = 0x40, - COLOR_REM = 0x50, - COLOR_KEY = 0x60, - COLOR_TABLE = 0x70, -}; - -const short FONT_MASK = 0x03; -const short TITLE_MASK = 0x0c; -const short COLOR_MASK = 0x70; -const short IMAGE_MASK = 0x80; - - - -class CText { -public: - CText(CInstanceManager *iMan, Gfx::CEngine* engine); - ~CText(); - - void SetDevice(Gfx::CDevice *device); - - void DrawText(char *string, char *format, int len, Math::Point pos, float width, int justif, float size, float stretch, int eol); - void DrawText(char *string, char *format, Math::Point pos, float width, int justif, float size, float stretch, int eol); - void DrawText(char *string, int len, Math::Point pos, float width, int justif, float size, float stretch, FontType font, int eol); - void DrawText(char *string, Math::Point pos, float width, int justif, float size, float stretch, FontType font, int eol); - void DimText(char *string, char *format, int len, Math::Point pos, int justif, float size, float stretch, Math::Point &start, Math::Point &end); - void DimText(char *string, char *format, Math::Point pos, int justif, float size, float stretch, Math::Point &start, Math::Point &end); - void DimText(char *string, int len, Math::Point pos, int justif, float size, float stretch, FontType font, Math::Point &start, Math::Point &end); - void DimText(char *string, Math::Point pos, int justif, float size, float stretch, FontType font, Math::Point &start, Math::Point &end); - - float RetAscent(float size, FontType font); - float RetDescent(float size, FontType font); - float RetHeight(float size, FontType font); - - float RetStringWidth(char *string, char *format, int len, float size, float stretch); - float RetStringWidth(char *string, int len, float size, float stretch, FontType font); - float RetCharWidth(int character, float offset, float size, float stretch, FontType font); - - int Justif(char *string, char *format, int len, float width, float size, float stretch); - int Justif(char *string, int len, float width, float size, float stretch, FontType font); - int Detect(char *string, char *format, int len, float offset, float size, float stretch); - int Detect(char *string, int len, float offset, float size, float stretch, FontType font); - -protected: - void DrawString(char *string, char *format, int len, Math::Point pos, float width, float size, float stretch, int eol); - void DrawString(char *string, int len, Math::Point pos, float width, float size, float stretch, FontType font, int eol); - void DrawColor(Math::Point pos, float size, float width, int color); - void DrawChar(int character, Math::Point pos, float size, float stretch, FontType font); - -protected: - CInstanceManager* m_iMan; - Gfx::CEngine* m_engine; - Gfx::CDevice* m_device; - -}; - -}; // namespace Gfx diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h deleted file mode 100644 index 787c2bf..0000000 --- a/src/graphics/common/texture.h +++ /dev/null @@ -1,237 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// texture.h - -#pragma once - -namespace Gfx { - -/** - \enum TexImgFormat - \brief Format of image data */ -enum TexImgFormat -{ - //! Try to determine automatically (may not work) - TEX_IMG_AUTO, - //! RGB triplet, 3 bytes - TEX_IMG_RGB, - //! BGR triplet, 3 bytes - TEX_IMG_BGR, - //! RGBA triplet, 4 bytes - TEX_IMG_RGBA, - //! BGRA triplet, 4 bytes - TEX_IMG_BGRA -}; - -/** - \enum TexMinFilter - \brief Texture minification filter - - Corresponds to OpenGL modes but should translate to DirectX too. */ -enum TexMinFilter -{ - TEX_MIN_FILTER_NEAREST, - TEX_MIN_FILTER_LINEAR, - TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST, - TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST, - TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR, - TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR -}; - -/** - \enum TexMagFilter - \brief Texture magnification filter */ -enum TexMagFilter -{ - TEX_MAG_FILTER_NEAREST, - TEX_MAG_FILTER_LINEAR -}; - -/** - \enum TexWrapMode - \brief Wrapping mode for texture coords */ -enum TexWrapMode -{ - TEX_WRAP_CLAMP, - TEX_WRAP_REPEAT -}; - -/** - \enum TexMixOperation - \brief Multitexture mixing operation */ -enum TexMixOperation -{ - //! Default operation on default params (modulate on computed & texture) - TEX_MIX_OPER_DEFAULT, - //! = Arg1 - TEX_MIX_OPER_REPLACE, - //! = Arg1 * Arg2 - TEX_MIX_OPER_MODULATE, - //! = Arg1 + Arg2 - TEX_MIX_OPER_ADD, - //! = Arg1 - Arg2 - TEX_MIX_OPER_SUBTRACT -}; - -/** - \enum TexMixArgument - \brief Multitexture mixing argument */ -enum TexMixArgument -{ - //! Color from current texture - TEX_MIX_ARG_TEXTURE, - //! Color computed by previous texture unit (current in DirectX; previous in OpenGL) - TEX_MIX_ARG_COMPUTED_COLOR, - //! (Source) color of textured fragment (diffuse in DirectX; primary color in OpenGL) - TEX_MIX_ARG_SRC_COLOR, - //! Constant color (texture factor in DirectX; texture env color in OpenGL) - TEX_MIX_ARG_FACTOR -}; - -/** - \struct TextureCreateParams - \brief Parameters for texture creation - - These params define how particular texture is created and later displayed. - They must be specified at texture creation time and cannot be changed later. */ -struct TextureCreateParams -{ - //! Whether to generate mipmaps - bool mipmap; - //! Format of source image data - Gfx::TexImgFormat format; - //! Minification filter - Gfx::TexMinFilter minFilter; - //! Magnification filter - Gfx::TexMagFilter magFilter; - - //! Constructor; calls LoadDefault() - TextureCreateParams() - { LoadDefault(); } - - //! Loads the default values - inline void LoadDefault() - { - format = Gfx::TEX_IMG_RGB; - mipmap = false; - - minFilter = Gfx::TEX_MIN_FILTER_NEAREST; - magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - } -}; - -/** - \struct TextureStageParams - \brief Parameters for a texture unit - - These params define the behavior of texturing units (stages). - They can be changed freely and are feature of graphics engine, not any particular texture. */ -struct TextureStageParams -{ - //! Mixing operation done on color values - Gfx::TexMixOperation colorOperation; - //! 1st argument of color operations - Gfx::TexMixArgument colorArg1; - //! 2nd argument of color operations - Gfx::TexMixArgument colorArg2; - //! Mixing operation done on alpha values - Gfx::TexMixOperation alphaOperation; - //! 1st argument of alpha operations - Gfx::TexMixArgument alphaArg1; - //! 2nd argument of alpha operations - Gfx::TexMixArgument alphaArg2; - //! Wrap mode for 1st tex coord - Gfx::TexWrapMode wrapS; - //! Wrap mode for 2nd tex coord - Gfx::TexWrapMode wrapT; - - //! Constructor; calls LoadDefault() - TextureStageParams() - { LoadDefault(); } - - //! Loads the default values - inline void LoadDefault() - { - colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; - colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; - colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - - alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; - alphaArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; - alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - - wrapS = wrapT = Gfx::TEX_WRAP_REPEAT; - } -}; - -/** - \struct Texture - \brief Info about a texture - - Identifies (through id) a texture created in graphics engine. - Also contains some additional data. */ -struct Texture -{ - //! Whether the texture (ID) is valid - bool valid; - //! ID of the texture in graphics engine - unsigned int id; - //! Width of texture - int width; - //! Height of texture - int height; - //! Whether the texture has alpha channel - bool alpha; - - Texture() - { - valid = false; - id = 0; - width = height = 0; - alpha = false; - } - - //! Comparator for use in texture maps and sets - inline bool operator<(const Gfx::Texture &other) const - { - // Invalid textures are always "less than" every other texture - - if ( (!valid) && (!other.valid) ) - return false; - - if (!valid) - return true; - - if (!other.valid) - return false; - - return id < other.id; - } - - //! Comparator - inline bool operator==(const Gfx::Texture &other) const - { - if (valid != other.valid) - return false; - if ( (!valid) && (!other.valid) ) - return true; - - return id == other.id; - } -}; - -}; // namespace Gfx diff --git a/src/graphics/common/vertex.h b/src/graphics/common/vertex.h deleted file mode 100644 index 0a74587..0000000 --- a/src/graphics/common/vertex.h +++ /dev/null @@ -1,141 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// vertex.h - -#pragma once - - -#include "graphics/common/color.h" -#include "math/vector.h" -#include "math/point.h" - -#include - -namespace Gfx { - -/** - * \struct Vertex - * \brief Vertex of a primitive - * - * This structure was created as analog to DirectX's D3DVERTEX. - * - * It contains: - * - vertex coordinates (x,y,z) as Math::Vector, - * - normal coordinates (nx,ny,nz) as Math::Vector - * - texture coordinates (u,v) as Math::Point. - */ -struct Vertex -{ - Math::Vector coord; - Math::Vector normal; - Math::Point texCoord; - - Vertex(Math::Vector aCoord = Math::Vector(), - Math::Vector aNormal = Math::Vector(), - Math::Point aTexCoord = Math::Point()) - : coord(aCoord), normal(aNormal), texCoord(aTexCoord) {} - - - //! Returns a string "(c: [...], n: [...], tc: [...])" - inline std::string ToString() const - { - std::stringstream s; - s.precision(3); - s << "(c: " << coord.ToString() << ", n: " << normal.ToString() - << ", tc: " << texCoord.ToString() << ")"; - return s.str(); - } -}; - -/** - * \struct VertexCol - * \brief Vertex with color information - * - * This structure was created as analog to DirectX's D3DLVERTEX. - * - * It contains: - * - vertex coordinates (x,y,z) as Math::Vector, - * - RGBA color as Gfx::Color, - * - RGBA specular color as Gfx::Color, - * - texture coordinates (u,v) as Math::Point. - */ -struct VertexCol -{ - Math::Vector coord; - Gfx::Color color; - Gfx::Color specular; - Math::Point texCoord; - - VertexCol(Math::Vector aCoord = Math::Vector(), - Gfx::Color aColor = Gfx::Color(), - Gfx::Color aSpecular = Gfx::Color(), - Math::Point aTexCoord = Math::Point()) - : coord(aCoord), color(aColor), specular(aSpecular), texCoord(aTexCoord) {} - - //! Returns a string "(c: [...], col: [...], sp: [...], tc: [...])" - inline std::string ToString() const - { - std::stringstream s; - s.precision(3); - s << "(c: " << coord.ToString() << ", col: " << color.ToString() << ", sp: " - << specular.ToString() << ", tc: " << texCoord.ToString() << ")"; - return s.str(); - } -}; - - -/** - * \struct VertexTex2 - * \brief Vertex with secondary texture coordinates - * - * In addition to fields from Gfx::Vector, it contains - * secondary texture coordinates (u2, v2) as Math::Point - */ -struct VertexTex2 -{ - Math::Vector coord; - Math::Vector normal; - Math::Point texCoord; - Math::Point texCoord2; - - VertexTex2(Math::Vector aCoord = Math::Vector(), - Math::Vector aNormal = Math::Vector(), - Math::Point aTexCoord = Math::Point(), - Math::Point aTexCoord2 = Math::Point()) - : coord(aCoord), normal(aNormal), texCoord(aTexCoord), texCoord2(aTexCoord2) {} - - //! Sets the fields from Gfx::Vertex with texCoord2 = (0,0) - void FromVertex(const Gfx::Vertex &v) - { - coord = v.coord; - normal = v.normal; - texCoord = v.texCoord; - texCoord2 = Math::Point(); - } - - //! Returns a string "(c: [...], n: [...], tc: [...], tc2: [...])" - inline std::string ToString() const - { - std::stringstream s; - s.precision(3); - s << "(c: " << coord.ToString() << ", n: " << normal.ToString() - << ", tc: " << texCoord.ToString() << ", tc2: " << texCoord2.ToString() << ")"; - return s.str(); - } -}; - -}; // namespace Gfx diff --git a/src/graphics/common/water.cpp b/src/graphics/common/water.cpp deleted file mode 100644 index 5172b9f..0000000 --- a/src/graphics/common/water.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// water.cpp - -#include "graphics/common/water.h" - - -// TODO implementation diff --git a/src/graphics/common/water.h b/src/graphics/common/water.h deleted file mode 100644 index f5224a4..0000000 --- a/src/graphics/common/water.h +++ /dev/null @@ -1,134 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// water.h - -#pragma once - -#include "graphics/common/engine.h" -#include "graphics/common/particle.h" -#include "common/event.h" - - -class CInstanceManager; -class CSound; - - -namespace Gfx { - -class CTerrain; - - -const short MAXWATERLINE = 500; - -struct WaterLine -{ - short x, y; // beginning - short len; // length by x - float px1, px2, pz; -}; - - -const short MAXWATVAPOR = 10; - -struct WaterVapor -{ - bool bUsed; - ParticleType type; - Math::Vector pos; - float delay; - float time; - float last; -}; - - -enum WaterType -{ - WATER_NULL = 0, // no water - WATER_TT = 1, // transparent texture - WATER_TO = 2, // opaque texture - WATER_CT = 3, // transparent color - WATER_CO = 4, // opaque color -}; - - -class CWater -{ -public: - CWater(CInstanceManager* iMan, Gfx::CEngine* engine); - ~CWater(); - - void SetGLDevice(Gfx::CDevice device); - bool EventProcess(const Event &event); - void Flush(); - bool Create(WaterType type1, WaterType type2, const char *filename, Gfx::Color diffuse, Gfx::Color ambient, float level, float glint, Math::Vector eddy); - void DrawBack(); - void DrawSurf(); - - bool SetLevel(float level); - float RetLevel(); - float RetLevel(CObject* object); - - void SetLava(bool bLava); - bool RetLava(); - - void AdjustEye(Math::Vector &eye); - -protected: - bool EventFrame(const Event &event); - void LavaFrame(float rTime); - void AdjustLevel(Math::Vector &pos, Math::Vector &norm, Math::Point &uv1, Math::Point &uv2); - bool RetWater(int x, int y); - bool CreateLine(int x, int y, int len); - - void VaporFlush(); - bool VaporCreate(ParticleType type, Math::Vector pos, float delay); - void VaporFrame(int i, float rTime); - -protected: - CInstanceManager* m_iMan; - CEngine* m_engine; - CDevice* m_pDevice; - CTerrain* m_terrain; - CParticle* m_particule; - CSound* m_sound; - - WaterType m_type[2]; - char m_filename[100]; - float m_level; // overall level - float m_glint; // amplitude of reflections - Math::Vector m_eddy; // amplitude of swirls - Gfx::Color m_diffuse; // diffuse color - Gfx::Color m_ambient; // ambient color - float m_time; - float m_lastLava; - int m_subdiv; - - int m_brick; // number of brick*mosaics - float m_size; // size of a item in an brick - - int m_lineUsed; - WaterLine m_line[MAXWATERLINE]; - - WaterVapor m_vapor[MAXWATVAPOR]; - - bool m_bDraw; - bool m_bLava; - long m_color; -}; - -}; // namespace Gfx diff --git a/src/graphics/core/README.txt b/src/graphics/core/README.txt new file mode 100644 index 0000000..12beef9 --- /dev/null +++ b/src/graphics/core/README.txt @@ -0,0 +1,6 @@ +src/graphics/core + +Abstract core of graphics engine + +Core types, enums, structs and CDevice abstract class that define +the abstract graphics device used in graphics engine diff --git a/src/graphics/core/color.cpp b/src/graphics/core/color.cpp new file mode 100644 index 0000000..8dec0e4 --- /dev/null +++ b/src/graphics/core/color.cpp @@ -0,0 +1,103 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// color.cpp + +#include "graphics/core/color.h" + +#include "math/func.h" + + +Gfx::ColorHSV Gfx::RGB2HSV(Gfx::Color color) +{ + Gfx::ColorHSV result; + + float min = Math::Min(color.r, color.g, color.b); + float max = Math::Max(color.r, color.g, color.b); + + result.v = max; // intensity + + if ( max == 0.0f ) + { + result.s = 0.0f; // saturation + result.h = 0.0f; // undefined color! + } + else + { + float delta = max-min; + result.s = delta/max; // saturation + + if ( color.r == max ) // between yellow & magenta + { + result.h = (color.g-color.b)/delta; + } + else if ( color.g == max ) // between cyan & yellow + { + result.h = 2.0f+(color.b-color.r)/delta; + } + else // between magenta & cyan + { + result.h = 4.0f+(color.r-color.g)/delta; + } + + result.h *= 60.0f; // in degrees + if ( result.h < 0.0f ) result.h += 360.0f; + result.h /= 360.0f; // 0..1 + } + + return result; +} + +Gfx::Color Gfx::HSV2RGB(Gfx::ColorHSV color) +{ + Gfx::Color result; + + color.h = Math::Norm(color.h)*360.0f; + color.s = Math::Norm(color.s); + color.v = Math::Norm(color.v); + + if ( color.s == 0.0f ) // zero saturation? + { + result.r = color.v; + result.g = color.v; + result.b = color.v; // gray + } + else + { + if ( color.h == 360.0f ) color.h = 0.0f; + color.h /= 60.0f; + int i = (int)color.h; // integer part (0 .. 5) + float f = color.h-i; // fractional part + + float v = color.v; + float p = color.v*(1.0f-color.s); + float q = color.v*(1.0f-(color.s*f)); + float t = color.v*(1.0f-(color.s*(1.0f-f))); + + switch (i) + { + case 0: result.r=v; result.g=t; result.b=p; break; + case 1: result.r=q; result.g=v; result.b=p; break; + case 2: result.r=p; result.g=v; result.b=t; break; + case 3: result.r=p; result.g=q; result.b=v; break; + case 4: result.r=t; result.g=p; result.b=v; break; + case 5: result.r=v; result.g=p; result.b=q; break; + } + } + + return result; +} + diff --git a/src/graphics/core/color.h b/src/graphics/core/color.h new file mode 100644 index 0000000..907a3b9 --- /dev/null +++ b/src/graphics/core/color.h @@ -0,0 +1,98 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// color.h + +#pragma once + + +#include + + +namespace Gfx { + +/** + \struct Color + \brief RGBA color */ +struct Color +{ + //! Red, green, blue and alpha components + float r, g, b, a; + + //! Constructor; default values are (0,0,0,0) = black + Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f) + : r(aR), g(aG), b(aB), a(aA) {} + + inline Gfx::Color Inverse() const + { + return Gfx::Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a); + } + + //! Returns the struct cast to \c float* array; use with care! + inline float* Array() + { + return (float*)this; + } + + //! Returns the struct cast to const float* array; use with care! + inline const float* Array() const + { + return (const float*)this; + } + + //! Returns a string (r, g, b, a) + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(" << r << ", " << g << ", " << b << ", " << a << ")"; + return s.str(); + } + + inline bool operator==(const Gfx::Color &other) const + { + return r == other.r && g == other.g && b == other.b && a == other.a; + } +}; + +/** + \struct ColorHSV + \brief HSV color */ +struct ColorHSV +{ + float h, s, v; + + ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f) + : h(aH), s(aS), v(aV) {} + + //! Returns a string "(h, s, v)" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(" << h << ", " << s << ", " << v << ")"; + return s.str(); + } +}; + +//! Converts a RGB color to HSV color +Gfx::ColorHSV RGB2HSV(Gfx::Color color); + +//! Converts a HSV color to RGB color +Gfx::Color HSV2RGB(Gfx::ColorHSV color); + +}; // namespace Gfx + diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h new file mode 100644 index 0000000..ae612b7 --- /dev/null +++ b/src/graphics/core/device.h @@ -0,0 +1,414 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// device.h + +#pragma once + + +#include "graphics/core/color.h" +#include "graphics/core/light.h" +#include "graphics/core/material.h" +#include "graphics/core/texture.h" +#include "graphics/core/vertex.h" +#include "math/matrix.h" + +#include + + +class CImage; + + +namespace Gfx { + +/** + \struct DeviceConfig + \brief General config for graphics device + + These settings are common window options set by SDL. +*/ +struct DeviceConfig +{ + //! Screen width + int width; + //! Screen height + int height; + //! Bits per pixel + int bpp; + //! Full screen + bool fullScreen; + //! Resizeable window + bool resizeable; + //! Double buffering + bool doubleBuf; + //! No window frame (also set with full screen) + bool noFrame; + + //! Constructor calls LoadDefault() + DeviceConfig() { LoadDefault(); } + + //! Loads the default values + inline void LoadDefault() + { + width = 800; + height = 600; + bpp = 32; + fullScreen = false; + resizeable = false; + doubleBuf = true; + noFrame = false; + } +}; + + +/** + \enum TransformType + \brief Type of transformation in rendering pipeline + + These correspond to DirectX's three transformation matrices. */ +enum TransformType +{ + TRANSFORM_WORLD, + TRANSFORM_VIEW, + TRANSFORM_PROJECTION +}; + +/** + \enum RenderState + \brief Render states that can be enabled/disabled */ +enum RenderState +{ + RENDER_STATE_LIGHTING, + RENDER_STATE_TEXTURING, + RENDER_STATE_BLENDING, + RENDER_STATE_FOG, + RENDER_STATE_DEPTH_TEST, + RENDER_STATE_DEPTH_WRITE, + RENDER_STATE_ALPHA_TEST, + RENDER_STATE_CULLING, + RENDER_STATE_DITHERING +}; + +/** + \enum CompFunc + \brief Type of function used to compare values */ +enum CompFunc +{ + COMP_FUNC_NEVER, + COMP_FUNC_LESS, + COMP_FUNC_EQUAL, + COMP_FUNC_NOTEQUAL, + COMP_FUNC_LEQUAL, + COMP_FUNC_GREATER, + COMP_FUNC_GEQUAL, + COMP_FUNC_ALWAYS +}; + +/** + \enum BlendFunc + \brief Type of blending function */ +enum BlendFunc +{ + BLEND_ZERO, + BLEND_ONE, + BLEND_SRC_COLOR, + BLEND_INV_SRC_COLOR, + BLEND_DST_COLOR, + BLEND_INV_DST_COLOR, + BLEND_SRC_ALPHA, + BLEND_INV_SRC_ALPHA, + BLEND_DST_ALPHA, + BLEND_INV_DST_ALPHA, + BLEND_SRC_ALPHA_SATURATE +}; + +/** + \enum FogMode + \brief Type of fog calculation function */ +enum FogMode +{ + FOG_LINEAR, + FOG_EXP, + FOG_EXP2 +}; + +/** + \enum CullMode + \brief Culling mode for polygons */ +enum CullMode +{ + //! Cull clockwise side + CULL_CW, + //! Cull counter-clockwise side + CULL_CCW +}; + +/** + \enum ShadeModel + \brief Shade model used in rendering */ +enum ShadeModel +{ + SHADE_FLAT, + SHADE_SMOOTH +}; + +/** + \enum FillMode + \brief Polygon fill mode */ +enum FillMode +{ + //! Draw only points + FILL_POINT, + //! Draw only lines + FILL_LINES, + //! Draw full polygons + FILL_FILL +}; + +/** + \enum PrimitiveType + \brief Type of primitive to render + + Only these two types are used. */ +enum PrimitiveType +{ + PRIMITIVE_LINES, + PRIMITIVE_TRIANGLES, + PRIMITIVE_TRIANGLE_STRIP +}; + +/** + \enum IntersectPlane + \brief Intersection plane of projection volume + + These flags can be OR'd together. */ +enum IntersectPlane +{ + INTERSECT_PLANE_LEFT = 0x01, + INTERSECT_PLANE_RIGHT = 0x02, + INTERSECT_PLANE_TOP = 0x04, + INTERSECT_PLANE_BOTTOM = 0x08, + INTERSECT_PLANE_FRONT = 0x10, + INTERSECT_PLANE_BACK = 0x20, + INTERSECT_PLANE_ALL = INTERSECT_PLANE_LEFT | INTERSECT_PLANE_RIGHT | + INTERSECT_PLANE_TOP | INTERSECT_PLANE_BOTTOM | + INTERSECT_PLANE_FRONT | INTERSECT_PLANE_BACK +}; + +/* + +Notes for rewriting DirectX code: + +>> SetRenderState() translates to many functions depending on param + +D3DRENDERSTATE_ALPHABLENDENABLE -> SetRenderState() with RENDER_STATE_BLENDING +D3DRENDERSTATE_ALPHAFUNC -> SetAlphaTestFunc() func +D3DRENDERSTATE_ALPHAREF -> SetAlphaTestFunc() ref +D3DRENDERSTATE_ALPHATESTENABLE -> SetRenderState() with RENDER_STATE_ALPHA_TEST +D3DRENDERSTATE_AMBIENT -> SetGlobalAmbient() +D3DRENDERSTATE_CULLMODE -> SetCullMode() +D3DRENDERSTATE_DESTBLEND -> SetBlendFunc() dest blending func +D3DRENDERSTATE_DITHERENABLE -> SetRenderState() with RENDER_STATE_DITHERING +D3DRENDERSTATE_FILLMODE -> SetFillMode() +D3DRENDERSTATE_FOGCOLOR -> SetFogParams() +D3DRENDERSTATE_FOGENABLE -> SetRenderState() with RENDER_STATE_FOG +D3DRENDERSTATE_FOGEND -> SetFogParams() +D3DRENDERSTATE_FOGSTART -> SetFogParams() +D3DRENDERSTATE_FOGVERTEXMODE -> SetFogParams() fog model +D3DRENDERSTATE_LIGHTING -> SetRenderState() with RENDER_STATE_LIGHTING +D3DRENDERSTATE_SHADEMODE -> SetShadeModel() +D3DRENDERSTATE_SPECULARENABLE -> doesn't matter (always enabled) +D3DRENDERSTATE_SRCBLEND -> SetBlendFunc() src blending func +D3DRENDERSTATE_TEXTUREFACTOR -> SetTextureFactor() +D3DRENDERSTATE_ZBIAS -> SetDepthBias() +D3DRENDERSTATE_ZENABLE -> SetRenderState() with RENDER_STATE_DEPTH_TEST +D3DRENDERSTATE_ZFUNC -> SetDepthTestFunc() +D3DRENDERSTATE_ZWRITEENABLE -> SetRenderState() with RENDER_STATE_DEPTH_WRITE + + +>> SetTextureStageState() translates to SetTextureParams() or CreateTexture() for some params + +Params from enum in struct TextureCreateParams or TextureParams + D3DTSS_ADDRESS -> Gfx::TexWrapMode wrapS, wrapT + D3DTSS_ALPHAARG1 -> Gfx::TexMixArgument alphaArg1 + D3DTSS_ALPHAARG2 -> Gfx::TexMixArgument alphaArg2 + D3DTSS_ALPHAOP -> Gfx::TexMixOperation alphaOperation + D3DTSS_COLORARG1 -> Gfx::TexMixArgument colorArg1 + D3DTSS_COLORARG2 -> Gfx::TexMixArgument colorArg2 + D3DTSS_COLOROP -> Gfx::TexMixOperation colorOperation + D3DTSS_MAGFILTER -> Gfx::TexMagFilter magFilter + D3DTSS_MINFILTER -> Gfx::TexMinFilter minFilter + D3DTSS_TEXCOORDINDEX -> doesn't matter (texture coords are set explicitly by glMultiTexCoordARB*) + +Note that D3DTSS_ALPHAOP or D3DTSS_COLOROP set to D3DTOP_DISABLE must translate to disabling the whole texture stage. +In DirectX, you shouldn't mix enabling one and disabling the other. +Also, if previous stage is disabled in DirectX, the later ones are disabled, too. In OpenGL, that is not the case. + +*/ + +/** + \class CDevice + \brief Abstract interface of graphics device + + It is based on DIRECT3DDEVICE class from DirectX to make it easier to port existing code. + It encapsulates the general graphics device state and provides a common interface + to graphics-specific functions which will be used throughout the program, + both in CEngine class and in UI classes. Note that it doesn't contain all functions from DirectX, + only those that were used in old code. + + */ +class CDevice +{ +public: + virtual ~CDevice() {} + + //! Initializes the device, setting the initial state + virtual bool Create() = 0; + //! Destroys the device, releasing every acquired resource + virtual void Destroy() = 0; + + //! Returns whether the device has been initialized + virtual bool GetWasInit() = 0; + //! Returns the last encountered error + virtual std::string GetError() = 0; + + //! Begins drawing the 3D scene + virtual void BeginScene() = 0; + //! Ends drawing the 3D scene + virtual void EndScene() = 0; + + //! Clears the screen to blank + virtual void Clear() = 0; + + //! Sets the transform matrix of given type + virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0; + //! Returns the current transform matrix of given type + virtual const Math::Matrix& GetTransform(TransformType type) = 0; + //! Multiplies the current transform matrix of given type by given matrix + virtual void MultiplyTransform(TransformType type, const Math::Matrix &matrix) = 0; + + //! Sets the current material + virtual void SetMaterial(const Gfx::Material &material) = 0; + //! Returns the current material + virtual const Gfx::Material& GetMaterial() = 0; + + //! Returns the maximum number of lights available + virtual int GetMaxLightCount() = 0; + //! Sets the light at given index + virtual void SetLight(int index, const Gfx::Light &light) = 0; + //! Returns the current light at given index + virtual const Gfx::Light& GetLight(int index) = 0; + //! Enables/disables the light at given index + virtual void SetLightEnabled(int index, bool enabled) = 0; + //! Returns the current enable state of light at given index + virtual bool GetLightEnabled(int index) = 0; + + //! Creates a texture from image; the image can be safely removed after that + virtual Gfx::Texture CreateTexture(CImage *image, const Gfx::TextureCreateParams ¶ms) = 0; + //! Deletes a given texture, freeing it from video memory + virtual void DestroyTexture(const Gfx::Texture &texture) = 0; + //! Deletes all textures created so far + virtual void DestroyAllTextures() = 0; + + //! Returns the maximum number of multitexture stages + virtual int GetMaxTextureCount() = 0; + //! Sets the (multi)texture at given index + virtual void SetTexture(int index, const Gfx::Texture &texture) = 0; + //! Returns the (multi)texture at given index + virtual Gfx::Texture GetTexture(int index) = 0; + //! Enables/disables the given texture stage + virtual void SetTextureEnabled(int index, bool enabled) = 0; + //! Returns the current enable state of given texture stage + virtual bool GetTextureEnabled(int index) = 0; + + //! Sets the params for texture stage with given index + virtual void SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms) = 0; + //! Returns the current params of texture stage with given index + virtual Gfx::TextureStageParams GetTextureStageParams(int index) = 0; + + //! Sets the texture factor to the given color value + virtual void SetTextureFactor(const Gfx::Color &color) = 0; + //! Returns the current texture factor + virtual Gfx::Color GetTextureFactor() = 0; + + //! Renders primitive composed of vertices with single texture + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices , int vertexCount) = 0; + //! Renders primitive composed of vertices with color information and single texture + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices , int vertexCount) = 0; + //! Renders primitive composed of vertices with multitexturing (2 textures) + virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount) = 0; + + //! Tests whether a sphere intersects the 6 clipping planes of projection volume + virtual int ComputeSphereVisibility(const Math::Vector ¢er, float radius) = 0; + + //! Enables/disables the given render state + virtual void SetRenderState(Gfx::RenderState state, bool enabled) = 0; + //! Returns the current setting of given render state + virtual bool GetRenderState(Gfx::RenderState state) = 0; + + //! Sets the function of depth test + virtual void SetDepthTestFunc(Gfx::CompFunc func) = 0; + //! Returns the current function of depth test + virtual Gfx::CompFunc GetDepthTestFunc() = 0; + + //! Sets the depth bias (constant value added to Z-coords) + virtual void SetDepthBias(float factor) = 0; + //! Returns the current depth bias + virtual float GetDepthBias() = 0; + + //! Sets the alpha test function and reference value + virtual void SetAlphaTestFunc(Gfx::CompFunc func, float refValue) = 0; + //! Returns the current alpha test function and reference value + virtual void GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue) = 0; + + //! Sets the blending functions for source and destination operations + virtual void SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBlend) = 0; + //! Returns the current blending functions for source and destination operations + virtual void GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) = 0; + + //! Sets the clear color + virtual void SetClearColor(const Gfx::Color &color) = 0; + //! Returns the current clear color + virtual Gfx::Color GetClearColor() = 0; + + //! Sets the global ambient color + virtual void SetGlobalAmbient(const Gfx::Color &color) = 0; + //! Returns the global ambient color + virtual Gfx::Color GetGlobalAmbient() = 0; + + //! Sets the fog parameters: mode, color, start distance, end distance and density (for exp models) + virtual void SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, float start, float end, float density) = 0; + //! Returns the current fog parameters: mode, color, start distance, end distance and density (for exp models) + virtual void GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) = 0; + + //! Sets the current cull mode + virtual void SetCullMode(Gfx::CullMode mode) = 0; + //! Returns the current cull mode + virtual Gfx::CullMode GetCullMode() = 0; + + //! Sets the shade model + virtual void SetShadeModel(Gfx::ShadeModel model) = 0; + //! Returns the current shade model + virtual Gfx::ShadeModel GetShadeModel() = 0; + + //! Sets the current fill mode + virtual void SetFillMode(Gfx::FillMode mode) = 0; + //! Returns the current fill mode + virtual Gfx::FillMode GetFillMode() = 0; +}; + +}; // namespace Gfx diff --git a/src/graphics/core/light.h b/src/graphics/core/light.h new file mode 100644 index 0000000..b787cb2 --- /dev/null +++ b/src/graphics/core/light.h @@ -0,0 +1,91 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// light.h + +#pragma once + + +#include "graphics/core/color.h" +#include "math/vector.h" + + +namespace Gfx { + +/** + \enum LightType + \brief Type of light in 3D scene */ +enum LightType +{ + LIGHT_POINT, + LIGHT_SPOT, + LIGHT_DIRECTIONAL +}; + +/** + \struct Light + \brief Properties of light in 3D scene + + This structure was created as analog to DirectX's D3DLIGHT. */ +struct Light +{ + //! Type of light source + Gfx::LightType type; + //! Color of ambient light + Gfx::Color ambient; + //! Color of diffuse light + Gfx::Color diffuse; + //! Color of specular light + Gfx::Color specular; + //! Position in world space (for point & spot lights) + Math::Vector position; + //! Direction in world space (for directional & spot lights) + Math::Vector direction; + //! Constant attenuation factor + float attenuation0; + //! Linear attenuation factor + float attenuation1; + //! Quadratic attenuation factor + float attenuation2; + //! Angle of spotlight cone (0-90 degrees) + float spotAngle; + //! Intensity of spotlight (0 = uniform; 128 = most intense) + float spotIntensity; + + //! Constructor; calls LoadDefault() + Light() + { + LoadDefault(); + } + + //! Loads default values + void LoadDefault() + { + type = LIGHT_POINT; + ambient = Gfx::Color(0.4f, 0.4f, 0.4f); + diffuse = Gfx::Color(0.8f, 0.8f, 0.8f); + specular = Gfx::Color(1.0f, 1.0f, 1.0f); + position = Math::Vector(0.0f, 0.0f, 0.0f); + direction = Math::Vector(0.0f, 0.0f, 1.0f); + attenuation0 = 1.0f; + attenuation1 = attenuation2 = 0.0f; + spotAngle = 90.0f; + spotIntensity = 0.0f; + } +}; + +}; // namespace Gfx diff --git a/src/graphics/core/material.h b/src/graphics/core/material.h new file mode 100644 index 0000000..31b42f3 --- /dev/null +++ b/src/graphics/core/material.h @@ -0,0 +1,47 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// material.h + +#pragma once + + +#include "graphics/core/color.h" + + +namespace Gfx { + +/** + * \struct Material + * \brief Material of a surface + * + * This structure was created as analog to DirectX's D3DMATERIAL. + * + * It contains values of 3 material colors: diffuse, ambient and specular. + * In D3DMATERIAL there are other fields, but they are not used + * by the graphics engine. + */ +struct Material +{ + //! Diffuse color + Gfx::Color diffuse; + //! Ambient color + Gfx::Color ambient; + //! Specular color + Gfx::Color specular; +}; + +}; // namespace Gfx diff --git a/src/graphics/core/texture.h b/src/graphics/core/texture.h new file mode 100644 index 0000000..787c2bf --- /dev/null +++ b/src/graphics/core/texture.h @@ -0,0 +1,237 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// texture.h + +#pragma once + +namespace Gfx { + +/** + \enum TexImgFormat + \brief Format of image data */ +enum TexImgFormat +{ + //! Try to determine automatically (may not work) + TEX_IMG_AUTO, + //! RGB triplet, 3 bytes + TEX_IMG_RGB, + //! BGR triplet, 3 bytes + TEX_IMG_BGR, + //! RGBA triplet, 4 bytes + TEX_IMG_RGBA, + //! BGRA triplet, 4 bytes + TEX_IMG_BGRA +}; + +/** + \enum TexMinFilter + \brief Texture minification filter + + Corresponds to OpenGL modes but should translate to DirectX too. */ +enum TexMinFilter +{ + TEX_MIN_FILTER_NEAREST, + TEX_MIN_FILTER_LINEAR, + TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST, + TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST, + TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR, + TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR +}; + +/** + \enum TexMagFilter + \brief Texture magnification filter */ +enum TexMagFilter +{ + TEX_MAG_FILTER_NEAREST, + TEX_MAG_FILTER_LINEAR +}; + +/** + \enum TexWrapMode + \brief Wrapping mode for texture coords */ +enum TexWrapMode +{ + TEX_WRAP_CLAMP, + TEX_WRAP_REPEAT +}; + +/** + \enum TexMixOperation + \brief Multitexture mixing operation */ +enum TexMixOperation +{ + //! Default operation on default params (modulate on computed & texture) + TEX_MIX_OPER_DEFAULT, + //! = Arg1 + TEX_MIX_OPER_REPLACE, + //! = Arg1 * Arg2 + TEX_MIX_OPER_MODULATE, + //! = Arg1 + Arg2 + TEX_MIX_OPER_ADD, + //! = Arg1 - Arg2 + TEX_MIX_OPER_SUBTRACT +}; + +/** + \enum TexMixArgument + \brief Multitexture mixing argument */ +enum TexMixArgument +{ + //! Color from current texture + TEX_MIX_ARG_TEXTURE, + //! Color computed by previous texture unit (current in DirectX; previous in OpenGL) + TEX_MIX_ARG_COMPUTED_COLOR, + //! (Source) color of textured fragment (diffuse in DirectX; primary color in OpenGL) + TEX_MIX_ARG_SRC_COLOR, + //! Constant color (texture factor in DirectX; texture env color in OpenGL) + TEX_MIX_ARG_FACTOR +}; + +/** + \struct TextureCreateParams + \brief Parameters for texture creation + + These params define how particular texture is created and later displayed. + They must be specified at texture creation time and cannot be changed later. */ +struct TextureCreateParams +{ + //! Whether to generate mipmaps + bool mipmap; + //! Format of source image data + Gfx::TexImgFormat format; + //! Minification filter + Gfx::TexMinFilter minFilter; + //! Magnification filter + Gfx::TexMagFilter magFilter; + + //! Constructor; calls LoadDefault() + TextureCreateParams() + { LoadDefault(); } + + //! Loads the default values + inline void LoadDefault() + { + format = Gfx::TEX_IMG_RGB; + mipmap = false; + + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + } +}; + +/** + \struct TextureStageParams + \brief Parameters for a texture unit + + These params define the behavior of texturing units (stages). + They can be changed freely and are feature of graphics engine, not any particular texture. */ +struct TextureStageParams +{ + //! Mixing operation done on color values + Gfx::TexMixOperation colorOperation; + //! 1st argument of color operations + Gfx::TexMixArgument colorArg1; + //! 2nd argument of color operations + Gfx::TexMixArgument colorArg2; + //! Mixing operation done on alpha values + Gfx::TexMixOperation alphaOperation; + //! 1st argument of alpha operations + Gfx::TexMixArgument alphaArg1; + //! 2nd argument of alpha operations + Gfx::TexMixArgument alphaArg2; + //! Wrap mode for 1st tex coord + Gfx::TexWrapMode wrapS; + //! Wrap mode for 2nd tex coord + Gfx::TexWrapMode wrapT; + + //! Constructor; calls LoadDefault() + TextureStageParams() + { LoadDefault(); } + + //! Loads the default values + inline void LoadDefault() + { + colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + alphaArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; + alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + wrapS = wrapT = Gfx::TEX_WRAP_REPEAT; + } +}; + +/** + \struct Texture + \brief Info about a texture + + Identifies (through id) a texture created in graphics engine. + Also contains some additional data. */ +struct Texture +{ + //! Whether the texture (ID) is valid + bool valid; + //! ID of the texture in graphics engine + unsigned int id; + //! Width of texture + int width; + //! Height of texture + int height; + //! Whether the texture has alpha channel + bool alpha; + + Texture() + { + valid = false; + id = 0; + width = height = 0; + alpha = false; + } + + //! Comparator for use in texture maps and sets + inline bool operator<(const Gfx::Texture &other) const + { + // Invalid textures are always "less than" every other texture + + if ( (!valid) && (!other.valid) ) + return false; + + if (!valid) + return true; + + if (!other.valid) + return false; + + return id < other.id; + } + + //! Comparator + inline bool operator==(const Gfx::Texture &other) const + { + if (valid != other.valid) + return false; + if ( (!valid) && (!other.valid) ) + return true; + + return id == other.id; + } +}; + +}; // namespace Gfx diff --git a/src/graphics/core/vertex.h b/src/graphics/core/vertex.h new file mode 100644 index 0000000..b7fab1c --- /dev/null +++ b/src/graphics/core/vertex.h @@ -0,0 +1,141 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// vertex.h + +#pragma once + + +#include "graphics/core/color.h" +#include "math/vector.h" +#include "math/point.h" + +#include + +namespace Gfx { + +/** + * \struct Vertex + * \brief Vertex of a primitive + * + * This structure was created as analog to DirectX's D3DVERTEX. + * + * It contains: + * - vertex coordinates (x,y,z) as Math::Vector, + * - normal coordinates (nx,ny,nz) as Math::Vector + * - texture coordinates (u,v) as Math::Point. + */ +struct Vertex +{ + Math::Vector coord; + Math::Vector normal; + Math::Point texCoord; + + Vertex(Math::Vector aCoord = Math::Vector(), + Math::Vector aNormal = Math::Vector(), + Math::Point aTexCoord = Math::Point()) + : coord(aCoord), normal(aNormal), texCoord(aTexCoord) {} + + + //! Returns a string "(c: [...], n: [...], tc: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", n: " << normal.ToString() + << ", tc: " << texCoord.ToString() << ")"; + return s.str(); + } +}; + +/** + * \struct VertexCol + * \brief Vertex with color information + * + * This structure was created as analog to DirectX's D3DLVERTEX. + * + * It contains: + * - vertex coordinates (x,y,z) as Math::Vector, + * - RGBA color as Gfx::Color, + * - RGBA specular color as Gfx::Color, + * - texture coordinates (u,v) as Math::Point. + */ +struct VertexCol +{ + Math::Vector coord; + Gfx::Color color; + Gfx::Color specular; + Math::Point texCoord; + + VertexCol(Math::Vector aCoord = Math::Vector(), + Gfx::Color aColor = Gfx::Color(), + Gfx::Color aSpecular = Gfx::Color(), + Math::Point aTexCoord = Math::Point()) + : coord(aCoord), color(aColor), specular(aSpecular), texCoord(aTexCoord) {} + + //! Returns a string "(c: [...], col: [...], sp: [...], tc: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", col: " << color.ToString() << ", sp: " + << specular.ToString() << ", tc: " << texCoord.ToString() << ")"; + return s.str(); + } +}; + + +/** + * \struct VertexTex2 + * \brief Vertex with secondary texture coordinates + * + * In addition to fields from Gfx::Vector, it contains + * secondary texture coordinates (u2, v2) as Math::Point + */ +struct VertexTex2 +{ + Math::Vector coord; + Math::Vector normal; + Math::Point texCoord; + Math::Point texCoord2; + + VertexTex2(Math::Vector aCoord = Math::Vector(), + Math::Vector aNormal = Math::Vector(), + Math::Point aTexCoord = Math::Point(), + Math::Point aTexCoord2 = Math::Point()) + : coord(aCoord), normal(aNormal), texCoord(aTexCoord), texCoord2(aTexCoord2) {} + + //! Sets the fields from Gfx::Vertex with texCoord2 = (0,0) + void FromVertex(const Gfx::Vertex &v) + { + coord = v.coord; + normal = v.normal; + texCoord = v.texCoord; + texCoord2 = Math::Point(); + } + + //! Returns a string "(c: [...], n: [...], tc: [...], tc2: [...])" + inline std::string ToString() const + { + std::stringstream s; + s.precision(3); + s << "(c: " << coord.ToString() << ", n: " << normal.ToString() + << ", tc: " << texCoord.ToString() << ", tc2: " << texCoord2.ToString() << ")"; + return s.str(); + } +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/README.txt b/src/graphics/engine/README.txt new file mode 100644 index 0000000..308b601 --- /dev/null +++ b/src/graphics/engine/README.txt @@ -0,0 +1,8 @@ +src/graphics/engine + +Graphics engine + +CEngine class and various other classes implementing the main features +of graphics engine from model loading to decorative particles + +Graphics operations are done on abstract interface from src/graphics/core diff --git a/src/graphics/engine/camera.cpp b/src/graphics/engine/camera.cpp new file mode 100644 index 0000000..04bf868 --- /dev/null +++ b/src/graphics/engine/camera.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// camera.cpp + +#include "graphics/engine/camera.h" + + +// TODO implementation diff --git a/src/graphics/engine/camera.h b/src/graphics/engine/camera.h new file mode 100644 index 0000000..76077bf --- /dev/null +++ b/src/graphics/engine/camera.h @@ -0,0 +1,270 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// camera.h + +#pragma once + +#include "engine.h" +#include "common/event.h" + + +class CInstanceManager; +class CObject; + +namespace Gfx { + + +enum CameraType +{ + CAMERA_NULL = 0, // camera undefined + CAMERA_FREE = 1, // camera free (never in principle) + CAMERA_EDIT = 2, // camera while editing a program + CAMERA_ONBOARD = 3, // camera on board a robot + CAMERA_BACK = 4, // camera behind a robot + CAMERA_FIX = 5, // static camera following robot + CAMERA_EXPLO = 6, // camera steady after explosion + CAMERA_SCRIPT = 7, // camera during a film script + CAMERA_INFO = 8, // camera for displaying information + CAMERA_VISIT = 9, // visit instead of an error + CAMERA_DIALOG = 10, // camera for dialogue + CAMERA_PLANE = 11, // static camera height +}; + +enum CameraSmooth +{ + CS_NONE = 0, // sharp + CS_NORM = 1, // normal + CS_HARD = 2, // hard + CS_SPEC = 3, // special +}; + +enum CenteringPhase +{ + CP_NULL = 0, + CP_START = 1, + CP_WAIT = 2, + CP_STOP = 3, +}; + +enum CameraEffect +{ + CE_NULL = 0, // no effect + CE_TERRAFORM = 1, // digging in + CE_CRASH = 2, // Vehicle driving is severely + CE_EXPLO = 3, // explosion + CE_SHOT = 4, // not mortal shot + CE_VIBRATION = 5, // vibration during construction + CE_PET = 6, // spleen reactor +}; + +enum OverEffect +{ + OE_NULL = 0, // no effect + OE_BLOOD = 1, // flash red + OE_FADEINw = 2, // white -> nothing + OE_FADEOUTw = 3, // nothing -> white + OE_FADEOUTb = 4, // nothing -> blue + OE_BLITZ = 5, // lightning +}; + + + +class CCamera { + + public: + CCamera(CInstanceManager* iMan); + ~CCamera(); + + bool EventProcess(const Event &event); + + void Init(Math::Vector eye, Math::Vector lookat, float delay); + + void SetObject(CObject* object); + CObject* RetObject(); + + void SetType(CameraType type); + CameraType RetType(); + + void SetSmooth(CameraSmooth type); + CameraSmooth RetSmoth(); + + void SetDist(float dist); + float RetDist(); + + void SetFixDirection(float angle); + float RetFixDirection(); + + void SetRemotePan(float value); + float RetRemotePan(); + + void SetRemoteZoom(float value); + float RetRemoteZoom(); + + void StartVisit(Math::Vector goal, float dist); + void StopVisit(); + + void RetCamera(Math::Vector &eye, Math::Vector &lookat); + + bool StartCentering(CObject *object, float angleH, float angleV, float dist, float time); + bool StopCentering(CObject *object, float time); + void AbortCentering(); + + void FlushEffect(); + void StartEffect(CameraEffect effect, Math::Vector pos, float force); + + void FlushOver(); + void SetOverBaseColor(Gfx::Color color); + void StartOver(OverEffect effect, Math::Vector pos, float force); + + void FixCamera(); + void SetScriptEye(Math::Vector eye); + void SetScriptLookat(Math::Vector lookat); + + void SetEffect(bool bEnable); + void SetCameraScroll(bool bScroll); + void SetCameraInvertX(bool bInvert); + void SetCameraInvertY(bool bInvert); + + float RetMotorTurn(); + Gfx::EngineMouseType RetMouseDef(Math::Point pos); + +protected: + bool EventMouseMove(const Event &event); + void EventMouseWheel(int dir); + bool EventFrame(const Event &event); + bool EventFrameFree(const Event &event); + bool EventFrameEdit(const Event &event); + bool EventFrameDialog(const Event &event); + bool EventFrameBack(const Event &event); + bool EventFrameFix(const Event &event); + bool EventFrameExplo(const Event &event); + bool EventFrameOnBoard(const Event &event); + bool EventFrameInfo(const Event &event); + bool EventFrameVisit(const Event &event); + bool EventFrameScript(const Event &event); + + void SetViewTime(const Math::Vector &vEyePt, const Math::Vector &vLookatPt, float rTime); + bool IsCollision(Math::Vector &eye, Math::Vector lookat); + bool IsCollisionBack(Math::Vector &eye, Math::Vector lookat); + bool IsCollisionFix(Math::Vector &eye, Math::Vector lookat); + + Math::Vector ExcludeTerrain(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); + Math::Vector ExcludeObject(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); + + void SetViewParams(const Math::Vector &eye, const Math::Vector &lookat, const Math::Vector &up); + void EffectFrame(const Event &event); + void OverFrame(const Event &event); + +protected: + CInstanceManager* m_iMan; + Gfx::CEngine* m_engine; + CTerrain* m_terrain; + CWater* m_water; + + CameraType m_type; // the type of camera (CAMERA *) + CameraSmooth m_smooth; // type of smoothing + CObject* m_cameraObj; // object linked to the camera + + float m_eyeDistance; // distance between the eyes + float m_initDelay; // time of initial centering + + Math::Vector m_actualEye; // current eye + Math::Vector m_actualLookat; // aim current + Math::Vector m_finalEye; // final eye + Math::Vector m_finalLookat; // aim final + Math::Vector m_normEye; // normal eye + Math::Vector m_normLookat; // aim normal + float m_focus; + + bool m_bRightDown; + Math::Point m_rightPosInit; + Math::Point m_rightPosCenter; + Math::Point m_rightPosMove; + + Math::Vector m_eyePt; // CAMERA_FREE: eye + float m_directionH; // CAMERA_FREE: horizontal direction + float m_directionV; // CAMERA_FREE: vertical direction + float m_heightEye; // CAMERA_FREE: height above the ground + float m_heightLookat; // CAMERA_FREE: height above the ground + float m_speed; // CAMERA_FREE: speed of movement + + float m_backDist; // CAMERA_BACK: distance + float m_backMin; // CAMERA_BACK: distance minimal + float m_addDirectionH; // CAMERA_BACK: additional direction + float m_addDirectionV; // CAMERA_BACK: additional direction + bool m_bTransparency; + + float m_fixDist; // CAMERA_FIX: distance + float m_fixDirectionH; // CAMERA_FIX: direction + float m_fixDirectionV; // CAMERA_FIX: direction + + Math::Vector m_visitGoal; // CAMERA_VISIT: target position + float m_visitDist; // CAMERA_VISIT: distance + float m_visitTime; // CAMERA_VISIT: relative time + CameraType m_visitType; // CAMERA_VISIT: initial type + float m_visitDirectionH; // CAMERA_VISIT: direction + float m_visitDirectionV; // CAMERA_VISIT: direction + + float m_editHeight; // CAMERA_EDIT: height + + float m_remotePan; + float m_remoteZoom; + + Math::Point m_mousePos; + float m_mouseDirH; + float m_mouseDirV; + float m_mouseMarging; + + float m_motorTurn; + + CenteringPhase m_centeringPhase; + float m_centeringAngleH; + float m_centeringAngleV; + float m_centeringDist; + float m_centeringCurrentH; + float m_centeringCurrentV; + float m_centeringTime; + float m_centeringProgress; + + CameraEffect m_effectType; + Math::Vector m_effectPos; + float m_effectForce; + float m_effectProgress; + Math::Vector m_effectOffset; + + OverEffect m_overType; + float m_overForce; + float m_overTime; + Gfx::Color m_overColorBase; + Gfx::Color m_overColor; + int m_overMode; + float m_overFadeIn; + float m_overFadeOut; + + Math::Vector m_scriptEye; + Math::Vector m_scriptLookat; + + bool m_bEffect; // shocks if explosion? + bool m_bCameraScroll; // scroll in the edges? + bool m_bCameraInvertX; // X inversion in the edges? + bool m_bCameraInvertY; // Y inversion in the edges? + +}; + + +}; // namespace Gfx diff --git a/src/graphics/engine/cloud.cpp b/src/graphics/engine/cloud.cpp new file mode 100644 index 0000000..d0e5ed8 --- /dev/null +++ b/src/graphics/engine/cloud.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// cloud.cpp + +#include "graphics/engine/cloud.h" + + +// TODO implementation diff --git a/src/graphics/engine/cloud.h b/src/graphics/engine/cloud.h new file mode 100644 index 0000000..d2d29d7 --- /dev/null +++ b/src/graphics/engine/cloud.h @@ -0,0 +1,94 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// cloud.h + +#pragma once + +#include "common/event.h" +#include "graphics/core/color.h" +#include "math/point.h" +#include "math/vector.h" + + + +class CInstanceManager; + + +namespace Gfx { + +class CEngine; +class CTerrain; + +const short MAXCLOUDLINE = 100; + +struct CloudLine +{ + short x, y; // beginning + short len; // in length x + float px1, px2, pz; +}; + + +class CCloud +{ +public: + CCloud(CInstanceManager* iMan, CEngine* engine); + ~CCloud(); + + bool EventProcess(const Event &event); + void Flush(); + bool Create(const char *filename, Gfx::Color diffuse, Gfx::Color ambient, float level); + void Draw(); + + bool SetLevel(float level); + float RetLevel(); + + void SetEnable(bool bEnable); + bool RetEnable(); + +protected: + bool EventFrame(const Event &event); + void AdjustLevel(Math::Vector &pos, Math::Vector &eye, float deep, Math::Point &uv1, Math::Point &uv2); + bool CreateLine(int x, int y, int len); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CTerrain* m_terrain; + + char m_filename[100]; + float m_level; // overall level + Math::Point m_speed; // feedrate (wind) + Gfx::Color m_diffuse; // diffuse color + Gfx::Color m_ambient; // ambient color + float m_time; + float m_lastTest; + int m_subdiv; + + Math::Vector m_wind; // wind speed + int m_brick; // brick mosaic + float m_size; // size of a brick element + + int m_lineUsed; + CloudLine m_line[MAXCLOUDLINE]; + + bool m_bEnable; +}; + + +}; // namespace Gfx diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp new file mode 100644 index 0000000..0914f9e --- /dev/null +++ b/src/graphics/engine/engine.cpp @@ -0,0 +1,729 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// engine.cpp + +#include "graphics/engine/engine.h" + +#include "app/app.h" +#include "common/iman.h" +#include "common/image.h" +#include "common/key.h" +#include "common/logger.h" +#include "graphics/core/device.h" +#include "math/geometry.h" + +// Initial size of various vectors +const int OBJECT_PREALLOCATE_COUNT = 1200; +const int SHADOW_PREALLOCATE_COUNT = 500; +const int GROUNDSPOT_PREALLOCATE_COUNT = 100; + +const int LEVEL1_PREALLOCATE_COUNT = 50; +const int LEVEL2_PREALLOCATE_COUNT = 100; +const int LEVEL3_PREALLOCATE_COUNT = 5; +const int LEVEL4_PREALLOCATE_COUNT = 10; +const int LEVEL5_PREALLOCATE_COUNT = 100; +const int LEVEL5_VERTEX_PREALLOCATE_COUNT = 200; + + +Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) +{ + m_iMan = iMan; + m_app = app; + m_device = NULL; + + m_wasInit = false; + + m_iMan = iMan; + m_iMan->AddInstance(CLASS_ENGINE, this); + m_app = app; + + m_lightMan = NULL; + m_text = NULL; + m_particle = NULL; + m_water = NULL; + m_cloud = NULL; + m_lightning = NULL; + m_planet = NULL; + m_sound = NULL; + m_terrain = NULL; + + m_dim.x = 640; + m_dim.y = 480; + m_lastDim = m_dim; + m_focus = 0.75f; + m_baseTime = 0; + m_lastTime = 0; + m_absTime = 0.0f; + m_rankView = 0; + + m_ambientColor[0] = Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); + m_ambientColor[1] = Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); + m_fogColor[0] = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f); + m_fogColor[1] = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f); + m_deepView[0] = 1000.0f; + m_deepView[1] = 1000.0f; + m_fogStart[0] = 0.75f; + m_fogStart[1] = 0.75f; + m_waterAddColor = Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f); + + m_pause = false; + m_render = true; + m_movieLock = false; + m_shadowVisible = true; + m_groundSpotVisible = true; + m_dirty = true; + m_fog = true; + m_speed = 1.0f; + m_secondTexNum = 0; + m_eyeDirH = 0.0f; + m_eyeDirV = 0.0f; + m_backgroundName = ""; // no background image + m_backgroundColorUp = 0; + m_backgroundColorDown = 0; + m_backgroundCloudUp = 0; + m_backgroundCloudDown = 0; + m_backgroundFull = false; + m_backgroundQuarter = false; + m_overFront = true; + m_overColor = 0; + m_overMode = ENG_RSTATE_TCOLOR_BLACK; + m_frontsizeName = ""; // no front image + m_hiliteRank[0] = -1; // empty list + m_eyePt = Math::Vector(0.0f, 0.0f, 0.0f); + m_lookatPt = Math::Vector(0.0f, 0.0f, 1.0f); + m_drawWorld = true; + m_drawFront = false; + m_limitLOD[0] = 100.0f; + m_limitLOD[1] = 200.0f; + m_particuleDensity = 1.0f; + m_clippingDistance = 1.0f; + m_lastClippingDistance = m_clippingDistance; + m_objectDetail = 1.0f; + m_lastObjectDetail = m_objectDetail; + m_terrainVision = 1000.0f; + m_gadgetQuantity = 1.0f; + m_textureQuality = 1; + m_totoMode = true; + m_lensMode = true; + m_waterMode = true; + m_skyMode = true; + m_backForce = true; + m_planetMode = true; + m_lightMode = true; + m_editIndentMode = true; + m_editIndentValue = 4; + m_tracePrecision = 1.0f; + + m_alphaMode = 1; + + m_forceStateColor = true; + m_stateColor = false; + + m_blackSrcBlend[0] = 0; + m_blackDestBlend[0] = 0; + m_whiteSrcBlend[0] = 0; + m_whiteDestBlend[0] = 0; + m_diffuseSrcBlend[0] = 0; + m_diffuseDestBlend[0] = 0; + m_alphaSrcBlend[0] = 0; + m_alphaDestBlend[0] = 0; + + m_updateGeometry = false; + + m_mice[Gfx::ENG_MOUSE_NORM] = Gfx::EngineMouse( 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f)); + m_mice[Gfx::ENG_MOUSE_WAIT] = Gfx::EngineMouse( 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f)); + m_mice[Gfx::ENG_MOUSE_HAND] = Gfx::EngineMouse( 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f)); + m_mice[Gfx::ENG_MOUSE_NO] = Gfx::EngineMouse( 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f)); + m_mice[Gfx::ENG_MOUSE_EDIT] = Gfx::EngineMouse( 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f)); + m_mice[Gfx::ENG_MOUSE_CROSS] = Gfx::EngineMouse(10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f)); + m_mice[Gfx::ENG_MOUSE_MOVEV] = Gfx::EngineMouse(12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f)); + m_mice[Gfx::ENG_MOUSE_MOVEH] = Gfx::EngineMouse(14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f)); + m_mice[Gfx::ENG_MOUSE_MOVED] = Gfx::EngineMouse(16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_MOVEI] = Gfx::EngineMouse(18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_MOVE] = Gfx::EngineMouse(20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f)); + m_mice[Gfx::ENG_MOUSE_TARGET] = Gfx::EngineMouse(22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLL] = Gfx::EngineMouse(24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLR] = Gfx::EngineMouse(26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLU] = Gfx::EngineMouse(28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f)); + m_mice[Gfx::ENG_MOUSE_SCROLLD] = Gfx::EngineMouse(30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f)); + + m_mouseSize = Math::Point(0.04f, 0.04f * (800.0f / 600.0f)); + m_mousePos = Math::Point(0.5f, 0.5f); + m_mouseType = Gfx::ENG_MOUSE_NORM; + m_mouseVisible = false; + + m_texPath = "textures/"; + m_defaultTexParams.format = Gfx::TEX_IMG_RGBA; + m_defaultTexParams.mipmap = true; + m_defaultTexParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; + m_defaultTexParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; + + m_objectTree.reserve(LEVEL1_PREALLOCATE_COUNT); + m_objects.reserve(OBJECT_PREALLOCATE_COUNT); + m_shadow.reserve(SHADOW_PREALLOCATE_COUNT); + m_groundSpot.reserve(GROUNDSPOT_PREALLOCATE_COUNT); +} + +Gfx::CEngine::~CEngine() +{ + m_iMan = NULL; + m_app = NULL; + m_device = NULL; + + m_sound = NULL; + m_terrain = NULL; +} + +bool Gfx::CEngine::GetWasInit() +{ + return m_wasInit; +} + +std::string Gfx::CEngine::GetError() +{ + return m_error; +} + +bool Gfx::CEngine::Create() +{ + m_wasInit = true; + + /*m_lightMan = new Gfx::CLight(m_iMan, this); + m_text = new Gfx::CText(m_iMan, this); + m_particle = new Gfx::CParticle(m_iMan, this); + m_water = new Gfx::CWater(m_iMan, this); + m_cloud = new Gfx::CCloud(m_iMan, this); + m_lightning = new Gfx::CLightning(m_iMan, this); + m_planet = new Gfx::CPlanet(m_iMan, this);*/ + + m_matWorldInterface.LoadIdentity(); + m_matViewInterface.LoadIdentity(); + Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); + + return true; +} + +void Gfx::CEngine::Destroy() +{ + // TODO + + /*delete m_lightMan; + m_lightMan = NULL; + + delete m_text; + m_text = NULL; + + delete m_particle; + m_particle = NULL; + + delete m_water; + m_water = NULL; + + delete m_cloud; + m_cloud = NULL; + + delete m_lightning; + m_lightning = NULL; + + delete m_planet; + m_planet = NULL;*/ + + m_wasInit = false; +} + +void Gfx::CEngine::SetDevice(Gfx::CDevice *device) +{ + m_device = device; +} + +Gfx::CDevice* Gfx::CEngine::GetDevice() +{ + return m_device; +} + +bool Gfx::CEngine::AfterDeviceSetInit() +{ + m_device->SetClearColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)); + + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); + + Gfx::TextureCreateParams params; + params.format = Gfx::TEX_IMG_RGB; + params.minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + params.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + params.mipmap = false; + m_miceTexture = CreateTexture("mouse.png", params); + + return true; +} + +Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams ¶ms) +{ + CImage img; + if (! img.Load(m_app->GetDataFilePath(m_texPath, texName))) + { + std::stringstream str; + str << "Couldn't load texture '" << texName << "': " << img.GetError(); + m_error = str.str(); + return Gfx::Texture(); // invalid texture + } + + Gfx::Texture result = m_device->CreateTexture(&img, params); + + if (! result.valid) + { + std::stringstream str; + str << "Couldn't load texture '" << texName << "': " << m_device->GetError(); + m_error = str.str(); + return result; + } + + m_texNameMap[texName] = result; + m_revTexNameMap[result] = texName; + + return result; +} + +Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName) +{ + return CreateTexture(texName, m_defaultTexParams); +} + +void Gfx::CEngine::DestroyTexture(const std::string &texName) +{ + std::map::iterator it = m_texNameMap.find(texName); + if (it == m_texNameMap.end()) + return; + + std::map::iterator revIt = m_revTexNameMap.find((*it).second); + + m_device->DestroyTexture((*it).second); + + m_revTexNameMap.erase(revIt); + m_texNameMap.erase(it); +} + +void Gfx::CEngine::SetTexture(const std::string &name, int stage) +{ + std::map::iterator it = m_texNameMap.find(name); + if (it != m_texNameMap.end()) + m_device->SetTexture(stage, (*it).second); + + // TODO if not present... +} + +void Gfx::CEngine::SetMaterial(const Gfx::Material &mat) +{ + m_device->SetMaterial(mat); +} + +void Gfx::CEngine::SetState(int state, Gfx::Color color) +{ + if ( state == m_lastState && color == m_lastColor ) + return; + + m_lastState = state; + m_lastColor = color; + + if ( m_alphaMode != 1 && (state & Gfx::ENG_RSTATE_ALPHA) ) + { + state &= ~Gfx::ENG_RSTATE_ALPHA; + + if (m_alphaMode == 2) + state |= Gfx::ENG_RSTATE_TTEXTURE_BLACK; + } + + // TODO other modes & thorough testing + + if (state & Gfx::ENG_RSTATE_TTEXTURE_BLACK) // The transparent black texture? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); + m_device->SetTextureEnabled(0, true); + m_device->SetTextureFactor(color); + + Gfx::TextureStageParams params; + params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; + params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; + params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + m_device->SetTextureStageParams(0, params); + } + else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); + m_device->SetTextureEnabled(0, true); + m_device->SetTextureFactor(color.Inverse()); + + Gfx::TextureStageParams params; + params.colorOperation = Gfx::TEX_MIX_OPER_ADD; + params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE; + params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR; + params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + m_device->SetTextureStageParams(0, params); + } + else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR); + + m_device->SetTextureFactor(color); + m_device->SetTextureEnabled(0, true); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); + } + else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO); + + m_device->SetTextureFactor(color.Inverse()); + m_device->SetTextureEnabled(0, true); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); + } + else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent? + { + /*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]); + m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]); + + m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); + m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + else if (state & Gfx::ENG_RSTATE_ALPHA) // image with alpha channel? + { + /*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false); + m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true); + m_device->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER); + m_device->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128)); + m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]); + m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]); + + m_device->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color); + m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); + m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/ + } + else // normal ? + { + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true); + m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, true); + m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, false); + m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false); + m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + m_device->SetTextureEnabled(0, true); + m_device->SetTextureStageParams(0, Gfx::TextureStageParams()); + + /*m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/ + } + + if (state & Gfx::ENG_RSTATE_FOG) + m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true); + + + bool second = m_groundSpotVisible || m_dirty; + + if ( !m_groundSpotVisible && (state & Gfx::ENG_RSTATE_SECOND) != 0 ) second = false; + if ( !m_dirty && (state & Gfx::ENG_RSTATE_SECOND) == 0 ) second = false; + + if ( (state & ENG_RSTATE_DUAL_BLACK) && second ) + { + /*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); + m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); + m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); + m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ + } + else if ( (state & ENG_RSTATE_DUAL_WHITE) && second ) + { + /*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD); + m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); + m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); + m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); + m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/ + } + else + { + m_device->SetTextureEnabled(1, false); + } + + if (state & Gfx::ENG_RSTATE_WRAP) + { + /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP); + m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/ + } + else if (state & Gfx::ENG_RSTATE_CLAMP) + { + /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); + m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ + } + else + { + /*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP); + m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/ + } + + if (state & Gfx::ENG_RSTATE_2FACE) + { + m_device->SetRenderState(Gfx::RENDER_STATE_CULLING, false); + } + else + { + m_device->SetRenderState(Gfx::RENDER_STATE_CULLING, true); + m_device->SetCullMode(Gfx::CULL_CCW); + } + + if (state & Gfx::ENG_RSTATE_LIGHT) + m_device->SetGlobalAmbient(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); + else + m_device->SetGlobalAmbient(m_ambientColor[m_rankView]); +} + +bool Gfx::CEngine::ProcessEvent(const Event &event) +{ + if (event.type == EVENT_MOUSE_MOVE) + { + m_mousePos = event.mouseMove.pos; + } + else if (event.type == EVENT_KEY_DOWN) + { + // !! Debug, to be removed later !! + + if (event.key.key == KEY(F1)) + { + m_mouseVisible = !m_mouseVisible; + m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile()); + } + else if (event.key.key == KEY(F2)) + { + int index = static_cast(m_mouseType); + m_mouseType = static_cast( (index + 1) % Gfx::ENG_MOUSE_COUNT ); + } + } + + // By default, pass on all events + return true; +} + +bool Gfx::CEngine::Render() +{ + m_statisticTriangle = 0; + + m_lastState = -1; + SetState(Gfx::ENG_RSTATE_NORMAL); + + m_device->BeginScene(); + + SetUp3DView(); + + if (! Draw3DScene() ) + return false; + + SetUpInterfaceView(); + + if (! DrawInterface() ) + return false; + + m_device->EndScene(); + + return true; +} + +void Gfx::CEngine::SetUp3DView() +{ + // TODO +} + +bool Gfx::CEngine::Draw3DScene() +{ + // TODO + return true; +} + +void Gfx::CEngine::SetUpInterfaceView() +{ + m_device->SetTransform(Gfx::TRANSFORM_WORLD, m_matWorldInterface); + m_device->SetTransform(Gfx::TRANSFORM_VIEW, m_matViewInterface); + m_device->SetTransform(Gfx::TRANSFORM_PROJECTION, m_matProjInterface); +} + +bool Gfx::CEngine::DrawInterface() +{ + Gfx::VertexCol vertices[3] = + { + Gfx::VertexCol(Math::Vector( 0.25f, 0.25f, 0.0f), Gfx::Color(1.0f, 0.0f, 0.0f)), + Gfx::VertexCol(Math::Vector( 0.75f, 0.25f, 0.0f), Gfx::Color(0.0f, 1.0f, 0.0f)), + Gfx::VertexCol(Math::Vector( 0.5f, 0.75f, 0.0f), Gfx::Color(0.0f, 0.0f, 1.0f)) + }; + + m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, vertices, 3); + + DrawMouse(); + + return true; +} + +void Gfx::CEngine::DrawMouse() +{ + if (! m_mouseVisible) + return; + + if (m_app->GetSystemMouseVisibile()) + return; + + Gfx::Material material; + material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f); + material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f); + + m_device->SetMaterial(material); + m_device->SetTexture(0, m_miceTexture); + + int index = static_cast(m_mouseType); + + Math::Point pos = m_mousePos; + pos.x = m_mousePos.x - (m_mice[index].hotPoint.x * m_mouseSize.x) / 32.0f; + pos.y = m_mousePos.y - ((32.0f - m_mice[index].hotPoint.y) * m_mouseSize.y) / 32.0f; + + Math::Point shadowPos; + shadowPos.x = pos.x + (4.0f/800.0f); + shadowPos.y = pos.y - (3.0f/600.0f); + + SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE); + DrawMouseSprite(shadowPos, m_mouseSize, m_mice[index].iconShadow); + + SetState(m_mice[index].mode1); + DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon1); + + SetState(m_mice[index].mode2); + DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon2); +} + +void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon) +{ + if (icon == -1) + return; + + Math::Point p1 = pos; + Math::Point p2 = p1 + size; + + float u1 = (32.0f / 256.0f) * (icon % 8); + float v1 = (32.0f / 256.0f) * (icon / 8); + float u2 = u1 + (32.0f / 256.0f); + float v2 = v1 + (32.0f / 256.0f); + + float dp = 0.5f / 256.0f; + u1 += dp; + v1 += dp; + u2 -= dp; + v2 -= dp; + + Math::Vector normal(0.0f, 0.0f, -1.0f); + + Gfx::Vertex vertex[4] = + { + Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), normal, Math::Point(u1, v2)), + Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), normal, Math::Point(u2, v2)), + Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)), + Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), normal, Math::Point(u2, v1)) + }; + + m_device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, vertex, 4); + AddStatisticTriangle(2); +} + +bool Gfx::CEngine::GetPause() +{ + return m_pause; +} + +Math::Vector Gfx::CEngine::GetLookatPt() +{ + return m_lookatPt; +} + +Math::Vector Gfx::CEngine::GetEyePt() +{ + return m_eyePt; +} + +void Gfx::CEngine::SetMouseVisible(bool visible) +{ + m_mouseVisible = visible; +} + +bool Gfx::CEngine::GetMouseVisible() +{ + return m_mouseVisible; +} + +void Gfx::CEngine::SetMousePos(Math::Point pos) +{ + m_mousePos = pos; +} + +Math::Point Gfx::CEngine::GetMousePos() +{ + return m_mousePos; +} + +void Gfx::CEngine::SetMouseType(Gfx::EngineMouseType type) +{ + m_mouseType = type; +} + +Gfx::EngineMouseType Gfx::CEngine::GetMouseType() +{ + return m_mouseType; +} + +void Gfx::CEngine::AddStatisticTriangle(int count) +{ + m_statisticTriangle += count; +} diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h new file mode 100644 index 0000000..1348cdd --- /dev/null +++ b/src/graphics/engine/engine.h @@ -0,0 +1,985 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// engine.h + +#pragma once + + +#include "common/event.h" +#include "graphics/core/color.h" +#include "graphics/core/material.h" +#include "graphics/core/texture.h" +#include "graphics/core/vertex.h" +#include "math/intpoint.h" +#include "math/matrix.h" +#include "math/point.h" +#include "math/vector.h" + + +#include +#include +#include + + +class CApplication; +class CInstanceManager; +class CObject; +class CSound; + + +namespace Gfx { + +class CDevice; +class CLightManager; +class CText; +class CParticle; +class CWater; +class CCloud; +class CLightning; +class CPlanet; +class CTerrain; + + +/** + \enum EngineTriangleType + \brief Type of triangles drawn for engine objects */ +enum EngineTriangleType +{ + //! Triangles + ENG_TRIANGLE_TYPE_6T = 1, + //! Surfaces + ENG_TRIANGLE_TYPE_6S = 2 +}; + +/** + \struct EngineTriangle + \brief A triangle drawn by the graphics engine */ +struct EngineTriangle +{ + //! Triangle vertices + Gfx::VertexTex2 triangle[3]; + //! Material + Gfx::Material material; + //! Render state (TODO: ?) + int state; + //! 1st texture + Gfx::Texture tex1; + //! 2nd texture + Gfx::Texture tex2; + + EngineTriangle() + { + state = 0; + } +}; + +/** + \enum EngineObjectType + \brief Class of graphics engine object */ +enum EngineObjectType +{ + //! Object doesn't exist + ENG_OBJTYPE_NULL = 0, + //! Terrain + ENG_OBJTYPE_TERRAIN = 1, + //! Fixed object + ENG_OBJTYPE_FIX = 2, + //! Moving object + ENG_OBJTYPE_VEHICULE = 3, + //! Part of a moving object + ENG_OBJTYPE_DESCENDANT = 4, + //! Fixed object type quartz + ENG_OBJTYPE_QUARTZ = 5, + //! Fixed object type metal + ENG_OBJTYPE_METAL = 6 +}; + +/** + \struct EngineObject + \brief Object drawn by the graphics engine */ +struct EngineObject +{ + //! If true, the object is drawn + bool visible; + //! If true, object is behind the 2D interface + bool drawWorld; + //! If true, the shape is before the 2D interface + bool drawFront; + //! Number of triangles + int totalTriangles; + //! Type of object + Gfx::EngineObjectType type; + //! Transformation matrix + Math::Matrix transform; + //! Distance view - origin (TODO: ?) + float distance; + //! Bounding box min (origin 0,0,0 always included) + Math::Vector bboxMin; + //! bounding box max (origin 0,0,0 always included) + Math::Vector bboxMax; + //! Radius of the sphere at the origin + float radius; + //! Rank of the associated shadow + int shadowRank; + //! Transparency of the object [0, 1] + float transparency; + + EngineObject() + { + visible = false; + drawWorld = false; + drawFront = false; + totalTriangles = 0; + distance = 0.0f; + radius = 0.0f; + shadowRank = 0; + transparency = 0.0f; + } +}; + +struct EngineObjLevel1; +struct EngineObjLevel2; +struct EngineObjLevel3; +struct EngineObjLevel4; +struct EngineObjLevel5; + +/** + \struct EngineObjLevel5 + \brief Tier 5 of object tree */ +struct EngineObjLevel5 +{ + Gfx::Material material; + int state; + Gfx::EngineTriangleType type; + std::vector vertices; + + EngineObjLevel5(); +}; + +/** + \struct EngineObjLevel4 + \brief Tier 4 of object tree */ +struct EngineObjLevel4 +{ + int reserved; + std::vector up; + Gfx::EngineObjLevel3* down; + + EngineObjLevel4(); +}; + +/** + \struct EngineObjLevel3 + \brief Tier 3 of object tree */ +struct EngineObjLevel3 +{ + float min; + float max; + std::vector up; + Gfx::EngineObjLevel2* down; + + EngineObjLevel3(); +}; + +/** + \struct EngineObjLevel2 + \brief Tier 2 of object tree */ +struct EngineObjLevel2 +{ + int objRank; + std::vector up; + Gfx::EngineObjLevel1* down; + + EngineObjLevel2(); +}; + +/** + \struct EngineObjLevel1 + \brief Tier 1 of object tree */ +struct EngineObjLevel1 +{ + Gfx::Texture tex1; + Gfx::Texture tex2; + std::vector up; + + EngineObjLevel1(); +}; + +/** + \struct EngineShadowType + \brief Type of shadow drawn by the graphics engine */ +enum EngineShadowType +{ + //! Normal shadow + ENG_SHADOW_NORM = 0, + //! TODO: ? + ENG_SHADOW_WORM = 1 +}; + +/** + \struct EngineShadow + \brief Shadow drawn by the graphics engine */ +struct EngineShadow +{ + //! If true, shadow is invisible (object being carried for example) + bool hide; + //! Rank of the associated object + int objRank; + //! Type of shadow + Gfx::EngineShadowType type; + //! Position of the shadow + Math::Vector pos; + //! Normal to the terrain + Math::Vector normal; + //! Angle of the shadow + float angle; + //! Radius of the shadow + float radius; + //! Intensity of the shadow + float intensity; + //! Height from the ground + float height; + + EngineShadow() + { + hide = false; + objRank = 0; + angle = radius = intensity = height = 0.0f; + } +}; + +/** + \struct EngineGroundSpot + \brief A spot (large shadow) drawn on the ground by the graphics engine */ +struct EngineGroundSpot +{ + //! Color of the shadow + Gfx::Color color; + //! Min altitude + float min; + //! Max altitude + float max; + //! Transition area + float smooth; + //! Position for the shadow + Math::Vector pos; + //! Radius of the shadow + float radius; + //! Position of the shadow drawn + Math::Vector drawPos; + //! Radius of the shadow drawn + float drawRadius; + + EngineGroundSpot() + { + min = max = smooth = radius = drawRadius = 0.0f; + } +}; + +/** + \enum EngineGroundMarkPhase + \brief Phase of life of an EngineGroundMark */ +enum EngineGroundMarkPhase +{ + //! Increase + ENG_GR_MARK_PHASE_INC = 1, + //! Fixed + ENG_GR_MARK_PHASE_FIX = 2, + //! Decrease + ENG_GR_MARK_PHASE_DEC = 2 +}; + +/** + \struct EngineGroundMark + \brief A mark on ground drawn by the graphics engine */ +struct EngineGroundMark +{ + //! If true, draw mark + bool draw; + //! Phase of life + Gfx::EngineGroundMarkPhase phase; + //! Times for 3 life phases + float delay[3]; + //! Fixed time + float fix; + //! Position for marks + Math::Vector pos; + //! Radius of marks + float radius; + //! Color intensity + float intensity; + //! Draw position for marks + Math::Vector drawPos; + //! Radius for marks + float drawRadius; + //! Draw intensity for marks + float drawIntensity; + //! X dimension of table + int dx; + //! Y dimension of table + int dy; + //! Pointer to the table + char* table; + + EngineGroundMark() + { + draw = false; + delay[0] = delay[1] = delay[2] = 0.0f; + fix = radius = intensity = drawRadius = drawIntensity = 0.0f; + dx = dy = 0; + table = NULL; + } +}; + +/** + \enum EngineTextureMapping + \brief Type of texture mapping + */ +enum EngineTextureMapping +{ + ENG_TEX_MAPPING_X = 1, + ENG_TEX_MAPPING_Y = 2, + ENG_TEX_MAPPING_Z = 3, + ENG_TEX_MAPPING_1X = 4, + ENG_TEX_MAPPING_1Y = 5, + ENG_TEX_MAPPING_1Z = 6 +}; + + +/** + \enum EngineRenderState + \brief Render state of graphics engine + + States are used for settings certain modes, for instance texturing and blending. + The enum is a bitmask and some of the states can be OR'd together. */ +enum EngineRenderState +{ + //! Normal opaque materials + ENG_RSTATE_NORMAL = 0, + //! The transparent texture (black = no) + ENG_RSTATE_TTEXTURE_BLACK = (1<<0), + //! The transparent texture (white = no) + ENG_RSTATE_TTEXTURE_WHITE = (1<<1), + //! The transparent diffuse color + ENG_RSTATE_TDIFFUSE = (1<<2), + //! Texture wrap + ENG_RSTATE_WRAP = (1<<3), + //! Texture borders with solid color + ENG_RSTATE_CLAMP = (1<<4), + //! Light texture (ambient max) + ENG_RSTATE_LIGHT = (1<<5), + //! Double black texturing + ENG_RSTATE_DUAL_BLACK = (1<<6), + //! Double white texturing + ENG_RSTATE_DUAL_WHITE = (1<<7), + //! Part 1 (no change in. MOD!) + ENG_RSTATE_PART1 = (1<<8), + //! Part 2 + ENG_RSTATE_PART2 = (1<<9), + //! Part 3 + ENG_RSTATE_PART3 = (1<<10), + //! Part 4 + ENG_RSTATE_PART4 = (1<<11), + //! Double-sided face + ENG_RSTATE_2FACE = (1<<12), + //! Image using alpha channel + ENG_RSTATE_ALPHA = (1<<13), + //! Always use 2nd floor texturing + ENG_RSTATE_SECOND = (1<<14), + //! Causes the fog + ENG_RSTATE_FOG = (1<<15), + //! The transparent color (black = no) + ENG_RSTATE_TCOLOR_BLACK = (1<<16), + //! The transparent color (white = no) + ENG_RSTATE_TCOLOR_WHITE = (1<<17) +}; + + +/** + \enum EngineMouseType + \brief Type of mouse cursor displayed in-game */ +enum EngineMouseType +{ + //! Normal cursor (arrow) + ENG_MOUSE_NORM = 0, + //! Busy + ENG_MOUSE_WAIT = 1, + //! Edit (I-beam) + ENG_MOUSE_EDIT = 2, + //! Hand + ENG_MOUSE_HAND = 3, + //! Small cross + ENG_MOUSE_CROSS = 4, + //! TODO: ? + ENG_MOUSE_SHOW = 5, + //! Crossed out sign + ENG_MOUSE_NO = 6, + //! Resize + ENG_MOUSE_MOVE = 7, + //! Resize horizontally + ENG_MOUSE_MOVEH = 8, + //! Resize vertically + ENG_MOUSE_MOVEV = 9, + //! Resize diagonally bottom-left to top-right + ENG_MOUSE_MOVED = 10, + //! Resize diagonally top-left to bottom-right + ENG_MOUSE_MOVEI = 11, + //! Scroll to the left + ENG_MOUSE_SCROLLL = 12, + //! Scroll to the right + ENG_MOUSE_SCROLLR = 13, + //! Scroll up + ENG_MOUSE_SCROLLU = 14, + //! Scroll down + ENG_MOUSE_SCROLLD = 15, + //! Larger crosshair + ENG_MOUSE_TARGET = 16, + + //! Number of items in enum + ENG_MOUSE_COUNT +}; + +/** + \struct EngineMouse + \brief Information about mouse cursor */ +struct EngineMouse +{ + //! Index of texture element for 1st image + int icon1; + //! Index of texture element for 2nd image + int icon2; + //! Shadow texture part + int iconShadow; + //! Mode to render 1st image in + Gfx::EngineRenderState mode1; + //! Mode to render 2nd image in + Gfx::EngineRenderState mode2; + //! Hot point + Math::Point hotPoint; + + EngineMouse(int icon1 = -1, int icon2 = -1, int iconShadow = -1, + Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL, + Gfx::EngineRenderState mode2 = Gfx::ENG_RSTATE_NORMAL, + Math::Point hotPoint = Math::Point()) + { + this->icon1 = icon1; + this->icon2 = icon2; + this->iconShadow = iconShadow; + this->mode1 = mode1; + this->mode2 = mode2; + this->hotPoint = hotPoint; + } +}; + + +/** + \class CEngine + \brief The graphics engine + + This is the main class for graphics engine. It is responsible for drawing the 3D scene, + setting various render states, and facilitating the drawing of 2D interface. + + It uses a lower-level CDevice object which is implementation-independent core engine. + + \section Objecs Engine objects + + The 3D scene is composed of objects which are basically collections of triangles forming + a surface or simply independent triangles in space. Objects are stored in the engine + as a tree structure which is composed of 5 tiers (EngineObjLevel1, EngineObjLevel2 and so on). + Each tier stores some data about object triangle, like textures or materials used. + Additional information on objects stored are in EngineObject structure. + Each object is uniquely identified by its rank. + + ... + */ +class CEngine +{ +public: + CEngine(CInstanceManager *iMan, CApplication *app); + ~CEngine(); + + bool GetWasInit(); + std::string GetError(); + + bool Create(); + void Destroy(); + + void SetDevice(Gfx::CDevice *device); + Gfx::CDevice* GetDevice(); + + bool AfterDeviceSetInit(); + + void SetTerrain(Gfx::CTerrain* terrain); + + bool ProcessEvent(const Event &event); + + bool Render(); + + + bool WriteProfile(); + + void SetPause(bool pause); + bool GetPause(); + + void SetMovieLock(bool lock); + bool GetMovieLock(); + + void SetShowStat(bool show); + bool GetShowStat(); + + void SetRenderEnable(bool enable); + + int OneTimeSceneInit(); + int InitDeviceObjects(); + int DeleteDeviceObjects(); + int RestoreSurfaces(); + int FrameMove(float rTime); + void StepSimulation(float rTime); + int FinalCleanup(); + void AddStatisticTriangle(int nb); + int GetStatisticTriangle(); + void SetHiliteRank(int *rankList); + bool GetHilite(Math::Point &p1, Math::Point &p2); + bool GetSpriteCoord(int &x, int &y); + void SetInfoText(int line, char* text); + char* GetInfoText(int line); + void FirstExecuteAdapt(bool first); + + bool GetFullScreen(); + + Math::Matrix* GetMatView(); + Math::Matrix* GetMatLeftView(); + Math::Matrix* GetMatRightView(); + + void TimeInit(); + void TimeEnterGel(); + void TimeExitGel(); + float TimeGet(); + + int GetRestCreate(); + int CreateObject(); + void FlushObject(); + bool DeleteObject(int objRank); + bool SetDrawWorld(int objRank, bool draw); + bool SetDrawFront(int objRank, bool draw); + + bool AddTriangle(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, + int state, std::string texName1, std::string texName2, + float min, float max, bool globalUpdate); + bool AddSurface(int objRank, Gfx::VertexTex2* vertex, int nb, const Gfx::Material &mat, + int state, std::string texName1, std::string texName2, + float min, float max, bool globalUpdate); + bool AddQuick(int objRank, Gfx::EngineObjLevel5* buffer, + std::string texName1, std::string texName2, + float min, float max, bool globalUpdate); + Gfx::EngineObjLevel5* SearchTriangle(int objRank, const Gfx::Material &mat, + int state, std::string texName1, std::string texName2, + float min, float max); + + void ChangeLOD(); + bool ChangeSecondTexture(int objRank, char* texName2); + int GetTotalTriangles(int objRank); + int GetTriangles(int objRank, float min, float max, Gfx::EngineTriangle* buffer, int size, float percent); + bool GetBBox(int objRank, Math::Vector &min, Math::Vector &max); + bool ChangeTextureMapping(int objRank, const Gfx::Material &mat, int state, + const std::string &texName1, const std::string &texName2, + float min, float max, Gfx::EngineTextureMapping mode, + float au, float bu, float av, float bv); + bool TrackTextureMapping(int objRank, const Gfx::Material &mat, int state, + const std::string &texName1, const std::string &texName2, + float min, float max, Gfx::EngineTextureMapping mode, + float pos, float factor, float tl, float ts, float tt); + bool SetObjectTransform(int objRank, const Math::Matrix &transform); + bool GetObjectTransform(int objRank, Math::Matrix &transform); + bool SetObjectType(int objRank, Gfx::EngineObjectType type); + Gfx::EngineObjectType GetObjectType(int objRank); + bool SetObjectTransparency(int objRank, float value); + + bool ShadowCreate(int objRank); + void ShadowDelete(int objRank); + bool SetObjectShadowHide(int objRank, bool hide); + bool SetObjectShadowType(int objRank, Gfx::EngineShadowType type); + bool SetObjectShadowPos(int objRank, const Math::Vector &pos); + bool SetObjectShadowNormal(int objRank, const Math::Vector &n); + bool SetObjectShadowAngle(int objRank, float angle); + bool SetObjectShadowRadius(int objRank, float radius); + bool SetObjectShadowIntensity(int objRank, float intensity); + bool SetObjectShadowHeight(int objRank, float h); + float GetObjectShadowRadius(int objRank); + + void GroundSpotFlush(); + int GroundSpotCreate(); + void GroundSpotDelete(int rank); + bool SetObjectGroundSpotPos(int rank, const Math::Vector &pos); + bool SetObjectGroundSpotRadius(int rank, float radius); + bool SetObjectGroundSpotColor(int rank, const Gfx::Color &color); + bool SetObjectGroundSpotMinMax(int rank, float min, float max); + bool SetObjectGroundSpotSmooth(int rank, float smooth); + + int GroundMarkCreate(Math::Vector pos, float radius, + float delay1, float delay2, float delay3, + int dx, int dy, char* table); + bool GroundMarkDelete(int rank); + + void Update(); + + void SetViewParams(const Math::Vector &eyePt, const Math::Vector &lookatPt, + const Math::Vector &upVec, float eyeDistance); + + Gfx::Texture CreateTexture(const std::string &texName, + const Gfx::TextureCreateParams ¶ms); + Gfx::Texture CreateTexture(const std::string &texName); + void DestroyTexture(const std::string &texName); + + bool LoadTexture(const std::string &name, int stage = 0); + bool LoadAllTextures(); + + void SetLimitLOD(int rank, float limit); + float GetLimitLOD(int rank, bool last=false); + + void SetTerrainVision(float vision); + + void SetGroundSpot(bool mode); + bool GetGroundSpot(); + void SetShadow(bool mode); + bool GetShadow(); + void SetDirty(bool mode); + bool GetDirty(); + void SetFog(bool mode); + bool GetFog(); + bool GetStateColor(); + + void SetSecondTexture(int texNum); + int GetSecondTexture(); + + void SetRankView(int rank); + int GetRankView(); + + void SetDrawWorld(bool draw); + void SetDrawFront(bool draw); + + void SetAmbientColor(const Gfx::Color &color, int rank = 0); + Gfx::Color GetAmbientColor(int rank = 0); + + void SetWaterAddColor(const Gfx::Color &color); + Gfx::Color GetWaterAddColor(); + + void SetFogColor(const Gfx::Color &color, int rank = 0); + Gfx::Color GetFogColor(int rank = 0); + + void SetDeepView(float length, int rank = 0, bool ref=false); + float GetDeepView(int rank = 0); + + void SetFogStart(float start, int rank = 0); + float GetFogStart(int rank = 0); + + void SetBackground(const std::string &name, Gfx::Color up = Gfx::Color(), Gfx::Color down = Gfx::Color(), + Gfx::Color cloudUp = Gfx::Color(), Gfx::Color cloudDown = Gfx::Color(), + bool full = false, bool quarter = false); + void GetBackground(const std::string &name, Gfx::Color &up, Gfx::Color &down, + Gfx::Color &cloudUp, Gfx::Color &cloudDown, + bool &full, bool &quarter); + void SetFrontsizeName(char *name); + void SetOverFront(bool front); + void SetOverColor(const Gfx::Color &color = Gfx::Color(), int mode = ENG_RSTATE_TCOLOR_BLACK); + + void SetParticleDensity(float value); + float GetParticleDensity(); + float ParticleAdapt(float factor); + + void SetClippingDistance(float value); + float GetClippingDistance(); + + void SetObjectDetail(float value); + float GetObjectDetail(); + + void SetGadgetQuantity(float value); + float GetGadgetQuantity(); + + void SetTextureQuality(int value); + int GetTextureQuality(); + + void SetTotoMode(bool present); + bool GetTotoMode(); + + void SetLensMode(bool present); + bool GetLensMode(); + + void SetWaterMode(bool present); + bool GetWaterMode(); + + void SetLightingMode(bool present); + bool GetLightingMode(); + + void SetSkyMode(bool present); + bool GetSkyMode(); + + void SetBackForce(bool present); + bool GetBackForce(); + + void SetPlanetMode(bool present); + bool GetPlanetMode(); + + void SetLightMode(bool present); + bool GetLightMode(); + + void SetEditIndentMode(bool autoIndent); + bool GetEditIndentMode(); + + void SetEditIndentValue(int value); + int GetEditIndentValue(); + + void SetSpeed(float speed); + float GetSpeed(); + + void SetTracePrecision(float factor); + float GetTracePrecision(); + + void SetFocus(float focus); + float GetFocus(); + Math::Vector GetEyePt(); + Math::Vector GetLookatPt(); + float GetEyeDirH(); + float GetEyeDirV(); + Math::Point GetDim(); + void UpdateMatProj(); + + void ApplyChange(); + + void FlushPressKey(); + void ResetKey(); + void SetKey(int keyRank, int option, int key); + int GetKey(int keyRank, int option); + + void SetJoystick(bool enable); + bool GetJoystick(); + + void SetDebugMode(bool mode); + bool GetDebugMode(); + bool GetSetupMode(); + + bool IsVisiblePoint(const Math::Vector &pos); + + int DetectObject(Math::Point mouse); + void SetState(int state, Gfx::Color color = Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); + void SetTexture(const std::string &name, int stage = 0); + void SetMaterial(const Gfx::Material &mat); + + void SetMouseVisible(bool show); + bool GetMouseVisible(); + void SetMousePos(Math::Point pos); + Math::Point GetMousePos(); + void SetMouseType(Gfx::EngineMouseType type); + Gfx::EngineMouseType GetMouseType(); + + CText* GetText(); + + bool ChangeColor(char *name, Gfx::Color colorRef1, Gfx::Color colorNew1, + Gfx::Color colorRef2, Gfx::Color colorNew2, + float tolerance1, float tolerance2, + Math::Point ts, Math::Point ti, + Math::Point *pExclu=0, float shift=0.0f, bool hSV=false); + bool OpenImage(char *name); + bool CopyImage(); + bool LoadImage(); + bool ScrollImage(int dx, int dy); + bool SetDot(int x, int y, Gfx::Color color); + bool CloseImage(); + bool WriteScreenShot(char *filename, int width, int height); + //bool GetRenderDC(HDC &hDC); + //bool ReleaseRenderDC(HDC &hDC); + //PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp); + //bool CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC); + +protected: + + void SetUp3DView(); + bool Draw3DScene(); + + void SetUpInterfaceView(); + bool DrawInterface(); + + void DrawGroundSpot(); + void DrawShadow(); + void DrawBackground(); + void DrawBackgroundGradient(Gfx::Color up, Gfx::Color down); + void DrawBackgroundImageQuarter(Math::Point p1, Math::Point p2, char *name); + void DrawBackgroundImage(); + void DrawPlanet(); + void DrawFrontsize(); + void DrawOverColor(); + void DrawHilite(); + void DrawMouse(); + void DrawMouseSprite(Math::Point pos, Math::Point dim, int icon); + + /* + Gfx::ObjLevel2* AddLevel1(Gfx::ObjLevel1 *&p1, char* texName1, char* texName2); + Gfx::ObjLevel3* AddLevel2(Gfx::ObjLevel2 *&p2, int objRank); + Gfx::ObjLevel4* AddLevel3(Gfx::ObjLevel3 *&p3, float min, float max); + Gfx::ObjLevel5* AddLevel4(Gfx::ObjLevel4 *&p4, int reserve); + Gfx::ObjLevel6* AddLevel5(Gfx::ObjLevel5 *&p5, Gfx::TriangleType type, const Gfx::Material &mat, int state, int nb);*/ + + bool IsVisible(int objRank); + bool DetectBBox(int objRank, Math::Point mouse); + bool GetBBox2D(int objRank, Math::Point &min, Math::Point &max); + bool DetectTriangle(Math::Point mouse, Gfx::VertexTex2 *triangle, int objRank, float &dist); + bool TransformPoint(Math::Vector &p2D, int objRank, Math::Vector p3D); + void ComputeDistance(); + void UpdateGeometry(); + +protected: + CInstanceManager* m_iMan; + CApplication* m_app; + CSound* m_sound; + Gfx::CDevice* m_device; + Gfx::CText* m_text; + Gfx::CLightManager* m_lightMan; + Gfx::CParticle* m_particle; + Gfx::CWater* m_water; + Gfx::CCloud* m_cloud; + Gfx::CLightning* m_lightning; + Gfx::CPlanet* m_planet; + Gfx::CTerrain* m_terrain; + + bool m_wasInit; + std::string m_error; + + int m_blackSrcBlend[2]; + int m_blackDestBlend[2]; + int m_whiteSrcBlend[2]; + int m_whiteDestBlend[2]; + int m_diffuseSrcBlend[2]; + int m_diffuseDestBlend[2]; + int m_alphaSrcBlend[2]; + int m_alphaDestBlend[2]; + + Math::Matrix m_matProj; + Math::Matrix m_matLeftView; + Math::Matrix m_matRightView; + Math::Matrix m_matView; + float m_focus; + + Math::Matrix m_matWorldInterface; + Math::Matrix m_matProjInterface; + Math::Matrix m_matViewInterface; + + long m_baseTime; + long m_stopTime; + float m_absTime; + float m_lastTime; + float m_speed; + bool m_pause; + bool m_render; + bool m_movieLock; + + Math::IntPoint m_dim; + Math::IntPoint m_lastDim; + + std::vector m_objectTree; + std::vector m_objects; + std::vector m_shadow; + std::vector m_groundSpot; + Gfx::EngineGroundMark m_groundMark; + + Math::Vector m_eyePt; + Math::Vector m_lookatPt; + float m_eyeDirH; + float m_eyeDirV; + int m_rankView; + Gfx::Color m_ambientColor[2]; + Gfx::Color m_backColor[2]; + Gfx::Color m_fogColor[2]; + float m_deepView[2]; + float m_fogStart[2]; + Gfx::Color m_waterAddColor; + int m_statisticTriangle; + bool m_updateGeometry; + //char m_infoText[10][200]; + int m_alphaMode; + bool m_stateColor; + bool m_forceStateColor; + bool m_groundSpotVisible; + bool m_shadowVisible; + bool m_dirty; + bool m_fog; + bool m_firstGroundSpot; + int m_secondTexNum; + std::string m_backgroundName; + Gfx::Color m_backgroundColorUp; + Gfx::Color m_backgroundColorDown; + Gfx::Color m_backgroundCloudUp; + Gfx::Color m_backgroundCloudDown; + bool m_backgroundFull; + bool m_backgroundQuarter; + bool m_overFront; + Gfx::Color m_overColor; + int m_overMode; + std::string m_frontsizeName; + bool m_drawWorld; + bool m_drawFront; + float m_limitLOD[2]; + float m_particuleDensity; + float m_clippingDistance; + float m_lastClippingDistance; + float m_objectDetail; + float m_lastObjectDetail; + float m_terrainVision; + float m_gadgetQuantity; + int m_textureQuality; + bool m_totoMode; + bool m_lensMode; + bool m_waterMode; + bool m_skyMode; + bool m_backForce; + bool m_planetMode; + bool m_lightMode; + bool m_editIndentMode; + int m_editIndentValue; + float m_tracePrecision; + + int m_hiliteRank[100]; + bool m_hilite; + Math::Point m_hiliteP1; + Math::Point m_hiliteP2; + + int m_lastState; + Gfx::Color m_lastColor; + char m_lastTexture[2][50]; + Gfx::Material m_lastMaterial; + + std::string m_texPath; + Gfx::TextureCreateParams m_defaultTexParams; + + std::map m_texNameMap; + std::map m_revTexNameMap; + + Gfx::EngineMouse m_mice[Gfx::ENG_MOUSE_COUNT]; + Gfx::Texture m_miceTexture; + Math::Point m_mouseSize; + Gfx::EngineMouseType m_mouseType; + Math::Point m_mousePos; + bool m_mouseVisible; + + //LPDIRECTDRAWSURFACE7 m_imageSurface; + //DDSURFACEDESC2 m_imageDDSD; + //WORD* m_imageCopy; + //int m_imageDX; + //int m_imageDY; +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/lightman.cpp b/src/graphics/engine/lightman.cpp new file mode 100644 index 0000000..9e15b5a --- /dev/null +++ b/src/graphics/engine/lightman.cpp @@ -0,0 +1,416 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// light.cpp + +#include "graphics/engine/lightman.h" + +#include "common/iman.h" +#include "graphics/core/device.h" +#include "math/geometry.h" + +#include + + +void Gfx::LightProgression::Init(float value) +{ + starting = value; + ending = value; + current = value; + progress = 0.0f; + speed = 100.0f; +} + +void Gfx::LightProgression::Update(float rTime) +{ + if (speed < 100.0f) + { + if (progress < 1.0f) + { + progress += speed * rTime; + if (progress > 1.0f) + progress = 1.0f; + } + + current = starting + progress * (ending - starting); + } + else + { + current = ending; + } +} + +void Gfx::LightProgression::SetTarget(float value) +{ + starting = current; + ending = value; + progress = 0.0f; +} + + +Gfx::DynamicLight::DynamicLight() +{ + used = enabled = false; +} + + + +Gfx::CLightManager::CLightManager(CInstanceManager* iMan, Gfx::CEngine* engine) +{ + m_iMan = iMan; + m_iMan->AddInstance(CLASS_LIGHT, this); + + m_device = NULL; + m_engine = engine; + + m_time = 0.0f; +} + +Gfx::CLightManager::~CLightManager() +{ + m_iMan->DeleteInstance(CLASS_LIGHT, this); + + m_iMan = NULL; + m_device = NULL; + m_engine = NULL; +} + +void Gfx::CLightManager::SetDevice(Gfx::CDevice* device) +{ + m_device = device; + + m_dynLights = std::vector(m_device->GetMaxLightCount(), Gfx::DynamicLight()); +} + +void Gfx::CLightManager::FlushLights() +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + m_dynLights[i].used = false; + m_device->SetLightEnabled(i, false); + } +} + +/** Returns the index of light created or -1 if all lights are used. */ +int Gfx::CLightManager::CreateLight() +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (m_dynLights[i].used) continue; + + m_dynLights[i] = Gfx::DynamicLight(); + + m_dynLights[i].used = true; + m_dynLights[i].enabled = true; + + m_dynLights[i].includeType = Gfx::ENG_OBJTYPE_NULL; + m_dynLights[i].excludeType = Gfx::ENG_OBJTYPE_NULL; + + m_dynLights[i].light.type = Gfx::LIGHT_DIRECTIONAL; + m_dynLights[i].light.diffuse = Gfx::Color(0.5f, 0.5f, 0.5f); + m_dynLights[i].light.position = Math::Vector(-100.0f, 100.0f, -100.0f); + m_dynLights[i].light.direction = Math::Vector( 1.0f, -1.0f, 1.0f); + + m_dynLights[i].intensity.Init(1.0f); // maximum + m_dynLights[i].colorRed.Init(0.5f); + m_dynLights[i].colorGreen.Init(0.5f); + m_dynLights[i].colorBlue.Init(0.5f); // gray + + return i; + } + + return -1; +} + +bool Gfx::CLightManager::DeleteLight(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].used = false; + m_device->SetLightEnabled(lightRank, false); + + return true; +} + +// Specifies a light. + +bool Gfx::CLightManager::SetLight(int lightRank, const Gfx::Light &light) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].light = light; + + m_dynLights[lightRank].colorRed.Init(m_dynLights[lightRank].light.diffuse.r); + m_dynLights[lightRank].colorGreen.Init(m_dynLights[lightRank].light.diffuse.g); + m_dynLights[lightRank].colorBlue.Init(m_dynLights[lightRank].light.diffuse.b); + + return true; +} + +bool Gfx::CLightManager::GetLight(int lightRank, Gfx::Light &light) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + light = m_dynLights[lightRank].light; + return true; +} + +bool Gfx::CLightManager::SetLightEnabled(int lightRank, bool enabled) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].enabled = enabled; + return true; +} + +bool Gfx::CLightManager::SetLightIncludeType(int lightRank, Gfx::EngineObjectType type) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].includeType = type; + return true; +} + +bool Gfx::CLightManager::SetLightExcludeType(int lightRank, Gfx::EngineObjectType type) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].excludeType = type; + return true; +} + +bool Gfx::CLightManager::SetLightPos(int lightRank, const Math::Vector &pos) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].light.position = pos; + return true; +} + +Math::Vector Gfx::CLightManager::GetLightPos(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return Math::Vector(0.0f, 0.0f, 0.0f); + + return m_dynLights[lightRank].light.position; +} + +bool Gfx::CLightManager::SetLightDir(int lightRank, const Math::Vector &dir) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].light.direction = dir; + return true; +} + +Math::Vector Gfx::CLightManager::GetLightDir(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return Math::Vector(0.0f, 0.0f, 0.0f); + + return m_dynLights[lightRank].light.direction; +} + +bool Gfx::CLightManager::SetLightIntensitySpeed(int lightRank, float speed) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].intensity.speed = speed; + return true; +} + +bool Gfx::CLightManager::SetLightIntensity(int lightRank, float value) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].intensity.SetTarget(value); + return true; +} + +float Gfx::CLightManager::GetLightIntensity(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return 0.0f; + + return m_dynLights[lightRank].intensity.current; +} + + +bool Gfx::CLightManager::SetLightColorSpeed(int lightRank, float speed) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].colorRed.speed = speed; + m_dynLights[lightRank].colorGreen.speed = speed; + m_dynLights[lightRank].colorBlue.speed = speed; + return true; +} + +bool Gfx::CLightManager::SetLightColor(int lightRank, const Gfx::Color &color) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return false; + + m_dynLights[lightRank].colorRed.SetTarget(color.r); + m_dynLights[lightRank].colorGreen.SetTarget(color.g); + m_dynLights[lightRank].colorBlue.SetTarget(color.b); + return true; +} + +Gfx::Color Gfx::CLightManager::GetLightColor(int lightRank) +{ + if ( (lightRank < 0) || (lightRank >= static_cast( m_dynLights.size() )) ) + return Gfx::Color(0.5f, 0.5f, 0.5f, 0.5f); + + Gfx::Color color; + color.r = m_dynLights[lightRank].colorRed.current; + color.g = m_dynLights[lightRank].colorGreen.current; + color.b = m_dynLights[lightRank].colorBlue.current; + return color; +} + +void Gfx::CLightManager::AdaptLightColor(const Gfx::Color &color, float factor) +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + + Gfx::Color value; + value.r = m_dynLights[i].colorRed.current; + value.g = m_dynLights[i].colorGreen.current; + value.b = m_dynLights[i].colorBlue.current; + + value.r += color.r * factor; + value.g += color.g * factor; + value.b += color.b * factor; + + m_dynLights[i].colorRed.Init(value.r); + m_dynLights[i].colorGreen.Init(value.g); + m_dynLights[i].colorBlue.Init(value.b); + } + + UpdateLights(); +} + +void Gfx::CLightManager::UpdateProgression(float rTime) +{ + if (m_engine->GetPause()) + return; + + m_time += rTime; + + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + + m_dynLights[i].intensity.Update(rTime); + m_dynLights[i].colorRed.Update(rTime); + m_dynLights[i].colorGreen.Update(rTime); + m_dynLights[i].colorBlue.Update(rTime); + + if (m_dynLights[i].includeType == Gfx::ENG_OBJTYPE_QUARTZ) + { + m_dynLights[i].light.direction.x = sinf(1.0f * (m_time + i*Math::PI*0.5f)); + m_dynLights[i].light.direction.z = cosf(1.1f * (m_time + i*Math::PI*0.5f)); + m_dynLights[i].light.direction.y = -1.0f + 0.5f * cosf((m_time + i*Math::PI*0.5f)*2.7f); + } + + if (m_dynLights[i].includeType == Gfx::ENG_OBJTYPE_METAL) + { + Math::Vector dir = m_engine->GetEyePt() - m_engine->GetLookatPt(); + float angle = Math::RotateAngle(dir.x, dir.z); + angle += Math::PI * 0.5f * i; + m_dynLights[i].light.direction.x = sinf(2.0f * angle); + m_dynLights[i].light.direction.z = cosf(2.0f * angle); + } + } +} + + +void Gfx::CLightManager::UpdateLights() +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + + bool enabled = m_dynLights[i].enabled; + if (m_dynLights[i].intensity.current == 0.0f) + enabled = false; + + if (enabled) + { + float value = m_dynLights[i].colorRed.current * m_dynLights[i].intensity.current; + m_dynLights[i].light.diffuse.r = value; + + value = m_dynLights[i].colorGreen.current * m_dynLights[i].intensity.current; + m_dynLights[i].light.diffuse.g = value; + + value = m_dynLights[i].colorBlue.current * m_dynLights[i].intensity.current; + m_dynLights[i].light.diffuse.b = value; + + m_device->SetLight(i, m_dynLights[i].light); + m_device->SetLightEnabled(i, enabled); + } + else + { + m_dynLights[i].light.diffuse.r = 0.0f; + m_dynLights[i].light.diffuse.g = 0.0f; + m_dynLights[i].light.diffuse.b = 0.0f; + + m_device->SetLightEnabled(i, enabled); + } + } +} + +void Gfx::CLightManager::UpdateLightsEnableState(Gfx::EngineObjectType type) +{ + for (int i = 0; i < static_cast( m_dynLights.size() ); i++) + { + if (! m_dynLights[i].used) + continue; + if (! m_dynLights[i].enabled) + continue; + if (m_dynLights[i].intensity.current == 0.0f) + continue; + + if (m_dynLights[i].includeType != Gfx::ENG_OBJTYPE_NULL) + { + bool enabled = (m_dynLights[i].includeType == type); + m_device->SetLightEnabled(i, enabled); + } + + if (m_dynLights[i].excludeType != Gfx::ENG_OBJTYPE_NULL) + { + bool enabled = (m_dynLights[i].excludeType != type); + m_device->SetLightEnabled(i, enabled); + } + } +} diff --git a/src/graphics/engine/lightman.h b/src/graphics/engine/lightman.h new file mode 100644 index 0000000..8272125 --- /dev/null +++ b/src/graphics/engine/lightman.h @@ -0,0 +1,181 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// lightman.h + +#pragma once + + +#include "graphics/core/color.h" +#include "graphics/core/light.h" +#include "graphics/engine/engine.h" +#include "math/vector.h" + + +namespace Gfx { + +/** + \struct LightProgression + \brief Describes the progression of light parameters change */ +struct LightProgression +{ + //! Starting value + float starting; + //! Ending (destination) value + float ending; + //! Current value + float current; + //! Progress from start to end + float progress; + //! Speed of progression + float speed; + + LightProgression() + { + starting = ending = current = progress = speed = 0.0f; + } + + //! Initializes the progression + void Init(float value); + + //! Updates the progression + void Update(float rTime); + + //! Sets the new end value (starting is set to current) + void SetTarget(float value); +}; + +/** + \struct DynamicLight + \brief Dynamic light in 3D scene + + It is an extension over standard light properties. Added are dynamic progressions for light + colors and intensity and types of objects included/excluded in lighting. */ +struct DynamicLight +{ + //! Whether the light is used + bool used; + //! Whether the light is turned on + bool enabled; + + //! Configuration of the light + Gfx::Light light; + + //! Progression of intensity [0, 1] + Gfx::LightProgression intensity; + //! Progression of red diffuse color + Gfx::LightProgression colorRed; + //! Progression of green diffuse color + Gfx::LightProgression colorGreen; + //! Progression of blue diffuse color + Gfx::LightProgression colorBlue; + + //! Type of objects included in lighting with this light; if Gfx::ENG_OBJTYPE_NULL is used, it is ignored + Gfx::EngineObjectType includeType; + //! Type of objects excluded from lighting with this light; if Gfx::ENG_OBJTYPE_NULL is used, it is ignored + Gfx::EngineObjectType excludeType; + + DynamicLight(); +}; + +/** + \class CLightManager + \brief Manager for dynamic lights in 3D scene + + (Old CLight class) + + The class is responsible for managing dynamic lights (struct Gfx::DynamicLight) used in 3D scene. + The dynamic lights are created, updated and deleted through the class' interface. + + Number of available lights depends on graphics device used. Class allocates vector + for the total number of lights, but only some are used. + */ +class CLightManager +{ +public: + //! Constructor + CLightManager(CInstanceManager *iMan, Gfx::CEngine* engine); + //! Destructor + virtual ~CLightManager(); + + //! Sets the device to be used + void SetDevice(Gfx::CDevice* device); + + //! Clears and disables all lights + void FlushLights(); + //! Creates a new dynamic light and returns its index (lightRank) + int CreateLight(); + //! Deletes and disables the given dynamic light + bool DeleteLight(int lightRank); + //! Sets the light parameters for dynamic light + bool SetLight(int lightRank, const Gfx::Light &light); + //! Returns the light parameters for given dynamic light + bool GetLight(int lightRank, Gfx::Light &light); + //! Enables/disables the given dynamic light + bool SetLightEnabled(int lightRank, bool enable); + + //! Sets what objects are included in given dynamic light + bool SetLightIncludeType(int lightRank, Gfx::EngineObjectType type); + //! Sets what objects are excluded from given dynamic light + bool SetLightExcludeType(int lightRank, Gfx::EngineObjectType type); + + //! Sets the position of dynamic light + bool SetLightPos(int lightRank, const Math::Vector &pos); + //! Returns the position of dynamic light + Math::Vector GetLightPos(int lightRank); + + //! Sets the direction of dynamic light + bool SetLightDir(int lightRank, const Math::Vector &dir); + //! Returns the direction of dynamic light + Math::Vector GetLightDir(int lightRank); + + //! Sets the destination intensity for dynamic light's intensity progression + bool SetLightIntensity(int lightRank, float value); + //! Returns the current light intensity + float GetLightIntensity(int lightRank); + //! Sets the rate of change for dynamic light intensity + bool SetLightIntensitySpeed(int lightRank, float speed); + + //! Adjusts the color of all dynamic lights + void AdaptLightColor(const Gfx::Color &color, float factor); + + //! Sets the destination color for dynamic light's color progression + bool SetLightColor(int lightRank, const Gfx::Color &color); + //! Returns current light color + Gfx::Color GetLightColor(int lightRank); + //! Sets the rate of change for dynamic light colors (RGB) + bool SetLightColorSpeed(int lightRank, float speed); + + //! Updates progression of dynamic lights + void UpdateProgression(float rTime); + //! Updates (recalculates) all dynamic lights + void UpdateLights(); + //! Enables or disables dynamic lights affecting the given object type + void UpdateLightsEnableState(Gfx::EngineObjectType type); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CDevice* m_device; + + //! Current time + float m_time; + //! List of dynamic lights + std::vector m_dynLights; +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/lightning.cpp b/src/graphics/engine/lightning.cpp new file mode 100644 index 0000000..4db5511 --- /dev/null +++ b/src/graphics/engine/lightning.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// lightning.cpp (aka blitz.cpp) + +#include "graphics/engine/lightning.h" + + +// TODO implementation diff --git a/src/graphics/engine/lightning.h b/src/graphics/engine/lightning.h new file mode 100644 index 0000000..957344c --- /dev/null +++ b/src/graphics/engine/lightning.h @@ -0,0 +1,85 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// lightning.h (aka blitz.h) + +#pragma once + +#include "common/misc.h" +#include "math/vector.h" + + +class CInstanceManager; +class CObject; +class CSound; + + +namespace Gfx { + +class CEngine; +class CTerrain; +class CCamera; + + +const float BLITZPARA = 200.0f; // radius of lightning protection +const short BLITZMAX = 50; + +enum BlitzPhase +{ + BPH_WAIT, + BPH_BLITZ, +}; + + +class CLightning +{ +public: + CLightning(CInstanceManager* iMan, CEngine* engine); + ~CLightning(); + + void Flush(); + bool EventProcess(const Event &event); + bool Create(float sleep, float delay, float magnetic); + bool GetStatus(float &sleep, float &delay, float &magnetic, float &progress); + bool SetStatus(float sleep, float delay, float magnetic, float progress); + void Draw(); + +protected: + bool EventFrame(const Event &event); + CObject* SearchObject(Math::Vector pos); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CTerrain* m_terrain; + CCamera* m_camera; + CSound* m_sound; + + bool m_bBlitzExist; + float m_sleep; + float m_delay; + float m_magnetic; + BlitzPhase m_phase; + float m_time; + float m_speed; + float m_progress; + Math::Vector m_pos; + Math::Point m_shift[BLITZMAX]; + float m_width[BLITZMAX]; +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/modelfile.cpp b/src/graphics/engine/modelfile.cpp new file mode 100644 index 0000000..537add4 --- /dev/null +++ b/src/graphics/engine/modelfile.cpp @@ -0,0 +1,841 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// modelfile.cpp (aka modfile.cpp) + +#include "graphics/engine/modelfile.h" + +#include "common/iman.h" +#include "common/ioutils.h" +#include "common/logger.h" +#include "common/stringutils.h" +#include "math/geometry.h" + +#include + +#include + + +//! How big the triangle vector is by default +const int TRIANGLE_PREALLOCATE_COUNT = 2000; + +/** + \struct ModelHeader + \brief Header info for model file + */ +struct ModelHeader +{ + //! Revision number + int revision; + //! Version number + int version; + //! Total number of vertices + int totalVertices; + //! Reserved area + int reserved[10]; + + ModelHeader() + { + memset(this, 0, sizeof(*this)); + } +}; + + +struct OldModelTriangle1 +{ + char used; + char selected; + Gfx::Vertex p1; + Gfx::Vertex p2; + Gfx::Vertex p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + + OldModelTriangle1() + { + memset(this, 0, sizeof(*this)); + } +}; + +struct OldModelTriangle2 +{ + char used; + char selected; + Gfx::Vertex p1; + Gfx::Vertex p2; + Gfx::Vertex p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + long state; + short reserved1; + short reserved2; + short reserved3; + short reserved4; + OldModelTriangle2() + { + memset(this, 0, sizeof(*this)); + } +}; + + +struct NewModelTriangle +{ + char used; + char selected; + Gfx::VertexTex2 p1; + Gfx::VertexTex2 p2; + Gfx::VertexTex2 p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + long state; + short texNum2; + short reserved2; + short reserved3; + short reserved4; + + NewModelTriangle() + { + memset(this, 0, sizeof(*this)); + } +}; + + +Gfx::Vertex ReadBinaryVertex(std::istream &stream) +{ + Gfx::Vertex result; + + result.coord.x = IOUtils::ReadBinaryFloat(stream); + result.coord.y = IOUtils::ReadBinaryFloat(stream); + result.coord.z = IOUtils::ReadBinaryFloat(stream); + result.normal.x = IOUtils::ReadBinaryFloat(stream); + result.normal.y = IOUtils::ReadBinaryFloat(stream); + result.normal.z = IOUtils::ReadBinaryFloat(stream); + result.texCoord.x = IOUtils::ReadBinaryFloat(stream); + result.texCoord.y = IOUtils::ReadBinaryFloat(stream); + + return result; +} + +void WriteBinaryVertex(Gfx::Vertex vertex, std::ostream &stream) +{ + IOUtils::WriteBinaryFloat(vertex.coord.x, stream); + IOUtils::WriteBinaryFloat(vertex.coord.y, stream); + IOUtils::WriteBinaryFloat(vertex.coord.z, stream); + IOUtils::WriteBinaryFloat(vertex.normal.x, stream); + IOUtils::WriteBinaryFloat(vertex.normal.y, stream); + IOUtils::WriteBinaryFloat(vertex.normal.z, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); +} + +Gfx::VertexTex2 ReadBinaryVertexTex2(std::istream &stream) +{ + Gfx::VertexTex2 result; + + result.coord.x = IOUtils::ReadBinaryFloat(stream); + result.coord.y = IOUtils::ReadBinaryFloat(stream); + result.coord.z = IOUtils::ReadBinaryFloat(stream); + result.normal.x = IOUtils::ReadBinaryFloat(stream); + result.normal.y = IOUtils::ReadBinaryFloat(stream); + result.normal.z = IOUtils::ReadBinaryFloat(stream); + result.texCoord.x = IOUtils::ReadBinaryFloat(stream); + result.texCoord.y = IOUtils::ReadBinaryFloat(stream); + result.texCoord2.x = IOUtils::ReadBinaryFloat(stream); + result.texCoord2.y = IOUtils::ReadBinaryFloat(stream); + + return result; +} + +void WriteBinaryVertexTex2(Gfx::VertexTex2 vertex, std::ostream &stream) +{ + IOUtils::WriteBinaryFloat(vertex.coord.x, stream); + IOUtils::WriteBinaryFloat(vertex.coord.y, stream); + IOUtils::WriteBinaryFloat(vertex.coord.z, stream); + IOUtils::WriteBinaryFloat(vertex.normal.x, stream); + IOUtils::WriteBinaryFloat(vertex.normal.y, stream); + IOUtils::WriteBinaryFloat(vertex.normal.z, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord2.x, stream); + IOUtils::WriteBinaryFloat(vertex.texCoord2.y, stream); +} + +Gfx::Material ReadBinaryMaterial(std::istream &stream) +{ + Gfx::Material result; + + result.diffuse.r = IOUtils::ReadBinaryFloat(stream); + result.diffuse.g = IOUtils::ReadBinaryFloat(stream); + result.diffuse.b = IOUtils::ReadBinaryFloat(stream); + result.diffuse.a = IOUtils::ReadBinaryFloat(stream); + + result.ambient.r = IOUtils::ReadBinaryFloat(stream); + result.ambient.g = IOUtils::ReadBinaryFloat(stream); + result.ambient.b = IOUtils::ReadBinaryFloat(stream); + result.ambient.a = IOUtils::ReadBinaryFloat(stream); + + result.specular.r = IOUtils::ReadBinaryFloat(stream); + result.specular.g = IOUtils::ReadBinaryFloat(stream); + result.specular.b = IOUtils::ReadBinaryFloat(stream); + result.specular.a = IOUtils::ReadBinaryFloat(stream); + + /* emissive.r = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.g = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.b = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.a = */ IOUtils::ReadBinaryFloat(stream); + + /* power = */ IOUtils::ReadBinaryFloat(stream); + + return result; +} + +void WriteBinaryMaterial(Gfx::Material material, std::ostream &stream) +{ + IOUtils::WriteBinaryFloat(material.diffuse.r, stream); + IOUtils::WriteBinaryFloat(material.diffuse.g, stream); + IOUtils::WriteBinaryFloat(material.diffuse.b, stream); + IOUtils::WriteBinaryFloat(material.diffuse.a, stream); + + IOUtils::WriteBinaryFloat(material.ambient.r, stream); + IOUtils::WriteBinaryFloat(material.ambient.g, stream); + IOUtils::WriteBinaryFloat(material.ambient.b, stream); + IOUtils::WriteBinaryFloat(material.ambient.a, stream); + + IOUtils::WriteBinaryFloat(material.specular.r, stream); + IOUtils::WriteBinaryFloat(material.specular.g, stream); + IOUtils::WriteBinaryFloat(material.specular.b, stream); + IOUtils::WriteBinaryFloat(material.specular.a, stream); + + /* emissive.r */ IOUtils::WriteBinaryFloat(0.0f, stream); + /* emissive.g */ IOUtils::WriteBinaryFloat(0.0f, stream); + /* emissive.b */ IOUtils::WriteBinaryFloat(0.0f, stream); + /* emissive.a */ IOUtils::WriteBinaryFloat(0.0f, stream); + + /* power */ IOUtils::WriteBinaryFloat(0.0f, stream); +} + +Gfx::ModelTriangle::ModelTriangle() +{ + min = 0.0f; + max = 0.0f; + state = 0L; +} + + +Gfx::CModelFile::CModelFile(CInstanceManager* iMan) +{ + m_iMan = iMan; + + m_engine = (CEngine*)m_iMan->SearchInstance(CLASS_ENGINE); + + m_triangles.reserve(TRIANGLE_PREALLOCATE_COUNT); +} + +Gfx::CModelFile::~CModelFile() +{ +} + +std::string Gfx::CModelFile::GetError() +{ + return m_error; +} + + +bool Gfx::CModelFile::ReadModel(const std::string &filename, bool edit, bool meta) +{ + m_triangles.clear(); + m_error = ""; + + std::ifstream stream; + stream.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); + if (! stream.good()) + { + m_error = std::string("Could not open file '") + filename + std::string("'"); + return false; + } + + return ReadModel(stream, edit, meta); +} + +bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) +{ + m_triangles.clear(); + m_error = ""; + + // FIXME: for now, reading models only from files, not metafile + + ModelHeader header; + + header.revision = IOUtils::ReadBinary<4, int>(stream); + header.version = IOUtils::ReadBinary<4, int>(stream); + header.totalVertices = IOUtils::ReadBinary<4, int>(stream); + for (int i = 0; i < 10; ++i) + header.reserved[i] = IOUtils::ReadBinary<4, int>(stream); + + + if (! stream.good()) + { + m_error = "Error reading model file header"; + return false; + } + + // Old model version #1 + if ( (header.revision == 1) && (header.version == 0) ) + { + for (int i = 0; i < header.totalVertices; ++i) + { + OldModelTriangle1 t; + t.used = IOUtils::ReadBinary<1, char>(stream); + t.selected = IOUtils::ReadBinary<1, char>(stream); + + t.p1 = ReadBinaryVertex(stream); + t.p2 = ReadBinaryVertex(stream); + t.p3 = ReadBinaryVertex(stream); + + t.material = ReadBinaryMaterial(stream); + stream.read(t.texName, 20); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + + if (! stream.good()) + { + m_error = "Error reading model data"; + return false; + } + + Gfx::ModelTriangle triangle; + triangle.p1.FromVertex(t.p1); + triangle.p2.FromVertex(t.p2); + triangle.p3.FromVertex(t.p3); + + triangle.material = t.material; + triangle.tex1Name = std::string(t.texName); + triangle.min = t.min; + triangle.max = t.max; + + m_triangles.push_back(triangle); + } + } + else if ( header.revision == 1 && header.version == 1 ) + { + for (int i = 0; i < header.totalVertices; ++i) + { + OldModelTriangle2 t; + t.used = IOUtils::ReadBinary<1, char>(stream); + t.selected = IOUtils::ReadBinary<1, char>(stream); + + t.p1 = ReadBinaryVertex(stream); + t.p2 = ReadBinaryVertex(stream); + t.p3 = ReadBinaryVertex(stream); + + t.material = ReadBinaryMaterial(stream); + stream.read(t.texName, 20); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + t.state = IOUtils::ReadBinary<4, long>(stream); + + t.reserved1 = IOUtils::ReadBinary<2, short>(stream); + t.reserved2 = IOUtils::ReadBinary<2, short>(stream); + t.reserved3 = IOUtils::ReadBinary<2, short>(stream); + t.reserved4 = IOUtils::ReadBinary<2, short>(stream); + + if (! stream.good()) + { + m_error = "Error reading model data"; + return false; + } + + Gfx::ModelTriangle triangle; + triangle.p1.FromVertex(t.p1); + triangle.p2.FromVertex(t.p2); + triangle.p3.FromVertex(t.p3); + + triangle.material = t.material; + triangle.tex1Name = std::string(t.texName); + triangle.min = t.min; + triangle.max = t.max; + triangle.state = t.state; + + m_triangles.push_back(triangle); + } + } + else + { + for (int i = 0; i < header.totalVertices; ++i) + { + NewModelTriangle t; + t.used = IOUtils::ReadBinary<1, char>(stream); + t.selected = IOUtils::ReadBinary<1, char>(stream); + + /* padding */ IOUtils::ReadBinary<2, unsigned int>(stream); + + t.p1 = ReadBinaryVertexTex2(stream); + t.p2 = ReadBinaryVertexTex2(stream); + t.p3 = ReadBinaryVertexTex2(stream); + + t.material = ReadBinaryMaterial(stream); + stream.read(t.texName, 20); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + t.state = IOUtils::ReadBinary<4, long>(stream); + t.texNum2 = IOUtils::ReadBinary<2, short>(stream); + + t.reserved2 = IOUtils::ReadBinary<2, short>(stream); + t.reserved3 = IOUtils::ReadBinary<2, short>(stream); + t.reserved4 = IOUtils::ReadBinary<2, short>(stream); + + if (! stream.good()) + { + m_error = "Error reading model data"; + return false; + } + + Gfx::ModelTriangle triangle; + triangle.p1 = t.p1; + triangle.p2 = t.p2; + triangle.p3 = t.p3; + + triangle.material = t.material; + triangle.tex1Name = std::string(t.texName); + char tex2Name[20] = { 0 }; + triangle.min = t.min; + triangle.max = t.max; + triangle.state = t.state; + + if (t.texNum2 != 0) + sprintf(tex2Name, "dirty%.2d.tga", t.texNum2); // hardcoded as in the original code + + triangle.tex2Name = std::string(tex2Name); + + m_triangles.push_back(triangle); + } + } + + for (int i = 0; i < (int) m_triangles.size(); ++i) + { + m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "tga"); + + GetLogger()->Info("ModelTriangle %d\n", i+1); + std::string s1 = m_triangles[i].p1.ToString(); + GetLogger()->Info(" p1: %s\n", s1.c_str()); + std::string s2 = m_triangles[i].p2.ToString(); + GetLogger()->Info(" p2: %s\n", s2.c_str()); + std::string s3 = m_triangles[i].p3.ToString(); + GetLogger()->Info(" p3: %s\n", s3.c_str()); + + std::string d = m_triangles[i].material.diffuse.ToString(); + std::string a = m_triangles[i].material.ambient.ToString(); + std::string s = m_triangles[i].material.specular.ToString(); + GetLogger()->Info(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); + + GetLogger()->Info(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), m_triangles[i].tex2Name.c_str()); + GetLogger()->Info(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); + GetLogger()->Info(" state: %ld\n", m_triangles[i].state); + } + + /* + if (! edit) + { + float limit[2]; + limit[0] = m_engine->RetLimitLOD(0); // frontier AB as config + limit[1] = m_engine->RetLimitLOD(1); // frontier BC as config + + // Standard frontiers -> config. + for (int i = 0; i < m_triangles.size(); ++i) + { + if ( m_triangles[i].min == 0.0f && + m_triangles[i].max == 100.0f ) // resolution A ? + { + m_triangles[i].max = limit[0]; + } + else if ( m_triangles[i].min == 100.0f && + m_triangles[i].max == 200.0f ) // resolution B ? + { + m_triangles[i].min = limit[0]; + m_triangles[i].max = limit[1]; + } + else if ( m_triangles[i].min == 200.0f && + m_triangles[i].max == 1000000.0f ) // resolution C ? + { + m_triangles[i].min = limit[1]; + } + } + }*/ + + return true; +} + +bool Gfx::CModelFile::WriteModel(const std::string &filename) +{ + m_error = ""; + + std::ofstream stream; + stream.open(filename.c_str(), std::ios_base::out | std::ios_base::binary); + if (! stream.good()) + { + m_error = std::string("Could not open file '") + filename + std::string("'"); + return false; + } + + return WriteModel(stream); +} + +bool Gfx::CModelFile::WriteModel(std::ostream &stream) +{ + m_error = ""; + + if (m_triangles.size() == 0) + { + m_error = "Empty model"; + return false; + } + + ModelHeader header; + header.revision = 1; + header.version = 2; + header.totalVertices = m_triangles.size(); + + IOUtils::WriteBinary<4, int>(header.revision, stream); + IOUtils::WriteBinary<4, int>(header.version, stream); + IOUtils::WriteBinary<4, int>(header.totalVertices, stream); + for (int i = 0; i < 10; ++i) + IOUtils::WriteBinary<4, int>(header.reserved[i], stream); + + for (int i = 0; i < (int)m_triangles.size(); ++i) + { + NewModelTriangle t; + + t.used = true; + + t.p1 = m_triangles[i].p1; + t.p2 = m_triangles[i].p2; + t.p3 = m_triangles[i].p3; + + t.material = m_triangles[i].material; + strncpy(t.texName, m_triangles[i].tex1Name.c_str(), 20); + t.min = m_triangles[i].min; + t.max = m_triangles[i].max; + t.state = m_triangles[i].state; + int no = 0; + sscanf(m_triangles[i].tex2Name.c_str(), "dirty%d.tga", &no); // hardcoded as in the original code + t.texNum2 = no; + + + IOUtils::WriteBinary<1, char>(t.used, stream); + IOUtils::WriteBinary<1, char>(t.selected, stream); + + WriteBinaryVertexTex2(t.p1, stream); + WriteBinaryVertexTex2(t.p2, stream); + WriteBinaryVertexTex2(t.p3, stream); + + WriteBinaryMaterial(t.material, stream); + stream.write(t.texName, 20); + IOUtils::WriteBinaryFloat(t.min, stream); + IOUtils::WriteBinaryFloat(t.max, stream); + IOUtils::WriteBinary<4, long>(t.state, stream); + IOUtils::WriteBinary<2, short>(t.texNum2, stream); + + IOUtils::WriteBinary<2, short>(t.reserved2, stream); + IOUtils::WriteBinary<2, short>(t.reserved3, stream); + IOUtils::WriteBinary<2, short>(t.reserved4, stream); + } + + return true; +} + +bool Gfx::CModelFile::ReadDXF(const std::string &filename, float min, float max) +{ + m_triangles.clear(); + m_error = ""; + + std::ifstream stream; + stream.open(filename.c_str(), std::ios_base::in); + if (! stream.good()) + { + m_error = std::string("Couldn't open file '") + filename + std::string("'"); + return false; + } + + return ReadDXF(stream, min, max); +} + +bool Gfx::CModelFile::ReadDXF(std::istream &stream, float min, float max) +{ + m_triangles.clear(); + m_error = ""; + + if (! stream.good()) + { + m_error = "Invalid stream"; + return false; + } + + // Input state + bool waitNumVertex = false; + bool waitNumFace = false; + bool waitVertexX = false; + bool waitVertexY = false; + bool waitVertexZ = false; + bool waitFaceX = false; + bool waitFaceY = false; + bool waitFaceZ = false; + + // Vertex array + std::vector vertices; + vertices.reserve(TRIANGLE_PREALLOCATE_COUNT); + + // Number of vertices & faces of the primitive to be read + int vertexNum = 0, faceNum = 0; + // Vertex coords + Math::Vector coords; + // Indexes of face (triangle) points + int p1 = 0, p2 = 0, p3 = 0; + + // Input line + std::string line; + while (! stream.eof() ) + { + // Read line with command + std::getline(stream, line); + int command = StrUtils::FromString(line); + + // Read line with param + std::getline(stream, line); + + bool ok = true; + + + if (command == 66) + { + waitNumVertex = true; + } + + if ( command == 71 && waitNumVertex ) + { + waitNumVertex = false; + vertexNum = StrUtils::FromString(line, &ok); + waitNumFace = true; + } + + if ( command == 72 && waitNumFace ) + { + waitNumFace = false; + faceNum = StrUtils::FromString(line, &ok); + waitVertexX = true; + } + + if ( command == 10 && waitVertexX ) + { + waitVertexX = false; + coords.x = StrUtils::FromString(line, &ok); + waitVertexY = true; + } + + if ( command == 20 && waitVertexY ) + { + waitVertexY = false; + coords.y = StrUtils::FromString(line, &ok); + waitVertexZ = true; + } + + if ( command == 30 && waitVertexZ ) + { + waitVertexZ = false; + coords.z = StrUtils::FromString(line, &ok); + + vertexNum --; + if ( vertexNum >= 0 ) + { + Math::Vector p(coords.x, coords.z, coords.y); // permutation of Y and Z! + vertices.push_back(p); + waitVertexX = true; + } + else + { + waitFaceX = true; + } + } + + if ( command == 71 && waitFaceX ) + { + waitFaceX = false; + p1 = StrUtils::FromString(line, &ok); + if ( p1 < 0 ) p1 = -p1; + waitFaceY = true; + } + + if ( command == 72 && waitFaceY ) + { + waitFaceY = false; + p2 = StrUtils::FromString(line, &ok); + if ( p2 < 0 ) p2 = -p2; + waitFaceZ = true; + } + + if ( command == 73 && waitFaceZ ) + { + waitFaceZ = false; + p3 = StrUtils::FromString(line, &ok); + if ( p3 < 0 ) p3 = -p3; + + faceNum --; + if ( faceNum >= 0 ) + { + assert( (p1-1 >= 0) && (p1-1 < (int)vertices.size() ) ); + assert( (p2-1 >= 0) && (p2-1 < (int)vertices.size() ) ); + assert( (p3-1 >= 0) && (p3-1 < (int)vertices.size() ) ); + + CreateTriangle(vertices[p3-1], vertices[p2-1], vertices[p1-1], min, max); + waitFaceX = true; + } + } + + if (! ok) + { + m_error = "Error reading data"; + return false; + } + + } + + return true; +} + +bool Gfx::CModelFile::CreateEngineObject(int objRank, int addState) +{ + /*char texName1[20]; + char texName2[20]; + int texNum, i, state; + + for (int i = 0; i < m_trianglesUsed; i++) + { + if (! m_triangles[i].used) continue; + + state = m_triangles[i].state; + strcpy(texName1, m_triangles[i].texName); + texName2[0] = 0; + + if ( strcmp(texName1, "plant.tga") == 0 ) // ??? + { + state |= D3DSTATEALPHA; + } + + if ( m_triangles[i].texNum2 != 0 ) + { + if ( m_triangles[i].texNum2 == 1 ) + { + texNum = m_engine->RetSecondTexture(); + } + else + { + texNum = m_triangles[i].texNum2; + } + + if ( texNum >= 1 && texNum <= 10 ) + { + state |= D3DSTATEDUALb; + } + if ( texNum >= 11 && texNum <= 20 ) + { + state |= D3DSTATEDUALw; + } + sprintf(texName2, "dirty%.2d.tga", texNum); // ??? + } + + m_engine->AddTriangle(objRank, &m_triangles[i].p1, 3, + m_triangles[i].material, + state + addState, + texName1, texName2, + m_triangles[i].min, + m_triangles[i].max, false); + }*/ + return true; +} + +void Gfx::CModelFile::Mirror() +{ + for (int i = 0; i < (int)m_triangles.size(); i++) + { + Gfx::VertexTex2 t = m_triangles[i].p1; + m_triangles[i].p1 = m_triangles[i].p2; + m_triangles[i].p2 = t; + + m_triangles[i].p1.coord.z = -m_triangles[i].p1.coord.z; + m_triangles[i].p2.coord.z = -m_triangles[i].p2.coord.z; + m_triangles[i].p3.coord.z = -m_triangles[i].p3.coord.z; + + m_triangles[i].p1.normal.z = -m_triangles[i].p1.normal.z; + m_triangles[i].p2.normal.z = -m_triangles[i].p2.normal.z; + m_triangles[i].p3.normal.z = -m_triangles[i].p3.normal.z; + } +} + +std::vector& Gfx::CModelFile::GetTriangles() +{ + return m_triangles; +} + +int Gfx::CModelFile::GetTriangleCount() +{ + return m_triangles.size(); +} + +float Gfx::CModelFile::GetHeight(Math::Vector pos) +{ + float limit = 5.0f; + + for (int i = 0; i < (int)m_triangles.size(); i++) + { + if ( fabs(pos.x - m_triangles[i].p1.coord.x) < limit && + fabs(pos.z - m_triangles[i].p1.coord.z) < limit ) + return m_triangles[i].p1.coord.y; + + if ( fabs(pos.x - m_triangles[i].p2.coord.x) < limit && + fabs(pos.z - m_triangles[i].p2.coord.z) < limit ) + return m_triangles[i].p2.coord.y; + + if ( fabs(pos.x - m_triangles[i].p3.coord.x) < limit && + fabs(pos.z - m_triangles[i].p3.coord.z) < limit ) + return m_triangles[i].p3.coord.y; + } + + return 0.0f; +} + +void Gfx::CModelFile::CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max) +{ + Gfx::ModelTriangle triangle; + + Math::Vector n = Math::NormalToPlane(p3, p2, p1); + triangle.p1 = Gfx::VertexTex2(p1, n); + triangle.p2 = Gfx::VertexTex2(p2, n); + triangle.p3 = Gfx::VertexTex2(p3, n); + + triangle.material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f, 0.0f); + triangle.material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f, 0.0f); + + triangle.min = min; + triangle.max = max; + + m_triangles.push_back(triangle); +} diff --git a/src/graphics/engine/modelfile.h b/src/graphics/engine/modelfile.h new file mode 100644 index 0000000..6a30487 --- /dev/null +++ b/src/graphics/engine/modelfile.h @@ -0,0 +1,120 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// modelfile.h (aka modfile.h) + +#include "graphics/engine/engine.h" +#include "graphics/core/vertex.h" +#include "graphics/core/material.h" +#include "math/vector.h" + +#include +#include +#include + + +class CInstanceManager; + + +namespace Gfx { + +/** + \struct ModelTriangle + \brief Triangle of a 3D model + */ +struct ModelTriangle +{ + //! 1st vertex + Gfx::VertexTex2 p1; + //! 2nd vertex + Gfx::VertexTex2 p2; + //! 3rd vertex + Gfx::VertexTex2 p3; + //! Material + Gfx::Material material; + //! Name of 1st texture + std::string tex1Name; + //! Name of 2nd texture + std::string tex2Name; + //! Min LOD threshold + float min; + //! Max LOD threshold + float max; + //! Rendering state to be set + long state; + + ModelTriangle(); +}; + + +/** + \class CModelFile + \brief Model file reader/writer + + Allows reading and writing model objects. Models are collections of ModelTriangle structs. */ +class CModelFile +{ +public: + CModelFile(CInstanceManager* iMan); + ~CModelFile(); + + //! Returns the last error encountered + std::string GetError(); + + //! Reads a binary Colobot model from file + bool ReadModel(const std::string &filename, bool edit = false, bool meta = true); + //! Reads a binary Colobot model from stream + bool ReadModel(std::istream &stream, bool edit = false, bool meta = true); + //! Writes the model to Colobot binary model file + bool WriteModel(const std::string &filename); + //! Writes the model to Colobot binary model file + bool WriteModel(std::ostream &stream); + + //! Reads a DXF model from file + bool ReadDXF(const std::string &filename, float min, float max); + //! Reads a DXF model from stream + bool ReadDXF(std::istream &stream, float min, float max); + + //! Returns the number of triangles in model + int GetTriangleCount(); + //! Returns the triangle vector + std::vector& GetTriangles(); + //! Returns the height of model -- closest point to X and Z coords of \a pos + float GetHeight(Math::Vector pos); + + //! Mirrors the model along the Z axis + void Mirror(); + + //! Creates an object in the graphics engine from the model + bool CreateEngineObject(int objRank, int addState = 0); + +protected: + //! Adds a triangle to the list + void CreateTriangle(Math::Vector p1, Math::Vector p2, Math::Vector p3, float min, float max); + +protected: + CInstanceManager* m_iMan; + Gfx::CEngine* m_engine; + + //! Last error + std::string m_error; + + //! Model triangles + std::vector m_triangles; +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/particle.cpp b/src/graphics/engine/particle.cpp new file mode 100644 index 0000000..84e2f9d --- /dev/null +++ b/src/graphics/engine/particle.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// particle.cpp (aka particule.cpp) + +#include "graphics/engine/particle.h" + + +// TODO implementation diff --git a/src/graphics/engine/particle.h b/src/graphics/engine/particle.h new file mode 100644 index 0000000..bd9741f --- /dev/null +++ b/src/graphics/engine/particle.h @@ -0,0 +1,337 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// particle.h (aka particule.h) + +#pragma once + +#include "engine.h" +#include "sound/sound.h" + + +class CInstanceManager; +class CRobotMain; +class CObject; +class CSound; + + + +namespace Gfx { + +const short MAXPARTICULE = 500; +const short MAXPARTITYPE = 5; +const short MAXTRACK = 100; +const short MAXTRACKLEN = 10; +const short MAXPARTIFOG = 100; +const short MAXWHEELTRACE = 1000; + +const short SH_WORLD = 0; // particle in the world in the interface +const short SH_FRONT = 1; // particle in the world on the interface +const short SH_INTERFACE = 2; // particle in the interface +const short SH_MAX = 3; + +// type == 0 -> triangles +// type == 1 -> effect00 (black background) +// type == 2 -> effect01 (black background) +// type == 3 -> effect02 (black background) +// type == 4 -> text (white background) + + +enum ParticleType +{ + PARTIEXPLOT = 1, // technology explosion + PARTIEXPLOO = 2, // organic explosion + PARTIMOTOR = 3, // the engine exhaust gas + PARTIGLINT = 4, // reflection + PARTIBLITZ = 5, // lightning recharging battery + PARTICRASH = 6, // dust after fall + PARTIGAS = 7, // gas from the reactor + PARTIFIRE = 9, // fireball shrinks + PARTIFIREZ = 10, // fireball grows + PARTIBLUE = 11, // blue ball + PARTISELY = 12, // yellow selection + PARTISELR = 13, // red selection + PARTIGUN1 = 18, // a bullet (fireball) + PARTIGUN2 = 19, // bullet 2 (ant) + PARTIGUN3 = 20, // bullet 3 (spider) + PARTIGUN4 = 21, // bullet 4 (orgaball) + PARTIFRAG = 22, // triangular fragment + PARTIQUEUE = 23, // inflamed tail + PARTIORGANIC1 = 24, // organic ball mother + PARTIORGANIC2 = 25, // organic ball daughter + PARTISMOKE1 = 26, // black smoke + PARTISMOKE2 = 27, // black smoke + PARTISMOKE3 = 28, // black smoke + PARTISMOKE4 = 29, // black smoke + PARTIBLOOD = 30, // human blood + PARTIBLOODM = 31, // blood laying + PARTIVAPOR = 32, // steam + PARTIVIRUS1 = 33, // virus 1 + PARTIVIRUS2 = 34, // virus 2 + PARTIVIRUS3 = 35, // virus 3 + PARTIVIRUS4 = 36, // virus 4 + PARTIVIRUS5 = 37, // virus 5 + PARTIVIRUS6 = 38, // virus 6 + PARTIVIRUS7 = 39, // virus 7 + PARTIVIRUS8 = 40, // virus 8 + PARTIVIRUS9 = 41, // virus 9 + PARTIVIRUS10 = 42, // virus 10 + PARTIRAY1 = 43, // ray 1 (turn) + PARTIRAY2 = 44, // ray 2 (electric arc) + PARTIRAY3 = 45, // ray 3 + PARTIRAY4 = 46, // ray 4 + PARTIFLAME = 47, // flame + PARTIBUBBLE = 48, // bubble + PARTIFLIC = 49, // circles in the water + PARTIEJECT = 50, // ejection from the reactor + PARTISCRAPS = 51, // waste from the reactor + PARTITOTO = 52, // reactor of tot + PARTIERROR = 53, // toto says no + PARTIWARNING = 54, // foo says blah + PARTIINFO = 54, // toto says yes + PARTIQUARTZ = 55, // reflection crystal + PARTISPHERE0 = 56, // explosion sphere + PARTISPHERE1 = 57, // energy sphere + PARTISPHERE2 = 58, // analysis sphere + PARTISPHERE3 = 59, // shield sphere + PARTISPHERE4 = 60, // information sphere (emit) + PARTISPHERE5 = 61, // botanical sphere (gravity root) + PARTISPHERE6 = 62, // information sphere (receive) + PARTISPHERE7 = 63, // sphere + PARTISPHERE8 = 64, // sphere + PARTISPHERE9 = 65, // sphere + PARTIGUNDEL = 66, // bullet destroyed by shield + PARTIPART = 67, // object part + PARTITRACK1 = 68, // drag 1 + PARTITRACK2 = 69, // drag 2 + PARTITRACK3 = 70, // drag 3 + PARTITRACK4 = 71, // drag 4 + PARTITRACK5 = 72, // drag 5 + PARTITRACK6 = 73, // drag 6 + PARTITRACK7 = 74, // drag 7 + PARTITRACK8 = 75, // drag 8 + PARTITRACK9 = 76, // drag 9 + PARTITRACK10 = 77, // drag 10 + PARTITRACK11 = 78, // drag 11 + PARTITRACK12 = 79, // drag 12 + PARTITRACK13 = 80, // drag 13 + PARTITRACK14 = 81, // drag 14 + PARTITRACK15 = 82, // drag 15 + PARTITRACK16 = 83, // drag 16 + PARTITRACK17 = 84, // drag 17 + PARTITRACK18 = 85, // drag 18 + PARTITRACK19 = 86, // drag 19 + PARTITRACK20 = 87, // drag 20 + PARTIGLINTb = 88, // blue reflection + PARTIGLINTr = 89, // red reflection + PARTILENS1 = 90, // brilliance 1 (orange) + PARTILENS2 = 91, // brilliance 2 (yellow) + PARTILENS3 = 92, // brilliance 3 (red) + PARTILENS4 = 93, // brilliance 4 (violet) + PARTICONTROL = 94, // reflection on button + PARTISHOW = 95, // shows a place + PARTICHOC = 96, // shock wave + PARTIGFLAT = 97, // shows if the ground is flat + PARTIRECOVER = 98, // blue ball recycler + PARTIROOT = 100, // gravity root smoke + PARTIPLOUF0 = 101, // splash + PARTIPLOUF1 = 102, // splash + PARTIPLOUF2 = 103, // splash + PARTIPLOUF3 = 104, // splash + PARTIPLOUF4 = 105, // splash + PARTIDROP = 106, // drop + PARTIFOG0 = 107, // fog 0 + PARTIFOG1 = 108, // fog 1 + PARTIFOG2 = 109, // fog 2 + PARTIFOG3 = 110, // fog 3 + PARTIFOG4 = 111, // fog 4 + PARTIFOG5 = 112, // fog 5 + PARTIFOG6 = 113, // fog 6 + PARTIFOG7 = 114, // fog 7 + PARTIFOG8 = 115, // fog 8 + PARTIFOG9 = 116, // fog 9 + PARTILIMIT1 = 117, // shows the limits 1 + PARTILIMIT2 = 118, // shows the limits 2 + PARTILIMIT3 = 119, // shows the limits 3 + PARTILIMIT4 = 120, // shows the limits 4 + PARTIWATER = 121, // drop of water + PARTIEXPLOG1 = 122, // ball explosion 1 + PARTIEXPLOG2 = 123, // ball explosion 2 + PARTIBASE = 124, // gases of spaceship + PARTITRACE0 = 140, // trace + PARTITRACE1 = 141, // trace + PARTITRACE2 = 142, // trace + PARTITRACE3 = 143, // trace + PARTITRACE4 = 144, // trace + PARTITRACE5 = 145, // trace + PARTITRACE6 = 146, // trace + PARTITRACE7 = 147, // trace + PARTITRACE8 = 148, // trace + PARTITRACE9 = 149, // trace + PARTITRACE10 = 150, // trace + PARTITRACE11 = 151, // trace + PARTITRACE12 = 152, // trace + PARTITRACE13 = 153, // trace + PARTITRACE14 = 154, // trace + PARTITRACE15 = 155, // trace + PARTITRACE16 = 156, // trace + PARTITRACE17 = 157, // trace + PARTITRACE18 = 158, // trace + PARTITRACE19 = 159, // trace +}; + +enum ParticlePhase +{ + PARPHSTART = 0, + PARPHEND = 1, +}; + +struct Particle +{ + char bUsed; // TRUE -> particle used + char bRay; // TRUE -> ray with goal + unsigned short uniqueStamp; // unique mark + short sheet; // sheet (0..n) + ParticleType type; // type PARTI* + ParticlePhase phase; // phase PARPH* + float mass; // mass of the particle (in rebounding) + float weight; // weight of the particle (for noise) + float duration; // length of life + Math::Vector pos; // absolute position (relative if object links) + Math::Vector goal; // goal position (if bRay) + Math::Vector speed; // speed of displacement + float windSensitivity; + short bounce; // number of rebounds + Math::Point dim; // dimensions of the rectangle + float zoom; // zoom (0..1) + float angle; // angle of rotation + float intensity; // intensity + Math::Point texSup; // coordinated upper texture + Math::Point texInf; // coordinated lower texture + float time; // age of the particle (0..n) + float phaseTime; // age at the beginning of phase + float testTime; // time since last test + CObject* objLink; // father object (for example reactor) + CObject* objFather; // father object (for example reactor) + short objRank; // rank of the object, or -1 + short trackRank; // rank of the drag +}; + +struct Track +{ + char bUsed; // TRUE -> drag used + char bDrawParticle; + float step; // duration of not + float last; // increase last not memorized + float intensity; // intensity at starting (0..1) + float width; // tail width + int used; // number of positions in "pos" + int head; // head to write index + Math::Vector pos[MAXTRACKLEN]; + float len[MAXTRACKLEN]; +}; + +struct WheelTrace +{ + ParticleType type; // type PARTI* + Math::Vector pos[4]; // rectangle positions + float startTime; // beginning of life +}; + + + +class CParticle +{ +public: + CParticle(CInstanceManager* iMan, CEngine* engine); + ~CParticle(); + + void SetGLDevice(CDevice device); + + void FlushParticle(); + void FlushParticle(int sheet); + int CreateParticle(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreateFrag(Math::Vector pos, Math::Vector speed, Gfx::EngineTriangle *triangle, ParticleType type, float duration=1.0f, float mass=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreatePart(Math::Vector pos, Math::Vector speed, ParticleType type, float duration=1.0f, float mass=0.0f, float weight=0.0f, float windSensitivity=1.0f, int sheet=0); + int CreateRay(Math::Vector pos, Math::Vector goal, ParticleType type, Math::Point dim, float duration=1.0f, int sheet=0); + int CreateTrack(Math::Vector pos, Math::Vector speed, Math::Point dim, ParticleType type, float duration=1.0f, float mass=0.0f, float length=10.0f, float width=1.0f); + void CreateWheelTrace(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3, const Math::Vector &p4, ParticleType type); + void DeleteParticle(ParticleType type); + void DeleteParticle(int channel); + void SetObjectLink(int channel, CObject *object); + void SetObjectFather(int channel, CObject *object); + void SetPosition(int channel, Math::Vector pos); + void SetDimension(int channel, Math::Point dim); + void SetZoom(int channel, float zoom); + void SetAngle(int channel, float angle); + void SetIntensity(int channel, float intensity); + void SetParam(int channel, Math::Vector pos, Math::Point dim, float zoom, float angle, float intensity); + void SetPhase(int channel, ParticlePhase phase, float duration); + bool GetPosition(int channel, Math::Vector &pos); + + Gfx::Color RetFogColor(Math::Vector pos); + + void SetFrameUpdate(int sheet, bool bUpdate); + void FrameParticle(float rTime); + void DrawParticle(int sheet); + + bool WriteWheelTrace(char *filename, int width, int height, Math::Vector dl, Math::Vector ur); + +protected: + void DeleteRank(int rank); + bool CheckChannel(int &channel); + void DrawParticleTriangle(int i); + void DrawParticleNorm(int i); + void DrawParticleFlat(int i); + void DrawParticleFog(int i); + void DrawParticleRay(int i); + void DrawParticleSphere(int i); + void DrawParticleCylinder(int i); + void DrawParticleWheel(int i); + CObject* SearchObjectGun(Math::Vector old, Math::Vector pos, ParticleType type, CObject *father); + CObject* SearchObjectRay(Math::Vector pos, Math::Vector goal, ParticleType type, CObject *father); + void Play(Sound sound, Math::Vector pos, float amplitude); + bool TrackMove(int i, Math::Vector pos, float progress); + void TrackDraw(int i, ParticleType type); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CDevice* m_pDevice; + CRobotMain* m_main; + CTerrain* m_terrain; + CWater* m_water; + CSound* m_sound; + + Gfx::Particle m_particule[MAXPARTICULE*MAXPARTITYPE]; + Gfx::EngineTriangle m_triangle[MAXPARTICULE]; // triangle if PartiType == 0 + Track m_track[MAXTRACK]; + int m_wheelTraceTotal; + int m_wheelTraceIndex; + WheelTrace m_wheelTrace[MAXWHEELTRACE]; + int m_totalInterface[MAXPARTITYPE][SH_MAX]; + bool m_bFrameUpdate[SH_MAX]; + int m_fogTotal; + int m_fog[MAXPARTIFOG]; + int m_uniqueStamp; + int m_exploGunCounter; + float m_lastTimeGunDel; + float m_absTime; +}; + + +}; // namespace Gfx diff --git a/src/graphics/engine/planet.cpp b/src/graphics/engine/planet.cpp new file mode 100644 index 0000000..4f1f614 --- /dev/null +++ b/src/graphics/engine/planet.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// planet.cpp + +#include "graphics/engine/planet.h" + + +// TODO implementation diff --git a/src/graphics/engine/planet.h b/src/graphics/engine/planet.h new file mode 100644 index 0000000..264d05c --- /dev/null +++ b/src/graphics/engine/planet.h @@ -0,0 +1,80 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// planet.h + +#pragma once + +#include "common/event.h" +#include "math/point.h" + + +class CInstanceManager; + + +namespace Gfx { + +class CEngine; + + +const short MAXPLANET = 10; + +struct Planet +{ + char bUsed; // TRUE -> planet exists + Math::Point start; // initial position in degrees + Math::Point angle; // current position in degrees + float dim; // dimensions (0..1) + float speed; // speed + float dir; // direction in the sky + char name[20]; // name of the texture + Math::Point uv1, uv2; // texture mapping + char bTGA; // texture .TGA +}; + + + + +class CPlanet { +public: + CPlanet(CInstanceManager* iMan, CEngine* engine); + ~CPlanet(); + + void Flush(); + bool EventProcess(const Event &event); + bool Create(int mode, Math::Point start, float dim, float speed, float dir, char *name, Math::Point uv1, Math::Point uv2); + bool PlanetExist(); + void LoadTexture(); + void Draw(); + void SetMode(int mode); + int RetMode(); + +protected: + bool EventFrame(const Event &event); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + + float m_time; + int m_mode; + Planet m_planet[2][MAXPLANET]; + bool m_bPlanetExist; +}; + + +}; // namespace Gfx diff --git a/src/graphics/engine/pyro.cpp b/src/graphics/engine/pyro.cpp new file mode 100644 index 0000000..e699db2 --- /dev/null +++ b/src/graphics/engine/pyro.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// pyro.cpp + +#include "graphics/engine/pyro.h" + + +// TODO implementation diff --git a/src/graphics/engine/pyro.h b/src/graphics/engine/pyro.h new file mode 100644 index 0000000..d663ca5 --- /dev/null +++ b/src/graphics/engine/pyro.h @@ -0,0 +1,175 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// pyro.h + +#pragma once + +#include "common/misc.h" +#include "graphics/engine/engine.h" +//#include "object/object.h" +// TEMPORARILY! +enum ObjectType {}; + + +class CInstanceManager; +class CObject; +class CDisplayText; +class CRobotMain; +class CSound; + + +namespace Gfx { + +class CEngine; +class CTerrain; +class CCamera; +class CParticle; +class CLight; + + +enum PyroType +{ + PT_NULL = 0, + PT_FRAGT = 1, // fragmentation of technical object + PT_FRAGO = 2, // fragmentation of organic object + PT_FRAGW = 4, // fragmentation of object under water + PT_EXPLOT = 5, // explosion of technical object + PT_EXPLOO = 6, // explosion of organic object + PT_EXPLOW = 8, // explosion of object under water + PT_SHOTT = 9, // hit technical object + PT_SHOTH = 10, // hit human + PT_SHOTM = 11, // hit queen + PT_SHOTW = 12, // hit under water + PT_EGG = 13, // break the egg + PT_BURNT = 14, // burning of technical object + PT_BURNO = 15, // burning of organic object + PT_SPIDER = 16, // spider explosion + PT_FALL = 17, // cargo falling + PT_WPCHECK = 18, // indicator reaches + PT_FLCREATE = 19, // flag create + PT_FLDELETE = 20, // flag destroy + PT_RESET = 21, // reset position of the object + PT_WIN = 22, // fireworks + PT_LOST = 23, // black smoke + PT_DEADG = 24, // shooting death + PT_DEADW = 25, // drowning death + PT_FINDING = 26, // object discovered +}; + + +struct PyroBurnPart +{ + int part; + Math::Vector initialPos; + Math::Vector finalPos; + Math::Vector initialAngle; + Math::Vector finalAngle; +}; + +struct PyroLightOper +{ + float progress; + float intensity; + Gfx::Color color; +}; + + + +class CPyro { +public: + CPyro(CInstanceManager* iMan); + ~CPyro(); + + void DeleteObject(bool bAll=false); + bool Create(PyroType type, CObject* pObj, float force=1.0f); + bool EventProcess(const Event &event); + Error IsEnded(); + void CutObjectLink(CObject* pObj); + +protected: + void DisplayError(PyroType type, CObject* pObj); + bool CreateLight(Math::Vector pos, float height); + void DeleteObject(bool bPrimary, bool bSecondary); + + void CreateTriangle(CObject* pObj, ObjectType oType, int part); + + void ExploStart(); + void ExploTerminate(); + + void BurnStart(); + void BurnAddPart(int part, Math::Vector pos, Math::Vector angle); + void BurnProgress(); + bool BurnIsKeepPart(int part); + void BurnTerminate(); + + void FallStart(); + CObject* FallSearchBeeExplo(); + void FallProgress(float rTime); + Error FallIsEnded(); + + void LightOperFlush(); + void LightOperAdd(float progress, float intensity, float r, float g, float b); + void LightOperFrame(float rTime); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CTerrain* m_terrain; + CCamera* m_camera; + CParticle* m_particule; + CLight* m_light; + CObject* m_object; + CDisplayText* m_displayText; + CRobotMain* m_main; + CSound* m_sound; + + Math::Vector m_pos; // center of the effect + Math::Vector m_posPower; // center of the battery + bool m_bPower; // battery exists? + PyroType m_type; + float m_force; + float m_size; + float m_progress; + float m_speed; + float m_time; + float m_lastParticule; + float m_lastParticuleSmoke; + int m_soundChannel; + + int m_lightRank; + int m_lightOperTotal; + PyroLightOper m_lightOper[10]; + float m_lightHeight; + + ObjectType m_burnType; + int m_burnPartTotal; + PyroBurnPart m_burnPart[10]; + int m_burnKeepPart[10]; + float m_burnFall; + + float m_fallFloor; + float m_fallSpeed; + float m_fallBulletTime; + bool m_bFallEnding; + + int m_crashSphereUsed; // number of spheres used + Math::Vector m_crashSpherePos[50]; + float m_crashSphereRadius[50]; +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/terrain.cpp b/src/graphics/engine/terrain.cpp new file mode 100644 index 0000000..c489321 --- /dev/null +++ b/src/graphics/engine/terrain.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// terrain.cpp + +#include "graphics/engine/terrain.h" + + +// TODO implementation diff --git a/src/graphics/engine/terrain.h b/src/graphics/engine/terrain.h new file mode 100644 index 0000000..8d8b165 --- /dev/null +++ b/src/graphics/engine/terrain.h @@ -0,0 +1,211 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// terrain.h + +#pragma once + +#include "graphics/engine/engine.h" + + +class CInstanceManager; + + +namespace Gfx { + +class CEngine; +class CWater; + + +const short FLATLIMIT = (5.0f*Math::PI/180.0f); + + +enum TerrainRes +{ + TR_NULL = 0, + TR_STONE = 1, + TR_URANIUM = 2, + TR_POWER = 3, + TR_KEYa = 4, + TR_KEYb = 5, + TR_KEYc = 6, + TR_KEYd = 7, +}; + + +const short MAXBUILDINGLEVEL = 100; + +struct BuildingLevel +{ + Math::Vector center; + float factor; + float min; + float max; + float level; + float height; + float bboxMinX; + float bboxMaxX; + float bboxMinZ; + float bboxMaxZ; +}; + + +const short MAXMATTERRAIN = 100; + +struct TerrainMaterial +{ + short id; + char texName[20]; + float u,v; + float hardness; + char mat[4]; // up, right, down, left +}; + +struct DotLevel +{ + short id; + char mat[4]; // up, right, down, left +}; + + +const short MAXFLYINGLIMIT = 10; + +struct FlyingLimit +{ + Math::Vector center; + float extRadius; + float intRadius; + float maxHeight; +}; + + + +class CTerrain +{ +public: + CTerrain(CInstanceManager* iMan); + ~CTerrain(); + + bool Generate(int mosaic, int brickP2, float size, float vision, int depth, float hardness); + bool InitTextures(char* baseName, int* table, int dx, int dy); + void LevelFlush(); + bool LevelMaterial(int id, char* baseName, float u, float v, int up, int right, int down, int left, float hardness); + bool LevelInit(int id); + bool LevelGenerate(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius); + void FlushRelief(); + bool ReliefFromBMP(const char* filename, float scaleRelief, bool adjustBorder); + bool ReliefFromDXF(const char* filename, float scaleRelief); + bool ResFromBMP(const char* filename); + bool CreateObjects(bool bMultiRes); + bool Terraform(const Math::Vector &p1, const Math::Vector &p2, float height); + + void SetWind(Math::Vector speed); + Math::Vector RetWind(); + + float RetFineSlope(const Math::Vector &pos); + float RetCoarseSlope(const Math::Vector &pos); + bool GetNormal(Math::Vector &n, const Math::Vector &p); + float RetFloorLevel(const Math::Vector &p, bool bBrut=false, bool bWater=false); + float RetFloorHeight(const Math::Vector &p, bool bBrut=false, bool bWater=false); + bool MoveOnFloor(Math::Vector &p, bool bBrut=false, bool bWater=false); + bool ValidPosition(Math::Vector &p, float marging); + TerrainRes RetResource(const Math::Vector &p); + void LimitPos(Math::Vector &pos); + + void FlushBuildingLevel(); + bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor); + bool UpdateBuildingLevel(Math::Vector center); + bool DeleteBuildingLevel(Math::Vector center); + float RetBuildingFactor(const Math::Vector &p); + float RetHardness(const Math::Vector &p); + + int RetMosaic(); + int RetBrick(); + float RetSize(); + float RetScaleRelief(); + + void GroundFlat(Math::Vector pos); + float RetFlatZoneRadius(Math::Vector center, float max); + + void SetFlyingMaxHeight(float height); + float RetFlyingMaxHeight(); + void FlushFlyingLimit(); + bool AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight); + float RetFlyingLimit(Math::Vector pos, bool bNoLimit); + +protected: + bool ReliefAddDot(Math::Vector pos, float scaleRelief); + void AdjustRelief(); + Math::Vector RetVector(int x, int y); + Gfx::VertexTex2 RetVertex(int x, int y, int step); + bool CreateMosaic(int ox, int oy, int step, int objRank, const Gfx::Material &mat, float min, float max); + bool CreateSquare(bool bMultiRes, int x, int y); + + TerrainMaterial* LevelSearchMat(int id); + void LevelTextureName(int x, int y, char *name, Math::Point &uv); + float LevelRetHeight(int x, int y); + bool LevelGetDot(int x, int y, float min, float max, float slope); + int LevelTestMat(char *mat); + void LevelSetDot(int x, int y, int id, char *mat); + bool LevelIfDot(int x, int y, int id, char *mat); + bool LevelPutDot(int x, int y, int id); + void LevelOpenTable(); + void LevelCloseTable(); + + void AdjustBuildingLevel(Math::Vector &p); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CWater* m_water; + + int m_mosaic; // number of mosaics + int m_brick; // number of bricks per mosaics + float m_size; // size of an item in an brick + float m_vision; // vision before a change of resolution + float* m_relief; // table of the relief + int* m_texture; // table of textures + int* m_objRank; // table of rows of objects + bool m_bMultiText; + bool m_bLevelText; + float m_scaleMapping; // scale of the mapping + float m_scaleRelief; + int m_subdivMapping; + int m_depth; // number of different resolutions (1,2,3,4) + char m_texBaseName[20]; + char m_texBaseExt[10]; + float m_defHardness; + + TerrainMaterial m_levelMat[MAXMATTERRAIN+1]; + int m_levelMatTotal; + int m_levelMatMax; + int m_levelDotSize; + DotLevel* m_levelDot; + int m_levelID; + + int m_buildingUsed; + BuildingLevel m_buildingTable[MAXBUILDINGLEVEL]; + + unsigned char* m_resources; + Math::Vector m_wind; // wind speed + + float m_flyingMaxHeight; + int m_flyingLimitTotal; + FlyingLimit m_flyingLimit[MAXFLYINGLIMIT]; +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/test/CMakeLists.txt b/src/graphics/engine/test/CMakeLists.txt new file mode 100644 index 0000000..bd83773 --- /dev/null +++ b/src/graphics/engine/test/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 2.8) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") + +include_directories(. ../../..) +add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/logger.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp) diff --git a/src/graphics/engine/test/modelfile_test.cpp b/src/graphics/engine/test/modelfile_test.cpp new file mode 100644 index 0000000..f7ed87f --- /dev/null +++ b/src/graphics/engine/test/modelfile_test.cpp @@ -0,0 +1,48 @@ +#include "graphics/engine/modelfile.h" +#include "common/iman.h" + +#include + + +int main(int argc, char *argv[]) +{ + if (argc != 4) + { + std::cerr << "Usage: " << argv[0] << " {mod|dxf} in_file out_file" << std::endl; + return 1; + } + + CInstanceManager iMan; + Gfx::CModelFile modfile(&iMan); + + std::string mode(argv[1]); + if (mode == "mod") + { + if (! modfile.ReadModel(argv[2], false, false) ) + { + std::cerr << "Read error: " << modfile.GetError() << std::endl; + return 2; + } + } + else if (mode == "dxf") + { + if (! modfile.ReadDXF(argv[2], false, false) ) + { + std::cerr << "Read error: " << modfile.GetError() << std::endl; + return 2; + } + } + else + { + std::cerr << "Usage: " << argv[0] << " {mod|dxf} in_file out_file" << std::endl; + return 1; + } + + if (! modfile.WriteModel(argv[3]) ) + { + std::cerr << "Write error: " << modfile.GetError() << std::endl; + return 3; + } + + return 0; +} diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp new file mode 100644 index 0000000..2a9543c --- /dev/null +++ b/src/graphics/engine/text.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// text.cpp + +#include "graphics/engine/text.h" + + +// TODO implementation diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h new file mode 100644 index 0000000..c2de220 --- /dev/null +++ b/src/graphics/engine/text.h @@ -0,0 +1,113 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// text.h + +#pragma once + +#include "graphics/engine/engine.h" +#include "graphics/core/device.h" +#include "math/point.h" + + +class CInstanceManager; + + +namespace Gfx { + +const float SMALLFONT = 10.0f; +const float BIGFONT = 15.0f; + +const float NORMSTRETCH = 0.8f; + + + +enum FontType +{ + FONT_COLOBOT = 0, + FONT_COURIER = 1, + FONT_BUTTON = 2, +}; + +enum FontTitle +{ + TITLE_BIG = 0x04, + TITLE_NORM = 0x08, + TITLE_LITTLE = 0x0c, +}; + +enum FontColor +{ + COLOR_LINK = 0x10, + COLOR_TOKEN = 0x20, + COLOR_TYPE = 0x30, + COLOR_CONST = 0x40, + COLOR_REM = 0x50, + COLOR_KEY = 0x60, + COLOR_TABLE = 0x70, +}; + +const short FONT_MASK = 0x03; +const short TITLE_MASK = 0x0c; +const short COLOR_MASK = 0x70; +const short IMAGE_MASK = 0x80; + + + +class CText { +public: + CText(CInstanceManager *iMan, Gfx::CEngine* engine); + ~CText(); + + void SetDevice(Gfx::CDevice *device); + + void DrawText(char *string, char *format, int len, Math::Point pos, float width, int justif, float size, float stretch, int eol); + void DrawText(char *string, char *format, Math::Point pos, float width, int justif, float size, float stretch, int eol); + void DrawText(char *string, int len, Math::Point pos, float width, int justif, float size, float stretch, FontType font, int eol); + void DrawText(char *string, Math::Point pos, float width, int justif, float size, float stretch, FontType font, int eol); + void DimText(char *string, char *format, int len, Math::Point pos, int justif, float size, float stretch, Math::Point &start, Math::Point &end); + void DimText(char *string, char *format, Math::Point pos, int justif, float size, float stretch, Math::Point &start, Math::Point &end); + void DimText(char *string, int len, Math::Point pos, int justif, float size, float stretch, FontType font, Math::Point &start, Math::Point &end); + void DimText(char *string, Math::Point pos, int justif, float size, float stretch, FontType font, Math::Point &start, Math::Point &end); + + float RetAscent(float size, FontType font); + float RetDescent(float size, FontType font); + float RetHeight(float size, FontType font); + + float RetStringWidth(char *string, char *format, int len, float size, float stretch); + float RetStringWidth(char *string, int len, float size, float stretch, FontType font); + float RetCharWidth(int character, float offset, float size, float stretch, FontType font); + + int Justif(char *string, char *format, int len, float width, float size, float stretch); + int Justif(char *string, int len, float width, float size, float stretch, FontType font); + int Detect(char *string, char *format, int len, float offset, float size, float stretch); + int Detect(char *string, int len, float offset, float size, float stretch, FontType font); + +protected: + void DrawString(char *string, char *format, int len, Math::Point pos, float width, float size, float stretch, int eol); + void DrawString(char *string, int len, Math::Point pos, float width, float size, float stretch, FontType font, int eol); + void DrawColor(Math::Point pos, float size, float width, int color); + void DrawChar(int character, Math::Point pos, float size, float stretch, FontType font); + +protected: + CInstanceManager* m_iMan; + Gfx::CEngine* m_engine; + Gfx::CDevice* m_device; + +}; + +}; // namespace Gfx diff --git a/src/graphics/engine/water.cpp b/src/graphics/engine/water.cpp new file mode 100644 index 0000000..a157e82 --- /dev/null +++ b/src/graphics/engine/water.cpp @@ -0,0 +1,23 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// water.cpp + +#include "graphics/engine/water.h" + + +// TODO implementation diff --git a/src/graphics/engine/water.h b/src/graphics/engine/water.h new file mode 100644 index 0000000..67be9dc --- /dev/null +++ b/src/graphics/engine/water.h @@ -0,0 +1,134 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// water.h + +#pragma once + +#include "graphics/engine/engine.h" +#include "graphics/engine/particle.h" +#include "common/event.h" + + +class CInstanceManager; +class CSound; + + +namespace Gfx { + +class CTerrain; + + +const short MAXWATERLINE = 500; + +struct WaterLine +{ + short x, y; // beginning + short len; // length by x + float px1, px2, pz; +}; + + +const short MAXWATVAPOR = 10; + +struct WaterVapor +{ + bool bUsed; + ParticleType type; + Math::Vector pos; + float delay; + float time; + float last; +}; + + +enum WaterType +{ + WATER_NULL = 0, // no water + WATER_TT = 1, // transparent texture + WATER_TO = 2, // opaque texture + WATER_CT = 3, // transparent color + WATER_CO = 4, // opaque color +}; + + +class CWater +{ +public: + CWater(CInstanceManager* iMan, Gfx::CEngine* engine); + ~CWater(); + + void SetGLDevice(Gfx::CDevice device); + bool EventProcess(const Event &event); + void Flush(); + bool Create(WaterType type1, WaterType type2, const char *filename, Gfx::Color diffuse, Gfx::Color ambient, float level, float glint, Math::Vector eddy); + void DrawBack(); + void DrawSurf(); + + bool SetLevel(float level); + float RetLevel(); + float RetLevel(CObject* object); + + void SetLava(bool bLava); + bool RetLava(); + + void AdjustEye(Math::Vector &eye); + +protected: + bool EventFrame(const Event &event); + void LavaFrame(float rTime); + void AdjustLevel(Math::Vector &pos, Math::Vector &norm, Math::Point &uv1, Math::Point &uv2); + bool RetWater(int x, int y); + bool CreateLine(int x, int y, int len); + + void VaporFlush(); + bool VaporCreate(ParticleType type, Math::Vector pos, float delay); + void VaporFrame(int i, float rTime); + +protected: + CInstanceManager* m_iMan; + CEngine* m_engine; + CDevice* m_pDevice; + CTerrain* m_terrain; + CParticle* m_particule; + CSound* m_sound; + + WaterType m_type[2]; + char m_filename[100]; + float m_level; // overall level + float m_glint; // amplitude of reflections + Math::Vector m_eddy; // amplitude of swirls + Gfx::Color m_diffuse; // diffuse color + Gfx::Color m_ambient; // ambient color + float m_time; + float m_lastLava; + int m_subdiv; + + int m_brick; // number of brick*mosaics + float m_size; // size of a item in an brick + + int m_lineUsed; + WaterLine m_line[MAXWATERLINE]; + + WaterVapor m_vapor[MAXWATVAPOR]; + + bool m_bDraw; + bool m_bLava; + long m_color; +}; + +}; // namespace Gfx diff --git a/src/graphics/opengl/README.txt b/src/graphics/opengl/README.txt index 11aba8d..0aba0ed 100644 --- a/src/graphics/opengl/README.txt +++ b/src/graphics/opengl/README.txt @@ -2,5 +2,5 @@ src/graphics/opengl OpenGL engine implementation -Contains the concreate implementation using OpenGL of functions -of grahpics engine in graphics/common. +Contains the concrete implementation using OpenGL of abstract CDevice class +from src/graphics/core diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index b779123..313ea02 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -19,7 +19,7 @@ #pragma once -#include "graphics/common/device.h" +#include "graphics/core/device.h" #include #include diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 793e858..8ed7364 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -37,7 +37,7 @@ texture_test.cpp set(MODEL_SOURCES ../gldevice.cpp -../../common/modelfile.cpp +../../engine/modelfile.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp @@ -84,4 +84,4 @@ add_executable(transform_test ${TRANSFORM_SOURCES}) target_link_libraries(transform_test ${LIBS}) add_executable(light_test ${LIGHT_SOURCES}) -target_link_libraries(light_test ${LIBS}) \ No newline at end of file +target_link_libraries(light_test ${LIBS}) diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index 3e3d100..3e8efe6 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -2,7 +2,7 @@ #include "common/logger.h" #include "common/image.h" #include "common/iman.h" -#include "graphics/common/modelfile.h" +#include "graphics/engine/modelfile.h" #include "graphics/opengl/gldevice.h" #include "math/geometry.h" -- cgit v1.2.3-1-g7c22 From 851300d61d7745edd94295433e7a3afbf089f4cf Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 27 Jul 2012 18:44:43 +0200 Subject: Whitespace fix --- src/common/config.h.cmake | 16 +- src/common/event.cpp | 136 +++++----- src/common/logger.cpp | 2 +- src/common/logger.h | 2 +- src/sound/sound.h | 642 +++++++++++++++++++++++----------------------- 5 files changed, 399 insertions(+), 399 deletions(-) diff --git a/src/common/config.h.cmake b/src/common/config.h.cmake index b066387..d8bff91 100644 --- a/src/common/config.h.cmake +++ b/src/common/config.h.cmake @@ -1,8 +1,8 @@ -#pragma once - -// Macros set by CMake -#cmakedefine DEBUG -#cmakedefine PLATFORM_WINDOWS @PLATFORM_WINDOWS@ -#cmakedefine PLATFORM_LINUX @PLATFORM_LINUX@ -#cmakedefine PLATFORM_OTHER @PLATFORM_OTHER@ - +#pragma once + +// Macros set by CMake +#cmakedefine DEBUG +#cmakedefine PLATFORM_WINDOWS @PLATFORM_WINDOWS@ +#cmakedefine PLATFORM_LINUX @PLATFORM_LINUX@ +#cmakedefine PLATFORM_OTHER @PLATFORM_OTHER@ + diff --git a/src/common/event.cpp b/src/common/event.cpp index e8595d0..6a5f4d3 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -1,68 +1,68 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// event.cpp - -#include "common/event.h" -#include "common/iman.h" - - - -CEventQueue::CEventQueue(CInstanceManager* iMan) -{ - m_iMan = iMan; - m_iMan->AddInstance(CLASS_EVENT, this); - - Flush(); -} - -CEventQueue::~CEventQueue() -{ -} - -void CEventQueue::Flush() -{ - m_head = 0; - m_tail = 0; - m_total = 0; -} - -/** If the maximum size of queue has been reached, returns \c false. - Else, adds the event to the queue and returns \c true. */ -bool CEventQueue::AddEvent(const Event &event) -{ - if ( m_total >= MAX_EVENT_QUEUE ) return false; - - m_fifo[m_head++] = event; - if ( m_head >= MAX_EVENT_QUEUE ) m_head = 0; - m_total ++; - - return true; -} - -/** If the queue is empty, returns \c false. - Else, gets the event from the front, puts it into \a event and returns \c true. */ -bool CEventQueue::GetEvent(Event &event) -{ - if ( m_head == m_tail ) return false; - - event = m_fifo[m_tail++]; - if ( m_tail >= MAX_EVENT_QUEUE ) m_tail = 0; - m_total --; - - return true; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// event.cpp + +#include "common/event.h" +#include "common/iman.h" + + + +CEventQueue::CEventQueue(CInstanceManager* iMan) +{ + m_iMan = iMan; + m_iMan->AddInstance(CLASS_EVENT, this); + + Flush(); +} + +CEventQueue::~CEventQueue() +{ +} + +void CEventQueue::Flush() +{ + m_head = 0; + m_tail = 0; + m_total = 0; +} + +/** If the maximum size of queue has been reached, returns \c false. + Else, adds the event to the queue and returns \c true. */ +bool CEventQueue::AddEvent(const Event &event) +{ + if ( m_total >= MAX_EVENT_QUEUE ) return false; + + m_fifo[m_head++] = event; + if ( m_head >= MAX_EVENT_QUEUE ) m_head = 0; + m_total ++; + + return true; +} + +/** If the queue is empty, returns \c false. + Else, gets the event from the front, puts it into \a event and returns \c true. */ +bool CEventQueue::GetEvent(Event &event) +{ + if ( m_head == m_tail ) return false; + + event = m_fifo[m_tail++]; + if ( m_tail >= MAX_EVENT_QUEUE ) m_tail = 0; + m_total --; + + return true; +} + diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 41d60eb..f24726e 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -55,7 +55,7 @@ void CLogger::Log(LogType type, const char *str, va_list args) { if (type < mLogLevel) return; - + switch (type) { case LOG_WARN: fprintf(IsOpened() ? mFile : stderr, "[WARN]: "); break; case LOG_INFO: fprintf(IsOpened() ? mFile : stderr, "[INFO]: "); break; diff --git a/src/common/logger.h b/src/common/logger.h index 1b3829c..a67aefe 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -49,7 +49,7 @@ enum LogType * * @brief Class for loggin information to file or console * -*/ +*/ class CLogger : public CSingleton { public: diff --git a/src/sound/sound.h b/src/sound/sound.h index d27721a..a1b8fe3 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -1,321 +1,321 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// soundinterface.h - -/** - * @file sound/soundinterface.h - * @brief Sound plugin interface - */ - -#pragma once - -#include - -#include - -#include - - -/*! - * Maximum possible audio volume - */ -#define MAXVOLUME 100 - - -/** - * \public - * \enum Sound sound/soundinterface.h - * \brief Sound enum representing sound file -**/ -enum Sound -{ - SOUND_CLICK = 0, - SOUND_BOUM = 1, - SOUND_EXPLO = 2, - SOUND_FLYh = 3, /*!< human */ - SOUND_FLY = 4, - SOUND_STEPs = 5, /*!< smooth */ - SOUND_MOTORw = 6, /*!< wheel */ - SOUND_MOTORt = 7, /*!< tank */ - SOUND_MOTORr = 8, /*!< roller */ - SOUND_ERROR = 9, - SOUND_CONVERT = 10, - SOUND_ENERGY = 11, - SOUND_PLOUF = 12, - SOUND_BLUP = 13, - SOUND_WARNING = 14, - SOUND_DERRICK = 15, - SOUND_LABO = 16, - SOUND_STATION = 17, - SOUND_REPAIR = 18, - SOUND_RESEARCH = 19, - SOUND_INSECTs = 20, /*!< spider */ - SOUND_BURN = 21, - SOUND_TZOING = 22, - SOUND_GGG = 23, - SOUND_MANIP = 24, - SOUND_FIRE = 25, /*!< shooting with fireball */ - SOUND_HUMAN1 = 26, /*!< breathing */ - SOUND_STEPw = 27, /*!< water */ - SOUND_SWIM = 28, - SOUND_RADAR = 29, - SOUND_BUILD = 30, - SOUND_ALARM = 31, /*!< energy alarm */ - SOUND_SLIDE = 32, - SOUND_EXPLOi = 33, /*!< insect */ - SOUND_INSECTa = 34, /*!< ant */ - SOUND_INSECTb = 35, /*!< bee */ - SOUND_INSECTw = 36, /*!< worm */ - SOUND_INSECTm = 37, /*!< mother */ - SOUND_TREMBLE = 38, - SOUND_PSHHH = 39, - SOUND_NUCLEAR = 40, - SOUND_INFO = 41, - SOUND_OPEN = 42, - SOUND_CLOSE = 43, - SOUND_FACTORY = 44, - SOUND_EGG = 45, - SOUND_MOTORs = 46, /*!< submarine */ - SOUND_MOTORi = 47, /*!< insect (legs) */ - SOUND_SHIELD = 48, - SOUND_FIREi = 49, /*!< shooting with orgaball (insect) */ - SOUND_GUNDEL = 50, - SOUND_PSHHH2 = 51, /*!< shield */ - SOUND_MESSAGE = 52, - SOUND_BOUMm = 53, /*!< metal */ - SOUND_BOUMv = 54, /*!< plant */ - SOUND_BOUMs = 55, /*!< smooth */ - SOUND_EXPLOl = 56, /*!< little */ - SOUND_EXPLOlp = 57, /*!< little power */ - SOUND_EXPLOp = 58, /*!< power */ - SOUND_STEPh = 59, /*!< hard */ - SOUND_STEPm = 60, /*!< metal */ - SOUND_POWERON = 61, - SOUND_POWEROFF = 62, - SOUND_AIE = 63, - SOUND_WAYPOINT = 64, - SOUND_RECOVER = 65, - SOUND_DEADi = 66, - SOUND_JOSTLE = 67, - SOUND_GFLAT = 68, - SOUND_DEADg = 69, /*!< shooting death */ - SOUND_DEADw = 70, /*!< drowning */ - SOUND_FLYf = 71, /*!< reactor fail */ - SOUND_ALARMt = 72, /*!< temperature alarm */ - SOUND_FINDING = 73, /*!< finds a cache object */ - SOUND_THUMP = 74, - SOUND_TOUCH = 75, - SOUND_BLITZ = 76, - SOUND_MUSHROOM = 77, - SOUND_FIREp = 78, /*!< shooting with phazer */ - SOUND_EXPLOg1 = 79, /*!< impact gun 1 */ - SOUND_EXPLOg2 = 80, /*!< impact gun 2 */ - SOUND_MOTORd = 81, /*!< engine friction */ -}; - - -/** - * \public - * \enum SoundNext sound/soundinterface.h - * \brief Enum representing operation that will be performend on a sound at given time -**/ -enum SoundNext -{ - SOPER_CONTINUE = 1, /*!< continue playing */ - SOPER_STOP = 2, /*!< stop playing */ - SOPER_LOOP = 3, /*!< start over */ -}; - - -/** -* @class CSoundInterface -* -* @brief Sound plugin interface -* -*/ -class CSoundInterface : public CPlugin -{ - public: - CSoundInterface() { - //CInstanceManager::getInstance().AddInstance(CLASS_SOUND, this); - //m_iMan->AddInstance(CLASS_SOUND, this); - }; - virtual ~CSoundInterface() = 0; - - /** Function to initialize sound device - * @param bool b3D - enable support for 3D sound - */ - virtual bool Create(bool b3D) = 0; - - /** Function called to cache all sound effect files. - * Function calls \link CSoundInterface::Cache() \endlink for each file - */ - virtual void CacheAll() = 0; - - /** Function called to cache sound effect file. - * This function is called by plugin interface for each file. - * @param Sound bSound - id of a file, will be used to identify sound files - * @param std::string bFile - file to load - * @return return true on success - */ - virtual bool Cache(Sound bSound, std::string bFile) = 0; - - /** Return if plugin is enabled - * @return return true if plugin is enabled - */ - virtual bool RetEnable() = 0; - - /** Change sound mode to 2D/3D - * @param bool bMode - true to enable 3D sound - */ - virtual void SetSound3D(bool bMode) = 0; - - /** Return if we use 3D sound - * @return true if we have 3D sound enabled - */ - virtual bool RetSound3D() = 0; - - /** Return if we have 3D sound capable card - * @return true for 3D sound support - */ - virtual bool RetSound3DCap() = 0; - - /** Change global sound volume - * @param int volume - range from 0 to MAXVOLUME - */ - virtual void SetAudioVolume(int volume) = 0; - - /** Return global sound volume - * @return global volume as int in range from 0 to MAXVOLUME - */ - virtual int RetAudioVolume() = 0; - - /** Set music volume - * @param int volume - range from 0 to MAXVOLUME - */ - virtual void SetMusicVolume(int volume) = 0; - - /** Return music volume - * @return music volume as int in range from 0 to MAXVOLUME - */ - virtual int RetMusicVolume() = 0; - - /** Set listener position - * @param Math::Vector eye - position of listener - * @param Math::Vector lookat - direction listener is looking at - */ - virtual void SetListener(Math::Vector eye, Math::Vector lookat) = 0; - - /** Update data each frame - * @param float rTime - time since last update - */ - virtual void FrameMove(float rTime) = 0; - - /** Play specific sound - * @param Sound sound - sound to play - * @param float amplitude - change amplitude of sound before playing - * @param float frequency - change sound frequency before playing (0.5 octave down, 2.0 octave up) - * @param bool bLoop - loop sound - * @return identifier of channel that sound will be played on - */ - virtual int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false) = 0; - - /** Play specific sound - * @param Sound sound - sound to play - * @param Math:Vector pos - position of sound in space - * @param float amplitude - change amplitude of sound before playing - * @param float frequency - change sound frequency before playing (0.5 octave down, 2.0 octave up) - * @param bool bLoop - loop sound - * @return identifier of channel that sound will be played on - */ - virtual int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false) = 0; - - /** Remove all operations that would be made on sound in channel. - * @param int channel - channel to work on - * @return return true on success - */ - virtual bool FlushEnvelope(int channel) = 0; - - /** Add envelope to sound. Envelope is a operatino that will be performend on sound in future like changing frequency - * @param int channel - channel to work on - * @param float amplitude - change amplitude - * @param float frequency - change frequency - * @param float time - when to change (sample time) - * @param SoundNext oper - operation to perform - * @return return true on success - */ - virtual bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) = 0; - - /** Set sound position in space - * @param int channel - channel to work on - * @param Math::Vector pos - new positino of a sound - * @return return true on success - */ - virtual bool Position(int channel, Math::Vector pos) = 0; - - /** Set sound frequency - * @param int channel - channel to work on - * @param float frequency - change sound frequency - * @return return true on success - */ - virtual bool Frequency(int channel, float frequency) = 0; - - /** Stop playing sound - * @param int channel - channel to work on - * @return return true on success - */ - virtual bool Stop(int channel) = 0; - - /** Stop playing all sounds - * @return return true on success - */ - virtual bool StopAll() = 0; - - /** Mute/unmute all sounds - * @param bool bMute - * @return return true on success - */ - virtual bool MuteAll(bool bMute) = 0; - - /** Start playing music - * @param int rank - track number - * @param bool bRepeat - repeat playing - * @return return true on success - */ - virtual bool PlayMusic(int rank, bool bRepeat) = 0; - - /** Restart music - * @return return true on success - */ - virtual bool RestartMusic() = 0; - - /** Susspend paying music - * @return return true on success - */ - virtual void SuspendMusic() = 0; - - /** Stop playing music - * @return return true on success - */ - virtual void StopMusic() = 0; - - /** Check if music if playing - * @return return true if music is playing - */ - virtual bool IsPlayingMusic() = 0; -}; +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// soundinterface.h + +/** + * @file sound/soundinterface.h + * @brief Sound plugin interface + */ + +#pragma once + +#include + +#include + +#include + + +/*! + * Maximum possible audio volume + */ +#define MAXVOLUME 100 + + +/** + * \public + * \enum Sound sound/soundinterface.h + * \brief Sound enum representing sound file +**/ +enum Sound +{ + SOUND_CLICK = 0, + SOUND_BOUM = 1, + SOUND_EXPLO = 2, + SOUND_FLYh = 3, /*!< human */ + SOUND_FLY = 4, + SOUND_STEPs = 5, /*!< smooth */ + SOUND_MOTORw = 6, /*!< wheel */ + SOUND_MOTORt = 7, /*!< tank */ + SOUND_MOTORr = 8, /*!< roller */ + SOUND_ERROR = 9, + SOUND_CONVERT = 10, + SOUND_ENERGY = 11, + SOUND_PLOUF = 12, + SOUND_BLUP = 13, + SOUND_WARNING = 14, + SOUND_DERRICK = 15, + SOUND_LABO = 16, + SOUND_STATION = 17, + SOUND_REPAIR = 18, + SOUND_RESEARCH = 19, + SOUND_INSECTs = 20, /*!< spider */ + SOUND_BURN = 21, + SOUND_TZOING = 22, + SOUND_GGG = 23, + SOUND_MANIP = 24, + SOUND_FIRE = 25, /*!< shooting with fireball */ + SOUND_HUMAN1 = 26, /*!< breathing */ + SOUND_STEPw = 27, /*!< water */ + SOUND_SWIM = 28, + SOUND_RADAR = 29, + SOUND_BUILD = 30, + SOUND_ALARM = 31, /*!< energy alarm */ + SOUND_SLIDE = 32, + SOUND_EXPLOi = 33, /*!< insect */ + SOUND_INSECTa = 34, /*!< ant */ + SOUND_INSECTb = 35, /*!< bee */ + SOUND_INSECTw = 36, /*!< worm */ + SOUND_INSECTm = 37, /*!< mother */ + SOUND_TREMBLE = 38, + SOUND_PSHHH = 39, + SOUND_NUCLEAR = 40, + SOUND_INFO = 41, + SOUND_OPEN = 42, + SOUND_CLOSE = 43, + SOUND_FACTORY = 44, + SOUND_EGG = 45, + SOUND_MOTORs = 46, /*!< submarine */ + SOUND_MOTORi = 47, /*!< insect (legs) */ + SOUND_SHIELD = 48, + SOUND_FIREi = 49, /*!< shooting with orgaball (insect) */ + SOUND_GUNDEL = 50, + SOUND_PSHHH2 = 51, /*!< shield */ + SOUND_MESSAGE = 52, + SOUND_BOUMm = 53, /*!< metal */ + SOUND_BOUMv = 54, /*!< plant */ + SOUND_BOUMs = 55, /*!< smooth */ + SOUND_EXPLOl = 56, /*!< little */ + SOUND_EXPLOlp = 57, /*!< little power */ + SOUND_EXPLOp = 58, /*!< power */ + SOUND_STEPh = 59, /*!< hard */ + SOUND_STEPm = 60, /*!< metal */ + SOUND_POWERON = 61, + SOUND_POWEROFF = 62, + SOUND_AIE = 63, + SOUND_WAYPOINT = 64, + SOUND_RECOVER = 65, + SOUND_DEADi = 66, + SOUND_JOSTLE = 67, + SOUND_GFLAT = 68, + SOUND_DEADg = 69, /*!< shooting death */ + SOUND_DEADw = 70, /*!< drowning */ + SOUND_FLYf = 71, /*!< reactor fail */ + SOUND_ALARMt = 72, /*!< temperature alarm */ + SOUND_FINDING = 73, /*!< finds a cache object */ + SOUND_THUMP = 74, + SOUND_TOUCH = 75, + SOUND_BLITZ = 76, + SOUND_MUSHROOM = 77, + SOUND_FIREp = 78, /*!< shooting with phazer */ + SOUND_EXPLOg1 = 79, /*!< impact gun 1 */ + SOUND_EXPLOg2 = 80, /*!< impact gun 2 */ + SOUND_MOTORd = 81, /*!< engine friction */ +}; + + +/** + * \public + * \enum SoundNext sound/soundinterface.h + * \brief Enum representing operation that will be performend on a sound at given time +**/ +enum SoundNext +{ + SOPER_CONTINUE = 1, /*!< continue playing */ + SOPER_STOP = 2, /*!< stop playing */ + SOPER_LOOP = 3, /*!< start over */ +}; + + +/** +* @class CSoundInterface +* +* @brief Sound plugin interface +* +*/ +class CSoundInterface : public CPlugin +{ + public: + CSoundInterface() { + //CInstanceManager::getInstance().AddInstance(CLASS_SOUND, this); + //m_iMan->AddInstance(CLASS_SOUND, this); + }; + virtual ~CSoundInterface() = 0; + + /** Function to initialize sound device + * @param bool b3D - enable support for 3D sound + */ + virtual bool Create(bool b3D) = 0; + + /** Function called to cache all sound effect files. + * Function calls \link CSoundInterface::Cache() \endlink for each file + */ + virtual void CacheAll() = 0; + + /** Function called to cache sound effect file. + * This function is called by plugin interface for each file. + * @param Sound bSound - id of a file, will be used to identify sound files + * @param std::string bFile - file to load + * @return return true on success + */ + virtual bool Cache(Sound bSound, std::string bFile) = 0; + + /** Return if plugin is enabled + * @return return true if plugin is enabled + */ + virtual bool RetEnable() = 0; + + /** Change sound mode to 2D/3D + * @param bool bMode - true to enable 3D sound + */ + virtual void SetSound3D(bool bMode) = 0; + + /** Return if we use 3D sound + * @return true if we have 3D sound enabled + */ + virtual bool RetSound3D() = 0; + + /** Return if we have 3D sound capable card + * @return true for 3D sound support + */ + virtual bool RetSound3DCap() = 0; + + /** Change global sound volume + * @param int volume - range from 0 to MAXVOLUME + */ + virtual void SetAudioVolume(int volume) = 0; + + /** Return global sound volume + * @return global volume as int in range from 0 to MAXVOLUME + */ + virtual int RetAudioVolume() = 0; + + /** Set music volume + * @param int volume - range from 0 to MAXVOLUME + */ + virtual void SetMusicVolume(int volume) = 0; + + /** Return music volume + * @return music volume as int in range from 0 to MAXVOLUME + */ + virtual int RetMusicVolume() = 0; + + /** Set listener position + * @param Math::Vector eye - position of listener + * @param Math::Vector lookat - direction listener is looking at + */ + virtual void SetListener(Math::Vector eye, Math::Vector lookat) = 0; + + /** Update data each frame + * @param float rTime - time since last update + */ + virtual void FrameMove(float rTime) = 0; + + /** Play specific sound + * @param Sound sound - sound to play + * @param float amplitude - change amplitude of sound before playing + * @param float frequency - change sound frequency before playing (0.5 octave down, 2.0 octave up) + * @param bool bLoop - loop sound + * @return identifier of channel that sound will be played on + */ + virtual int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false) = 0; + + /** Play specific sound + * @param Sound sound - sound to play + * @param Math:Vector pos - position of sound in space + * @param float amplitude - change amplitude of sound before playing + * @param float frequency - change sound frequency before playing (0.5 octave down, 2.0 octave up) + * @param bool bLoop - loop sound + * @return identifier of channel that sound will be played on + */ + virtual int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false) = 0; + + /** Remove all operations that would be made on sound in channel. + * @param int channel - channel to work on + * @return return true on success + */ + virtual bool FlushEnvelope(int channel) = 0; + + /** Add envelope to sound. Envelope is a operatino that will be performend on sound in future like changing frequency + * @param int channel - channel to work on + * @param float amplitude - change amplitude + * @param float frequency - change frequency + * @param float time - when to change (sample time) + * @param SoundNext oper - operation to perform + * @return return true on success + */ + virtual bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) = 0; + + /** Set sound position in space + * @param int channel - channel to work on + * @param Math::Vector pos - new positino of a sound + * @return return true on success + */ + virtual bool Position(int channel, Math::Vector pos) = 0; + + /** Set sound frequency + * @param int channel - channel to work on + * @param float frequency - change sound frequency + * @return return true on success + */ + virtual bool Frequency(int channel, float frequency) = 0; + + /** Stop playing sound + * @param int channel - channel to work on + * @return return true on success + */ + virtual bool Stop(int channel) = 0; + + /** Stop playing all sounds + * @return return true on success + */ + virtual bool StopAll() = 0; + + /** Mute/unmute all sounds + * @param bool bMute + * @return return true on success + */ + virtual bool MuteAll(bool bMute) = 0; + + /** Start playing music + * @param int rank - track number + * @param bool bRepeat - repeat playing + * @return return true on success + */ + virtual bool PlayMusic(int rank, bool bRepeat) = 0; + + /** Restart music + * @return return true on success + */ + virtual bool RestartMusic() = 0; + + /** Susspend paying music + * @return return true on success + */ + virtual void SuspendMusic() = 0; + + /** Stop playing music + * @return return true on success + */ + virtual void StopMusic() = 0; + + /** Check if music if playing + * @return return true if music is playing + */ + virtual bool IsPlayingMusic() = 0; +}; -- cgit v1.2.3-1-g7c22 From db0b880674c1f1f0f18d99ca38a09db7bbf3ba01 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 27 Jul 2012 18:50:29 +0200 Subject: Updated Doxygen main page --- src/app/main.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 9eea6e4..dce13da 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -37,22 +37,28 @@ Doxygen documentation of Colobot project The source code released by Epitec was sparsely documented. This documentation, written from scratch, will aim to describe the various components of the code. -Currently, the only documented classes are the ones written from scratch or the old ones rewritten to match the new code. +Currently, the only documented classes are the ones written from scratch or the old ones rewritten +to match the new code. In time, the documentation will be extended to cover every major part of the code. \section Structure Code structure -The source code was split from the original all-in-one directory to subdirectories, each containing one major part of the project. -The current layout is this: +The source code was split from the original all-in-one directory to subdirectories, +each containing one major part of the project. +The current layout is the following: - src/CBot - separate library with CBot language - - src/app - class CApplication and everything concerned with SDL plus other system-dependent code such as displaying a message box, finding files, etc. + - src/app - class CApplication and everything concerned with SDL plus other system-dependent + code such as displaying a message box, finding files, etc. - src/common - shared structs, enums, defines, etc.; should not have any external dependencies - - src/graphics/common - interface of graphics engine (CEngine) and device (CDevice), without concrete implementation, shared structs such as Vertex, Material, etc., “effects” classes: CCamera, CLight, CParticle that will use the graphics engine interface - - src/graphics/opengl - concrete implementation of CEngine and CDevice classes in OpenGL: CGLEngine and CGLDevice + - src/graphics/core - abstract interface of graphics device (abstract CDevice class) + (split from old src/graphics/common) + - src/graphics/engine - main graphics engine based on abstract graphics device; is composed + of CEngine class and associated classes implementing the 3D engine (split from old src/graphics/common) + - src/graphics/opengl - concrete implementation of CDevice class in OpenGL: CGLDevice - src/graphics/d3d - in (far) future - perhaps a newer implementation in DirectX (9? 10?) - src/math - mathematical structures and functions - - src/object - non-graphical game engine, that is robots, buildings, etc.; dependent only on interface of graphics engine, not on concrete implementation - - src/ui - 2D user interface (menu, buttons, check boxes, etc.); also without dependencies to concrete implementation of graphics engine + - src/object - non-graphical game engine, that is robots, buildings, etc. + - src/ui - 2D user interface (menu, buttons, check boxes, etc.) - src/sound - sound and music engine written using fmod library - src/physics - physics engine - src/script - link with the CBot library -- cgit v1.2.3-1-g7c22 From da63248bb9127cc64b6f174e0de254d6c01b7809 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 27 Jul 2012 20:21:02 +0200 Subject: Updated readme files; added license - updated info in README and HOWTO files - added file with full license --- HOWTO.txt | 113 +++++----- LICENSE.txt | 674 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ README-DEV.txt | 2 +- README.txt | 60 ++++- 4 files changed, 792 insertions(+), 57 deletions(-) create mode 100644 LICENSE.txt diff --git a/HOWTO.txt b/HOWTO.txt index 9a4b7bc..f93bb0e 100644 --- a/HOWTO.txt +++ b/HOWTO.txt @@ -1,77 +1,88 @@ +EN + How to... -1. Compile the game with MinGW. +1. Compile the game. + + 1.1 Windows: - 1. Download and install DirectX 8.1 SDK. It can be difficult to find it now as it is old SDK, but here is a working dowload link: - http://dl.dropbox.com/u/32866936/dx81sdk_full.exe - 2. Download and install MinGW and MSYS: + 1. Download and install MinGW and MSYS: http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20111118/ When installing, select all available components. - 3. Download and install CMake: + 2. Download and install CMake: http://www.cmake.org/cmake/resources/software.html (the Windows zip file) Unpack the contents of the archive to where MinGW is installed (files from bin/ should go into bin/, etc.) - 4. In the file src/CMakeLists.txt, change "set(DXSDK_DIR "c:/dxsdk") to the directory, where you have DirectX SDK - (the slashes must be in this form: /, not \). - 5. Run MinGW console from the shortcut in menu start. - 6. Change to the directory where you have the Colobot sources by typing "cd /c/where/the/sources/are" - 7. Type "cmake -G 'MSYS Makefiles' ." - 8. Type "make" - 9. Everything should compile without errors. + 3. Download the following libraries, installing them in your MinGW directory like with CMake: + SDL, SDL_imgage, SDL_ttf, libpng + 4. Run MinGW console from the shortcut in menu start. + 5. Change to the directory where you have the Colobot sources by typing "cd /c/where/the/sources/are" + 6. Type "cmake -G 'MSYS Makefiles' ." + 7. Type "make" + 8. Everything should compile without errors. -2. Run the compiled game. + Note: you might experience some troubles with OpenGL headers, as Windows (used to?) ship with outdated header files. + Download the newest ones from SDK of your graphics card vendor. + + 1.2 Linux: + + Since you're running Linux, you probably know how to do this anyway ;) + But just in case, here's what you need: + gcc compiler (with gcc-g++), cmake, libraries with header files: SDL, SDL_image, SDL_ttf, libpng + Instructions are the same: + $ cmake . + $ make + + 1.3 Other platforms, compilers, etc. - 1. Download and unpack the package with the game data files. - 2. Copy the compiled files from bin/colobot.exe, bin/CBot/libCBot.dll - and from the directory, where MinGW is installed bin/libgcc_s_dw2-1.dll and bin/libstdc++-6.dll - to the directory with game data (there should be several files named colobot*.dat). - 3. Create a shortcut to the colobot.exe executable and add to the executed command " -nocd" option. - 4. Run the shortcut and enjoy the game. + We haven't checked other platforms yet but the code isn't particularly tied to any compiler or platform, so in theory it should work. + If you can, please try to compile the code on your platform and let us know how it goes. -3. But it's in French! How to change the language? +2. Run the compiled game. - 1. In the source code, find language.h file and change the line #define FRENCH TRUE to #define FRENCH FALSE and do the reverse - on on the language of your choice (English, German or Polish). - 2. Recompile the game and copy bin/colobot.exe. - 3. In the directory with game data switch the directories scene, script and help with those from the directory of given language - (e.g. english/). - 4. Run the game. + 1. Download development data package - make sure you get the latest version as the files will be changed/moved around. + Currently the files are hosted at: http://colobot.info/files (packages are named colobot-data-YYYY-MM-DD.zip) + 2. Unpack the data package to any place you want. + 3. Run the game with commandline option "-datadir where_you_put_the_data_dir" and enjoy the game. PL Jak... -1. Skompilować projekt pod MinGW. +1. Skompilować grę. + + 1.1 Windows: - 1. Ściągamy i instalujemy DirectX SDK w wersji 8.1. Może być problem ze znalezieniem linka bo to już stary SDK, ale można ściągnąć stąd: - http://dl.dropbox.com/u/32866936/dx81sdk_full.exe - 2. Ściągamy i instalujemy MinGW i MSYS: + 1. Ściągamy i instalujemy MinGW i MSYS: http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20111118/ Przy instalacji zaznaczamy wszystkie komponenty do instalacji. - 3. Ściągamy i instalujemy CMake: + 2. Ściągamy i instalujemy CMake: http://www.cmake.org/cmake/resources/software.html (plik zip dla Windowsa) Zip rozpakowujemy do katalogu, gdzie zainstalowany jest MinGW (pliki z bin/ mają trafić do bin/ itd.). - 4. W pliku src/CMakeLists.txt zmieniamy set(DXSDK_DIR "c:/dxsdk") na katalog, gdzie jest zainstalowany DirectX SDK (w wersji 8.1) - (slashe mają być właśnie w takiej postaci: / a nie \). - 5. Uruchamiamy MinGW console ze skrótu w menu start. - 6. Przechodzimy do katalogu, gdzie są źródła wpisując "cd /c/tam/gdzie/sa/zrodla" - 7. Wpisujemy "cmake -G 'MSYS Makefiles' ." - 8. Wpisujemy "make" - 9. Wszystko powinno się skomplikować bez błędów. + 3. Ścągamy następujące biblioteki i instalujemy je tam, gdzie MinGW, podobnie jak z CMake: + SDL, SDL_image, SDL_ttf, libpng + 4. Uruchamiamy MinGW console ze skrótu w menu start. + 5. Przechodzimy do katalogu, gdzie są źródła wpisując "cd /c/tam/gdzie/sa/zrodla" + 6. Wpisujemy "cmake -G 'MSYS Makefiles' ." + 7. Wpisujemy "make" + 8. Wszystko powinno się skomplikować bez błędów. -2. Uruchomić skompilowaną grę. + 1.2 Linux: - 1. Ściągamy paczkę z plikami danych gry. - 2. Kopiujemy skompilowane pliki bin/colobot.exe, bin/CBot/libCBot.dll - i z katalogu, gdzie jest zainstalowany MinGW bin/libgcc_s_dw2-1.dll i bin/libstdc++-6.dll - do katalogu z plikami danych (powinno być tam kilka plików colobot*.dat). - 3. Tworzymy skrót do colobot.exe, przy czym w wywoływanej komendzie dopisujemy na końcu opcję " -nocd". - 4. Odpalamy skrót i cieszymy się grą. + Skoro już masz Linuksa, to prawdpobodobnie wiesz co robić ;) + Ale na wszelki wypadek, potrzebujesz tego: + kompilator gcc (razem z gcc-g++), cmake, biblioteki wraz z nagłówkami: SDL, SDL_image, SDL_ttf, libpng + Polecenia są takie same: + $ cmake . + $ make -3. Ale gra jest po francusku! Jak zmienić język? + 1.3 Inne platformy, kompilatory, etc. + + Nie sprawdzaliśmy jeszcze innych platform, ale kod nie jest jakoś specjalnie związany z danym kompilatorem czy platformą, więc w teorii powinien zadziałać. + Jeśli możesz, spróbuj skompilować kod na twojej platformie i daj nam znać jak poszło. + +2. Uruchomić skompilowaną grę. - 1. W kodzie źródłowym znajdujemy plik language.h i zmieniamy #define FRENCH TRUE na #define FRENCH FALSE i robimy odwrotnie - dla wybranego języka (angielski, niemiecki lub polski). - 2. Kompilujemy od nowa grę i kopiujemy bin/colobot.exe. - 3. W katalogu z plikami danych podmieniamy katalogi scene, script i help z tymi z katalogu danego języka (np. english/). - 4. Odpalamy grę. + 1. Ściągamy paczkę developerską z plikami danych gry - upewnij się, że jest to najnowsza wersja, bo pliki będą zmieniane/przenoszone. + 2. Wypakowujemy pliki gdziekolwiek. + 3. Uruchamiamy grę wraz z opcją "-datadir tam_gdzie_rozpakowałeś_paczkę" i cieszymy się grą. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program 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 3 of the License, or + (at your option) any later version. + + This program 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 this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README-DEV.txt b/README-DEV.txt index fb4f464..eabd8b3 100644 --- a/README-DEV.txt +++ b/README-DEV.txt @@ -1,4 +1,4 @@ README for Developers Please refer to our wiki for developers for current information. Its current address is: -http://colobot.info/wiki/doku.php?id=developers +http://colobot.info/wiki/ diff --git a/README.txt b/README.txt index 6388b43..7745186 100644 --- a/README.txt +++ b/README.txt @@ -1,14 +1,30 @@ +EN + Welcome to Colobot project repository -This repository contains the source files of Colobot game released on open source license (GNU GPLv3) by the producer Epsitec CH. The sources were released and the rights granted to a group of Polish Colobot fans centered around the site http://colobot.cba.pl/ . This repository contains only the source code of the game. The necessary data files will soon be available as a separate download. For now, though, you can download the original download package released by Epsitec (the link is on http://colobot.cba.pl/ site). +This is official repository for the open-source Colobot project developed by Polish Portal of Colobot (PPC; Polish: Polski Portal Colobota) with the official site at: http://colobot.cba.pl/. + +The source code contained here was released by Epsitec -- the original creator of the game -- on open source (GPLv3) license. The code was given and the rights granted specifically to PPC community in March 2012. Since then, we have been modifying the code and working on our goals, which are briefly summed up below. More information about the project in general will soon appear on our site and a wiki we are writing. + +This repository contains only the source code of the game. The game requires also data files which will soon be made available in separate packages. For now, though, you can download the original download package released by Epsitec (the link is on our site). -For more information on the project, see the wiki pages. Status -Our first goal has been reached now: the project has been successfully ported to CMake build system and MinGW compiler from the original MSVC6 project. +Our main goals can be summed up in three milestones: + +Milestone 1 - Colobot Classic + +This is a version of the game that is comprised of the original source files with only minor changes and bugfixes. It is currently maintained in branch named "original" and the master branch is synced to it to provide a working version of the project. There will soon be download packages for the game, in several language versions. + +Milestone 2 - Colobot Gold + +This is a version of the game that we are now focusing our efforts on. It will be the original, refreshed game rewritten using SDL and OpenGL libraries, thus making it multiplatform. Development for this version is continued in dev branch and sub-branches dev-*. The data files will not be altered and the game will be as close to the original as possible. With this release, we hope to reach wider audience and gain more support. + +Milestone 3 - Colobot 2 + +This will be a new installment in the Colobot series. We have many ideas for the new game and we are still discussing them. Generally, the development of this version will begin only after finishing Colobot Gold (it will be probably hosted in another repository, forked off the Colobot Gold code). -Now our goal is to port the game to OpenGL and SDL, thus making it multiplatform. Further goals include adding new features to the game, though that will be decided later on. Compiling and running the game @@ -16,4 +32,38 @@ For these instructions see HOWTO.txt file. Contact -If you want to help in the project, please contact us on the forum on our website (there is also an English board). +If you want to help in the project, please contact us on our IRC channel #colobot at pirc.pl or the forum on our website: http://colobot.cba.pl/forum (there is also an English board). + +PL + +Witamy w repozytorium projektu Colobot + +To jest oficjalne repozytorium z kodem projektu open-source Colobot rozwijanego przez Polski Portal Colobota (PPC; angielski: Polish Portal of Colobot) z oficjalną stroną: http://colobot.cba.pl/. + +Kod źródłowy zawarty tutaj został wydany przez Epsitec -- oryginalnego twórcę gry -- na otwartej licencji (GPLv3). Kod został wydany i prawa nadane specjalnie dla społeczności PPC w marcu 2012. Od tamtej pory, zajmujemy się modyfikowaniem kodu i pracowaniem nad naszymi celami, które są krótko podsumowane poniżej. Więcej informacji o projekcie w ogóle pojawi się wkrótce na naszej stronie i na wiki, które w tej chwili piszemy. + +To repozytorium zawiera tylko kod źródłowy gry. Gra wymaga też plików danych, które niedługo zostaną przygotowane w osobnej paczce. Na razie, możesz pobrać oryginalną paczkę wydaną przez Epsitec (link znajduje się na naszej stronie). + +Status + +Nasze główne cele można podsumować w trzech krokach, które chcemy osiągnąć: + +Krok 1 - Colobot Classic + +To jest wersja gry, która składa się z oryginalnych źródeł, z jedynie niewielkimi zmianami i poprawkami. Obecnie znajduje się w gałęzi nazwanej "original", a gałąź master jest z nią zsynchronizowana, aby udostępnić działającą wersję projektu. Niedługo pojawią się paczki do pobrania gry w kilku wersjach językowych. + +Krok 2 - Colobot Gold + +To jest wersja gry, na której obecnie się skupiamy. Jest to oryginalna, odświeżona gra, przepisana z użyciem bibliotek SDL i OpenGL, w ten sposób czyniąc ją wieloplatformową. Rozwój tej wersji kontynuujemy w gałęzi dev i podgałęziach dev-*. Pliki danych nie będą zmienione dla tej wersji i gra będzie na tyle zbliżona do oryginału na ile to możliwe. Z tym wydaniem, chcemy dotrzeć do szerszej społeczności i uzyskać większe poparcie. + +Krok 3 - Colobot 2 + +To będzie nowa część z cyklu gier Colobot. Mamy wiele pomysłów na nową grę i nadal dyskutujemy nad nimi. Ogólnie, rozwój tej wersji zacznie się po skończeniu wersji Colobot Gold (prawdopodobnie będzie hostowane w osobnym repozytorium, sforkowanym z kodu Colobot Gold). + +Kompilacja i uruchomienie gry + +Instrukcje te znajdują się w pliku HOWTO.txt. + +Kontakt + +Jeżeli chcesz pomóc w projekcie, prosimy o kontakt na naszym kanale IRC: #colobot na pirc.pl albo na forum na naszej stronie: http://colobot.cba.pl/forum. \ No newline at end of file -- cgit v1.2.3-1-g7c22 From 72c0188ec37c3783133baf6960d72cb3c9d12a6c Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 28 Jul 2012 23:36:12 +0200 Subject: GLEW Added GLEW for loading OpenGL extensions --- CMakeLists.txt | 20 ++++++++++++---- HOWTO.txt | 26 ++++++++++++++++---- cmake/FindGLEW.cmake | 51 ++++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 24 +++++++++++++++++++ src/common/config.h.cmake | 2 ++ src/graphics/opengl/gldevice.cpp | 36 ++++++++++++++++++++++++---- 6 files changed, 144 insertions(+), 15 deletions(-) create mode 100644 cmake/FindGLEW.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 82a6e80..092a812 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,12 +5,19 @@ cmake_minimum_required(VERSION 2.8) project(colobot C CXX) # Required packages -find_package(OpenGL REQUIRED) -find_package(SDL REQUIRED) -find_package(SDL_image REQUIRED) -find_package(PNG REQUIRED) +find_package(OpenGL 1.4 REQUIRED) +find_package(SDL 1.2.10 REQUIRED) +find_package(SDL_image 1.2 REQUIRED) +find_package(PNG 1.2 REQUIRED) -# TODO: check for SDL version. Should be >= 1.2.10 +# GLEW requirement depends on platform +# By default it is auto detected +# This setting may be used to override +# Possible values: +# - auto -> determine automatically +# - 1 -> always enable +# - 0 -> always disable +set(USE_GLEW auto) # Build with debugging symbols set(CMAKE_BUILD_TYPE debug) @@ -19,5 +26,8 @@ set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall -std=gnu++0x") set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -std=gnu++0x") +# Include cmake directory +SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${colobot_SOURCE_DIR}/cmake") + # Subdirectory with sources add_subdirectory(src bin) diff --git a/HOWTO.txt b/HOWTO.txt index f93bb0e..4c17234 100644 --- a/HOWTO.txt +++ b/HOWTO.txt @@ -13,16 +13,19 @@ How to... http://www.cmake.org/cmake/resources/software.html (the Windows zip file) Unpack the contents of the archive to where MinGW is installed (files from bin/ should go into bin/, etc.) 3. Download the following libraries, installing them in your MinGW directory like with CMake: - SDL, SDL_imgage, SDL_ttf, libpng + SDL >=1.2.10, SDL_imgage >= 1.2, SDL_ttf >= 2.0, libpng >= 1.2, GLEW >= 1.8.0 + Note #1: For most libraries, you can download binary packages with compiled files. + However, you must ensure that they work with MinGW as some are built with MSVC + and may be incompatible. If that is the case, you should compile the libraries from sources + using MinGW. + Note #2: For GLEW, you need to compile from source under MinGW. Since there is no automated + make script for that, follow the instructions here: http://stackoverflow.com/questions/6005076/ 4. Run MinGW console from the shortcut in menu start. 5. Change to the directory where you have the Colobot sources by typing "cd /c/where/the/sources/are" 6. Type "cmake -G 'MSYS Makefiles' ." 7. Type "make" 8. Everything should compile without errors. - Note: you might experience some troubles with OpenGL headers, as Windows (used to?) ship with outdated header files. - Download the newest ones from SDK of your graphics card vendor. - 1.2 Linux: Since you're running Linux, you probably know how to do this anyway ;) @@ -32,6 +35,9 @@ How to... $ cmake . $ make + Note: If you experience problems with OpenGL's extensions, install GLEW library and enable + it in compilation by setting USE_GLEW to 1 in CMakeLists.txt + 1.3 Other platforms, compilers, etc. We haven't checked other platforms yet but the code isn't particularly tied to any compiler or platform, so in theory it should work. @@ -60,7 +66,14 @@ Jak... http://www.cmake.org/cmake/resources/software.html (plik zip dla Windowsa) Zip rozpakowujemy do katalogu, gdzie zainstalowany jest MinGW (pliki z bin/ mają trafić do bin/ itd.). 3. Ścągamy następujące biblioteki i instalujemy je tam, gdzie MinGW, podobnie jak z CMake: - SDL, SDL_image, SDL_ttf, libpng + SDL >= 1.2.10, SDL_image >= 1.2, SDL_ttf >= 2.0, libpng >= 1.2 + Uwaga #1: W większości wymienionych bibliotek można ściągnąć paczki binarne ze skompilowanymi plikami. + Jednak musisz się upewnić, że pliki te będą współpracowały z MinGW, bo część z nich + jest kompilowana MSVC i może być niezgodna. W takim wypadku, musisz skompilować bibliotekę + ze źródeł pod MinGW. + + Uwaga #2: W przypadku GLEW, musisz skompilować bibiotekę ze źródeł pod MinGW. Ponieważ nie ma skryptu + make do tego, użyj poleceń opisanych tutaj: http://stackoverflow.com/questions/6005076/ 4. Uruchamiamy MinGW console ze skrótu w menu start. 5. Przechodzimy do katalogu, gdzie są źródła wpisując "cd /c/tam/gdzie/sa/zrodla" 6. Wpisujemy "cmake -G 'MSYS Makefiles' ." @@ -76,6 +89,9 @@ Jak... $ cmake . $ make + Uwaga: Jeśli natrafisz na problemy z rozszerzeniami OpenGL, zainstaluj bibliotekę GLEW i włącz ją + przy kompilacji, ustawiając USE_GLEW na 1 w CMakeLists.txt + 1.3 Inne platformy, kompilatory, etc. Nie sprawdzaliśmy jeszcze innych platform, ale kod nie jest jakoś specjalnie związany z danym kompilatorem czy platformą, więc w teorii powinien zadziałać. diff --git a/cmake/FindGLEW.cmake b/cmake/FindGLEW.cmake new file mode 100644 index 0000000..94c9b32 --- /dev/null +++ b/cmake/FindGLEW.cmake @@ -0,0 +1,51 @@ +# CMake module to find GLEW +# Borrowed from http://code.google.com/p/nvidia-texture-tools/ +# MIT license Copyright (c) 2007 NVIDIA Corporation + +# Try to find GLEW library and include path. +# Once done this will define +# +# GLEW_FOUND +# GLEW_INCLUDE_PATH +# GLEW_LIBRARY +# + +IF (WIN32) + FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h + $ENV{PROGRAMFILES}/GLEW/include + ${PROJECT_SOURCE_DIR}/src/nvgl/glew/include + DOC "The directory where GL/glew.h resides") + FIND_LIBRARY( GLEW_LIBRARY + NAMES glew GLEW glew32 glew32s + PATHS + /mingw/bin # for MinGW's MSYS + /mingw/lib + ${PROJECT_SOURCE_DIR}/glew/bin # or in local directory + ${PROJECT_SOURCE_DIR}/glew/lib + DOC "The GLEW library") +ELSE (WIN32) + FIND_PATH( GLEW_INCLUDE_PATH GL/glew.h + /usr/include + /usr/local/include + /sw/include + /opt/local/include + DOC "The directory where GL/glew.h resides") + FIND_LIBRARY( GLEW_LIBRARY + NAMES GLEW glew + PATHS + /usr/lib64 + /usr/lib + /usr/local/lib64 + /usr/local/lib + /sw/lib + /opt/local/lib + DOC "The GLEW library") +ENDIF (WIN32) + +IF (GLEW_INCLUDE_PATH) + SET( GLEW_FOUND 1 CACHE STRING "Set to 1 if GLEW is found, 0 otherwise") +ELSE (GLEW_INCLUDE_PATH) + SET( GLEW_FOUND 0 CACHE STRING "Set to 1 if GLEW is found, 0 otherwise") +ENDIF (GLEW_INCLUDE_PATH) + +MARK_AS_ADVANCED( GLEW_FOUND ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index da8463b..3896e40 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,18 +11,40 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(PLATFORM_WINDOWS 1) set(PLATFORM_LINUX 0) set(PLATFORM_OTHER 0) + # On Windows, GLEW is required + if (${USE_GLEW} MATCHES "auto") + set(USE_GLEW 1) + endif() elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 1) set(PLATFORM_OTHER 0) + # On Linux, we should be fine without GLEW + if (${USE_GLEW} MATCHES "auto") + set(USE_GLEW 0) + endif() # for clock_gettime set(PLATFORM_LIBS "-lrt") else() set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 0) set(PLATFORM_OTHER 1) + # Use GLEW to be safe + if (${USE_GLEW} MATCHES "auto") + set(USE_GLEW 1) + endif() endif() +set(OPTIONAL_LIBS "") +set(OPTIONAL_INCLUDE_DIRS "") + +if(${USE_GLEW} EQUAL 1) + find_package(GLEW REQUIRED) + set(OPTIONAL_LIBS ${OPTIONAL_LIBS} ${GLEW_LIBRARY}) + set(OPTIONAL_INCLUDE_DIRS ${OPTIONAL_INCLUDE_DIRS} ${GLEW_INCLUDE_PATH}) +endif() + + # Configure file configure_file(common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h) @@ -156,6 +178,7 @@ ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${PNG_LIBRARIES} +${OPTIONAL_LIBS} ${PLATFORM_LIBS} CBot ) @@ -165,6 +188,7 @@ ${SDL_INCLUDE_DIR} ${SDL_IMAGE_INCLUDE_DIR} ${SDLTTF_INCLUDE_DIR} ${PNG_INCLUDE_DIRS} +${OPTIONAL_INCLUDE_DIRS} ) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/CBot) diff --git a/src/common/config.h.cmake b/src/common/config.h.cmake index d8bff91..f496db0 100644 --- a/src/common/config.h.cmake +++ b/src/common/config.h.cmake @@ -2,7 +2,9 @@ // Macros set by CMake #cmakedefine DEBUG + #cmakedefine PLATFORM_WINDOWS @PLATFORM_WINDOWS@ #cmakedefine PLATFORM_LINUX @PLATFORM_LINUX@ #cmakedefine PLATFORM_OTHER @PLATFORM_OTHER@ +#cmakedefine USE_GLEW @USE_GLEW@ diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index d31d007..bfe7fd7 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -16,10 +16,20 @@ // gldevice.cpp -#include "common/image.h" #include "graphics/opengl/gldevice.h" + +#include "common/config.h" +#include "common/image.h" #include "math/geometry.h" + +#if defined(USE_GLEW) + +// When using GLEW, only glew.h is needed +#include + +#else + // Should define prototypes of used extensions as OpenGL functions #define GL_GLEXT_PROTOTYPES @@ -27,9 +37,11 @@ #include #include +#endif // if defined(GLEW) + #include -#include +#include @@ -73,9 +85,23 @@ std::string Gfx::CGLDevice::GetError() bool Gfx::CGLDevice::Create() { - /* NOTE: extension testing is not done here as the assumed version of OpenGL to be used (1.4+) - must already have the required extensions. The used extensions are listed here for reference: - GL_ARB_multitexture, GL_EXT_texture_env_combine, GL_EXT_secondary_color */ +#if defined(USE_GLEW) + if (glewInit() != GLEW_OK) + { + m_error = "GLEW initialization failed"; + return false; + } + + if ( (! GLEW_ARB_multitexture) || (! GLEW_EXT_texture_env_combine) || (! GLEW_EXT_secondary_color) ) + { + m_error = "GLEW reports required extensions not supported"; + return false; + } + +#endif + + /* NOTE: when not using GLEW, extension testing is not performed, as it is assumed that + glext.h is up-to-date and the OpenGL shared library has the required functions present. */ m_wasInit = true; -- cgit v1.2.3-1-g7c22 From 7c5a3514dd6e907866bddcbb09b4d9cbd958dd8e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 29 Jul 2012 15:09:53 +0200 Subject: Video mode changing - added video mode querying & changing - added joystick querying & changing - cleaned up CApplication interface --- src/app/app.cpp | 408 ++++++++++++++++++++++++--------------- src/app/app.h | 123 ++++++++---- src/common/event.h | 33 +++- src/graphics/core/device.h | 10 +- src/graphics/core/texture.h | 10 +- src/graphics/engine/engine.cpp | 20 +- src/graphics/engine/engine.h | 21 +- src/graphics/opengl/gldevice.cpp | 44 +++-- src/graphics/opengl/gldevice.h | 6 +- src/math/intsize.h | 61 ++++++ src/math/size.h | 66 +++++++ 11 files changed, 580 insertions(+), 222 deletions(-) create mode 100644 src/math/intsize.h create mode 100644 src/math/size.h diff --git a/src/app/app.cpp b/src/app/app.cpp index c778a63..5c6ef49 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -56,19 +56,14 @@ struct ApplicationPrivate SDL_Event currentEvent; //! Joystick SDL_Joystick *joystick; - //! Index of joystick device - int joystickIndex; //! Id of joystick timer SDL_TimerID joystickTimer; - //! Current configuration of OpenGL display device - Gfx::GLDeviceConfig deviceConfig; ApplicationPrivate() { memset(¤tEvent, 0, sizeof(SDL_Event)); surface = NULL; joystick = NULL; - joystickIndex = 0; joystickTimer = 0; } }; @@ -77,11 +72,8 @@ struct ApplicationPrivate CApplication::CApplication() { - m_private = new ApplicationPrivate(); - m_exitCode = 0; - - m_iMan = new CInstanceManager(); - + m_private = new ApplicationPrivate(); + m_iMan = new CInstanceManager(); m_eventQueue = new CEventQueue(m_iMan); m_engine = NULL; @@ -90,21 +82,16 @@ CApplication::CApplication() m_sound = NULL; m_keyState = 0; - m_axeKey = Math::Vector(0.0f, 0.0f, 0.0f); - m_axeJoy = Math::Vector(0.0f, 0.0f, 0.0f); - - m_active = false; - m_activateApp = false; - m_ready = false; - m_joystickEnabled = false; + m_axeKey = Math::Vector(0.0f, 0.0f, 0.0f); + m_axeJoy = Math::Vector(0.0f, 0.0f, 0.0f); - m_time = 0.0f; + m_exitCode = 0; + m_active = false; + m_debugMode = false; - m_windowTitle = "COLOBOT"; + m_windowTitle = "COLOBOT"; - m_showStats = false; - m_debugMode = false; - m_setupMode = true; + m_joystickEnabled = false; m_dataPath = "./data"; @@ -139,7 +126,6 @@ bool CApplication::ParseArguments(int argc, char *argv[]) if (arg == "-debug") { - m_showStats = true; SetDebugMode(true); } else if (arg == "-datadir") @@ -165,7 +151,7 @@ bool CApplication::Create() // TODO: verify that data directory exists // Temporarily -- only in windowed mode - m_private->deviceConfig.fullScreen = false; + m_deviceConfig.fullScreen = false; // Create the 3D engine m_engine = new Gfx::CEngine(m_iMan, this); @@ -186,71 +172,30 @@ bool CApplication::Create() if (SDL_Init(initFlags) < 0) { - SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL initialization error:\n" + - std::string(SDL_GetError()) ); + SystemDialog( SDT_ERROR, "COLOBOT - Fatal Error", + "SDL initialization error:\n" + + std::string(SDL_GetError()) ); m_exitCode = 2; return false; } if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0) { - SystemDialog( SDT_ERROR, "COLOBOT - Error", std::string("SDL_Image initialization error:\n") + - std::string(IMG_GetError()) ); + SystemDialog( SDT_ERROR, "COLOBOT - Fatal Error", + std::string("SDL_Image initialization error:\n") + + std::string(IMG_GetError()) ); m_exitCode = 3; return false; } - const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); - if (videoInfo == NULL) - { - SystemDialog( SDT_ERROR, "COLOBOT - Error", "SDL error while getting video info:\n " + - std::string(SDL_GetError()) ); - m_exitCode = 2; - return false; - } - - Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; - - if (m_private->deviceConfig.resizeable) - videoFlags |= SDL_RESIZABLE; - - // Use hardware surface if available - if (videoInfo->hw_available) - videoFlags |= SDL_HWSURFACE; - else - videoFlags |= SDL_SWSURFACE; - - // Enable hardware blit if available - if (videoInfo->blit_hw) - videoFlags |= SDL_HWACCEL; - - if (m_private->deviceConfig.fullScreen) - videoFlags |= SDL_FULLSCREEN; - - // Set OpenGL attributes - - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, m_private->deviceConfig.redSize); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, m_private->deviceConfig.greenSize); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, m_private->deviceConfig.blueSize); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, m_private->deviceConfig.alphaSize); - - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, m_private->deviceConfig.depthSize); - - if (m_private->deviceConfig.doubleBuf) - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - /* If hardware acceleration specifically requested, this will force the hw accel - and fail with error if not available */ - if (m_private->deviceConfig.hardwareAccel) - SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); - - m_private->surface = SDL_SetVideoMode(m_private->deviceConfig.width, m_private->deviceConfig.height, - m_private->deviceConfig.bpp, videoFlags); + if (! CreateVideoSurface()) + return false; // dialog is in function if (m_private->surface == NULL) { - SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("SDL error while setting video mode:\n") + - std::string(SDL_GetError()) ); + SystemDialog( SDT_ERROR, "COLOBT - Fatal Error", + std::string("SDL error while setting video mode:\n") + + std::string(SDL_GetError()) ); m_exitCode = 2; return false; } @@ -269,11 +214,12 @@ bool CApplication::Create() // The video is ready, we can create and initalize the graphics device - m_device = new Gfx::CGLDevice(); + m_device = new Gfx::CGLDevice(m_deviceConfig); if (! m_device->Create() ) { - SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CDevice::Create() :\n") + - std::string(m_device->GetError()) ); + SystemDialog( SDT_ERROR, "COLOBT - Fatal Error", + std::string("Error in CDevice::Create() :\n") + + std::string(m_device->GetError()) ); m_exitCode = 1; return false; } @@ -281,23 +227,74 @@ bool CApplication::Create() m_engine->SetDevice(m_device); if (! m_engine->Create() ) { - SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CEngine::Create() :\n") + - std::string(m_engine->GetError()) ); + SystemDialog( SDT_ERROR, "COLOBT - Fatal Error", + std::string("Error in CEngine::Create() :\n") + + std::string(m_engine->GetError()) ); m_exitCode = 1; return false; } if (! m_engine->AfterDeviceSetInit() ) { - SystemDialog( SDT_ERROR, "COLOBT - Error", std::string("Error in CEngine::AfterDeviceSetInit() :\n") + - std::string(m_engine->GetError()) ); + SystemDialog( SDT_ERROR, "COLOBT - Fatal Error", + std::string("Error in CEngine::AfterDeviceSetInit() :\n") + + std::string(m_engine->GetError()) ); m_exitCode = 1; return false; } + return true; +} + +bool CApplication::CreateVideoSurface() +{ + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + if (videoInfo == NULL) + { + SystemDialog( SDT_ERROR, "COLOBOT - Fatal Error", + std::string("SDL error while getting video info:\n ") + + std::string(SDL_GetError()) ); + m_exitCode = 2; + return false; + } + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + // Use hardware surface if available + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + // Enable hardware blit if available + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + if (m_deviceConfig.fullScreen) + videoFlags |= SDL_FULLSCREEN; + + if (m_deviceConfig.resizeable) + videoFlags |= SDL_RESIZABLE; + + // Set OpenGL attributes + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, m_deviceConfig.redSize); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, m_deviceConfig.greenSize); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, m_deviceConfig.blueSize); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, m_deviceConfig.alphaSize); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, m_deviceConfig.depthSize); + + if (m_deviceConfig.doubleBuf) + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + /* If hardware acceleration specifically requested, this will force the hw accel + and fail with error if not available */ + if (m_deviceConfig.hardwareAccel) + SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); - // The app is ready to go - m_ready = true; + m_private->surface = SDL_SetVideoMode(m_deviceConfig.size.w, m_deviceConfig.size.h, + m_deviceConfig.bpp, videoFlags); return true; } @@ -351,15 +348,73 @@ void CApplication::Destroy() SDL_Quit(); } +bool CApplication::ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig) +{ + static bool restore = false; + + m_lastDeviceConfig = m_deviceConfig; + m_deviceConfig = newConfig; + + + SDL_FreeSurface(m_private->surface); + + if (! CreateVideoSurface()) + { + // Fatal error, so post the quit event + m_eventQueue->AddEvent(Event(EVENT_QUIT)); + return false; + } + + if (m_private->surface == NULL) + { + if (! restore) + { + SystemDialog( SDT_ERROR, "COLOBT - Error", + std::string("SDL error while setting video mode:\n") + + std::string(SDL_GetError()) + std::string("\n") + + std::string("Previous mode will be restored") ); + + restore = true; + ChangeVideoConfig(m_lastDeviceConfig); + return false; + } + else + { + restore = false; + + SystemDialog( SDT_ERROR, "COLOBT - Fatal Error", + std::string("SDL error while restoring previous video mode:\n") + + std::string(SDL_GetError()) ); + + + // Fatal error, so post the quit event + m_eventQueue->AddEvent(Event(EVENT_QUIT)); + return false; + } + } + + ( static_cast(m_device) )->ConfigChanged(m_deviceConfig); + + m_engine->ResetAfterDeviceChanged(); + + return true; +} + bool CApplication::OpenJoystick() { - m_private->joystick = SDL_JoystickOpen(m_private->joystickIndex); + if ( (m_joystick.index < 0) || (m_joystick.index >= SDL_NumJoysticks()) ) + return false; + + m_private->joystick = SDL_JoystickOpen(m_joystick.index); if (m_private->joystick == NULL) return false; + m_joystick.axisCount = SDL_JoystickNumAxes(m_private->joystick); + m_joystick.buttonCount = SDL_JoystickNumButtons(m_private->joystick); + // Create the vectors with joystick axis & button states to exactly the required size - m_joyAxeState = std::vector(SDL_JoystickNumAxes(m_private->joystick), 0); - m_joyButtonState = std::vector(SDL_JoystickNumButtons(m_private->joystick), false); + m_joyAxeState = std::vector(m_joystick.axisCount, 0); + m_joyButtonState = std::vector(m_joystick.buttonCount, false); // Create a timer for polling joystick state m_private->joystickTimer = SDL_AddTimer(JOYSTICK_TIMER_INTERVAL, JoystickTimerCallback, NULL); @@ -367,7 +422,6 @@ bool CApplication::OpenJoystick() return true; } - void CApplication::CloseJoystick() { // Timer will remove itself automatically @@ -376,6 +430,17 @@ void CApplication::CloseJoystick() m_private->joystick = NULL; } +bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick) +{ + if ( (newJoystick.index < 0) || (newJoystick.index >= SDL_NumJoysticks()) ) + return false; + + if (m_private->joystick != NULL) + CloseJoystick(); + + return OpenJoystick(); +} + Uint32 JoystickTimerCallback(Uint32 interval, void *) { CApplication *app = CApplication::GetInstancePointer(); @@ -452,6 +517,8 @@ int CApplication::Run() // To be sure no old event remains m_private->currentEvent.type = SDL_NOEVENT; + // Call SDL_PumpEvents() only once here + // (SDL_PeepEvents() doesn't call it) if (m_active) SDL_PumpEvents(); @@ -462,11 +529,11 @@ int CApplication::Run() int count = 0; // Use SDL_PeepEvents() if the app is active, so we can use idle time to - // render the scene. Else, use SDL_PollEvent() to avoid eating CPU time. + // render the scene. Else, use SDL_WaitEvent() to avoid eating CPU time. if (m_active) count = SDL_PeepEvents(&m_private->currentEvent, 1, SDL_GETEVENT, SDL_ALLEVENTS); else - count = SDL_PollEvent(&m_private->currentEvent); + count = SDL_WaitEvent(&m_private->currentEvent); // If received an event if (count > 0) @@ -492,7 +559,7 @@ int CApplication::Run() } // Enter game update & frame rendering only if active - if (m_active && m_ready) + if (m_active) { Event event; while (m_eventQueue->GetEvent(event)) @@ -554,14 +621,14 @@ PressState TranslatePressState(unsigned char state) - y: 0=down, 1=up */ Math::Point CApplication::WindowToInterfaceCoords(Math::IntPoint pos) { - return Math::Point( (float)pos.x / (float)m_private->deviceConfig.width, - 1.0f - (float)pos.y / (float)m_private->deviceConfig.height); + return Math::Point( (float)pos.x / (float)m_deviceConfig.size.w, + 1.0f - (float)pos.y / (float)m_deviceConfig.size.h); } Math::IntPoint CApplication::InterfaceToWindowCoords(Math::Point pos) { - return Math::IntPoint((int)(pos.x * m_private->deviceConfig.width), - (int)((1.0f - pos.y) * m_private->deviceConfig.height)); + return Math::IntPoint((int)(pos.x * m_deviceConfig.size.w), + (int)((1.0f - pos.y) * m_deviceConfig.size.h)); } /** The SDL event parsed is stored internally. @@ -626,6 +693,19 @@ Event CApplication::ParseEvent() event.joyButton.button = m_private->currentEvent.jbutton.button; event.joyButton.state = TranslatePressState(m_private->currentEvent.jbutton.state); } + else if (m_private->currentEvent.type == SDL_ACTIVEEVENT) + { + event.type = EVENT_ACTIVE; + + if (m_private->currentEvent.active.type & SDL_APPINPUTFOCUS) + event.active.flags |= ACTIVE_INPUT; + if (m_private->currentEvent.active.type & SDL_APPMOUSEFOCUS) + event.active.flags |= ACTIVE_MOUSE; + if (m_private->currentEvent.active.type & SDL_APPACTIVE) + event.active.flags |= ACTIVE_APP; + + event.active.gain = m_private->currentEvent.active.gain == 1; + } return event; } @@ -636,6 +716,14 @@ Event CApplication::ParseEvent() bool CApplication::ProcessEvent(const Event &event) { CLogger *l = GetLogger(); + + if (event.type == EVENT_ACTIVE) + { + m_active = event.active.gain; + if (m_debugMode) + l->Info("Focus change: active = %s\n", m_active ? "true" : "false"); + } + // Print the events in debug mode to test the code if (m_debugMode) { @@ -672,6 +760,11 @@ bool CApplication::ProcessEvent(const Event &event) l->Info(" button = %d\n", event.joyButton.button); l->Info(" state = %s\n", (event.joyButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED"); break; + case EVENT_ACTIVE: + l->Info("EVENT_ACTIVE:\n"); + l->Info(" flags = 0x%x\n", event.active.flags); + l->Info(" gain = %s\n", event.active.gain ? "true" : "false"); + break; default: break; } @@ -688,48 +781,58 @@ bool CApplication::Render() if (! result) return false; - if (m_private->deviceConfig.doubleBuf) + if (m_deviceConfig.doubleBuf) SDL_GL_SwapBuffers(); return true; } -/** Called in to toggle the pause state of the app. */ -void CApplication::Pause(bool pause) -{ - static long appPausedCount = 0L; - - appPausedCount += ( pause ? +1 : -1 ); - m_ready = appPausedCount == 0; - - // Handle the first pause request (of many, nestable pause requests) - if( pause && ( 1 == appPausedCount ) ) - { - // Stop the scene from animating - //m_engine->TimeEnterGel(); - } - - // Final pause request done - if (appPausedCount == 0) - { - // Restart the scene - //m_engine->TimeExitGel(); - } -} - void CApplication::StepSimulation(float rTime) { // TODO } -void CApplication::SetShowStat(bool show) +VideoQueryResult CApplication::GetVideoResolutionList(std::vector &resolutions, + bool fullScreen, bool resizeable) { - m_showStats = show; -} + resolutions.clear(); -bool CApplication::GetShowStat() -{ - return m_showStats; + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + if (videoInfo == NULL) + return VIDEO_QUERY_ERROR; + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + // Use hardware surface if available + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + // Enable hardware blit if available + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + if (resizeable) + videoFlags |= SDL_RESIZABLE; + + if (fullScreen) + videoFlags |= SDL_FULLSCREEN; + + + SDL_Rect **modes = SDL_ListModes(NULL, videoFlags); + + if (modes == (SDL_Rect **)0) + return VIDEO_QUERY_NONE; // no modes available + + if (modes == (SDL_Rect **)-1) + return VIDEO_QUERY_ALL; // all resolutions are possible + + + for (int i = 0; modes[i] != NULL; ++i) + resolutions.push_back(Math::IntSize(modes[i]->w, modes[i]->h)); + + return VIDEO_QUERY_OK; } void CApplication::SetDebugMode(bool mode) @@ -742,11 +845,6 @@ bool CApplication::GetDebugMode() return m_debugMode; } -bool CApplication::GetSetupMode() -{ - return m_setupMode; -} - void CApplication::FlushPressKey() { // TODO @@ -803,6 +901,28 @@ Math::Point CApplication::GetSystemMousePos() return m_systemMousePos; } +std::vector CApplication::GetJoystickList() +{ + std::vector result; + + int count = SDL_NumJoysticks(); + + for (int index = 0; index < count; ++index) + { + JoystickDevice device; + device.index = index; + device.name = SDL_JoystickName(index); + result.push_back(device); + } + + return result; +} + +JoystickDevice CApplication::GetJoystick() +{ + return m_joystick; +} + void CApplication::SetJoystickEnabled(bool enable) { m_joystickEnabled = enable; @@ -825,32 +945,6 @@ bool CApplication::GetJoystickEnabled() return m_joystickEnabled; } -bool CApplication::WriteScreenShot(char *filename, int width, int height) -{ - // TODO - return false; -} - -void CApplication::InitText() -{ - // TODO -} - -void CApplication::DrawSuppl() -{ - // TODO -} - -void CApplication::ShowStats() -{ - // TODO -} - -void CApplication::OutputText(long x, long y, char* str) -{ - // TODO -} - std::string CApplication::GetDataFilePath(const std::string& dirName, const std::string& fileName) { return m_dataPath + "/" + dirName + "/" + fileName; diff --git a/src/app/app.h b/src/app/app.h index 956eab8..483aa55 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -24,6 +24,8 @@ #include "common/singleton.h" #include "graphics/core/device.h" #include "graphics/engine/engine.h" +#include "graphics/opengl/gldevice.h" +#include "math/intsize.h" #include #include @@ -34,6 +36,35 @@ class CEvent; class CRobotMain; class CSound; +/** + \struct JoystickDevice + \brief Information about a joystick device */ +struct JoystickDevice +{ + //! Device index (-1 = invalid device) + int index; + //! Device name + std::string name; + //! Number of axes (only available after joystick opened) + int axisCount; + //! Number of buttons (only available after joystick opened) + int buttonCount; + + JoystickDevice() + : index(-1), axisCount(0), buttonCount(0) {} +}; + +/** + \enum VideoQueryResult + \brief Result of querying for available video resolutions */ +enum VideoQueryResult +{ + VIDEO_QUERY_ERROR, + VIDEO_QUERY_NONE, + VIDEO_QUERY_ALL, + VIDEO_QUERY_OK +}; + struct ApplicationPrivate; @@ -46,26 +77,38 @@ struct ApplicationPrivate; * * It is a singleton class with only one instance that can be created. * - * Creation of other main objects + * \section Creation Creation of other main objects * * The class creates the only instance of CInstanceManager, CEventQueue, CEngine, * CRobotMain and CSound classes. * - * Window management + * \section Window Window management * * The class is responsible for creating app window, setting and changing the video mode, - * setting the position of mouse and changing the cursor, grabbing and writing screenshots. + * joystick management, grabbing input and changing the system mouse cursor + * position and visibility. + * ("System mouse cursor" means the cursor displayed by the OS in constrast to the cursor + * displayed by CEngine). + * + * \section Events Events * - * Events + * Events are taken from SDL event queue, translated to common events from src/common.h + * and pushed to global event queue CEventQueue. * - * Events are taken from SDL event queue and either handled by CApplication or translated - * to common events from src/common.h and pushed to global event queue CEventQueue. * Joystick events are generated somewhat differently, by running a separate timer, * polling the device for changes and synthesising events on change. It avoids flooding * the event queue with too many joystick events and the granularity of the timer can be * adjusted. * - * The events are further handled in CRobotMain class. + * The events are passed to ProcessEvent() of classes in this order: CApplication, CEngine + * and CRobotMain. CApplication and CEngine's ProcessEvent() functions return bool, which + * means whether to pass the event on, or stop the chain. This is to enable handling some + * events which are internal to CApplication or CEngine. + * + * \section Portability Portability + * + * Currently, the class only handles OpenGL devices. SDL can be used with DirectX, but + * for that to work, video initialization and video setting must be done differently. * */ class CApplication : public CSingleton @@ -89,26 +132,36 @@ public: //! Cleans up before exit void Destroy(); - //! Enters the pause mode - void Pause(bool pause); + //! Returns a list of possible video modes + VideoQueryResult GetVideoResolutionList(std::vector &resolutions, + bool fullScreen, bool resizeable); + + //! Returns the current video mode + Gfx::GLDeviceConfig GetVideoConfig(); + + //! Change the video mode to given mode + bool ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig); //! Updates the simulation state void StepSimulation(float rTime); - //! Polls the state of joystick axes and buttons - void UpdateJoystick(); - - void SetShowStat(bool show); - bool GetShowStat(); + //! Returns a list of available joystick devices + std::vector GetJoystickList(); - void SetDebugMode(bool mode); - bool GetDebugMode(); + //! Returns info about the current joystick + JoystickDevice GetJoystick(); - bool GetSetupMode(); + //! Change the current joystick device + bool ChangeJoystick(const JoystickDevice &newJoystick); + //! Enables/disables joystick void SetJoystickEnabled(bool enable); + //! Returns whether joystick is enabled bool GetJoystickEnabled(); + //! Polls the state of joystick axes and buttons + void UpdateJoystick(); + void FlushPressKey(); void ResetKey(); void SetKey(int keyRank, int option, int key); @@ -129,12 +182,18 @@ public: //! Returns the position of system mouse cursor (in interface coords) Math::Point GetSystemMousePos(); - bool WriteScreenShot(char *filename, int width, int height); + //! Enables/disables debug mode (prints more info in logger) + void SetDebugMode(bool mode); + //! Returns whether debug mode is enabled + bool GetDebugMode(); //! Returns the full path to a file in data directory std::string GetDataFilePath(const std::string &dirName, const std::string &fileName); protected: + //! Creates the window's SDL_Surface + bool CreateVideoSurface(); + //! Processes the captured SDL event to Event struct Event ParseEvent(); //! Handles some incoming events @@ -152,11 +211,6 @@ protected: //! Converts the interface coords to window coords Math::IntPoint InterfaceToWindowCoords(Math::Point pos); - void InitText(); - void DrawSuppl(); - void ShowStats(); - void OutputText(long x, long y, char* str); - protected: //! Instance manager CInstanceManager* m_iMan; @@ -175,17 +229,15 @@ protected: //! Code to return at exit int m_exitCode; - + //! Whether application window is active bool m_active; - bool m_activateApp; - bool m_ready; - - bool m_showStats; + //! Whether debug mode is enabled bool m_debugMode; - bool m_setupMode; - //! Whether joystick is enabled - bool m_joystickEnabled; + //! Current configuration of OpenGL display device + Gfx::GLDeviceConfig m_deviceConfig; + //! Previous configuration of OpenGL display device + Gfx::GLDeviceConfig m_lastDeviceConfig; //! Text set as window title std::string m_windowTitle; @@ -196,14 +248,17 @@ protected: Math::Point m_systemMousePos; long m_mouseWheel; + long m_key[50][2]; + + //! Info about current joystick device + JoystickDevice m_joystick; + //! Whether joystick is enabled + bool m_joystickEnabled; //! Current state of joystick axes; may be updated from another thread std::vector m_joyAxeState; //! Current state of joystick buttons; may be updated from another thread std::vector m_joyButtonState; - float m_time; - long m_key[50][2]; - //! Path to directory with data files std::string m_dataPath; }; diff --git a/src/common/event.h b/src/common/event.h index 6ce1a79..0d9aa7c 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -55,6 +55,9 @@ enum EventType //! Event sent after releasing a key EVENT_KEY_UP = 9, + //! Event sent when application window loses/gains focus + EVENT_ACTIVE = 10, + //? EVENT_CHAR = 10, //? EVENT_FOCUS = 11, @@ -608,7 +611,7 @@ struct JoyAxisEventData }; /** \struct JoyButtonEventData - \brief Joystick button event structure */ + \brief Additional data for joystick button event */ struct JoyButtonEventData { //! The joystick button index @@ -620,7 +623,31 @@ struct JoyButtonEventData : button(0), state(STATE_PRESSED) {} }; -// TODO: JoyHatEventData? JoyBallEventData? +/** \enum ActiveEventFlags + \brief Type of focus gained/lost */ +enum ActiveEventFlags +{ + //! Application window focus + ACTIVE_APP = 0x01, + //! Input focus + ACTIVE_INPUT = 0x02, + //! Mouse focus + ACTIVE_MOUSE = 0x04 + +}; + +/** \struct ActiveEventData + \brief Additional data for active event */ +struct ActiveEventData +{ + //! Flags (bitmask of enum values ActiveEventFlags) + unsigned char flags = 0; + //! True if the focus was gained; false otherwise + bool gain; + + ActiveEventData() + : flags(0), gain(false) {} +}; /** @@ -652,6 +679,8 @@ struct Event JoyAxisEventData joyAxis; //! Additional data for EVENT_JOY_AXIS JoyButtonEventData joyButton; + //! Additional data for EVENT_ACTIVE + ActiveEventData active; //? long param; // parameter //? Math::Point pos; // mouse position (0 .. 1) diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h index ae612b7..1f6cacf 100644 --- a/src/graphics/core/device.h +++ b/src/graphics/core/device.h @@ -25,6 +25,7 @@ #include "graphics/core/material.h" #include "graphics/core/texture.h" #include "graphics/core/vertex.h" +#include "math/intsize.h" #include "math/matrix.h" #include @@ -43,10 +44,8 @@ namespace Gfx { */ struct DeviceConfig { - //! Screen width - int width; - //! Screen height - int height; + //! Screen size + Math::IntSize size; //! Bits per pixel int bpp; //! Full screen @@ -64,8 +63,7 @@ struct DeviceConfig //! Loads the default values inline void LoadDefault() { - width = 800; - height = 600; + size = Math::IntSize(800, 600); bpp = 32; fullScreen = false; resizeable = false; diff --git a/src/graphics/core/texture.h b/src/graphics/core/texture.h index 787c2bf..8d6b082 100644 --- a/src/graphics/core/texture.h +++ b/src/graphics/core/texture.h @@ -18,6 +18,9 @@ #pragma once +#include "math/intsize.h" + + namespace Gfx { /** @@ -190,10 +193,8 @@ struct Texture bool valid; //! ID of the texture in graphics engine unsigned int id; - //! Width of texture - int width; - //! Height of texture - int height; + //! Size of texture + Math::IntSize size; //! Whether the texture has alpha channel bool alpha; @@ -201,7 +202,6 @@ struct Texture { valid = false; id = 0; - width = height = 0; alpha = false; } diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 0914f9e..e544ee3 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -62,9 +62,6 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_sound = NULL; m_terrain = NULL; - m_dim.x = 640; - m_dim.y = 480; - m_lastDim = m_dim; m_focus = 0.75f; m_baseTime = 0; m_lastTime = 0; @@ -272,6 +269,12 @@ bool Gfx::CEngine::AfterDeviceSetInit() return true; } +void Gfx::CEngine::ResetAfterDeviceChanged() +{ + // TODO +} + + Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams ¶ms) { CImage img; @@ -727,3 +730,14 @@ void Gfx::CEngine::AddStatisticTriangle(int count) { m_statisticTriangle += count; } + +void Gfx::CEngine::SetShowStat(bool show) +{ + m_showStats = show; +} + +bool Gfx::CEngine::GetShowStat() +{ + return m_showStats; +} + diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index 1348cdd..25c5e5d 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -26,6 +26,7 @@ #include "graphics/core/texture.h" #include "graphics/core/vertex.h" #include "math/intpoint.h" +#include "math/intsize.h" #include "math/matrix.h" #include "math/point.h" #include "math/vector.h" @@ -513,21 +514,33 @@ public: CEngine(CInstanceManager *iMan, CApplication *app); ~CEngine(); + //! Returns whether the device was initialized bool GetWasInit(); + //! Returns the last error encountered std::string GetError(); + //! Performs the first initialization, before a device was set bool Create(); + //! Frees all resources before exit void Destroy(); + //! Sets the device to be used void SetDevice(Gfx::CDevice *device); + //! Returns the current device Gfx::CDevice* GetDevice(); + //! Performs initialization after a device was created and set bool AfterDeviceSetInit(); + //! Resets some states and flushes textures after device was changed (e.g. resoulution changed) + void ResetAfterDeviceChanged(); + void SetTerrain(Gfx::CTerrain* terrain); + //! Processes incoming event bool ProcessEvent(const Event &event); + //! Renders a single frame bool Render(); @@ -859,6 +872,9 @@ protected: bool m_wasInit; std::string m_error; + //! Whether to show stats (FPS, etc) + bool m_showStats; + int m_blackSrcBlend[2]; int m_blackDestBlend[2]; int m_whiteSrcBlend[2]; @@ -887,8 +903,9 @@ protected: bool m_render; bool m_movieLock; - Math::IntPoint m_dim; - Math::IntPoint m_lastDim; + //! Current size of window + Math::IntSize m_size; + Math::IntSize m_lastSize; std::vector m_objectTree; std::vector m_objects; diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index bfe7fd7..1fd6a18 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -61,8 +61,9 @@ void Gfx::GLDeviceConfig::LoadDefault() -Gfx::CGLDevice::CGLDevice() +Gfx::CGLDevice::CGLDevice(const Gfx::GLDeviceConfig &config) { + m_config = config; m_wasInit = false; m_lighting = false; m_texturing = false; @@ -86,18 +87,24 @@ std::string Gfx::CGLDevice::GetError() bool Gfx::CGLDevice::Create() { #if defined(USE_GLEW) - if (glewInit() != GLEW_OK) - { - m_error = "GLEW initialization failed"; - return false; - } + static bool glewInited = false; - if ( (! GLEW_ARB_multitexture) || (! GLEW_EXT_texture_env_combine) || (! GLEW_EXT_secondary_color) ) + if (!glewInited) { - m_error = "GLEW reports required extensions not supported"; - return false; - } + glewInited = true; + + if (glewInit() != GLEW_OK) + { + m_error = "GLEW initialization failed"; + return false; + } + if ( (! GLEW_ARB_multitexture) || (! GLEW_EXT_texture_env_combine) || (! GLEW_EXT_secondary_color) ) + { + m_error = "GLEW reports required extensions not supported"; + return false; + } + } #endif /* NOTE: when not using GLEW, extension testing is not performed, as it is assumed that @@ -123,6 +130,8 @@ bool Gfx::CGLDevice::Create() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + glViewport(0, 0, m_config.size.w, m_config.size.h); + m_lights = std::vector(GL_MAX_LIGHTS, Gfx::Light()); m_lightsEnabled = std::vector (GL_MAX_LIGHTS, false); @@ -153,6 +162,17 @@ void Gfx::CGLDevice::Destroy() m_wasInit = false; } +void Gfx::CGLDevice::ConfigChanged(const Gfx::GLDeviceConfig& newConfig) +{ + m_config = newConfig; + + // Reset state + m_lighting = false; + m_texturing = false; + Destroy(); + Create(); +} + void Gfx::CGLDevice::BeginScene() { Clear(); @@ -375,8 +395,8 @@ Gfx::Texture Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCrea } result.valid = true; - result.width = data->surface->w; - result.height = data->surface->h; + result.size.w = data->surface->w; + result.size.h = data->surface->h; // Use & enable 1st texture stage glActiveTexture(GL_TEXTURE0); diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 313ea02..1864000 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -70,7 +70,7 @@ struct GLDevicePrivate; class CGLDevice : public Gfx::CDevice { public: - CGLDevice(); + CGLDevice(const Gfx::GLDeviceConfig &config); virtual ~CGLDevice(); virtual bool GetWasInit(); @@ -79,6 +79,8 @@ public: virtual bool Create(); virtual void Destroy(); + void ConfigChanged(const Gfx::GLDeviceConfig &newConfig); + virtual void BeginScene(); virtual void EndScene(); @@ -159,6 +161,8 @@ private: void UpdateLightPosition(int index); private: + //! Current config + Gfx::GLDeviceConfig m_config; //! Was initialized? bool m_wasInit; //! Last encountered error diff --git a/src/math/intsize.h b/src/math/intsize.h new file mode 100644 index 0000000..f4b2431 --- /dev/null +++ b/src/math/intsize.h @@ -0,0 +1,61 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/** @defgroup MathIntSizeModule math/intsize.h + Contains the IntSize struct. + */ + +#pragma once + +// Math module namespace +namespace Math +{ + +/* @{ */ // start of group + +/** \struct IntSize math/size.h + \brief 2D size with integer dimensions */ +struct IntSize +{ + //! Width + int w; + //! Height + int h; + + //! Constructs a zero size: (0,0) + inline IntSize() + { + LoadZero(); + } + + //! Constructs a size from given dimensions: (w,h) + inline explicit IntSize(int w, int h) + { + this->w = w; + this->h = h; + } + + //! Sets the zero size: (0,0) + inline void LoadZero() + { + w = h = 0; + } +}; // struct Size + + +/* @} */ // end of group + +}; // namespace Math diff --git a/src/math/size.h b/src/math/size.h new file mode 100644 index 0000000..781b9a4 --- /dev/null +++ b/src/math/size.h @@ -0,0 +1,66 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2012, Polish Portal of Colobot (PPC) +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/** @defgroup MathSizeModule math/size.h + Contains the Size struct. + */ + +#pragma once + +// Math module namespace +namespace Math +{ + +/* @{ */ // start of group + +/** \struct Size math/size.h + \brief 2D size + + Represents a 2D size (w, h). + Is separate from Math::Point to avoid confusion. + + */ +struct Size +{ + //! Width + float w; + //! Height + float h; + + //! Constructs a zero size: (0,0) + inline Size() + { + LoadZero(); + } + + //! Constructs a size from given dimensions: (w,h) + inline explicit Size(float w, float h) + { + this->w = w; + this->h = h; + } + + //! Sets the zero size: (0,0) + inline void LoadZero() + { + w = h = 0.0f; + } +}; // struct Size + + +/* @} */ // end of group + +}; // namespace Math -- cgit v1.2.3-1-g7c22 From 220ff9fe52fb2940f71592f121e6238203eae902 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 29 Jul 2012 18:42:56 +0200 Subject: CCamera rewrite Rewritten old code; doesn't compile yet because changes in other modules are needed. --- src/CMakeLists.txt | 2 +- src/graphics/engine/camera.cpp | 1643 +++++++++++++++++++++++++++++++++++++++- src/graphics/engine/camera.h | 348 ++++++--- 3 files changed, 1875 insertions(+), 118 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3896e40..9bcd288 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -67,7 +67,7 @@ common/iman.cpp # common/restext.cpp common/stringutils.cpp graphics/core/color.cpp -graphics/engine/camera.cpp +# graphics/engine/camera.cpp # new code but depends on other modules graphics/engine/cloud.cpp graphics/engine/engine.cpp graphics/engine/lightman.cpp diff --git a/src/graphics/engine/camera.cpp b/src/graphics/engine/camera.cpp index 04bf868..c7ca503 100644 --- a/src/graphics/engine/camera.cpp +++ b/src/graphics/engine/camera.cpp @@ -19,5 +19,1646 @@ #include "graphics/engine/camera.h" +#include "common/iman.h" +#include "graphics/engine/engine.h" +#include "graphics/engine/terrain.h" +#include "graphics/engine/water.h" +#include "math/const.h" +#include "math/geometry.h" +#include "object/object.h" +#include "physics/physics.h" -// TODO implementation + +//! Changes the level of transparency of an object and objects transported (battery & cargo) +void SetTransparency(CObject* obj, float value) +{ + obj->SetTransparency(value); + + CObject *fret = obj->GetFret(); + if (fret != NULL) + fret->SetTransparency(value); + + fret = obj->GetPower(); + if (fret != NULL) + fret->SetTransparency(value); +} + + + +Gfx::CCamera::CCamera(CInstanceManager* iMan) +{ + m_iMan = iMan; + m_iMan->AddInstance(CLASS_CAMERA, this); + + m_engine = static_cast ( m_iMan->SearchInstance(CLASS_ENGINE) ); + m_terrain = static_cast( m_iMan->SearchInstance(CLASS_TERRAIN) ); + m_water = static_cast ( m_iMan->SearchInstance(CLASS_WATER) ); + + m_type = Gfx::CAM_TYPE_FREE; + m_smooth = Gfx::CAM_SMOOTH_NORM; + m_cameraObj = 0; + + m_eyeDistance = 10.0f; + m_initDelay = 0.0f; + + m_actualEye = Math::Vector(0.0f, 0.0f, 0.0f); + m_actualLookat = Math::Vector(0.0f, 0.0f, 0.0f); + m_finalEye = Math::Vector(0.0f, 0.0f, 0.0f); + m_finalLookat = Math::Vector(0.0f, 0.0f, 0.0f); + m_normEye = Math::Vector(0.0f, 0.0f, 0.0f); + m_normLookat = Math::Vector(0.0f, 0.0f, 0.0f); + m_focus = 1.0f; + + m_rightDown = false; + m_rightPosInit = Math::Point(0.5f, 0.5f); + m_rightPosCenter = Math::Point(0.5f, 0.5f); + m_rightPosMove = Math::Point(0.5f, 0.5f); + + m_eyePt = Math::Vector(0.0f, 0.0f, 0.0f); + m_directionH = 0.0f; + m_directionV = 0.0f; + m_heightEye = 20.0f; + m_heightLookat = 0.0f; + m_speed = 2.0f; + + m_backDist = 0.0f; + m_backMin = 0.0f; + m_addDirectionH = 0.0f; + m_addDirectionV = 0.0f; + m_transparency = false; + + m_fixDist = 0.0f; + m_fixDirectionH = 0.0f; + m_fixDirectionV = 0.0f; + + m_visitGoal = Math::Vector(0.0f, 0.0f, 0.0f); + m_visitDist = 0.0f; + m_visitTime = 0.0f; + m_visitType = Gfx::CAM_TYPE_NULL; + m_visitDirectionH = 0.0f; + m_visitDirectionV = 0.0f; + + m_editHeight = 40.0f; + + m_remotePan = 0.0f; + m_remoteZoom = 0.0f; + + m_mouseDirH = 0.0f; + m_mouseDirV = 0.0f; + m_mouseMarging = 0.01f; + + m_motorTurn = 0.0f; + + m_centeringPhase = Gfx::CAM_PHASE_NULL; + m_centeringAngleH = 0.0f; + m_centeringAngleV = 0.0f; + m_centeringDist = 0.0f; + m_centeringCurrentH = 0.0f; + m_centeringCurrentV = 0.0f; + m_centeringTime = 0.0f; + m_centeringProgress = 0.0f; + + m_effectType = Gfx::CAM_EFFECT_NULL; + m_effectPos = Math::Vector(0.0f, 0.0f, 0.0f); + m_effectForce = 0.0f; + m_effectProgress = 0.0f; + m_effectOffset = Math::Vector(0.0f, 0.0f, 0.0f); + + m_scriptEye = Math::Vector(0.0f, 0.0f, 0.0f); + m_scriptLookat = Math::Vector(0.0f, 0.0f, 0.0f); + + m_effect = true; + m_cameraScroll = true; + m_cameraInvertX = false; + m_cameraInvertY = false; +} + +Gfx::CCamera::~CCamera() +{ +} + +void Gfx::CCamera::SetEffect(bool enable) +{ + m_effect = enable; +} + +void Gfx::CCamera::SetCameraScroll(bool scroll) +{ + m_cameraScroll = scroll; +} + +void Gfx::CCamera::SetCameraInvertX(bool invert) +{ + m_cameraInvertX = invert; +} + +void Gfx::CCamera::SetCameraInvertY(bool invert) +{ + m_cameraInvertY = invert; +} + +float Gfx::CCamera::GetMotorTurn() +{ + if (m_type == Gfx::CAM_TYPE_BACK) + return m_motorTurn; + return 0.0f; +} + +void Gfx::CCamera::Init(Math::Vector eye, Math::Vector lookat, float delay) +{ + m_initDelay = delay; + + eye.y += m_terrain->GetFloorLevel(eye, true); + lookat.y += m_terrain->GetFloorLevel(lookat, true); + + m_type = Gfx::CAM_TYPE_FREE; + m_eyePt = eye; + + m_directionH = Math::RotateAngle(eye.x - lookat.x, eye.z - lookat.z) + Math::PI / 2.0f; + m_directionV = -Math::RotateAngle(Math::DistanceProjected(eye, lookat), eye.y - lookat.y); + + m_eyeDistance = 10.0f; + m_heightLookat = 10.0f; + m_backDist = 30.0f; + m_backMin = 10.0f; + m_addDirectionH = 0.0f; + m_addDirectionV = -Math::PI*0.05f; + m_fixDist = 50.0f; + m_fixDirectionH = Math::PI*0.25f; + m_fixDirectionV = -Math::PI*0.10f; + m_centeringPhase = Gfx::CAM_PHASE_NULL; + m_actualEye = m_eyePt; + m_actualLookat = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, 50.0f); + m_finalEye = m_actualEye; + m_finalLookat = m_actualLookat; + m_scriptEye = m_actualEye; + m_scriptLookat = m_actualLookat; + m_focus = 1.00f; + m_remotePan = 0.0f; + m_remoteZoom = 0.0f; + + FlushEffect(); + FlushOver(); + SetType(Gfx::CAM_TYPE_FREE); +} + + +void Gfx::CCamera::SetObject(CObject* object) +{ + m_cameraObj = object; +} + +CObject* Gfx::CCamera::GetObject() +{ + return m_cameraObj; +} + +void Gfx::CCamera::SetType(CameraType type) +{ + m_remotePan = 0.0f; + m_remoteZoom = 0.0f; + + if ( (m_type == Gfx::CAM_TYPE_BACK) && m_transparency ) + { + for (int i = 0; i < 1000000; i++) + { + CObject* obj = static_cast( m_iMan->SearchInstance(CLASS_OBJECT, i) ); + if (obj == NULL) + break; + + if (obj->GetTruck()) + continue; // battery or cargo? + + SetTransparency(obj, 0.0f); // opaque object + } + } + m_transparency = false; + + if (type == Gfx::CAM_TYPE_INFO || + type == Gfx::CAM_TYPE_VISIT) // xx -> info ? + { + m_normEye = m_engine->GetEyePt(); + m_normLookat = m_engine->GetLookatPt(); + + m_engine->SetFocus(1.00f); // normal + m_type = type; + return; + } + + if (m_type == Gfx::CAM_TYPE_INFO || + m_type == Gfx::CAM_TYPE_VISIT) // info -> xx ? + { + m_engine->SetFocus(m_focus); // gives initial focus + m_type = type; + + Math::Vector upVec = Math::Vector(0.0f, 1.0f, 0.0f); + SetViewParams(m_normEye, m_normLookat, upVec); + return; + } + + if ( m_type == Gfx::CAM_TYPE_BACK && type == Gfx::CAM_TYPE_FREE ) // back -> free ? + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, -50.0f); + + if ( m_type == Gfx::CAM_TYPE_BACK && type == Gfx::CAM_TYPE_EDIT ) // back -> edit ? + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, -1.0f); + + if ( m_type == Gfx::CAM_TYPE_ONBOARD && type == Gfx::CAM_TYPE_FREE ) // onboard -> free ? + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, -30.0f); + + if ( m_type == Gfx::CAM_TYPE_ONBOARD && type == Gfx::CAM_TYPE_EDIT ) // onboard -> edit ? + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, -30.0f); + + if ( m_type == Gfx::CAM_TYPE_ONBOARD && type == Gfx::CAM_TYPE_EXPLO ) // onboard -> explo ? + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, -50.0f); + + if ( m_type == Gfx::CAM_TYPE_BACK && type == Gfx::CAM_TYPE_EXPLO ) // back -> explo ? + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, -20.0f); + + if ( type == Gfx::CAM_TYPE_FIX || + type == Gfx::CAM_TYPE_PLANE ) + AbortCentering(); // Special stops framing + + m_fixDist = 50.0f; + if ( type == Gfx::CAM_TYPE_PLANE ) + m_fixDist = 60.0f; + + if ( type == Gfx::CAM_TYPE_BACK ) + { + AbortCentering(); // Special stops framing + m_addDirectionH = 0.0f; + m_addDirectionV = -Math::PI*0.05f; + + ObjectType oType; + if ( m_cameraObj == 0 ) oType = OBJECT_NULL; + else oType = m_cameraObj->GetType(); + + m_backDist = 30.0f; + if ( oType == OBJECT_BASE ) m_backDist = 200.0f; + if ( oType == OBJECT_HUMAN ) m_backDist = 20.0f; + if ( oType == OBJECT_TECH ) m_backDist = 20.0f; + if ( oType == OBJECT_FACTORY ) m_backDist = 50.0f; + if ( oType == OBJECT_RESEARCH ) m_backDist = 40.0f; + if ( oType == OBJECT_DERRICK ) m_backDist = 40.0f; + if ( oType == OBJECT_REPAIR ) m_backDist = 35.0f; + if ( oType == OBJECT_DESTROYER) m_backDist = 35.0f; + if ( oType == OBJECT_TOWER ) m_backDist = 45.0f; + if ( oType == OBJECT_NUCLEAR ) m_backDist = 70.0f; + if ( oType == OBJECT_PARA ) m_backDist = 180.0f; + if ( oType == OBJECT_SAFE ) m_backDist = 50.0f; + if ( oType == OBJECT_HUSTON ) m_backDist = 120.0f; + + m_backMin = m_backDist/3.0f; + if ( oType == OBJECT_HUMAN ) m_backMin = 10.0f; + if ( oType == OBJECT_TECH ) m_backMin = 10.0f; + if ( oType == OBJECT_FACTORY ) m_backMin = 30.0f; + if ( oType == OBJECT_RESEARCH ) m_backMin = 20.0f; + if ( oType == OBJECT_NUCLEAR ) m_backMin = 32.0f; + if ( oType == OBJECT_PARA ) m_backMin = 40.0f; + if ( oType == OBJECT_SAFE ) m_backMin = 25.0f; + if ( oType == OBJECT_HUSTON ) m_backMin = 80.0f; + } + + if ( type != Gfx::CAM_TYPE_ONBOARD && m_cameraObj != 0 ) + m_cameraObj->SetGunGoalH(0.0f); // puts the cannon right + + if ( type == Gfx::CAM_TYPE_ONBOARD ) + m_focus = 1.50f; // Wide + else + m_focus = 1.00f; // normal + m_engine->SetFocus(m_focus); + + m_type = type; + + SetSmooth(Gfx::CAM_SMOOTH_NORM); +} + +CameraType Gfx::CCamera::GetType() +{ + return m_type; +} + +void Gfx::CCamera::SetSmooth(CameraSmooth type) +{ + m_smooth = type; +} + +CameraSmooth Gfx::CCamera::GetSmoth() +{ + return m_smooth; +} + +void Gfx::CCamera::SetDist(float dist) +{ + m_fixDist = dist; +} + +float Gfx::CCamera::GetDist() +{ + return m_fixDist; +} + +void Gfx::CCamera::SetFixDirection(float angle) +{ + m_fixDirectionH = angle; +} + +float Gfx::CCamera::GetFixDirection() +{ + return m_fixDirectionH; +} + +void Gfx::CCamera::SetRemotePan(float value) +{ + m_remotePan = value; +} + +float Gfx::CCamera::GetRemotePan() +{ + return m_remotePan; +} + +void Gfx::CCamera::SetRemoteZoom(float value) +{ + value = Math::Norm(value); + + if ( m_type == Gfx::CAM_TYPE_BACK ) + m_backDist = m_backMin + (200.0f - m_backMin) * value; + + if ( m_type == Gfx::CAM_TYPE_FIX || + m_type == Gfx::CAM_TYPE_PLANE ) + m_fixDist = 10.0f + (200.0f - 10.0f) * value; +} + +float Gfx::CCamera::GetRemoteZoom() +{ + if ( m_type == Gfx::CAM_TYPE_BACK ) + return (m_backDist - m_backMin) / (200.0f - m_backMin); + + if ( m_type == Gfx::CAM_TYPE_FIX || + m_type == Gfx::CAM_TYPE_PLANE ) + return (m_fixDist - 10.0f) / (200.0f - 10.0f); + + return 0.0f; +} + +void Gfx::CCamera::StartVisit(Math::Vector goal, float dist) +{ + m_visitType = m_type; + SetType(Gfx::CAM_TYPE_VISIT); + m_visitGoal = goal; + m_visitDist = dist; + m_visitTime = 0.0f; + m_visitDirectionH = 0.0f; + m_visitDirectionV = -Math::PI*0.10f; +} + +void Gfx::CCamera::StopVisit() +{ + SetType(m_visitType); // presents the initial type +} + +void Gfx::CCamera::GetCamera(Math::Vector &eye, Math::Vector &lookat) +{ + eye = m_eyePt; + lookat = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, 50.0f); +} + +bool Gfx::CCamera::StartCentering(CObject *object, float angleH, float angleV, + float dist, float time) +{ + if (m_type != Gfx::CAM_TYPE_BACK) + return false; + if (object != m_cameraObj) + return false; + + if (m_centeringPhase != Gfx::CAM_PHASE_NULL) + return false; + + if (m_addDirectionH > Math::PI) + angleH = Math::PI * 2.0f - angleH; + + m_centeringPhase = Gfx::CAM_PHASE_START; + m_centeringAngleH = angleH; + m_centeringAngleV = angleV; + m_centeringDist = dist; + m_centeringCurrentH = 0.0f; + m_centeringCurrentV = 0.0f; + m_centeringTime = time; + m_centeringProgress = 0.0f; + + return true; +} + +bool Gfx::CCamera::StopCentering(CObject *object, float time) +{ + if (m_type != Gfx::CAM_TYPE_BACK) + return false; + if (object != m_cameraObj) + return false; + + if (m_centeringPhase != Gfx::CAM_PHASE_START && + m_centeringPhase != Gfx::CAM_PHASE_WAIT) + return false; + + m_centeringPhase = Gfx::CAM_PHASE_STOP; + + if (m_centeringAngleH != 99.9f) + m_centeringAngleH = m_centeringCurrentH; + + if (m_centeringAngleV != 99.9f) + m_centeringAngleV = m_centeringCurrentV; + + m_centeringTime = time; + m_centeringProgress = 0.0f; + + return true; +} + +void Gfx::CCamera::AbortCentering() +{ + if (m_type == Gfx::CAM_TYPE_INFO || + m_type == Gfx::CAM_TYPE_VISIT ) + return; + + if (m_centeringPhase == Gfx::CAM_PHASE_NULL) + return; + + m_centeringPhase = Gfx::CAM_PHASE_NULL; + + if ( m_centeringAngleH != 99.9f ) + m_addDirectionH = m_centeringCurrentH; + + if (m_centeringAngleV != 99.9f) + m_addDirectionV = m_centeringCurrentV; +} + +void Gfx::CCamera::FlushEffect() +{ + m_effectType = Gfx::CAM_EFFECT_NULL; + m_effectForce = 0.0f; + m_effectProgress = 0.0f; + m_effectOffset = Math::Vector(0.0f, 0.0f, 0.0f); +} + +void Gfx::CCamera::StartEffect(CameraEffect effect, Math::Vector pos, float force) +{ + if ( !m_effect ) return; + + m_effectType = effect; + m_effectPos = pos; + m_effectForce = force; + m_effectProgress = 0.0f; +} + +void Gfx::CCamera::EffectFrame(const Event &event) +{ + if (m_type == Gfx::CAM_TYPE_INFO || + m_type == Gfx::CAM_TYPE_VISIT) + return; + + if (m_effectType == Gfx::CAM_EFFECT_NULL) + return; + + m_effectOffset = Math::Vector(0.0f, 0.0f, 0.0f); + + float force = m_effectForce; + + if ( m_effectType == Gfx::CAM_EFFECT_TERRAFORM ) + { + m_effectProgress += event.rTime * 0.7f; + m_effectOffset.x = (Math::Rand() - 0.5f) * 10.0f; + m_effectOffset.y = (Math::Rand() - 0.5f) * 10.0f; + m_effectOffset.z = (Math::Rand() - 0.5f) * 10.0f; + + force *= 1.0f-m_effectProgress; + } + + if ( m_effectType == Gfx::CAM_EFFECT_EXPLO ) + { + m_effectProgress += event.rTime * 1.0f; + m_effectOffset.x = (Math::Rand() - 0.5f) *5.0f; + m_effectOffset.y = (Math::Rand() - 0.5f) * 5.0f; + m_effectOffset.z = (Math::Rand() - 0.5f) * 5.0f; + + force *= 1.0f-m_effectProgress; + } + + if ( m_effectType == Gfx::CAM_EFFECT_SHOT ) + { + m_effectProgress += event.rTime * 1.0f; + m_effectOffset.x = (Math::Rand() - 0.5f) * 2.0f; + m_effectOffset.y = (Math::Rand() - 0.5f) * 2.0f; + m_effectOffset.z = (Math::Rand() - 0.5f) * 2.0f; + + force *= 1.0f-m_effectProgress; + } + + if ( m_effectType == Gfx::CAM_EFFECT_CRASH ) + { + m_effectProgress += event.rTime * 5.0f; + m_effectOffset.y = sinf(m_effectProgress * Math::PI) * 1.5f; + m_effectOffset.x = (Math::Rand() - 0.5f) * 1.0f * (1.0f - m_effectProgress); + m_effectOffset.z = (Math::Rand() - 0.5f) * 1.0f * (1.0f - m_effectProgress); + } + + if ( m_effectType == Gfx::CAM_EFFECT_VIBRATION ) + { + m_effectProgress += event.rTime * 0.1f; + m_effectOffset.y = (Math::Rand() - 0.5f) * 1.0f * (1.0f - m_effectProgress); + m_effectOffset.x = (Math::Rand() - 0.5f) * 1.0f * (1.0f - m_effectProgress); + m_effectOffset.z = (Math::Rand() - 0.5f) * 1.0f * (1.0f - m_effectProgress); + } + + if ( m_effectType == Gfx::CAM_EFFECT_PET ) + { + m_effectProgress += event.rTime *5.0f; + m_effectOffset.x = (Math::Rand() - 0.5f) * 0.2f; + m_effectOffset.y = (Math::Rand() - 0.5f) * 2.0f; + m_effectOffset.z = (Math::Rand() - 0.5f) * 0.2f; + } + + float dist = Math::Distance(m_eyePt, m_effectPos); + dist = Math::Norm((dist - 100.f) / 100.0f); + + force *= 1.0f-dist; +#if _TEEN + force *= 2.0f; +#endif + m_effectOffset *= force; + + if (m_effectProgress >= 1.0f) + FlushEffect(); +} + +void Gfx::CCamera::FlushOver() +{ + m_overType = Gfx::CAM_OVER_EFFECT_NULL; + m_overColorBase = Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f); // black + m_engine->SetOverColor(); // nothing +} + +void Gfx::CCamera::SetOverBaseColor(Gfx::Color color) +{ + m_overColorBase = color; +} + +void Gfx::CCamera::StartOver(Gfx::CameraOverEffect effect, Math::Vector pos, float force) +{ + m_overType = effect; + m_overTime = 0.0f; + + float decay; + if (m_overType == Gfx::CAM_OVER_EFFECT_LIGHTNING) + decay = 400.0f; + else + decay = 100.0f; + + float dist = Math::Distance(m_eyePt, pos); + dist = (dist - decay) / decay; + if (dist < 0.0f) dist = 0.0f; + if (dist > 1.0f) dist = 1.0f; + + m_overForce = force * (1.0f - dist); + + if (m_overType == Gfx::CAM_OVER_EFFECT_BLOOD) + { + m_overColor = Gfx::Color(0.8f, 0.1f, 0.1f); // red + m_overMode = Gfx::ENG_RSTATE_TCOLOR_BLACK; + + m_overFadeIn = 0.4f; + m_overFadeOut = 0.8f; + m_overForce = 1.0f; + } + + if ( m_overType == Gfx::CAM_OVER_EFFECT_FADEIN_WHITE ) + { + m_overColor = Gfx::Color(1.0f, 1.0f, 1.0f); // white + m_overMode = Gfx::ENG_RSTATE_TCOLOR_BLACK; + + m_overFadeIn = 0.0f; + m_overFadeOut = 20.0f; + m_overForce = 1.0f; + } + + if ( m_overType == Gfx::CAM_OVER_EFFECT_FADEOUT_WHITE ) + { + m_overColor = Gfx::Color(1.0f, 1.0f, 1.0f); // white + m_overMode = Gfx::ENG_RSTATE_TCOLOR_BLACK; + + m_overFadeIn = 6.0f; + m_overFadeOut = 100000.0f; + m_overForce = 1.0f; + } + + if ( m_overType == Gfx::CAM_OVER_EFFECT_FADEOUT_BLACK ) + { + m_overColor = m_engine->GetFogColor(1); // fog color underwater + m_overMode = Gfx::ENG_RSTATE_TTEXTURE_WHITE; + + m_overFadeIn = 4.0f; + m_overFadeOut = 100000.0f; + m_overForce = 1.0f; + } + + if ( m_overType == Gfx::CAM_OVER_EFFECT_LIGHTNING ) + { + m_overColor = Gfx::Color(0.9f, 1.0f, 1.0f); // white-cyan + m_overMode = Gfx::ENG_RSTATE_TCOLOR_BLACK; + + m_overFadeIn = 0.0f; + m_overFadeOut = 1.0f; + } +} + +void Gfx::CCamera::OverFrame(const Event &event) +{ + if (m_type == Gfx::CAM_TYPE_INFO || + m_type == Gfx::CAM_TYPE_VISIT) + return; + + if (m_overType == Gfx::CAM_OVER_EFFECT_NULL) + return; + + m_overTime += event.rTime; + + if (m_overType == Gfx::CAM_OVER_EFFECT_LIGHTNING) + { + Gfx::Color color; + if (rand() % 2 == 0) + { + color.r = m_overColor.r * m_overForce; + color.g = m_overColor.g * m_overForce; + color.b = m_overColor.b * m_overForce; + } + else + { + color = Gfx::Color(0.0f. 0.0f, 0.0f); + } + color.a = 0.0f; + m_engine->SetOverColor(color, m_overMode); + } + else + { + if ( (m_overFadeIn > 0.0f) && (m_overTime < m_overFadeIn) ) + { + float intensity = m_overTime / m_overFadeIn; + intensity *= m_overForce; + + Gfx::Color color; + if (m_overMode == Gfx::ENG_RSTATE_TCOLOR_WHITE) + { + color.r = 1.0f - (1.0f - m_overColor.r) * intensity; + color.g = 1.0f - (1.0f - m_overColor.g) * intensity; + color.b = 1.0f - (1.0f - m_overColor.b) * intensity; + } + else + { + color.r = m_overColor.r * intensity; + color.g = m_overColor.g * intensity; + color.b = m_overColor.b * intensity; + + color.r = 1.0f - (1.0f - color.r) * (1.0f - m_overColorBase.r); + color.g = 1.0f - (1.0f - color.g) * (1.0f - m_overColorBase.g); + color.b = 1.0f - (1.0f - color.b) * (1.0f - m_overColorBase.b); + } + color.a = 0.0f; + m_engine->SetOverColor(color, m_overMode); + } + else if ( (m_overFadeOut > 0.0f) && (m_overTime - m_overFadeIn < m_overFadeOut) ) + { + float intensity = 1.0f - (m_overTime - m_overFadeIn) / m_overFadeOut; + intensity *= m_overForce; + + Gfx::Color color; + if (m_overMode == Gfx::ENG_RSTATE_TCOLOR_WHITE) + { + color.r = 1.0f-(1.0f-m_overColor.r) * intensity; + color.g = 1.0f-(1.0f-m_overColor.g) * intensity; + color.b = 1.0f-(1.0f-m_overColor.b) * intensity; + } + else + { + color.r = m_overColor.r * intensity; + color.g = m_overColor.g * intensity; + color.b = m_overColor.b * intensity; + + color.r = 1.0f - (1.0f - color.r)*(1.0f - m_overColorBase.r); + color.g = 1.0f - (1.0f - color.g)*(1.0f - m_overColorBase.g); + color.b = 1.0f - (1.0f - color.b)*(1.0f - m_overColorBase.b); + } + color.a = 0.0f; + m_engine->SetOverColor(color, m_overMode); + } + } + + if ( m_overTime >= m_overFadeIn+m_overFadeOut ) + { + FlushOver(); + return; + } +} + +void Gfx::CCamera::FixCamera() +{ + m_initDelay = 0.0f; + m_actualEye = m_finalEye = m_scriptEye; + m_actualLookat = m_finalLookat = m_scriptLookat; + SetViewTime(m_scriptEye, m_scriptLookat, 0.0f); +} + +void Gfx::CCamera::SetViewTime(const Math::Vector &eyePt, + const Math::Vector &lookatPt, + float rTime) +{ + Math::Vector eye, lookat; + + if (m_type == Gfx::CAM_TYPE_INFO) + { + eye = eyePt; + lookat = lookatPt; + } + else + { + if (m_initDelay > 0.0f) + { + m_initDelay -= rTime; + if (m_initDelay < 0.0f) + m_initDelay = 0.0f; + rTime /= 1.0f+m_initDelay; + } + + eye = eyePt; + lookat = lookatPt; + if ( !IsCollision(eye, lookat) ) + { + m_finalEye = eye; + m_finalLookat = lookat; + } + + float prog = 0.0f; + float dist = Math::Distance(m_finalEye, m_actualEye); + + if (m_smooth == Gfx::CAM_SMOOTH_NONE) prog = dist; + if (m_smooth == Gfx::CAM_SMOOTH_NORM) prog = powf(dist, 1.5f) * rTime * 0.5f; + if (m_smooth == Gfx::CAM_SMOOTH_HARD) prog = powf(dist, 1.0f) * rTime * 4.0f; + if (m_smooth == Gfx::CAM_SMOOTH_SPEC) prog = powf(dist, 1.0f) * rTime * 0.05f; + if (dist == 0.0f) + { + m_actualEye = m_finalEye; + } + else + { + if (prog > dist) + prog = dist; + m_actualEye = (m_finalEye - m_actualEye) / dist * prog + m_actualEye; + } + + dist = Math::Distance(m_finalLookat, m_actualLookat); + if ( m_smooth == Gfx::CAM_SMOOTH_NONE ) prog = dist; + if ( m_smooth == Gfx::CAM_SMOOTH_NORM ) prog = powf(dist, 1.5f) * rTime * 2.0f; + if ( m_smooth == Gfx::CAM_SMOOTH_HARD ) prog = powf(dist, 1.0f) * rTime * 4.0f; + if ( m_smooth == Gfx::CAM_SMOOTH_SPEC ) prog = powf(dist, 1.0f) * rTime * 4.0f; + if ( dist == 0.0f ) + { + m_actualLookat = m_finalLookat; + } + else + { + if (prog > dist) + prog = dist; + m_actualLookat = (m_finalLookat - m_actualLookat) / dist * prog + m_actualLookat; + } + + eye = m_effectOffset+m_actualEye; + m_water->AdjustEye(eye); + + float h = m_terrain->GetFloorLevel(eye); + if (eye.y < h + 4.0f) + eye.y = h + 4.0f; + + lookat = m_effectOffset+m_actualLookat; + } + + Math::Vector upVec = Math::Vector(0.0f, 1.0f, 0.0f); + SetViewParams(eye, lookat, upVec); +} + +bool Gfx::CCamera::IsCollision(Math::Vector &eye, Math::Vector lookat) +{ + if (m_type == Gfx::CAM_TYPE_BACK ) return IsCollisionBack(eye, lookat); + if (m_type == Gfx::CAM_TYPE_FIX ) return IsCollisionFix (eye, lookat); + if (m_type == Gfx::CAM_TYPE_PLANE) return IsCollisionFix (eye, lookat); + return false; +} + +bool Gfx::CCamera::IsCollisionBack(Math::Vector &eye, Math::Vector lookat) +{ + ObjectType iType; + if (m_cameraObj == NULL) + iType = OBJECT_NULL; + else + iType = m_cameraObj->GetType(); + + Math::Vector min; + min.x = Math::Min(m_actualEye.x, m_actualLookat.x); + min.y = Math::Min(m_actualEye.y, m_actualLookat.y); + min.z = Math::Min(m_actualEye.z, m_actualLookat.z); + + Math::Vector max; + max.x = Math::Max(m_actualEye.x, m_actualLookat.x); + max.y = Math::Max(m_actualEye.y, m_actualLookat.y); + max.z = Math::Max(m_actualEye.z, m_actualLookat.z); + + m_transparency = false; + + for (int i = 0 ;i < 1000000; i++) + { + CObject *obj = (CObject*)m_iMan->SearchInstance(CLASS_OBJECT, i); + if (obj == NULL) break; + + if (obj->GetTruck()) continue; // battery or cargo? + + SetTransparency(obj, 0.0f); // opaque object + + if (obj == m_cameraObj) continue; + + if ( iType == OBJECT_BASE || // building? + iType == OBJECT_DERRICK || + iType == OBJECT_FACTORY || + iType == OBJECT_STATION || + iType == OBJECT_CONVERT || + iType == OBJECT_REPAIR || + iType == OBJECT_DESTROYER|| + iType == OBJECT_TOWER || + iType == OBJECT_RESEARCH || + iType == OBJECT_RADAR || + iType == OBJECT_ENERGY || + iType == OBJECT_LABO || + iType == OBJECT_NUCLEAR || + iType == OBJECT_PARA || + iType == OBJECT_SAFE || + iType == OBJECT_HUSTON ) continue; + + ObjType oType = obj->GetType(); + if ( oType == OBJECT_HUMAN || + oType == OBJECT_TECH || + oType == OBJECT_TOTO || + oType == OBJECT_FIX || + oType == OBJECT_FRET || + oType == OBJECT_ANT || + oType == OBJECT_SPIDER || + oType == OBJECT_BEE || + oType == OBJECT_WORM ) continue; + + Math::Vector oPos; + float oRadius = 0.0f; + obj->GetGlobalSphere(oPos, oRadius); + if ( oRadius <= 2.0f ) continue; // ignores small objects + + if ( oPos.x+oRadius < min.x || + oPos.y+oRadius < min.y || + oPos.z+oRadius < min.z || + oPos.x-oRadius > max.x || + oPos.y-oRadius > max.y || + oPos.z-oRadius > max.z ) continue; + + Math::Vector proj = Projection(m_actualEye, m_actualLookat, oPos); + float dpp = Math::Distance(proj, oPos); + if ( dpp > oRadius ) continue; + + if ( oType == OBJECT_FACTORY ) + { + float angle = Math::RotateAngle(m_actualEye.x-oPos.x, oPos.z-m_actualEye.z); // CW ! + angle = Math::Direction(angle, obj->GetAngleY(0)); + if ( fabs(angle) < 30.0f*Math::PI/180.0f ) continue; // in the gate? + } + + float del = Math::Distance(m_actualEye, m_actualLookat); + if (oType == OBJECT_FACTORY) + del += oRadius; + + float len = Math::Distance(m_actualEye, proj); + if (len > del) continue; + + SetTransparency(obj, 1.0f); // transparent object + m_transparency = true; + } + return false; +} + +bool Gfx::CCamera::IsCollisionFix(Math::Vector &eye, Math::Vector lookat) +{ + for (int i = 0; i < 1000000; i++) + { + CObject *obj = static_cast( m_iMan->SearchInstance(CLASS_OBJECT, i) ); + if (obj == NULL) break; + + if (obj == m_cameraObj) continue; + + ObjectType type = obj->GetType(); + if ( type == OBJECT_TOTO || + type == OBJECT_FRET || + type == OBJECT_STONE || + type == OBJECT_URANIUM || + type == OBJECT_METAL || + type == OBJECT_POWER || + type == OBJECT_ATOMIC || + type == OBJECT_BULLET || + type == OBJECT_BBOX || + type == OBJECT_KEYa || + type == OBJECT_KEYb || + type == OBJECT_KEYc || + type == OBJECT_KEYd || + type == OBJECT_ANT || + type == OBJECT_SPIDER || + type == OBJECT_BEE || + type == OBJECT_WORM ) continue; + + Math::Vector objPos; + float objRadius = 0.0f; + obj->GetGlobalSphere(objPos, objRadius); + if (objRadius == 0.0f) continue; + + float dist = Math::Distance(eye, objPos); + if (dist < objRadius) + { + dist = Math::Distance(eye, lookat); + Math::Vector proj = Projection(eye, lookat, objPos); + eye = (lookat - eye) * objRadius / dist + proj; + return false; + } + } + return false; +} + +bool Gfx::CCamera::EventProcess(const Event &event) +{ + switch (event.type) + { + // TODO: frame update event + case EVENT_FRAME: + EventFrame(event); + break; + + case EVENT_MOUSE_MOVE: + EventMouseMove(event); + break; + + case EVENT_KEY_DOWN: + // TODO: mouse wheel event + if ( event.param == VK_WHEELUP ) EventMouseWheel(+1); + if ( event.param == VK_WHEELDOWN ) EventMouseWheel(-1); + break; + + default: + break; + } + return true; +} + +bool Gfx::CCamera::EventMouseMove(const Event &event) +{ + m_mousePos = event.pos; + return true; +} + +void Gfx::CCamera::EventMouseWheel(int dir) +{ + if (m_type == Gfx::CAM_TYPE_BACK) + { + if (dir > 0) + { + m_backDist -= 8.0f; + if (m_backDist < m_backMin) + m_backDist = m_backMin; + } + if (dir < 0) + { + m_backDist += 8.0f; + if (m_backDist > 200.0f) + m_backDist = 200.0f; + } + } + + if ( m_type == Gfx::CAM_TYPE_FIX || + m_type == Gfx::CAM_TYPE_PLANE ) + { + if (dir > 0) + { + m_fixDist -= 8.0f; + if (m_fixDist < 10.0f) + m_fixDist = 10.0f; + } + if (dir < 0) + { + m_fixDist += 8.0f; + if (m_fixDist > 200.0f) + m_fixDist = 200.0f; + } + } + + if ( m_type == Gfx::CAM_TYPE_VISIT ) + { + if (dir > 0) + { + m_visitDist -= 8.0f; + if (m_visitDist < 20.0f) + m_visitDist = 20.0f; + } + if (dir < 0) + { + m_visitDist += 8.0f; + if (m_visitDist > 200.0f) + m_visitDist = 200.0f; + } + } +} + +bool Gfx::CCamera::EventFrame(const Event &event) +{ + EffectFrame(event); + OverFrame(event); + + if (m_type == Gfx::CAM_TYPE_FREE) + return EventFrameFree(event); + + if (m_type == Gfx::CAM_TYPE_EDIT) + return EventFrameEdit(event); + + if (m_type == Gfx::CAM_TYPE_DIALOG) + return EventFrameDialog(event); + + if (m_type == Gfx::CAM_TYPE_BACK) + return EventFrameBack(event); + + if (m_type == Gfx::CAM_TYPE_FIX || + m_type == Gfx::CAM_TYPE_PLANE) + return EventFrameFix(event); + + if (m_type == Gfx::CAM_TYPE_EXPLO) + return EventFrameExplo(event); + + if (m_type == Gfx::CAM_TYPE_ONBOARD) + return EventFrameOnBoard(event); + + if (m_type == Gfx::CAM_TYPE_SCRIPT) + return EventFrameScript(event); + + if (m_type == Gfx::CAM_TYPE_INFO) + return EventFrameInfo(event); + + if (m_type == Gfx::CAM_TYPE_VISIT) + return EventFrameVisit(event); + + return true; +} + +Gfx::EngineMouseType Gfx::CCamera::GetMouseDef(Math::Point pos) +{ + Gfx::EngineMouseType type = Gfx::ENG_MOUSE_NORM; + m_mousePos = pos; + + if (m_type == Gfx::CAM_TYPE_INFO) + return type; + + if (m_rightDown) // the right button pressed? + { + m_rightPosMove.x = pos.x - m_rightPosCenter.x; + m_rightPosMove.y = pos.y - m_rightPosCenter.y; + type = Gfx::ENG_MOUSE_MOVE; + } + else + { + if (!m_cameraScroll) + return type; + + m_mouseDirH = 0.0f; + m_mouseDirV = 0.0f; + + if (pos.x < m_mouseMarging) + m_mouseDirH = pos.x / m_mouseMarging - 1.0f; + + if (pos.x > 1.0f - m_mouseMarging) + m_mouseDirH = 1.0f - (1.0f - pos.x) / m_mouseMarging; + + if (pos.y < m_mouseMarging) + m_mouseDirV = pos.y / m_mouseMarging - 1.0f; + + if (pos.y > 1.0f-m_mouseMarging) + m_mouseDirV = 1.0f - (1.0f - pos.y) / m_mouseMarging; + + if ( m_type == Gfx::CAM_TYPE_FREE || + m_type == Gfx::CAM_TYPE_EDIT || + m_type == Gfx::CAM_TYPE_BACK || + m_type == Gfx::CAM_TYPE_FIX || + m_type == Gfx::CAM_TYPE_PLANE || + m_type == Gfx::CAM_TYPE_EXPLO ) + { + if (m_mouseDirH > 0.0f) + type = Gfx::ENG_MOUSE_SCROLLR; + if (m_mouseDirH < 0.0f) + type = Gfx::ENG_MOUSE_SCROLLL; + } + + if ( m_type == Gfx::CAM_TYPE_FREE || + m_type == Gfx::CAM_TYPE_EDIT ) + { + if (m_mouseDirV > 0.0f) + type = Gfx::ENG_MOUSE_SCROLLU; + if (m_mouseDirV < 0.0f) + type = Gfx::ENG_MOUSE_SCROLLD; + } + + if (m_cameraInvertX) + m_mouseDirH = -m_mouseDirH; + } + + return type; +} + +bool Gfx::CCamera::EventFrameFree(const Event &event) +{ + float factor = m_heightEye * 0.5f + 30.0f; + + if ( m_mouseDirH != 0.0f ) + m_directionH -= m_mouseDirH * event.rTime * 0.7f * m_speed; + if ( m_mouseDirV != 0.0f ) + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, m_mouseDirV * event.rTime * factor * m_speed); + + // Up/Down + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, event.axeY * event.rTime * factor * m_speed); + + // Left/Right + if ( event.keyState & KS_CONTROL ) + { + if ( event.axeX < 0.0f ) + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH + Math::PI / 2.0f, m_directionV, -event.axeX * event.rTime * factor * m_speed); + if ( event.axeX > 0.0f ) + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH - Math::PI / 2.0f, m_directionV, event.axeX * event.rTime * factor * m_speed); + } + else + { + m_directionH -= event.axeX * event.rTime * 0.7f * m_speed; + } + + // PageUp/PageDown + if ( event.keyState & KS_NUMMINUS ) + { + if (m_heightEye < 500.0f) + m_heightEye += event.rTime * factor * m_speed; + } + if ( event.keyState & KS_NUMPLUS ) + { + if (m_heightEye > -2.0f) + m_heightEye -= event.rTime * factor * m_speed; + } + + m_terrain->ValidPosition(m_eyePt, 10.0f); + + if (m_terrain->MoveOnFloor(m_eyePt, true)) + { + m_eyePt.y += m_heightEye; + + Math::Vector pos = m_eyePt; + if (m_terrain->MoveOnFloor(pos, true)) + { + pos.y -= 2.0f; + if (m_eyePt.y < pos.y) + m_eyePt.y = pos.y; + } + + } + + Math::Vector lookatPt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, 50.0f); + + if (m_terrain->MoveOnFloor(lookatPt, true)) + lookatPt.y += m_heightLookat; + + SetViewTime(m_eyePt, lookatPt, event.rTime); + + return true; +} + +bool Gfx::CCamera::EventFrameEdit(const Event &event) +{ + float factor = m_editHeight * 0.5f + 30.0f; + + if (m_mouseDirH != 0.0f) + m_directionH -= m_mouseDirH * event.rTime * 0.7f * m_speed; + if (m_mouseDirV != 0.0f) + m_eyePt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, m_mouseDirV * event.rTime * factor * m_speed); + + if (m_cameraScroll) + { + // Left/Right. + m_fixDirectionH += m_mouseDirH * event.rTime * 1.0f * m_speed; + m_fixDirectionH = Math::NormAngle(m_fixDirectionH); + } + + m_terrain->ValidPosition(m_eyePt, 10.0f); + + if (m_terrain->MoveOnFloor(m_eyePt, false)) + { + m_eyePt.y += m_editHeight; + + Math::Vector pos = m_eyePt; + if (m_terrain->MoveOnFloor(pos, false)) + { + pos.y += 2.0f; + if (m_eyePt.y < pos.y) + m_eyePt.y = pos.y; + } + + } + + Math::Vector lookatPt = Math::LookatPoint( m_eyePt, m_directionH, m_directionV, 50.0f ); + + if ( m_terrain->MoveOnFloor(lookatPt, true)) + lookatPt.y += m_heightLookat; + + SetViewTime(m_eyePt, lookatPt, event.rTime); + + return true; +} + +bool Gfx::CCamera::EventFrameDialog(const Event &event) +{ + return true; +} + +bool Gfx::CCamera::EventFrameBack(const Event &event) +{ + ObjectType type; + if (m_cameraObj == NULL) + type = OBJECT_NULL; + else + type = m_cameraObj->GetType(); + + // +/-. + if (event.keyState & KS_NUMPLUS) + { + m_backDist -= event.rTime * 30.0f * m_speed; + if (m_backDist < m_backMin) m_backDist = m_backMin; + } + if (event.keyState & KS_NUMMINUS) + { + m_backDist += event.rTime * 30.0f * m_speed; + if (m_backDist > 200.0f) m_backDist = 200.0f; + } + + m_motorTurn = 0.0f; + + if (m_rightDown) + { + m_addDirectionH = m_rightPosMove.x * 6.0f; + m_addDirectionV = -m_rightPosMove.y * 2.0f; + } + else + { + if (m_cameraScroll) + { + // Left/Right + m_addDirectionH += m_mouseDirH * event.rTime * 1.0f * m_speed; + m_addDirectionH = Math::NormAngle(m_addDirectionH); + } + } + + if ((m_mouseDirH != 0) || (m_mouseDirV != 0)) + AbortCentering(); // special stops framing + + // Increase the special framework + float centeringH = 0.0f; + float centeringV = 0.0f; + float centeringD = 0.0f; + + if (m_centeringPhase == Gfx::CAM_PHASE_START) + { + m_centeringProgress += event.rTime / m_centeringTime; + if (m_centeringProgress > 1.0f) m_centeringProgress = 1.0f; + centeringH = m_centeringProgress; + centeringV = m_centeringProgress; + centeringD = m_centeringProgress; + if (m_centeringProgress >= 1.0f) + m_centeringPhase = Gfx::CAM_PHASE_WAIT; + } + + if (m_centeringPhase == Gfx::CAM_PHASE_WAIT) + { + centeringH = 1.0f; + centeringV = 1.0f; + centeringD = 1.0f; + } + + if (m_centeringPhase == Gfx::CAM_PHASE_STOP) + { + m_centeringProgress += event.rTime / m_centeringTime; + if (m_centeringProgress > 1.0f) m_centeringProgress = 1.0f; + centeringH = 1.0f-m_centeringProgress; + centeringV = 1.0f-m_centeringProgress; + centeringD = 1.0f-m_centeringProgress; + if (m_centeringProgress >= 1.0f) + m_centeringPhase = Gfx::CAM_PHASE_NULL; + } + + if (m_centeringAngleH == 99.9f) centeringH = 0.0f; + if (m_centeringAngleV == 99.9f) centeringV = 0.0f; + if (m_centeringDist == 0.0f) centeringD = 0.0f; + + if (m_cameraObj != NULL) + { + Math::Vector lookatPt = m_cameraObj->GetPosition(0); + if (type == OBJECT_BASE ) lookatPt.y += 40.0f; + else if (type == OBJECT_HUMAN) lookatPt.y += 1.0f; + else if (type == OBJECT_TECH ) lookatPt.y += 1.0f; + else lookatPt.y += 4.0f; + + float h = -m_cameraObj->GetAngleY(0); // angle vehicle / building + + if ( type == OBJECT_DERRICK || + type == OBJECT_FACTORY || + type == OBJECT_REPAIR || + type == OBJECT_DESTROYER|| + type == OBJECT_STATION || + type == OBJECT_CONVERT || + type == OBJECT_TOWER || + type == OBJECT_RESEARCH || + type == OBJECT_RADAR || + type == OBJECT_INFO || + type == OBJECT_ENERGY || + type == OBJECT_LABO || + type == OBJECT_NUCLEAR || + type == OBJECT_PARA || + type == OBJECT_SAFE || + type == OBJECT_HUSTON || + type == OBJECT_START || + type == OBJECT_END ) // building? + { + h += Math::PI * 0.20f; // nearly face + } + else // vehicle? + { + h += Math::PI; // back + } + h = Math::NormAngle(h)+m_remotePan; + float v = 0.0f; //? + + h += m_centeringCurrentH; + h += m_addDirectionH * (1.0f - centeringH); + h = Math::NormAngle(h); + + if (type == OBJECT_MOBILEdr) // designer? + v -= 0.3f; // Camera top + + v += m_centeringCurrentV; + v += m_addDirectionV * (1.0f - centeringV); + + float d = m_backDist; + d += m_centeringDist * centeringD; + + m_centeringCurrentH = m_centeringAngleH * centeringH; + m_centeringCurrentV = m_centeringAngleV * centeringV; + + m_eyePt = RotateView(lookatPt, h, v, d); + + CPhysics* physics = m_cameraObj->GetPhysics(); + if ( (physics != NULL) && physics->GetLand() ) // ground? + { + Math::Vector pos = lookatPt + (lookatPt - m_eyePt); + float floor = m_terrain->GetFloorHeight(pos) - 4.0f; + if (floor > 0.0f) + m_eyePt.y += floor; // shows the descent in front + } + + m_eyePt = ExcludeTerrain(m_eyePt, lookatPt, h, v); + m_eyePt = ExcludeObject(m_eyePt, lookatPt, h, v); + + SetViewTime(m_eyePt, lookatPt, event.rTime); + + m_directionH = h + Math::PI / 2.0f; + m_directionV = v; + } + + return true; +} + +bool Gfx::CCamera::EventFrameFix(const Event &event) +{ + // +/-. + if (event.keyState & KS_NUMPLUS) + { + m_fixDist -= event.rTime * 30.0f * m_speed; + if (m_fixDist < 10.0f) m_fixDist = 10.0f; + } + if (event.keyState & KS_NUMMINUS) + { + m_fixDist += event.rTime * 30.0f * m_speed; + if (m_fixDist > 200.0f) m_fixDist = 200.0f; + } + + if (m_cameraScroll) + { + // Left/Right + m_fixDirectionH += m_mouseDirH * event.rTime * 1.0f * m_speed; + m_fixDirectionH = Math::NormAngle(m_fixDirectionH); + } + + if ((m_mouseDirH != 0) || (m_mouseDirV != 0)) + AbortCentering(); // special stops framing + + if (m_cameraObj != NULL) + { + Math::Vector lookatPt = m_cameraObj->GetPosition(0); + + float h = m_fixDirectionH + m_remotePan; + float v = m_fixDirectionV; + + float d = m_fixDist; + m_eyePt = RotateView(lookatPt, h, v, d); + if (m_type == Gfx::CAM_TYPE_PLANE) m_eyePt.y += m_fixDist / 2.0f; + m_eyePt = ExcludeTerrain(m_eyePt, lookatPt, h, v); + m_eyePt = ExcludeObject(m_eyePt, lookatPt, h, v); + + SetViewTime(m_eyePt, lookatPt, event.rTime); + + m_directionH = h + Math::PI / 2.0f; + m_directionV = v; + } + + return true; +} + +bool Gfx::CCamera::EventFrameExplo(const Event &event) +{ + float factor = m_heightEye * 0.5f + 30.0f; + + if (m_mouseDirH != 0.0f) + m_directionH -= m_mouseDirH * event.rTime * 0.7f * m_speed; + + m_terrain->ValidPosition(m_eyePt, 10.0f); + + if ( m_terrain->MoveOnFloor(m_eyePt, false) ) + { + m_eyePt.y += m_heightEye; + + Math::Vector pos = m_eyePt; + if ( m_terrain->MoveOnFloor(pos, false) ) + { + pos.y += 2.0f; + if ( m_eyePt.y < pos.y ) + m_eyePt.y = pos.y; + } + + } + + Math::Vector lookatPt = Math::LookatPoint(m_eyePt, m_directionH, m_directionV, 50.0f); + + if (m_terrain->MoveOnFloor(lookatPt, true)) + lookatPt.y += m_heightLookat; + + SetViewTime(m_eyePt, lookatPt, event.rTime); + + return true; +} + +bool Gfx::CCamera::EventFrameOnBoard(const Event &event) +{ + if (m_cameraObj != NULL) + { + Math::Vector lookatPt, upVec; + m_cameraObj->SetViewFromHere(m_eyePt, m_directionH, m_directionV, + lookatPt, vUpVec, m_type); + Math::Vector eye = m_effectOffset * 0.3f + m_eyePt; + Math::Vector lookat = m_effectOffset * 0.3f + lookatPt; + + SetViewParams(eye, lookat, upVec); + m_actualEye = eye; + m_actualLookat = lookat; + } + return true; +} + +bool Gfx::CCamera::EventFrameInfo(const Event &event) +{ + SetViewTime(Math::Vector(0.0f, 0.0f, 0.0f), + Math::Vector(0.0f, 0.0f, 1.0f), + event.rTime); + return true; +} + +bool Gfx::CCamera::EventFrameVisit(const Event &event) +{ + m_visitTime += event.rTime; + + // +/-. + if (event.keyState & KS_NUMPLUS) + { + m_visitDist -= event.rTime * 50.0f * m_speed; + if (m_visitDist < 20.0f) m_visitDist = 20.0f; + } + if (event.keyState & KS_NUMMINUS) + { + m_visitDist += event.rTime * 50.0f * m_speed; + if (m_visitDist > 200.0f) m_visitDist = 200.0f; + } + + // PageUp/Down. + if (event.keyState & KS_PAGEUP) + { + m_visitDirectionV -= event.rTime * 1.0f * m_speed; + if (m_visitDirectionV < -Math::PI * 0.40f) m_visitDirectionV = -Math::PI * 0.40f; + } + if (event.keyState & KS_PAGEDOWN) + { + m_visitDirectionV += event.rTime * 1.0f * m_speed; + if (m_visitDirectionV > 0.0f ) m_visitDirectionV = 0.0f; + } + + if (m_cameraScroll) + { + m_visitDist -= m_mouseDirV * event.rTime * 30.0f * m_speed; + if (m_visitDist < 20.0f) m_visitDist = 20.0f; + if (m_visitDist > 200.0f) m_visitDist = 200.0f; + } + + float angleH = (m_visitTime / 10.0f) * (Math::PI * 2.0f); + float angleV = m_visitDirectionV; + Math::Vector eye = RotateView(m_visitGoal, angleH, angleV, m_visitDist); + eye = ExcludeTerrain(eye, m_visitGoal, angleH, angleV); + eye = ExcludeObject(eye, m_visitGoal, angleH, angleV); + SetViewTime(eye, m_visitGoal, event.rTime); + + return true; +} + +bool Gfx::CCamera::EventFrameScript(const Event &event) +{ + SetViewTime(m_scriptEye + m_effectOffset, + m_scriptLookat + m_effectOffset, event.rTime); + return true; +} + +void Gfx::CCamera::SetScriptEye(Math::Vector eye) +{ + m_scriptEye = eye; +} + +void Gfx::CCamera::SetScriptLookat(Math::Vector lookat) +{ + m_scriptLookat = lookat; +} + +void Gfx::CCamera::SetViewParams(const Math::Vector &eye, const Math::Vector &lookat, + const Math::Vector &up) +{ + m_engine->SetViewParams(eye, lookat, up, m_eyeDistance); + + bool under = (eye.y < m_water->GetLevel()); // Is it underwater? + if (m_type == Gfx::CAM_TYPE_INFO) + under = false; + + m_engine->SetRankView(under ? 1 : 0); +} + +Math::Vector Gfx::CCamera::ExcludeTerrain(Math::Vector eye, Math::Vector lookat, + float &angleH, float &angleV) +{ + Math::Vector pos = eye; + if (m_terrain->MoveOnFloor(pos)) + { + float dist = Math::DistanceProjected(lookat, pos); + pos.y += 2.0f+dist*0.1f; + if ( pos.y > eye.y ) + { + angleV = -Math::RotateAngle(dist, pos.y-lookat.y); + eye = RotateView(lookat, angleH, angleV, dist); + } + } + return eye; +} + +Math::Vector Gfx::CCamera::ExcludeObject(Math::Vector eye, Math::Vector lookat, + float &angleH, float &angleV) +{ + return eye; + +// TODO: check the commented out code: +/* + for (int i = 0; i < 1000000; i++) + { + CObject* obj = static_cast( m_iMan->SearchInstance(CLASS_OBJECT, i) ); + if (obj == NULL) + break; + + int j = 0; + Math::Vector oPos; + float oRad; + while (obj->GetCrashSphere(j++, oPos, oRad)) + { + float dist = Math::Distance(oPos, eye); + if (dist < oRad + 2.0f) + eye.y = oPos.y + oRad + 2.0f; + } + } + + return eye;*/ +} diff --git a/src/graphics/engine/camera.h b/src/graphics/engine/camera.h index 76077bf..935f8b0 100644 --- a/src/graphics/engine/camera.h +++ b/src/graphics/engine/camera.h @@ -29,210 +29,322 @@ class CObject; namespace Gfx { +/** + \enum CameraType + \brief Type of camera */ enum CameraType { - CAMERA_NULL = 0, // camera undefined - CAMERA_FREE = 1, // camera free (never in principle) - CAMERA_EDIT = 2, // camera while editing a program - CAMERA_ONBOARD = 3, // camera on board a robot - CAMERA_BACK = 4, // camera behind a robot - CAMERA_FIX = 5, // static camera following robot - CAMERA_EXPLO = 6, // camera steady after explosion - CAMERA_SCRIPT = 7, // camera during a film script - CAMERA_INFO = 8, // camera for displaying information - CAMERA_VISIT = 9, // visit instead of an error - CAMERA_DIALOG = 10, // camera for dialogue - CAMERA_PLANE = 11, // static camera height + //! Undefined + CAM_TYPE_NULL = 0, + //! Free camera (? never in principle ?) + CAM_TYPE_FREE = 1, + //! Camera while editing a program + CAM_TYPE_EDIT = 2, + //! Camera on board a robot + CAM_TYPE_ONBOARD = 3, + //! Camera behind a robot + CAM_TYPE_BACK = 4, + //! Static camera following robot + CAM_TYPE_FIX = 5, + //! Camera steady after explosion + CAM_TYPE_EXPLO = 6, + //! Camera during a film script + CAM_TYPE_SCRIPT = 7, + //! Camera for displaying information + CAM_TYPE_INFO = 8, + //! Visit instead of an error + CAM_TYPE_VISIT = 9, + //! Camera for dialog + CAM_TYPE_DIALOG = 10, + //! Static camera height + CAM_TYPE_PLANE = 11, }; enum CameraSmooth { - CS_NONE = 0, // sharp - CS_NORM = 1, // normal - CS_HARD = 2, // hard - CS_SPEC = 3, // special + //! Sharp + CAM_SMOOTH_NONE = 0, + //! Normal + CAM_SMOOTH_NORM = 1, + //! Hard + CAM_SMOOTH_HARD = 2, + //! Special + CAM_SMOOTH_SPEC = 3, }; enum CenteringPhase { - CP_NULL = 0, - CP_START = 1, - CP_WAIT = 2, - CP_STOP = 3, + CAM_PHASE_NULL = 0, + CAM_PHASE_START = 1, + CAM_PHASE_WAIT = 2, + CAM_PHASE_STOP = 3, }; enum CameraEffect { - CE_NULL = 0, // no effect - CE_TERRAFORM = 1, // digging in - CE_CRASH = 2, // Vehicle driving is severely - CE_EXPLO = 3, // explosion - CE_SHOT = 4, // not mortal shot - CE_VIBRATION = 5, // vibration during construction - CE_PET = 6, // spleen reactor + //! No effect + CAM_EFFECT_NULL = 0, + //! Digging in + CAM_EFFECT_TERRAFORM = 1, + //! ? Vehicle driving is severely ? + CAM_EFFECT_CRASH = 2, + //! Explosion + CAM_EFFECT_EXPLO = 3, + //! ? Not mortal shot ? + CAM_EFFECT_SHOT = 4, + //! Vibration during construction + CAM_EFFECT_VIBRATION = 5, + //! ? Spleen reactor ? + CAM_EFFECT_PET = 6, }; -enum OverEffect +enum CameraOverEffect { - OE_NULL = 0, // no effect - OE_BLOOD = 1, // flash red - OE_FADEINw = 2, // white -> nothing - OE_FADEOUTw = 3, // nothing -> white - OE_FADEOUTb = 4, // nothing -> blue - OE_BLITZ = 5, // lightning + //! No effect + CAM_OVER_EFFECT_NULL = 0, + //! Flash red + CAM_OVER_EFFECT_BLOOD = 1, + //! White -> nothing + CAM_OVER_EFFECT_FADEIN_WHITE = 2, + //! Nothing -> white + CAM_OVER_EFFECT_FADEOUT_WHITE = 3, + //! Nothing -> blue + CAM_OVER_EFFECT_FADEOUT_BLACK = 4, + //! Lightning + CAM_OVER_EFFECT_LIGHTNING = 5, }; +/** + \class CCamera + \brief Camera moving in 3D scene + ... */ class CCamera { public: CCamera(CInstanceManager* iMan); ~CCamera(); + //! Management of an event bool EventProcess(const Event &event); + //! Initializes the camera void Init(Math::Vector eye, Math::Vector lookat, float delay); + //! Sets the object controlling the camera void SetObject(CObject* object); - CObject* RetObject(); + CObject* GetObject(); - void SetType(CameraType type); - CameraType RetType(); + //! Change the type of camera + void SetType(Gfx::CameraType type); + Gfx::CameraType GetType(); - void SetSmooth(CameraSmooth type); - CameraSmooth RetSmoth(); + //! Management of the smoothing mode + void SetSmooth(CameraSmooth type); + Gfx::CameraSmooth GetSmoth(); + //! Management of the setback distance void SetDist(float dist); - float RetDist(); + float GetDist(); + //! Manage angle mode Gfx::CAM_TYPE_FIX void SetFixDirection(float angle); - float RetFixDirection(); + float GetFixDirection(); + //! Managing the triggering mode of the camera panning void SetRemotePan(float value); - float RetRemotePan(); + float GetRemotePan(); + //! Management of the remote zoom (0 .. 1) of the camera void SetRemoteZoom(float value); - float RetRemoteZoom(); + float GetRemoteZoom(); + //! Start with a tour round the camera void StartVisit(Math::Vector goal, float dist); + //! Circular end of a visit with the camera void StopVisit(); - void RetCamera(Math::Vector &eye, Math::Vector &lookat); + //! Returns the point of view of the camera + void GetCamera(Math::Vector &eye, Math::Vector &lookat); + //! Specifies a special movement of camera to frame action bool StartCentering(CObject *object, float angleH, float angleV, float dist, float time); + //! Ends a special movement of camera to frame action bool StopCentering(CObject *object, float time); + //! Stop framing special in the current position void AbortCentering(); + //! Removes the special effect with the camera void FlushEffect(); - void StartEffect(CameraEffect effect, Math::Vector pos, float force); + //! Starts a special effect with the camera + void StartEffect(Gfx::CameraEffect effect, Math::Vector pos, float force); + //! Removes the effect of superposition in the foreground void FlushOver(); + //! Specifies the base color void SetOverBaseColor(Gfx::Color color); - void StartOver(OverEffect effect, Math::Vector pos, float force); + void StartOver(Gfx::CameraOverEffect effect, Math::Vector pos, float force); + //! Sets the soft movement of the camera void FixCamera(); void SetScriptEye(Math::Vector eye); void SetScriptLookat(Math::Vector lookat); - void SetEffect(bool bEnable); - void SetCameraScroll(bool bScroll); - void SetCameraInvertX(bool bInvert); - void SetCameraInvertY(bool bInvert); + void SetEffect(bool enable); + void SetCameraScroll(bool scroll); + void SetCameraInvertX(bool invert); + void SetCameraInvertY(bool invert); - float RetMotorTurn(); - Gfx::EngineMouseType RetMouseDef(Math::Point pos); + //! Returns an additional force to turn + float GetMotorTurn(); + //! Returns the default sprite to use for the mouse + Gfx::EngineMouseType GetMouseDef(Math::Point pos); protected: + //! Changes the camera according to the mouse moved bool EventMouseMove(const Event &event); + //! Mouse wheel operation void EventMouseWheel(int dir); + //! Changes the camera according to the time elapsed bool EventFrame(const Event &event); + //! Moves the point of view bool EventFrameFree(const Event &event); + //! Moves the point of view bool EventFrameEdit(const Event &event); + //! Moves the point of view bool EventFrameDialog(const Event &event); + //! Moves the point of view bool EventFrameBack(const Event &event); + //! Moves the point of view bool EventFrameFix(const Event &event); + //! Moves the point of view bool EventFrameExplo(const Event &event); + //! Moves the point of view bool EventFrameOnBoard(const Event &event); + //! Moves the point of view bool EventFrameInfo(const Event &event); + //! Moves the point of view bool EventFrameVisit(const Event &event); + //! Moves the point of view bool EventFrameScript(const Event &event); + //! Specifies the location and direction of view to the 3D engine void SetViewTime(const Math::Vector &vEyePt, const Math::Vector &vLookatPt, float rTime); + //! Avoid the obstacles bool IsCollision(Math::Vector &eye, Math::Vector lookat); + //! Avoid the obstacles bool IsCollisionBack(Math::Vector &eye, Math::Vector lookat); + //! Avoid the obstacles bool IsCollisionFix(Math::Vector &eye, Math::Vector lookat); - Math::Vector ExcludeTerrain(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); - Math::Vector ExcludeObject(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); + //! Adjusts the camera not to enter the ground + Math::Vector ExcludeTerrain(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); + //! Adjusts the camera not to enter an object + Math::Vector ExcludeObject(Math::Vector eye, Math::Vector lookat, float &angleH, float &angleV); + //! Specifies the location and direction of view void SetViewParams(const Math::Vector &eye, const Math::Vector &lookat, const Math::Vector &up); + //! Advances the effect of the camera void EffectFrame(const Event &event); + //! Advanced overlay effect in the foreground void OverFrame(const Event &event); protected: CInstanceManager* m_iMan; - Gfx::CEngine* m_engine; - CTerrain* m_terrain; - CWater* m_water; - - CameraType m_type; // the type of camera (CAMERA *) - CameraSmooth m_smooth; // type of smoothing - CObject* m_cameraObj; // object linked to the camera - - float m_eyeDistance; // distance between the eyes - float m_initDelay; // time of initial centering - - Math::Vector m_actualEye; // current eye - Math::Vector m_actualLookat; // aim current - Math::Vector m_finalEye; // final eye - Math::Vector m_finalLookat; // aim final - Math::Vector m_normEye; // normal eye - Math::Vector m_normLookat; // aim normal + Gfx::CEngine* m_engine; + Gfx::CTerrain* m_terrain; + Gfx::CWater* m_water; + + //! The type of camera + Gfx::CameraType m_type; + //! Type of smoothing + Gfx::CameraSmooth m_smooth; + //! Object linked to the camera + CObject* m_cameraObj; + + //! Distance between the eyes + float m_eyeDistance; + //! Time of initial centering + float m_initDelay; + + //! Current eye + Math::Vector m_actualEye; + //! Current aim + Math::Vector m_actualLookat; + //! Final eye + Math::Vector m_finalEye; + //! Final aim + Math::Vector m_finalLookat; + //! Normal eye + Math::Vector m_normEye; + //! Normal aim + Math::Vector m_normLookat; + float m_focus; - bool m_bRightDown; + bool m_rightDown; Math::Point m_rightPosInit; Math::Point m_rightPosCenter; Math::Point m_rightPosMove; - Math::Vector m_eyePt; // CAMERA_FREE: eye - float m_directionH; // CAMERA_FREE: horizontal direction - float m_directionV; // CAMERA_FREE: vertical direction - float m_heightEye; // CAMERA_FREE: height above the ground - float m_heightLookat; // CAMERA_FREE: height above the ground - float m_speed; // CAMERA_FREE: speed of movement - - float m_backDist; // CAMERA_BACK: distance - float m_backMin; // CAMERA_BACK: distance minimal - float m_addDirectionH; // CAMERA_BACK: additional direction - float m_addDirectionV; // CAMERA_BACK: additional direction - bool m_bTransparency; - - float m_fixDist; // CAMERA_FIX: distance - float m_fixDirectionH; // CAMERA_FIX: direction - float m_fixDirectionV; // CAMERA_FIX: direction - - Math::Vector m_visitGoal; // CAMERA_VISIT: target position - float m_visitDist; // CAMERA_VISIT: distance - float m_visitTime; // CAMERA_VISIT: relative time - CameraType m_visitType; // CAMERA_VISIT: initial type - float m_visitDirectionH; // CAMERA_VISIT: direction - float m_visitDirectionV; // CAMERA_VISIT: direction - - float m_editHeight; // CAMERA_EDIT: height - - float m_remotePan; - float m_remoteZoom; - - Math::Point m_mousePos; - float m_mouseDirH; - float m_mouseDirV; - float m_mouseMarging; - - float m_motorTurn; - - CenteringPhase m_centeringPhase; + //! CAM_TYPE_FREE: eye + Math::Vector m_eyePt; + //! CAM_TYPE_FREE: horizontal direction + float m_directionH; + //! CAM_TYPE_FREE: vertical direction + float m_directionV; + //! CAM_TYPE_FREE: height above the ground + float m_heightEye; + //! CAM_TYPE_FREE: height above the ground + float m_heightLookat; + //! CAM_TYPE_FREE: speed of movement + float m_speed; + + //! CAM_TYPE_BACK: distance + float m_backDist; + //! CAM_TYPE_BACK: distance minimal + float m_backMin; + //! CAM_TYPE_BACK: additional direction + float m_addDirectionH; + //! CAM_TYPE_BACK: additional direction + float m_addDirectionV; + bool m_transparency; + + //! CAM_TYPE_FIX: distance + float m_fixDist; + //! CAM_TYPE_FIX: direction + float m_fixDirectionH; + //! CAM_TYPE_FIX: direction + float m_fixDirectionV; + + //! CAM_TYPE_VISIT: target position + Math::Vector m_visitGoal; + //! CAM_TYPE_VISIT: distance + float m_visitDist; + //! CAM_TYPE_VISIT: relative time + float m_visitTime; + //! CAM_TYPE_VISIT: initial type + Gfx::CameraType m_visitType; + //! CAM_TYPE_VISIT: direction + float m_visitDirectionH; + //! CAM_TYPE_VISIT: direction + float m_visitDirectionV; + + //! CAM_TYPE_EDIT: height + float m_editHeight; + + float m_remotePan; + float m_remoteZoom; + + Math::Point m_mousePos; + float m_mouseDirH; + float m_mouseDirV; + float m_mouseMarging; + + float m_motorTurn; + + Gfx::CenteringPhase m_centeringPhase; float m_centeringAngleH; float m_centeringAngleV; float m_centeringDist; @@ -256,13 +368,17 @@ protected: float m_overFadeIn; float m_overFadeOut; - Math::Vector m_scriptEye; - Math::Vector m_scriptLookat; - - bool m_bEffect; // shocks if explosion? - bool m_bCameraScroll; // scroll in the edges? - bool m_bCameraInvertX; // X inversion in the edges? - bool m_bCameraInvertY; // Y inversion in the edges? + Math::Vector m_scriptEye; + Math::Vector m_scriptLookat; + + //! Shocks if explosion? + bool m_effect; + //! Scroll in the edges? + bool m_cameraScroll; + //! X inversion in the edges? + bool m_cameraInvertX; + //! Y inversion in the edges? + bool m_cameraInvertY; }; -- cgit v1.2.3-1-g7c22 From d8a0c8d32e160e7ae86bb5b85ead8e5f71b1fd01 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 30 Jul 2012 22:32:28 +0200 Subject: Lines and line strip primitives Will probably be useful --- src/graphics/core/device.h | 6 +++--- src/graphics/opengl/gldevice.cpp | 36 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h index 1f6cacf..c10b853 100644 --- a/src/graphics/core/device.h +++ b/src/graphics/core/device.h @@ -179,12 +179,12 @@ enum FillMode /** \enum PrimitiveType - \brief Type of primitive to render - - Only these two types are used. */ + \brief Type of primitive to render */ enum PrimitiveType { + PRIMITIVE_POINTS, PRIMITIVE_LINES, + PRIMITIVE_LINE_STRIP, PRIMITIVE_TRIANGLES, PRIMITIVE_TRIANGLE_STRIP }; diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 1fd6a18..e80b101 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -746,14 +746,24 @@ Gfx::Color Gfx::CGLDevice::GetTextureFactor() return Gfx::Color(color[0], color[1], color[2], color[3]); } +GLenum TranslateGfxPrimitive(Gfx::PrimitiveType type) +{ + GLenum flag = 0; + switch (type) + { + case Gfx::PRIMITIVE_POINTS: flag = GL_POINTS; break; + case Gfx::PRIMITIVE_LINES: flag = GL_LINES; break; + case Gfx::PRIMITIVE_LINE_STRIP: flag = GL_LINE_STRIP; break; + case Gfx::PRIMITIVE_TRIANGLES: flag = GL_TRIANGLES; break; + case Gfx::PRIMITIVE_TRIANGLE_STRIP: flag = GL_TRIANGLE_STRIP; break; + default: assert(false); break; + } + return flag; +} + void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Vertex *vertices, int vertexCount) { - if (type == Gfx::PRIMITIVE_LINES) - glBegin(GL_LINES); - else if (type == Gfx::PRIMITIVE_TRIANGLES) - glBegin(GL_TRIANGLES); - else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) - glBegin(GL_TRIANGLE_STRIP); + glBegin(TranslateGfxPrimitive(type)); glColor3f(1.0f, 1.0f, 1.0f); @@ -769,12 +779,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Vertex *vertic void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices, int vertexCount) { - if (type == Gfx::PRIMITIVE_LINES) - glBegin(GL_LINES); - else if (type == Gfx::PRIMITIVE_TRIANGLES) - glBegin(GL_TRIANGLES); - else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) - glBegin(GL_TRIANGLE_STRIP); + glBegin(TranslateGfxPrimitive(type)); for (int i = 0; i < vertexCount; ++i) { @@ -789,12 +794,7 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const VertexTex2 *vertices, int vertexCount) { - if (type == Gfx::PRIMITIVE_LINES) - glBegin(GL_LINES); - else if (type == Gfx::PRIMITIVE_TRIANGLES) - glBegin(GL_TRIANGLES); - else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP) - glBegin(GL_TRIANGLE_STRIP); + glBegin(TranslateGfxPrimitive(type)); glColor3f(1.0f, 1.0f, 1.0f); -- cgit v1.2.3-1-g7c22 From 5e637ca0288ddd631ec33e1d620cd4a73bcdc2be Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 30 Jul 2012 22:59:18 +0200 Subject: Switched to new style casts - rewrote old C-style casts to new ..._cast<> - corrected some dangerous casts - added -Wold-style-cast to compile flags --- CMakeLists.txt | 4 +- src/app/app.cpp | 16 ++++---- src/common/image.cpp | 6 +-- src/common/iman.cpp | 2 +- src/common/ioutils.h | 18 +++++---- src/common/stringutils.cpp | 8 ++-- src/graphics/core/color.cpp | 2 +- src/graphics/core/color.h | 4 +- src/graphics/engine/modelfile.cpp | 16 ++++---- src/graphics/opengl/gldevice.cpp | 80 +++++++++++++++++++-------------------- src/math/func.h | 10 ++--- src/math/matrix.h | 2 +- src/math/point.h | 4 +- src/math/vector.h | 4 +- 14 files changed, 90 insertions(+), 86 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 092a812..98e9732 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,8 +23,8 @@ set(USE_GLEW auto) set(CMAKE_BUILD_TYPE debug) # Global compile flags -set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall -std=gnu++0x") -set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -std=gnu++0x") +set(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wall -Wold-style-cast -std=gnu++0x") +set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wold-style-cast -std=gnu++0x") # Include cmake directory SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${colobot_SOURCE_DIR}/cmake") diff --git a/src/app/app.cpp b/src/app/app.cpp index 5c6ef49..d20232d 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -461,7 +461,7 @@ void CApplication::UpdateJoystick() SDL_JoystickUpdate(); - for (int axis = 0; axis < (int) m_joyAxeState.size(); ++axis) + for (int axis = 0; axis < static_cast( m_joyAxeState.size() ); ++axis) { int newValue = SDL_JoystickGetAxis(m_private->joystick, axis); @@ -480,7 +480,7 @@ void CApplication::UpdateJoystick() } } - for (int button = 0; button < (int) m_joyButtonState.size(); ++button) + for (int button = 0; button < static_cast( m_joyButtonState.size() ); ++button) { bool newValue = SDL_JoystickGetButton(m_private->joystick, button) == 1; @@ -621,14 +621,14 @@ PressState TranslatePressState(unsigned char state) - y: 0=down, 1=up */ Math::Point CApplication::WindowToInterfaceCoords(Math::IntPoint pos) { - return Math::Point( (float)pos.x / (float)m_deviceConfig.size.w, - 1.0f - (float)pos.y / (float)m_deviceConfig.size.h); + return Math::Point( static_cast(pos.x) / static_cast(m_deviceConfig.size.w), + 1.0f - static_cast(pos.y) / static_cast(m_deviceConfig.size.h) ); } Math::IntPoint CApplication::InterfaceToWindowCoords(Math::Point pos) { - return Math::IntPoint((int)(pos.x * m_deviceConfig.size.w), - (int)((1.0f - pos.y) * m_deviceConfig.size.h)); + return Math::IntPoint(static_cast(pos.x * m_deviceConfig.size.w), + static_cast((1.0f - pos.y) * m_deviceConfig.size.h)); } /** The SDL event parsed is stored internally. @@ -822,10 +822,10 @@ VideoQueryResult CApplication::GetVideoResolutionList(std::vector SDL_Rect **modes = SDL_ListModes(NULL, videoFlags); - if (modes == (SDL_Rect **)0) + if (modes == reinterpret_cast(0) ) return VIDEO_QUERY_NONE; // no modes available - if (modes == (SDL_Rect **)-1) + if (modes == reinterpret_cast(-1) ) return VIDEO_QUERY_ALL; // all resolutions are possible diff --git a/src/common/image.cpp b/src/common/image.cpp index 78834b4..3d64377 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -102,7 +102,7 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf) info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + png_destroy_write_struct(&png_ptr, static_cast(NULL)); PNG_ERROR = "png_create_info_struct() error!"; return false; } @@ -123,9 +123,9 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf) png_write_info(png_ptr, info_ptr); png_set_packing(png_ptr); - row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surf->h); + row_pointers = static_cast( malloc(sizeof(png_bytep)*surf->h) ); for (i = 0; i < surf->h; i++) - row_pointers[i] = (png_bytep)(Uint8 *)surf->pixels + i*surf->pitch; + row_pointers[i] = static_cast( static_cast(surf->pixels) ) + i*surf->pitch; png_write_image(png_ptr, row_pointers); png_write_end(png_ptr, info_ptr); diff --git a/src/common/iman.cpp b/src/common/iman.cpp index 28ee3d3..90b0c1e 100644 --- a/src/common/iman.cpp +++ b/src/common/iman.cpp @@ -93,7 +93,7 @@ bool CInstanceManager::AddInstance(ClassType classType, void* pointer, int max) if ( m_table[classType].classPointer == 0 ) { - m_table[classType].classPointer = (void**)malloc(max*sizeof(void*)); + m_table[classType].classPointer = static_cast( malloc(max*sizeof(void*)) ); m_table[classType].totalPossible = max; m_table[classType].totalUsed = 0; } diff --git a/src/common/ioutils.h b/src/common/ioutils.h index f17c961..a7bd044 100644 --- a/src/common/ioutils.h +++ b/src/common/ioutils.h @@ -21,6 +21,7 @@ #include +#include namespace IOUtils { @@ -35,7 +36,7 @@ void WriteBinary(T value, std::ostream &ostr) for (int i = 0; i < N; ++i) { unsigned char byte = (value >> (i*8)) & 0xFF; - ostr.write((char*)&byte, 1); + ostr.write(reinterpret_cast(&byte), 1); } } @@ -51,7 +52,7 @@ T ReadBinary(std::istream &istr) for (int i = 0; i < N; ++i) { unsigned char byte = 0; - istr.read((char*)&byte, 1); + istr.read(reinterpret_cast(&byte), 1); value |= byte << (i*8); } return value; @@ -63,8 +64,10 @@ T ReadBinary(std::istream &istr) NOTE: code is probably not portable as there are platforms with other float representations. */ void WriteBinaryFloat(float value, std::ostream &ostr) { - unsigned int iValue = *( (unsigned int*)( (void*)(&value) ) ); - IOUtils::WriteBinary<4, unsigned int>(iValue, ostr); + union { float fValue; unsigned int iValue; } u; + memset(&u, 0, sizeof(u)); + u.fValue = value; + IOUtils::WriteBinary<4, unsigned int>(u.iValue, ostr); } //! Reads a binary 32-bit float from input stream @@ -73,9 +76,10 @@ void WriteBinaryFloat(float value, std::ostream &ostr) NOTE: code is probably not portable as there are platforms with other float representations. */ float ReadBinaryFloat(std::istream &istr) { - unsigned int iValue = IOUtils::ReadBinary<4, unsigned int>(istr); - float result = *( (float*)( (void*)(&iValue) ) ); - return result; + union { float fValue; unsigned int iValue; } u; + memset(&u, 0, sizeof(u)); + u.iValue = IOUtils::ReadBinary<4, unsigned int>(istr); + return u.fValue; } }; // namespace IOUtils diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp index 9a7aa61..585bb46 100644 --- a/src/common/stringutils.cpp +++ b/src/common/stringutils.cpp @@ -36,7 +36,7 @@ std::string StrUtils::UnicodeCharToUtf8(unsigned int ch) std::string result; if (ch < 0x0080) { - result += (char)(ch); + result += static_cast(ch); } else if (ch < 0x0800) { @@ -61,7 +61,7 @@ std::string StrUtils::UnicodeStringToUtf8(const std::wstring &str) { std::string result; for (unsigned int i = 0; i < str.size(); ++i) - result += StrUtils::UnicodeCharToUtf8((unsigned int)str[i]); + result += StrUtils::UnicodeCharToUtf8(static_cast(str[i])); return result; } @@ -75,7 +75,7 @@ unsigned int StrUtils::Utf8CharToUnicode(const std::string &ch) if ((ch[0] & 0x80) == 0) { if (ch.size() == 1) - result = (unsigned int)ch[0]; + result = static_cast(ch[0]); } else if ((ch[0] & 0xC0) == 0xC0) { @@ -111,7 +111,7 @@ std::wstring StrUtils::Utf8StringToUnicode(const std::string &str) break; std::string ch = str.substr(pos, len); - result += (wchar_t)(StrUtils::Utf8CharToUnicode(ch)); + result += static_cast(StrUtils::Utf8CharToUnicode(ch)); pos += len; } return result; diff --git a/src/graphics/core/color.cpp b/src/graphics/core/color.cpp index 8dec0e4..f241227 100644 --- a/src/graphics/core/color.cpp +++ b/src/graphics/core/color.cpp @@ -79,7 +79,7 @@ Gfx::Color Gfx::HSV2RGB(Gfx::ColorHSV color) { if ( color.h == 360.0f ) color.h = 0.0f; color.h /= 60.0f; - int i = (int)color.h; // integer part (0 .. 5) + int i = static_cast(color.h); // integer part (0 .. 5) float f = color.h-i; // fractional part float v = color.v; diff --git a/src/graphics/core/color.h b/src/graphics/core/color.h index 907a3b9..6973644 100644 --- a/src/graphics/core/color.h +++ b/src/graphics/core/color.h @@ -44,13 +44,13 @@ struct Color //! Returns the struct cast to \c float* array; use with care! inline float* Array() { - return (float*)this; + return reinterpret_cast(this); } //! Returns the struct cast to const float* array; use with care! inline const float* Array() const { - return (const float*)this; + return reinterpret_cast(this); } //! Returns a string (r, g, b, a) diff --git a/src/graphics/engine/modelfile.cpp b/src/graphics/engine/modelfile.cpp index 537add4..844958f 100644 --- a/src/graphics/engine/modelfile.cpp +++ b/src/graphics/engine/modelfile.cpp @@ -246,7 +246,7 @@ Gfx::CModelFile::CModelFile(CInstanceManager* iMan) { m_iMan = iMan; - m_engine = (CEngine*)m_iMan->SearchInstance(CLASS_ENGINE); + m_engine = static_cast(m_iMan->SearchInstance(CLASS_ENGINE)); m_triangles.reserve(TRIANGLE_PREALLOCATE_COUNT); } @@ -431,7 +431,7 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) } } - for (int i = 0; i < (int) m_triangles.size(); ++i) + for (int i = 0; i < static_cast( m_triangles.size() ); ++i) { m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "tga"); @@ -521,7 +521,7 @@ bool Gfx::CModelFile::WriteModel(std::ostream &stream) for (int i = 0; i < 10; ++i) IOUtils::WriteBinary<4, int>(header.reserved[i], stream); - for (int i = 0; i < (int)m_triangles.size(); ++i) + for (int i = 0; i < static_cast( m_triangles.size() ); ++i) { NewModelTriangle t; @@ -701,9 +701,9 @@ bool Gfx::CModelFile::ReadDXF(std::istream &stream, float min, float max) faceNum --; if ( faceNum >= 0 ) { - assert( (p1-1 >= 0) && (p1-1 < (int)vertices.size() ) ); - assert( (p2-1 >= 0) && (p2-1 < (int)vertices.size() ) ); - assert( (p3-1 >= 0) && (p3-1 < (int)vertices.size() ) ); + assert( (p1-1 >= 0) && (p1-1 < static_cast(vertices.size())) ); + assert( (p2-1 >= 0) && (p2-1 < static_cast(vertices.size())) ); + assert( (p3-1 >= 0) && (p3-1 < static_cast(vertices.size())) ); CreateTriangle(vertices[p3-1], vertices[p2-1], vertices[p1-1], min, max); waitFaceX = true; @@ -774,7 +774,7 @@ bool Gfx::CModelFile::CreateEngineObject(int objRank, int addState) void Gfx::CModelFile::Mirror() { - for (int i = 0; i < (int)m_triangles.size(); i++) + for (int i = 0; i < static_cast( m_triangles.size() ); i++) { Gfx::VertexTex2 t = m_triangles[i].p1; m_triangles[i].p1 = m_triangles[i].p2; @@ -804,7 +804,7 @@ float Gfx::CModelFile::GetHeight(Math::Vector pos) { float limit = 5.0f; - for (int i = 0; i < (int)m_triangles.size(); i++) + for (int i = 0; i < static_cast( m_triangles.size() ); i++) { if ( fabs(pos.x - m_triangles[i].p1.coord.x) < limit && fabs(pos.z - m_triangles[i].p1.coord.z) < limit ) diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index e80b101..3a255f4 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -266,7 +266,7 @@ void Gfx::CGLDevice::UpdateModelviewMatrix() if (m_lighting) { - for (int index = 0; index < (int)m_lights.size(); ++index) + for (int index = 0; index < static_cast( m_lights.size() ); ++index) UpdateLightPosition(index); } } @@ -293,7 +293,7 @@ int Gfx::CGLDevice::GetMaxLightCount() void Gfx::CGLDevice::SetLight(int index, const Gfx::Light &light) { assert(index >= 0); - assert(index < (int)m_lights.size()); + assert(index < static_cast( m_lights.size() )); m_lights[index] = light; @@ -322,7 +322,7 @@ void Gfx::CGLDevice::SetLight(int index, const Gfx::Light &light) void Gfx::CGLDevice::UpdateLightPosition(int index) { assert(index >= 0); - assert(index < (int)m_lights.size()); + assert(index < static_cast( m_lights.size() )); if ((! m_lighting) || (! m_lightsEnabled[index])) return; @@ -357,7 +357,7 @@ void Gfx::CGLDevice::UpdateLightPosition(int index) const Gfx::Light& Gfx::CGLDevice::GetLight(int index) { assert(index >= 0); - assert(index < (int)m_lights.size()); + assert(index < static_cast( m_lights.size() )); return m_lights[index]; } @@ -365,7 +365,7 @@ const Gfx::Light& Gfx::CGLDevice::GetLight(int index) void Gfx::CGLDevice::SetLightEnabled(int index, bool enabled) { assert(index >= 0); - assert(index < (int)m_lights.size()); + assert(index < static_cast( m_lights.size() )); m_lightsEnabled[index] = enabled; @@ -375,7 +375,7 @@ void Gfx::CGLDevice::SetLightEnabled(int index, bool enabled) bool Gfx::CGLDevice::GetLightEnabled(int index) { assert(index >= 0); - assert(index < (int)m_lights.size()); + assert(index < static_cast( m_lights.size() )); return m_lightsEnabled[index]; } @@ -479,7 +479,7 @@ void Gfx::CGLDevice::DestroyTexture(const Gfx::Texture &texture) m_allTextures.erase(it); // Unbind the texture if in use anywhere - for (int index = 0; index < (int)m_currentTextures.size(); ++index) + for (int index = 0; index < static_cast( m_currentTextures.size() ); ++index) { if (m_currentTextures[index] == texture) SetTexture(index, Gfx::Texture()); // set to invalid texture @@ -508,7 +508,7 @@ int Gfx::CGLDevice::GetMaxTextureCount() void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) { assert(index >= 0); - assert(index < (int)m_currentTextures.size()); + assert(index < static_cast( m_currentTextures.size() )); // Enable the given texture stage glActiveTexture(GL_TEXTURE0 + index); @@ -536,7 +536,7 @@ void Gfx::CGLDevice::SetTexture(int index, const Gfx::Texture &texture) Gfx::Texture Gfx::CGLDevice::GetTexture(int index) { assert(index >= 0); - assert(index < (int)m_currentTextures.size()); + assert(index < static_cast( m_currentTextures.size() )); return m_currentTextures[index]; } @@ -544,7 +544,7 @@ Gfx::Texture Gfx::CGLDevice::GetTexture(int index) void Gfx::CGLDevice::SetTextureEnabled(int index, bool enabled) { assert(index >= 0); - assert(index < (int)m_currentTextures.size()); + assert(index < static_cast( m_currentTextures.size() )); m_texturesEnabled[index] = enabled; @@ -558,7 +558,7 @@ void Gfx::CGLDevice::SetTextureEnabled(int index, bool enabled) bool Gfx::CGLDevice::GetTextureEnabled(int index) { assert(index >= 0); - assert(index < (int)m_currentTextures.size()); + assert(index < static_cast( m_currentTextures.size() )); return m_texturesEnabled[index]; } @@ -570,7 +570,7 @@ bool Gfx::CGLDevice::GetTextureEnabled(int index) void Gfx::CGLDevice::SetTextureStageParams(int index, const Gfx::TextureStageParams ¶ms) { assert(index >= 0); - assert(index < (int)m_currentTextures.size()); + assert(index < static_cast( m_currentTextures.size() )); // Remember the settings m_textureStageParams[index] = params; @@ -708,7 +708,7 @@ after_tex_operations: Gfx::TextureStageParams Gfx::CGLDevice::GetTextureStageParams(int index) { assert(index >= 0); - assert(index < (int)m_currentTextures.size()); + assert(index < static_cast( m_currentTextures.size() )); return m_textureStageParams[index]; } @@ -716,7 +716,7 @@ Gfx::TextureStageParams Gfx::CGLDevice::GetTextureStageParams(int index) void Gfx::CGLDevice::SetTextureFactor(const Gfx::Color &color) { // Needs to be set for all texture stages - for (int index = 0; index < (int)m_currentTextures.size(); ++index) + for (int index = 0; index < static_cast( m_currentTextures.size() ); ++index) { // Activate stage glActiveTexture(GL_TEXTURE0 + index); @@ -908,7 +908,7 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) if (enabled) { - for (int index = 0; index < (int)m_lights.size(); ++index) + for (int index = 0; index < static_cast( m_lights.size() ); ++index) UpdateLightPosition(index); } @@ -919,7 +919,7 @@ void Gfx::CGLDevice::SetRenderState(Gfx::RenderState state, bool enabled) m_texturing = enabled; // Enable/disable stages with new setting - for (int index = 0; index < (int)m_currentTextures.size(); ++index) + for (int index = 0; index < static_cast( m_currentTextures.size() ); ++index) { glActiveTexture(GL_TEXTURE0 + index); if (m_texturing && m_texturesEnabled[index]) @@ -1019,9 +1019,9 @@ void Gfx::CGLDevice::SetDepthTestFunc(Gfx::CompFunc func) Gfx::CompFunc Gfx::CGLDevice::GetDepthTestFunc() { - GLenum flag = 0; - glGetIntegerv(GL_DEPTH_FUNC, (GLint*)&flag); - return TranslateGLCompFunc(flag); + GLint flag = 0; + glGetIntegerv(GL_DEPTH_FUNC, &flag); + return TranslateGLCompFunc(static_cast(flag)); } void Gfx::CGLDevice::SetDepthBias(float factor) @@ -1043,11 +1043,11 @@ void Gfx::CGLDevice::SetAlphaTestFunc(Gfx::CompFunc func, float refValue) void Gfx::CGLDevice::GetAlphaTestFunc(Gfx::CompFunc &func, float &refValue) { - GLenum flag = 0; - glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint*)&flag); - func = TranslateGLCompFunc(flag); + GLint flag = 0; + glGetIntegerv(GL_ALPHA_TEST_FUNC, &flag); + func = TranslateGLCompFunc(static_cast(flag)); - glGetFloatv(GL_ALPHA_TEST_REF, (GLfloat*) &refValue); + glGetFloatv(GL_ALPHA_TEST_REF, static_cast(&refValue)); } Gfx::BlendFunc TranslateGLBlendFunc(GLenum flag) @@ -1098,13 +1098,13 @@ void Gfx::CGLDevice::SetBlendFunc(Gfx::BlendFunc srcBlend, Gfx::BlendFunc dstBle void Gfx::CGLDevice::GetBlendFunc(Gfx::BlendFunc &srcBlend, Gfx::BlendFunc &dstBlend) { - GLenum srcFlag = 0; - glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint*)&srcFlag); - srcBlend = TranslateGLBlendFunc(srcFlag); + GLint srcFlag = 0; + glGetIntegerv(GL_ALPHA_TEST_FUNC, &srcFlag); + srcBlend = TranslateGLBlendFunc(static_cast(srcFlag)); - GLenum dstFlag = 0; - glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint*)&dstFlag); - dstBlend = TranslateGLBlendFunc(dstFlag); + GLint dstFlag = 0; + glGetIntegerv(GL_ALPHA_TEST_FUNC, &dstFlag); + dstBlend = TranslateGLBlendFunc(static_cast(dstFlag)); } void Gfx::CGLDevice::SetClearColor(const Gfx::Color &color) @@ -1145,16 +1145,16 @@ void Gfx::CGLDevice::SetFogParams(Gfx::FogMode mode, const Gfx::Color &color, fl void Gfx::CGLDevice::GetFogParams(Gfx::FogMode &mode, Gfx::Color &color, float &start, float &end, float &density) { - GLenum flag = 0; - glGetIntegerv(GL_FOG_MODE, (GLint*)&flag); + GLint flag = 0; + glGetIntegerv(GL_FOG_MODE, &flag); if (flag == GL_LINEAR) mode = Gfx::FOG_LINEAR; else if (flag == GL_EXP) mode = Gfx::FOG_EXP; else if (flag == GL_EXP2) mode = Gfx::FOG_EXP2; else assert(false); - glGetFloatv(GL_FOG_START, (GLfloat*)&start); - glGetFloatv(GL_FOG_END, (GLfloat*)&end); - glGetFloatv(GL_FOG_DENSITY, (GLfloat*)&density); + glGetFloatv(GL_FOG_START, static_cast(&start)); + glGetFloatv(GL_FOG_END, static_cast(&end)); + glGetFloatv(GL_FOG_DENSITY, static_cast(&density)); } void Gfx::CGLDevice::SetCullMode(Gfx::CullMode mode) @@ -1166,8 +1166,8 @@ void Gfx::CGLDevice::SetCullMode(Gfx::CullMode mode) Gfx::CullMode Gfx::CGLDevice::GetCullMode() { - GLenum flag = 0; - glGetIntegerv(GL_CULL_FACE, (GLint*)&flag); + GLint flag = 0; + glGetIntegerv(GL_CULL_FACE, &flag); if (flag == GL_CW) return Gfx::CULL_CW; else if (flag == GL_CCW) return Gfx::CULL_CCW; else assert(false); @@ -1183,8 +1183,8 @@ void Gfx::CGLDevice::SetShadeModel(Gfx::ShadeModel model) Gfx::ShadeModel Gfx::CGLDevice::GetShadeModel() { - GLenum flag = 0; - glGetIntegerv(GL_SHADE_MODEL, (GLint*)&flag); + GLint flag = 0; + glGetIntegerv(GL_SHADE_MODEL, &flag); if (flag == GL_FLAT) return Gfx::SHADE_FLAT; else if (flag == GL_SMOOTH) return Gfx::SHADE_SMOOTH; else assert(false); @@ -1201,8 +1201,8 @@ void Gfx::CGLDevice::SetFillMode(Gfx::FillMode mode) Gfx::FillMode Gfx::CGLDevice::GetFillMode() { - GLenum flag = 0; - glGetIntegerv(GL_POLYGON_MODE, (GLint*)&flag); + GLint flag = 0; + glGetIntegerv(GL_POLYGON_MODE, &flag); if (flag == GL_POINT) return Gfx::FILL_POINT; else if (flag == GL_LINE) return Gfx::FILL_LINES; else if (flag == GL_FILL) return Gfx::FILL_FILL; diff --git a/src/math/func.h b/src/math/func.h index 9a417dc..2127d1a 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -118,13 +118,13 @@ inline void Swap(float &a, float &b) Mod(n, 1) = fractional part of n */ inline float Mod(float a, float m) { - return a - ((int)(a/m))*m; + return a - ( static_cast(a / m) ) * m; } //! Returns a random value between 0 and 1. inline float Rand() { - return (float)rand()/RAND_MAX; + return static_cast(rand()) / static_cast(RAND_MAX); } //! Returns a normalized angle, that is in other words between 0 and 2 * PI @@ -153,10 +153,10 @@ inline bool TestAngle(float angle, float min, float max) //! Calculates a value (radians) proportional between a and b (degrees) inline float PropAngle(int a, int b, float p) { - float aa = (float)a * DEG_TO_RAD; - float bb = (float)b * DEG_TO_RAD; + float aa = static_cast(a) * DEG_TO_RAD; + float bb = static_cast(b) * DEG_TO_RAD; - return aa+p*(bb-aa); + return aa + p * (bb - aa); } //! Calculates the angle to rotate the angle \a a to the angle \a g diff --git a/src/math/matrix.h b/src/math/matrix.h index 7ee40e8..45a7d75 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -121,7 +121,7 @@ struct Matrix //! Returns the struct cast to \c float* array; use with care! inline float* Array() { - return (float*)this; + return reinterpret_cast(this); } //! Transposes the matrix diff --git a/src/math/point.h b/src/math/point.h index 740b275..ea20db9 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -71,13 +71,13 @@ struct Point //! Returns the struct cast to \c float* array; use with care! inline float* Array() { - return (float*)this; + return reinterpret_cast(this); } //! Returns the struct cast to const float* array; use with care! inline const float* Array() const { - return (const float*)this; + return reinterpret_cast(this); } //! Returns the distance from (0,0) to the point (x,y) diff --git a/src/math/vector.h b/src/math/vector.h index 148d648..147869f 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -76,13 +76,13 @@ struct Vector //! Returns the struct cast to \c float* array; use with care! inline float* Array() { - return (float*)this; + return reinterpret_cast(this); } //! Returns the struct cast to const float* array; use with care! inline const float* Array() const { - return (const float*)this; + return reinterpret_cast(this); } //! Returns the vector length -- cgit v1.2.3-1-g7c22 From d00a283519e2da0d4e0d1fb8a5d1bedd89ead45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopacki?= Date: Thu, 2 Aug 2012 19:41:13 +0200 Subject: Comments translation --- src/CBot/CBotToken.cpp | 176 +++++++++++++++++++++++++------------------------ src/CBot/CBotToken.h | 12 ++-- 2 files changed, 97 insertions(+), 91 deletions(-) diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index 03a5337..c76df7f 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -12,37 +12,40 @@ // * GNU General Public License for more details. // * // * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/.////////////////////////////////////////////////////////////////// -// Gestion des Tokens -// le texte d'un programme est d'abord transform -// en une suite de tokens pour facilit l'interprtation +// * along with this program. If not, see http://www.gnu.org/licenses/. -// il faudra traiter le seul cas d'erreur possible -// qui est un caractre illgal dans une string + +//CBotToken.cpp + +////////////////////////////////////////////////////////////////// +// Managing Tokens +// the text of a program is first transformed +// into a sequence of tokens for easy interpretation +// it will only treat the case as an error +// where there is an illegal character in a string #include "CBot.h" #include CBotStringArray CBotToken::m_ListKeyWords; -int CBotToken::m_ListIdKeyWords[200]; +int CBotToken::m_ListIdKeyWords[200]; CBotStringArray CBotToken::m_ListKeyDefine; -long CBotToken::m_ListKeyNums[MAXDEFNUM]; +long CBotToken::m_ListKeyNums[MAXDEFNUM]; -// constructeurs +//! contructors CBotToken::CBotToken() { - m_next = NULL; - m_prev = NULL; - - m_type = TokenTypVar; // priori un nom d'une variable - m_IdKeyWord = -1; + m_next = NULL; + m_prev = NULL; + m_type = TokenTypVar; // at the beginning a default variable type + m_IdKeyWord = -1; } CBotToken::CBotToken(const CBotToken* pSrc) { - m_next = NULL; - m_prev = NULL; + m_next = NULL; + m_prev = NULL; m_Text.Empty(); m_Sep.Empty(); @@ -55,12 +58,13 @@ CBotToken::CBotToken(const CBotToken* pSrc) if ( pSrc != NULL ) { - m_Text = pSrc->m_Text; - m_Sep = pSrc->m_Sep; m_type = pSrc->m_type; m_IdKeyWord = pSrc->m_IdKeyWord; + m_Text = pSrc->m_Text; + m_Sep = pSrc->m_Sep; + m_start = pSrc->m_start; m_end = pSrc->m_end; } @@ -68,14 +72,14 @@ CBotToken::CBotToken(const CBotToken* pSrc) CBotToken::CBotToken(const CBotString& mot, const CBotString& sep, int start, int end) { - m_Text = mot; // mot trouv comme token - m_Sep = sep; // sparateurs qui suivent + m_Text = mot; // word (mot) found as token + m_Sep = sep; // separator m_next = NULL; m_prev = NULL; m_start = start; m_end = end; - m_type = TokenTypVar; // priori un nom d'une variable + m_type = TokenTypVar; // at the beginning a default variable type m_IdKeyWord = -1; } @@ -86,13 +90,13 @@ CBotToken::CBotToken(const char* mot, const char* sep) m_next = NULL; m_prev = NULL; - m_type = TokenTypVar; // priori un nom d'une variable + m_type = TokenTypVar; // at the beginning a default variable type m_IdKeyWord = -1; } CBotToken::~CBotToken() { - delete m_next; // rcursif + delete m_next; // recursive m_next = NULL; } @@ -121,9 +125,9 @@ const CBotToken& CBotToken::operator=(const CBotToken& src) int CBotToken::GivType() { - if (this == NULL) return 0; - if (m_type == TokenTypKeyWord) return m_IdKeyWord; - return m_type; + if (this == NULL) return 0; + if (m_type == TokenTypKeyWord) return m_IdKeyWord; + return m_type; } long CBotToken::GivIdKey() @@ -214,85 +218,85 @@ bool Char2InList(const char c, const char cc, const char* list) } static char* sep1 = " \r\n\t,:()[]{}-+*/=;>!~^|&%."; // sparateurs oprationnels -static char* num = "0123456789"; // le point (unique) est test sparment +static char* sep2 = " \r\n\t"; // only separators +static char* sep3 = ",:()[]{}-+*/=;<>!~^|&%."; // operational separators +static char* num = "0123456789"; // point (single) is tested separately static char* hexnum = "0123456789ABCDEFabcdef"; -static char* nch = "\"\r\n\t"; // refus dans les chaines +static char* nch = "\"\r\n\t"; // forbidden in chains -//static char* duo = "+=-=*=/===!=<=>=++--///**/||&&";// les oprateurs doubles +//static char* duo = "+=-=*=/===!=<=>=++--///**/||&&"; // double operators -// cherche le prochain token dans une phrase -// ne doit pas commencer par des sparateurs -// qui sont pris avec le token prcdent +// looking for the next token in a sentence +// do not start with separators +// which are made in the previous token CBotToken* CBotToken::NextToken(char* &program, int& error, bool first) { - CBotString mot; // le mot trouv - CBotString sep; // les sparateurs qui le suivent + CBotString mot; // the word which is found + CBotString sep; // separators that are after char c; bool stop = first; if (*program == 0) return NULL; - c = *(program++); // prochain caractre + c = *(program++); // next character if (!first) { - mot = c; // construit le mot - c = *(program++); // prochain caractre + mot = c; // built the word + c = *(program++); // next character - // cas particulier pour les chanes de caractres + // special case for strings if ( mot[0] == '\"' ) { while (c != 0 && !CharInList(c, nch)) { mot += c; - c = *(program++); // prochain caractre + c = *(program++); // next character if ( c == '\\' ) { - c = *(program++); // prochain caractre + c = *(program++); // next character if ( c == 'n' ) c = '\n'; if ( c == 'r' ) c = '\r'; if ( c == 't' ) c = '\t'; mot += c; - c = *(program++); // prochain caractre + c = *(program++); // next character } } if ( c == '\"' ) { - mot += c; // chane complte - c = *(program++); // prochain caractre + mot += c; // string is complete + c = *(program++); // next character } stop = true; } - // cas particulier pour les nombres + // special case for numbers if ( CharInList(mot[0], num )) { - bool bdot = false; // trouv un point ? - bool bexp = false; // trouv un exposant ? + bool bdot = false; // found a point? + bool bexp = false; // found an exponent? char* liste = num; - if (mot[0] == '0' && c == 'x') // valeur hexadcimale ? + if (mot[0] == '0' && c == 'x') // hexadecimal value? { mot += c; - c = *(program++); // prochain caractre + c = *(program++); // next character liste = hexnum; } cw: while (c != 0 && CharInList(c, liste)) { cc: mot += c; - c = *(program++); // prochain caractre + c = *(program++); // next character } - if ( liste == num ) // pas pour les exadcimaux + if ( liste == num ) // not for hexadecimal { if ( !bdot && c == '.' ) { bdot = true; goto cc; } if ( !bexp && ( c == 'e' || c == 'E' ) ) { bexp = true; mot += c; - c = *(program++); // prochain caractre + c = *(program++); // next character if ( c == '-' || c == '+' ) goto cc; goto cw; @@ -302,13 +306,13 @@ cc: mot += c; stop = true; } - if (CharInList(mot[0], sep3)) // un sparateur oprationnel ? + if (CharInList(mot[0], sep3)) // an operational separator? { CBotString motc = mot; - while (motc += c, c != 0 && GivKeyWords(motc)>0) // cherche l'oprande le plus long possible + while (motc += c, c != 0 && GivKeyWords(motc)>0) // operand seeks the longest possible { - mot += c; // construit le mot - c = *(program++); // prochain caractre + mot += c; // build the word + c = *(program++); // next character } stop = true; @@ -321,36 +325,36 @@ cc: mot += c; { if (stop || c == 0 || CharInList(c, sep1)) { - if (!first && mot.IsEmpty()) return NULL; // fin de l'analyse + if (!first && mot.IsEmpty()) return NULL; // end of the analysis bis: while (CharInList(c, sep2)) { - sep += c; // tous les sparateurs qui suivent + sep += c; // after all the separators c = *(program++); } - if (c == '/' && *program == '/') // un commentaire dans le tas ? + if (c == '/' && *program == '/') // comment on the heap? { while( c != '\n' && c != 0 ) { sep += c; - c = *(program++); // prochain caractre + c = *(program++); // next character } goto bis; } - if (c == '/' && *program == '*') // un commentaire dans le tas ? + if (c == '/' && *program == '*') // comment on the heap? { while( c != 0 && (c != '*' || *program != '/')) { sep += c; - c = *(program++); // prochain caractre + c = *(program++); // next character } if ( c != 0 ) { sep += c; - c = *(program++); // prochain caractre + c = *(program++); // next character sep += c; - c = *(program++); // prochain caractre + c = *(program++); // next character } goto bis; } @@ -365,13 +369,13 @@ bis: token->m_IdKeyWord = GivKeyWords(mot); if (token->m_IdKeyWord > 0) token->m_type = TokenTypKeyWord; - else GivKeyDefNum(mot, token) ; // traite les DefineNum + else GivKeyDefNum(mot, token) ; // treats DefineNum return token; } - mot += c; // construit le mot - c = *(program++); // prochain caractre + mot += c; // built the word + c = *(program++); // next character } } @@ -394,24 +398,24 @@ CBotToken* CBotToken::CompileTokens(const char* program, int& error) char* pp = p; while (NULL != (nxt = NextToken(p, error))) { - prv->m_next = nxt; // ajoute la suite + prv->m_next = nxt; // added after nxt->m_prev = prv; - prv = nxt; // avance + prv = nxt; // advance nxt->m_start = pos; -/* pos += nxt->m_Text.GivLength(); // la chane peut tre plus courte (BOA supprims) +/* pos += nxt->m_Text.GivLength(); // chain may be shorter (BOA deleted) nxt->m_end = pos; pos += nxt->m_Sep.GivLength();*/ - pos += (p - pp); // taille totale + pos += (p - pp); // total size nxt->m_end = pos - nxt->m_Sep.GivLength(); pp = p; } - // ajoute un token comme terminateur - // ( utile pour avoir le prcdent ) + // adds a token as a terminator + // ( useful for the previous ) nxt = new CBotToken(); nxt->m_type = 0; - prv->m_next = nxt; // ajoute la suite + prv->m_next = nxt; // added after nxt->m_prev = prv; return tokenbase; @@ -423,7 +427,7 @@ void CBotToken::Delete(CBotToken* pToken) } -// recherche si un mot fait parti des mots clefs +// search if a word is part of the keywords int CBotToken::GivKeyWords(const char* w) { @@ -432,7 +436,7 @@ int CBotToken::GivKeyWords(const char* w) if (l == 0) { - LoadKeyWords(); // prend la liste la premire fois + LoadKeyWords(); // takes the list for the first time l = m_ListKeyWords.GivSize(); } @@ -462,22 +466,25 @@ bool CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) return false; } -// reprend la liste des mots clefs dans les ressources /// \todo Fixme Figure out how this should work. + +// recreates the list of keywords and its IDs basing on some resources +// defines of TokenKey.. are in CBotDll.h + void CBotToken::LoadKeyWords() { CBotString s; int i, n = 0; - i = TokenKeyWord; + i = TokenKeyWord; //start with keywords of the language while (s.LoadString(i)) { m_ListKeyWords.Add(s); m_ListIdKeyWords[n++] = i++; } - i = TokenKeyDeclare; + i = TokenKeyDeclare; //keywords of declarations while (s.LoadString(i)) { m_ListKeyWords.Add(s); @@ -485,14 +492,14 @@ void CBotToken::LoadKeyWords() } - i = TokenKeyVal; + i = TokenKeyVal; //keywords of values while (s.LoadString(i)) { m_ListKeyWords.Add(s); m_ListIdKeyWords[n++] = i++; } - i = TokenKeyOp; + i = TokenKeyOp; //operators while (s.LoadString(i)) { m_ListKeyWords.Add(s); @@ -526,9 +533,8 @@ bool IsOfType(CBotToken* &p, int type1, int type2) } return false; } - -// idem avec un nombre indfini d'arguments -// il faut mettre un zro comme dernier argument +// Same with any number of arguments +// There must be a zero as the last argument bool IsOfTypeList(CBotToken* &p, int type1, ...) { int i = type1; diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index 62e9bf3..35a696a 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -12,15 +12,15 @@ // * GNU General Public License for more details. // * // * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/.//////////////////////////////////////////////////////////////////// -// interprteur pour le language CBot du jeu COLOBOT +// * along with this program. If not, see http://www.gnu.org/licenses/. -// un programme crit est tout d'abord transform en une liste de tokens -// avant d'aborder le compilateur proprement dit -// par exemple +// interpreter of the lanuage CBot for game COLOBOT +// writing a program is first transformed into a list of tokens +// before tackling the compiler itself +// for example // int var = 3 * ( pos.y + x ) -// est dcompos en (chaque ligne est un token) +// is decomposed into (each line is a token) // int // var // = -- cgit v1.2.3-1-g7c22 From bf3f9e1860a768b0e96a07fe4f138e332a9918ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopacki?= Date: Fri, 3 Aug 2012 00:50:25 +0200 Subject: Commentary translation --- src/CBot/CBotProgram.cpp | 2230 ++++++++++++++++++------------------ src/CBot/CBotWhile.cpp | 2860 +++++++++++++++++++++++----------------------- src/CBot/ClassFILE.cpp | 180 +-- 3 files changed, 2637 insertions(+), 2633 deletions(-) diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index bbdc350..5f1c021 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -1,1117 +1,1113 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.////////////////////////////////////////////////////////////////////// -// gestion de base d'un programme CBot - -#include "CBot.h" -#include - -CBotProgram::CBotProgram() -{ - m_Prog = NULL; - m_pRun = NULL; - m_pClass = NULL; - m_pStack = NULL; - m_pInstance = NULL; - - m_ErrorCode = 0; - m_Ident = 0; - m_bDebugDD = 0; -} - -CBotProgram::CBotProgram(CBotVar* pInstance) -{ - m_Prog = NULL; - m_pRun = NULL; - m_pClass = NULL; - m_pStack = NULL; - m_pInstance = pInstance; - - m_ErrorCode = 0; - m_Ident = 0; - m_bDebugDD = 0; -} - - -CBotProgram::~CBotProgram() -{ -// delete m_pClass; - m_pClass->Purge(); - m_pClass = NULL; - - CBotClass::FreeLock(this); - - delete m_Prog; -#if STACKMEM - m_pStack->Delete(); -#else - delete m_pStack; -#endif -} - - -bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser ) -{ - int error = 0; - Stop(); - -// delete m_pClass; - m_pClass->Purge(); // purge les anciennes dfinitions des classes - // mais sans dtruire l'object - m_pClass = NULL; - delete m_Prog; m_Prog= NULL; - - ListFonctions.SetSize(0); - m_ErrorCode = 0; - - if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) - pUser = m_pInstance->m_pUserPtr; - - // transforme le programme en Tokens - CBotToken* pBaseToken = CBotToken::CompileTokens(program, error); - if ( pBaseToken == NULL ) return false; - - - CBotCStack* pStack = new CBotCStack(NULL); - CBotToken* p = pBaseToken->GivNext(); // saute le 1er token (sparateur) - - pStack->SetBotCall(this); // dfini les routines utilisables - CBotCall::SetPUser(pUser); - - // fait une premire passe rapide juste pour prendre les enttes de routines et de classes - while ( pStack->IsOk() && p != NULL && p->GivType() != 0) - { - if ( IsOfType(p, ID_SEP) ) continue; // des point-virgules qui trainent - - if ( p->GivType() == ID_CLASS || - ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) - { - CBotClass* nxt = CBotClass::Compile1(p, pStack); - if (m_pClass == NULL ) m_pClass = nxt; - else m_pClass->AddNext(nxt); - } - else - { - CBotFunction* next = CBotFunction::Compile1(p, pStack, NULL); - if (m_Prog == NULL ) m_Prog = next; - else m_Prog->AddNext(next); - } - } - if ( !pStack->IsOk() ) - { - m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); - delete m_Prog; - m_Prog = NULL; - delete pBaseToken; - return false; - } - -// CBotFunction* temp = NULL; - CBotFunction* next = m_Prog; // reprend la liste - - p = pBaseToken->GivNext(); // revient au dbut - - while ( pStack->IsOk() && p != NULL && p->GivType() != 0 ) - { - if ( IsOfType(p, ID_SEP) ) continue; // des point-virgules qui trainent - - if ( p->GivType() == ID_CLASS || - ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) - { - m_bCompileClass = true; - CBotClass::Compile(p, pStack); // complte la dfinition de la classe - } - else - { - m_bCompileClass = false; - CBotFunction::Compile(p, pStack, next); - if (next->IsExtern()) ListFonctions.Add(next->GivName()/* + next->GivParams()*/); - next->m_pProg = this; // garde le pointeur au module - next = next->Next(); - } - } - -// delete m_Prog; // la liste de la 1re passe -// m_Prog = temp; // la liste de la seconde passe - - if ( !pStack->IsOk() ) - { - m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); - delete m_Prog; - m_Prog = NULL; - } - - delete pBaseToken; - delete pStack; - - return (m_Prog != NULL); -} - - -bool CBotProgram::Start(const char* name) -{ -#if STACKMEM - m_pStack->Delete(); -#else - delete m_pStack; -#endif - m_pStack = NULL; - - m_pRun = m_Prog; - while (m_pRun != NULL) - { - if ( m_pRun->GivName() == name ) break; - m_pRun = m_pRun->m_next; - } - - if ( m_pRun == NULL ) - { - m_ErrorCode = TX_NORUN; - return false; - } - -#if STACKMEM - m_pStack = CBotStack::FirstStack(); -#else - m_pStack = new CBotStack(NULL); // cre une pile d'excution -#endif - - m_pStack->SetBotCall(this); // bases pour les routines - - return true; // on est prt pour un Run() -} - -bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop) -{ - CBotFunction* p = m_Prog; - while (p != NULL) - { - if ( p->GivName() == name ) break; - p = p->m_next; - } - - if ( p == NULL ) return false; - - p->GetPosition(start, stop, modestart, modestop); - return true; -} - -bool CBotProgram::Run(void* pUser, int timer) -{ - bool ok; - - if (m_pStack == NULL || m_pRun == NULL) goto error; - - m_ErrorCode = 0; - if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) - pUser = m_pInstance->m_pUserPtr; - - m_pStack->Reset(pUser); // vide l'ventuelle erreur prcdente, et remet le timer - if ( timer >= 0 ) m_pStack->SetTimer(timer); - - m_pStack->SetBotCall(this); // bases pour les routines - -#if STACKRUN - // reprend l'excution sur le haut de la pile - ok = m_pStack->Execute(); - if ( ok ) - { -#ifdef _DEBUG - CBotVar* ppVar[3]; - ppVar[0] = CBotVar::Create("aa", CBotTypInt); - ppVar[1] = CBotVar::Create("bb", CBotTypInt); - ppVar[2] = NULL; - ok = m_pRun->Execute(ppVar, m_pStack, m_pInstance); -#else - // revient sur l'excution normale - ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); -#endif - } -#else - ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); -#endif - - // termin sur une erreur ? - if (!ok && !m_pStack->IsOk()) - { - m_ErrorCode = m_pStack->GivError(m_ErrorStart, m_ErrorEnd); -#if STACKMEM - m_pStack->Delete(); -#else - delete m_pStack; -#endif - m_pStack = NULL; - return true; // excution termine !! - } - - if ( ok ) m_pRun = NULL; // plus de fonction en excution - return ok; - -error: - m_ErrorCode = TX_NORUN; - return true; -} - -void CBotProgram::Stop() -{ -#if STACKMEM - m_pStack->Delete(); -#else - delete m_pStack; -#endif - m_pStack = NULL; - m_pRun = NULL; -} - - - -bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) -{ - FunctionName = NULL; - start = end = 0; - if (m_pStack == NULL) return false; - - m_pStack->GetRunPos(FunctionName, start, end); - return true; -} - -CBotVar* CBotProgram::GivStackVars(const char* &FunctionName, int level) -{ - FunctionName = NULL; - if (m_pStack == NULL) return NULL; - - return m_pStack->GivStackVars(FunctionName, level); -} - - - - - - - -void CBotProgram::SetTimer(int n) -{ - CBotStack::SetTimer( n ); -} - -int CBotProgram::GivError() -{ - return m_ErrorCode; -} - -void CBotProgram::SetIdent(long n) -{ - m_Ident = n; -} - -long CBotProgram::GivIdent() -{ - return m_Ident; -} - -bool CBotProgram::GetError(int& code, int& start, int& end) -{ - code = m_ErrorCode; - start = m_ErrorStart; - end = m_ErrorEnd; - return code > 0; -} - -bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) -{ - code = m_ErrorCode; - start = m_ErrorStart; - end = m_ErrorEnd; - pProg = this; - return code > 0; -} - -CBotString CBotProgram::GivErrorText(int code) -{ - CBotString TextError; - - TextError.LoadString( code ); - if (TextError.IsEmpty()) - { - char buf[100]; - sprintf(buf, "Exception numro %d.", code); - TextError = buf; - } - return TextError; -} - - -CBotFunction* CBotProgram::GivFunctions() -{ - return m_Prog; -} - -bool CBotProgram::AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) -{ - // mmorise les pointeurs aux deux fonctions - return CBotCall::AddFunction(name, rExec, rCompile); -} - - -bool WriteWord(FILE* pf, unsigned short w) -{ - size_t lg; - - lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); - - return (lg == 1); -} - -bool ReadWord(FILE* pf, unsigned short& w) -{ - size_t lg; - - lg = fread(&w, sizeof( unsigned short ), 1, pf ); - - return (lg == 1); -} - -bool WriteFloat(FILE* pf, float w) -{ - size_t lg; - - lg = fwrite(&w, sizeof( float ), 1, pf ); - - return (lg == 1); -} - -bool ReadFloat(FILE* pf, float& w) -{ - size_t lg; - - lg = fread(&w, sizeof( float ), 1, pf ); - - return (lg == 1); -} - -bool WriteLong(FILE* pf, long w) -{ - size_t lg; - - lg = fwrite(&w, sizeof( long ), 1, pf ); - - return (lg == 1); -} - -bool ReadLong(FILE* pf, long& w) -{ - size_t lg; - - lg = fread(&w, sizeof( long ), 1, pf ); - - return (lg == 1); -} - -bool WriteString(FILE* pf, CBotString s) -{ - size_t lg1, lg2; - - lg1 = s.GivLength(); - if (!WriteWord(pf, lg1)) return false; - - lg2 = fwrite(s, 1, lg1, pf ); - return (lg1 == lg2); -} - -bool ReadString(FILE* pf, CBotString& s) -{ - unsigned short w; - char buf[1000]; - size_t lg1, lg2; - - if (!ReadWord(pf, w)) return false; - lg1 = w; - lg2 = fread(buf, 1, lg1, pf ); - buf[lg2] = 0; - - s = buf; - return (lg1 == lg2); -} - -bool WriteType(FILE* pf, CBotTypResult type) -{ - int typ = type.GivType(); - if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; - if ( !WriteWord(pf, typ) ) return false; - if ( typ == CBotTypClass ) - { - CBotClass* p = type.GivClass(); - if ( !WriteString(pf, p->GivName()) ) return false; - } - if ( type.Eq( CBotTypArrayBody ) || - type.Eq( CBotTypArrayPointer ) ) - { - if ( !WriteWord(pf, type.GivLimite()) ) return false; - if ( !WriteType(pf, type.GivTypElem()) ) return false; - } - return true; -} - -bool ReadType(FILE* pf, CBotTypResult& type) -{ - unsigned short w, ww; - if ( !ReadWord(pf, w) ) return false; - type.SetType(w); - - if ( type.Eq( CBotTypIntrinsic ) ) - { - type = CBotTypResult( w, "point" ); - } - - if ( type.Eq( CBotTypClass ) ) - { - CBotString s; - if ( !ReadString(pf, s) ) return false; - type = CBotTypResult( w, s ); - } - - if ( type.Eq( CBotTypArrayPointer ) || - type.Eq( CBotTypArrayBody ) ) - { - CBotTypResult r; - if ( !ReadWord(pf, ww) ) return false; - if ( !ReadType(pf, r) ) return false; - type = CBotTypResult( w, r ); - type.SetLimite((short)ww); - } - return true; -} - - -bool CBotProgram::DefineNum(const char* name, long val) -{ - return CBotToken::DefineNum(name, val); -} - - -bool CBotProgram::SaveState(FILE* pf) -{ - if (!WriteWord( pf, CBOTVERSION)) return false; - - - if ( m_pStack != NULL ) - { - if (!WriteWord( pf, 1)) return false; - if (!WriteString( pf, m_pRun->GivName() )) return false; - if (!m_pStack->SaveState(pf)) return false; - } - else - { - if (!WriteWord( pf, 0)) return false; - } - return true; -} - - -bool CBotProgram::RestoreState(FILE* pf) -{ - unsigned short w; - CBotString s; - - Stop(); - - if (!ReadWord( pf, w )) return false; - if ( w != CBOTVERSION ) return false; - - if (!ReadWord( pf, w )) return false; - if ( w == 0 ) return true; - - if (!ReadString( pf, s )) return false; - Start(s); // point de reprise - -#if STACKMEM - m_pStack->Delete(); -#else - delete m_pStack; -#endif - m_pStack = NULL; - - // rcupre la pile depuis l'enregistrement - // utilise un pointeur NULL (m_pStack) mais c'est ok comme a - if (!m_pStack->RestoreState(pf, m_pStack)) return false; - m_pStack->SetBotCall(this); // bases pour les routines - - // rtabli certains tats dans la pile selon la structure - m_pRun->RestoreState(NULL, m_pStack, m_pInstance); - return true; -} - -int CBotProgram::GivVersion() -{ - return CBOTVERSION; -} - - -////////////////////////////////////////////////////////////////////////////////////////////////////// - -CBotCall* CBotCall::m_ListCalls = NULL; - -CBotCall::CBotCall(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) -{ - m_name = name; - m_rExec = rExec; - m_rComp = rCompile; - m_next = NULL; - m_nFuncIdent = CBotVar::NextUniqNum(); -} - -CBotCall::~CBotCall() -{ - if (m_next) delete m_next; - m_next = NULL; -} - -void CBotCall::Free() -{ - delete CBotCall::m_ListCalls; -} - -bool CBotCall::AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) -{ - CBotCall* p = m_ListCalls; - CBotCall* pp = NULL; - - if ( p != NULL ) while ( p->m_next != NULL ) - { - if ( p->GivName() == name ) - { - // libre une fonction qu'on redfini - if ( pp ) pp->m_next = p->m_next; - else m_ListCalls = p->m_next; - pp = p; - p = p->m_next; - pp->m_next = NULL; // ne pas dtruire la suite de la liste - delete pp; - continue; - } - pp = p; // pointeur prcdent - p = p->m_next; - } - - pp = new CBotCall(name, rExec, rCompile); - - if (p) p->m_next = pp; - else m_ListCalls = pp; - - return true; -} - - -// transforme le tableau de pointeurs aux variables -// en une liste de variables chanes -CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false) -{ - int i = 0; - CBotVar* pVar = NULL; - - while( true ) - { - ppVars[i]; - if ( ppVars[i] == NULL ) break; - - CBotVar* pp = CBotVar::Create(ppVars[i]); - if (bSetVal) pp->Copy(ppVars[i]); - else - if ( ppVars[i]->GivType() == CBotTypPointer ) - pp->SetClass( ppVars[i]->GivClass()); -// copier le pointeur selon indirection - if (pVar == NULL) pVar = pp; - else pVar->AddNext(pp); - i++; - } - return pVar; -} - -// trouve un appel acceptable selon le nom de la procdure -// et les paramtres donns - -CBotTypResult CBotCall::CompileCall(CBotToken* &p, CBotVar** ppVar, CBotCStack* pStack, long& nIdent) -{ - nIdent = 0; - CBotCall* pt = m_ListCalls; - CBotString name = p->GivString(); - - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - CBotVar* pVar = MakeListVars(ppVar); - CBotVar* pVar2 = pVar; - CBotTypResult r = pt->m_rComp(pVar2, m_pUser); - int ret = r.GivType(); - - // si une classe est retourne, c'est en fait un pointeur - if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer ); - - if ( ret > 20 ) - { - if (pVar2) pStack->SetError(ret, p /*pVar2->GivToken()*/ ); - } - delete pVar; - nIdent = pt->m_nFuncIdent; - return r; - } - pt = pt->m_next; - } - return -1; -} - -void* CBotCall::m_pUser = NULL; - -void CBotCall::SetPUser(void* pUser) -{ - m_pUser = pUser; -} - -bool CBotCall::CheckCall(const char* name) -{ - CBotCall* p = m_ListCalls; - - while ( p != NULL ) - { - if ( name == p->GivName() ) return true; - p = p->m_next; - } - return false; -} - - - -CBotString CBotCall::GivName() -{ - return m_name; -} - -CBotCall* CBotCall::Next() -{ - return m_next; -} - - -int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype) -{ - CBotCall* pt = m_ListCalls; - - if ( nIdent ) while ( pt != NULL ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - goto fund; - } - pt = pt->m_next; - } - - pt = m_ListCalls; - - if ( token != NULL ) - { - CBotString name = token->GivString(); - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - nIdent = pt->m_nFuncIdent; - goto fund; - } - pt = pt->m_next; - } - } - - return -1; - -fund: -#if !STACKRUN - // fait la liste des paramtres selon le contenu de la pile (pStackVar) - - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVarToDelete = pVar; - - // cre une variable pour le rsultat - CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); - - CBotVar* pRes = pResult; - int Exception = 0; - int res = pt->m_rExec(pVar, pResult, Exception, pStack->GivPUser()); - - if ( pResult != pRes ) delete pRes; // si rsultat diffrent rendu - delete pVarToDelete; - - if (res == false) - { - if (Exception!=0) - { - pStack->SetError(Exception, token); - } - delete pResult; - return false; - } - pStack->SetVar(pResult); - - if ( rettype.GivType() > 0 && pResult == NULL ) - { - pStack->SetError(TX_NORETVAL, token); - } - nIdent = pt->m_nFuncIdent; - return true; - -#else - - CBotStack* pile = pStack->AddStackEOX(pt); - if ( pile == EOX ) return true; - - // fait la liste des paramtres selon le contenu de la pile (pStackVar) - - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVarToDelete = pVar; - - // cre une variable pour le rsultat - CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); - - pile->SetVar( pVar ); - - CBotStack* pile2 = pile->AddStack(); - pile2->SetVar( pResult ); - - pile->SetError(0, token); // pour la position en cas d'erreur + loin - return pt->Run( pStack ); - -#endif - -} - -#if STACKRUN - -bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) -{ - CBotCall* pt = m_ListCalls; - - { - CBotString name = token->GivString(); - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - nIdent = pt->m_nFuncIdent; - - CBotStack* pile = pStack->RestoreStackEOX(pt); - if ( pile == NULL ) return true; - - CBotStack* pile2 = pile->RestoreStack(); - return true; - } - pt = pt->m_next; - } - } - - return false; -} - -bool CBotCall::Run(CBotStack* pStack) -{ - CBotStack* pile = pStack->AddStackEOX(this); - if ( pile == EOX ) return true; - CBotVar* pVar = pile->GivVar(); - - CBotStack* pile2 = pile->AddStack(); - CBotVar* pResult = pile2->GivVar(); - CBotVar* pRes = pResult; - - int Exception = 0; - int res = m_rExec(pVar, pResult, Exception, pStack->GivPUser()); - - if (res == false) - { - if (Exception!=0) - { - pStack->SetError(Exception); - } - if ( pResult != pRes ) delete pResult; // si rsultat diffrent rendu - return false; - } - - if ( pResult != NULL ) pStack->SetCopyVar( pResult ); - if ( pResult != pRes ) delete pResult; // si rsultat diffrent rendu - - return true; -} - -#endif - -/////////////////////////////////////////////////////////////////////////////////////// - -CBotCallMethode::CBotCallMethode(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) -{ - m_name = name; - m_rExec = rExec; - m_rComp = rCompile; - m_next = NULL; - m_nFuncIdent = CBotVar::NextUniqNum(); -} - -CBotCallMethode::~CBotCallMethode() -{ - delete m_next; - m_next = NULL; -} - -// trouve un appel acceptable selon le nom de la procdure -// et les paramtres donns - -CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, - CBotVar** ppVar, CBotCStack* pStack, - long& nIdent) -{ - CBotCallMethode* pt = this; - nIdent = 0; - - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVar2 = pVar; - CBotTypResult r = pt->m_rComp(pThis, pVar2); - int ret = r.GivType(); - if ( ret > 20 ) - { - if (pVar2) pStack->SetError(ret, pVar2->GivToken()); - } - delete pVar; - nIdent = pt->m_nFuncIdent; - return r; - } - pt = pt->m_next; - } - return CBotTypResult(-1); -} - - -CBotString CBotCallMethode::GivName() -{ - return m_name; -} - -CBotCallMethode* CBotCallMethode::Next() -{ - return m_next; -} - -void CBotCallMethode::AddNext(CBotCallMethode* pt) -{ - CBotCallMethode* p = this; - while ( p->m_next != NULL ) p = p->m_next; - - p->m_next = pt; -} - - -int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken) -{ - CBotCallMethode* pt = this; - - // recherche selon l'identificateur - - if ( nIdent ) while ( pt != NULL ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - // fait la liste des paramtres selon le contenu de la pile (pStackVar) - - CBotVar* pVar = MakeListVars(ppVars, true); - CBotVar* pVarToDelete = pVar; - - // puis appelle la routine externe au module - - int Exception = 0; - int res = pt->m_rExec(pThis, pVar, pResult, Exception); - pStack->SetVar(pResult); - - if (res == false) - { - if (Exception!=0) - { -// pStack->SetError(Exception, pVar->GivToken()); - pStack->SetError(Exception, pToken); - } - delete pVarToDelete; - return false; - } - delete pVarToDelete; - return true; - } - pt = pt->m_next; - } - - // recherche selon le nom - - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - // fait la liste des paramtres selon le contenu de la pile (pStackVar) - - CBotVar* pVar = MakeListVars(ppVars, true); - CBotVar* pVarToDelete = pVar; - - int Exception = 0; - int res = pt->m_rExec(pThis, pVar, pResult, Exception); - pStack->SetVar(pResult); - - if (res == false) - { - if (Exception!=0) - { -// pStack->SetError(Exception, pVar->GivToken()); - pStack->SetError(Exception, pToken); - } - delete pVarToDelete; - return false; - } - delete pVarToDelete; - nIdent = pt->m_nFuncIdent; - return true; - } - pt = pt->m_next; - } - - return -1; -} - -bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - if ( pVar == NULL ) return TX_LOWPARAM; - - int i = 0; - pVar = pVar->GivItemList(); - - while ( pVar != NULL ) - { - i++; - pVar = pVar->GivNext(); - } - - pResult->SetValInt(i); - return true; -} - -CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) -{ - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - if ( pVar->GivType() != CBotTypArrayPointer ) - return CBotTypResult( TX_BADPARAM ); - return CBotTypResult( CBotTypInt ); -} - - -CBotString CBotProgram::m_DebugVarStr = ""; - -bool rCBotDebug( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - pResult->SetValString( CBotProgram::m_DebugVarStr ); - - return true; -} - -CBotTypResult cCBotDebug( CBotVar* &pVar, void* pUser ) -{ - // pas de paramtre - if ( pVar != NULL ) return CBotTypResult( TX_OVERPARAM ); - - // la fonction retourne un rsultat "string" - return CBotTypResult( CBotTypString ); -} - - -#include "StringFunctions.cpp" - -void CBotProgram::Init() -{ - CBotToken::DefineNum( "CBotErrOpenPar", 5000) ; // manque la parenthse ouvrante - CBotToken::DefineNum( "CBotErrClosePar", 5001) ; // manque la parenthse fermante - CBotToken::DefineNum( "CBotErrNotBoolean", 5002) ; // l'expression doit tre un boolean - CBotToken::DefineNum( "CBotErrUndefVar", 5003) ; // variable non dclare - CBotToken::DefineNum( "CBotErrBadLeft", 5004) ; // assignation impossible ( 5 = ... ) - CBotToken::DefineNum( "CBotErrNoTerminator", 5005) ;// point-virgule attendu - CBotToken::DefineNum( "CBotErrCaseOut", 5006) ; // case en dehors d'un switch - CBotToken::DefineNum( "CBotErrCloseBlock", 5008) ; // manque " } " - CBotToken::DefineNum( "CBotErrElseWhitoutIf", 5009) ;// else sans if correspondant - CBotToken::DefineNum( "CBotErrOpenBlock", 5010) ; // manque " { " - CBotToken::DefineNum( "CBotErrBadType1", 5011) ; // mauvais type pour l'assignation - CBotToken::DefineNum( "CBotErrRedefVar", 5012) ; // redfinition de la variable - CBotToken::DefineNum( "CBotErrBadType2", 5013) ; // 2 oprandes de type incompatibles - CBotToken::DefineNum( "CBotErrUndefCall", 5014) ; // routine inconnue - CBotToken::DefineNum( "CBotErrNoDoubleDots", 5015) ;// " : " attendu - CBotToken::DefineNum( "CBotErrBreakOutside", 5017) ;// break en dehors d'une boucle - CBotToken::DefineNum( "CBotErrUndefLabel", 5019) ; // label inconnu - CBotToken::DefineNum( "CBotErrLabel", 5018) ; // label ne peut se mettre ici - CBotToken::DefineNum( "CBotErrNoCase", 5020) ; // manque " case " - CBotToken::DefineNum( "CBotErrBadNum", 5021) ; // nombre attendu - CBotToken::DefineNum( "CBotErrVoid", 5022) ; // " void " pas possible ici - CBotToken::DefineNum( "CBotErrNoType", 5023) ; // dclaration de type attendue - CBotToken::DefineNum( "CBotErrNoVar", 5024) ; // nom de variable attendu - CBotToken::DefineNum( "CBotErrNoFunc", 5025) ; // nom de fonction attendu - CBotToken::DefineNum( "CBotErrOverParam", 5026) ; // trop de paramtres - CBotToken::DefineNum( "CBotErrRedefFunc", 5027) ; // cette fonction existe dj - CBotToken::DefineNum( "CBotErrLowParam", 5028) ; // pas assez de paramtres - CBotToken::DefineNum( "CBotErrBadParam", 5029) ; // mauvais types de paramtres - CBotToken::DefineNum( "CBotErrNbParam", 5030) ; // mauvais nombre de paramtres - CBotToken::DefineNum( "CBotErrUndefItem", 5031) ; // lment n'existe pas dans la classe - CBotToken::DefineNum( "CBotErrUndefClass", 5032) ; // variable n'est pas une classe - CBotToken::DefineNum( "CBotErrNoConstruct", 5033) ; // pas de constructeur appropri - CBotToken::DefineNum( "CBotErrRedefClass", 5034) ; // classe existe dj - CBotToken::DefineNum( "CBotErrCloseIndex", 5035) ; // " ] " attendu - CBotToken::DefineNum( "CBotErrReserved", 5036) ; // mot rserv (par un DefineNum) - -// voici la liste des erreurs pouvant tre retournes par le module -// pour l'excution - - CBotToken::DefineNum( "CBotErrZeroDiv", 6000) ; // division par zro - CBotToken::DefineNum( "CBotErrNotInit", 6001) ; // variable non initialise - CBotToken::DefineNum( "CBotErrBadThrow", 6002) ; // throw d'une valeur ngative - CBotToken::DefineNum( "CBotErrNoRetVal", 6003) ; // fonction n'a pas retourn de rsultat - CBotToken::DefineNum( "CBotErrNoRun", 6004) ; // Run() sans fonction active - CBotToken::DefineNum( "CBotErrUndefFunc", 6005) ; // appel d'une fonction qui n'existe plus - - CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); - - InitStringFunctions(); - - // une fonction juste pour les debug divers - CBotProgram::AddFunction("CBOTDEBUGDD", rCBotDebug, cCBotDebug); - //TODO implement this deletion - // DeleteFile("CbotDebug.txt"); - -} - -void CBotProgram::Free() -{ - CBotToken::Free() ; - CBotCall ::Free() ; - CBotClass::Free() ; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +////////////////////////////////////////////////////////////////////// +// database management of CBoT program + +#include "CBot.h" +#include + +CBotProgram::CBotProgram() +{ + m_Prog = NULL; + m_pRun = NULL; + m_pClass = NULL; + m_pStack = NULL; + m_pInstance = NULL; + + m_ErrorCode = 0; + m_Ident = 0; + m_bDebugDD = 0; +} + +CBotProgram::CBotProgram(CBotVar* pInstance) +{ + m_Prog = NULL; + m_pRun = NULL; + m_pClass = NULL; + m_pStack = NULL; + m_pInstance = pInstance; + + m_ErrorCode = 0; + m_Ident = 0; + m_bDebugDD = 0; +} + + +CBotProgram::~CBotProgram() +{ +// delete m_pClass; + m_pClass->Purge(); + m_pClass = NULL; + + CBotClass::FreeLock(this); + + delete m_Prog; +#if STACKMEM + m_pStack->Delete(); +#else + delete m_pStack; +#endif +} + + +bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser ) +{ + int error = 0; + Stop(); + +// delete m_pClass; + m_pClass->Purge(); // purge the old definitions of classes + // but without destroying the object + m_pClass = NULL; + delete m_Prog; m_Prog= NULL; + + ListFonctions.SetSize(0); + m_ErrorCode = 0; + + if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) + pUser = m_pInstance->m_pUserPtr; + + // transforms the program in Tokens + CBotToken* pBaseToken = CBotToken::CompileTokens(program, error); + if ( pBaseToken == NULL ) return false; + + + CBotCStack* pStack = new CBotCStack(NULL); + CBotToken* p = pBaseToken->GivNext(); // skips the first token (separator) + + pStack->SetBotCall(this); // defined used routines + CBotCall::SetPUser(pUser); + + // first made a quick pass just to take the headers of routines and classes + while ( pStack->IsOk() && p != NULL && p->GivType() != 0) + { + if ( IsOfType(p, ID_SEP) ) continue; // semicolons lurking + + if ( p->GivType() == ID_CLASS || + ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) + { + CBotClass* nxt = CBotClass::Compile1(p, pStack); + if (m_pClass == NULL ) m_pClass = nxt; + else m_pClass->AddNext(nxt); + } + else + { + CBotFunction* next = CBotFunction::Compile1(p, pStack, NULL); + if (m_Prog == NULL ) m_Prog = next; + else m_Prog->AddNext(next); + } + } + if ( !pStack->IsOk() ) + { + m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); + delete m_Prog; + m_Prog = NULL; + delete pBaseToken; + return false; + } + +// CBotFunction* temp = NULL; + CBotFunction* next = m_Prog; // rewind the list + + p = pBaseToken->GivNext(); // returns to the beginning + + while ( pStack->IsOk() && p != NULL && p->GivType() != 0 ) + { + if ( IsOfType(p, ID_SEP) ) continue; // semicolons lurking + + if ( p->GivType() == ID_CLASS || + ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) + { + m_bCompileClass = true; + CBotClass::Compile(p, pStack); // completes the definition of the class + } + else + { + m_bCompileClass = false; + CBotFunction::Compile(p, pStack, next); + if (next->IsExtern()) ListFonctions.Add(next->GivName()/* + next->GivParams()*/); + next->m_pProg = this; // keeps pointers to the module + next = next->Next(); + } + } + +// delete m_Prog; // the list of first pass +// m_Prog = temp; // list of the second pass + + if ( !pStack->IsOk() ) + { + m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); + delete m_Prog; + m_Prog = NULL; + } + + delete pBaseToken; + delete pStack; + + return (m_Prog != NULL); +} + + +bool CBotProgram::Start(const char* name) +{ +#if STACKMEM + m_pStack->Delete(); +#else + delete m_pStack; +#endif + m_pStack = NULL; + + m_pRun = m_Prog; + while (m_pRun != NULL) + { + if ( m_pRun->GivName() == name ) break; + m_pRun = m_pRun->m_next; + } + + if ( m_pRun == NULL ) + { + m_ErrorCode = TX_NORUN; + return false; + } + +#if STACKMEM + m_pStack = CBotStack::FirstStack(); +#else + m_pStack = new CBotStack(NULL); // creates an execution stack +#endif + + m_pStack->SetBotCall(this); // bases for routines + + return true; // we are ready for Run () +} + +bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop) +{ + CBotFunction* p = m_Prog; + while (p != NULL) + { + if ( p->GivName() == name ) break; + p = p->m_next; + } + + if ( p == NULL ) return false; + + p->GetPosition(start, stop, modestart, modestop); + return true; +} + +bool CBotProgram::Run(void* pUser, int timer) +{ + bool ok; + + if (m_pStack == NULL || m_pRun == NULL) goto error; + + m_ErrorCode = 0; + if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) + pUser = m_pInstance->m_pUserPtr; + + m_pStack->Reset(pUser); // empty the possible previous error, and resets the timer + if ( timer >= 0 ) m_pStack->SetTimer(timer); + + m_pStack->SetBotCall(this); // bases for routines + +#if STACKRUN + // resumes execution on the top of the stack + ok = m_pStack->Execute(); + if ( ok ) + { +#ifdef _DEBUG + CBotVar* ppVar[3]; + ppVar[0] = CBotVar::Create("aa", CBotTypInt); + ppVar[1] = CBotVar::Create("bb", CBotTypInt); + ppVar[2] = NULL; + ok = m_pRun->Execute(ppVar, m_pStack, m_pInstance); +#else + // returns to normal execution + ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); +#endif + } +#else + ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); +#endif + + // completed on a mistake? + if (!ok && !m_pStack->IsOk()) + { + m_ErrorCode = m_pStack->GivError(m_ErrorStart, m_ErrorEnd); +#if STACKMEM + m_pStack->Delete(); +#else + delete m_pStack; +#endif + m_pStack = NULL; + return true; // execution is finished! + } + + if ( ok ) m_pRun = NULL; // more function in execution + return ok; + +error: + m_ErrorCode = TX_NORUN; + return true; +} + +void CBotProgram::Stop() +{ +#if STACKMEM + m_pStack->Delete(); +#else + delete m_pStack; +#endif + m_pStack = NULL; + m_pRun = NULL; +} + + + +bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) +{ + FunctionName = NULL; + start = end = 0; + if (m_pStack == NULL) return false; + + m_pStack->GetRunPos(FunctionName, start, end); + return true; +} + +CBotVar* CBotProgram::GivStackVars(const char* &FunctionName, int level) +{ + FunctionName = NULL; + if (m_pStack == NULL) return NULL; + + return m_pStack->GivStackVars(FunctionName, level); +} + +void CBotProgram::SetTimer(int n) +{ + CBotStack::SetTimer( n ); +} + +int CBotProgram::GivError() +{ + return m_ErrorCode; +} + +void CBotProgram::SetIdent(long n) +{ + m_Ident = n; +} + +long CBotProgram::GivIdent() +{ + return m_Ident; +} + +bool CBotProgram::GetError(int& code, int& start, int& end) +{ + code = m_ErrorCode; + start = m_ErrorStart; + end = m_ErrorEnd; + return code > 0; +} + +bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) +{ + code = m_ErrorCode; + start = m_ErrorStart; + end = m_ErrorEnd; + pProg = this; + return code > 0; +} + +CBotString CBotProgram::GivErrorText(int code) +{ + CBotString TextError; + + TextError.LoadString( code ); + if (TextError.IsEmpty()) + { + char buf[100]; + sprintf(buf, "Exception numéro %d.", code); + TextError = buf; + } + return TextError; +} + + +CBotFunction* CBotProgram::GivFunctions() +{ + return m_Prog; +} + +bool CBotProgram::AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) +{ + // stores pointers to the two functions + return CBotCall::AddFunction(name, rExec, rCompile); +} + + +bool WriteWord(FILE* pf, unsigned short w) +{ + size_t lg; + + lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); + + return (lg == 1); +} + +bool ReadWord(FILE* pf, unsigned short& w) +{ + size_t lg; + + lg = fread(&w, sizeof( unsigned short ), 1, pf ); + + return (lg == 1); +} + +bool WriteFloat(FILE* pf, float w) +{ + size_t lg; + + lg = fwrite(&w, sizeof( float ), 1, pf ); + + return (lg == 1); +} + +bool ReadFloat(FILE* pf, float& w) +{ + size_t lg; + + lg = fread(&w, sizeof( float ), 1, pf ); + + return (lg == 1); +} + +bool WriteLong(FILE* pf, long w) +{ + size_t lg; + + lg = fwrite(&w, sizeof( long ), 1, pf ); + + return (lg == 1); +} + +bool ReadLong(FILE* pf, long& w) +{ + size_t lg; + + lg = fread(&w, sizeof( long ), 1, pf ); + + return (lg == 1); +} + +bool WriteString(FILE* pf, CBotString s) +{ + size_t lg1, lg2; + + lg1 = s.GivLength(); + if (!WriteWord(pf, lg1)) return false; + + lg2 = fwrite(s, 1, lg1, pf ); + return (lg1 == lg2); +} + +bool ReadString(FILE* pf, CBotString& s) +{ + unsigned short w; + char buf[1000]; + size_t lg1, lg2; + + if (!ReadWord(pf, w)) return false; + lg1 = w; + lg2 = fread(buf, 1, lg1, pf ); + buf[lg2] = 0; + + s = buf; + return (lg1 == lg2); +} + +bool WriteType(FILE* pf, CBotTypResult type) +{ + int typ = type.GivType(); + if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; + if ( !WriteWord(pf, typ) ) return false; + if ( typ == CBotTypClass ) + { + CBotClass* p = type.GivClass(); + if ( !WriteString(pf, p->GivName()) ) return false; + } + if ( type.Eq( CBotTypArrayBody ) || + type.Eq( CBotTypArrayPointer ) ) + { + if ( !WriteWord(pf, type.GivLimite()) ) return false; + if ( !WriteType(pf, type.GivTypElem()) ) return false; + } + return true; +} + +bool ReadType(FILE* pf, CBotTypResult& type) +{ + unsigned short w, ww; + if ( !ReadWord(pf, w) ) return false; + type.SetType(w); + + if ( type.Eq( CBotTypIntrinsic ) ) + { + type = CBotTypResult( w, "point" ); + } + + if ( type.Eq( CBotTypClass ) ) + { + CBotString s; + if ( !ReadString(pf, s) ) return false; + type = CBotTypResult( w, s ); + } + + if ( type.Eq( CBotTypArrayPointer ) || + type.Eq( CBotTypArrayBody ) ) + { + CBotTypResult r; + if ( !ReadWord(pf, ww) ) return false; + if ( !ReadType(pf, r) ) return false; + type = CBotTypResult( w, r ); + type.SetLimite((short)ww); + } + return true; +} + + +bool CBotProgram::DefineNum(const char* name, long val) +{ + return CBotToken::DefineNum(name, val); +} + + +bool CBotProgram::SaveState(FILE* pf) +{ + if (!WriteWord( pf, CBOTVERSION)) return false; + + + if ( m_pStack != NULL ) + { + if (!WriteWord( pf, 1)) return false; + if (!WriteString( pf, m_pRun->GivName() )) return false; + if (!m_pStack->SaveState(pf)) return false; + } + else + { + if (!WriteWord( pf, 0)) return false; + } + return true; +} + + +bool CBotProgram::RestoreState(FILE* pf) +{ + unsigned short w; + CBotString s; + + Stop(); + + if (!ReadWord( pf, w )) return false; + if ( w != CBOTVERSION ) return false; + + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) return true; + + if (!ReadString( pf, s )) return false; + Start(s); // point de reprise + +#if STACKMEM + m_pStack->Delete(); +#else + delete m_pStack; +#endif + m_pStack = NULL; + + // retrieves the stack from the memory + // uses a NULL pointer (m_pStack) but it's ok like that + if (!m_pStack->RestoreState(pf, m_pStack)) return false; + m_pStack->SetBotCall(this); // bases for routines + + // restored some states in the stack according to the structure + m_pRun->RestoreState(NULL, m_pStack, m_pInstance); + return true; +} + +int CBotProgram::GivVersion() +{ + return CBOTVERSION; +} + + +////////////////////////////////////////////////////////////////////////////////////////////////////// + +CBotCall* CBotCall::m_ListCalls = NULL; + +CBotCall::CBotCall(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) +{ + m_name = name; + m_rExec = rExec; + m_rComp = rCompile; + m_next = NULL; + m_nFuncIdent = CBotVar::NextUniqNum(); +} + +CBotCall::~CBotCall() +{ + if (m_next) delete m_next; + m_next = NULL; +} + +void CBotCall::Free() +{ + delete CBotCall::m_ListCalls; +} + +bool CBotCall::AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) +{ + CBotCall* p = m_ListCalls; + CBotCall* pp = NULL; + + if ( p != NULL ) while ( p->m_next != NULL ) + { + if ( p->GivName() == name ) + { + // frees redefined function + if ( pp ) pp->m_next = p->m_next; + else m_ListCalls = p->m_next; + pp = p; + p = p->m_next; + pp->m_next = NULL; // not to destroy the following list + delete pp; + continue; + } + pp = p; // previous pointer + p = p->m_next; + } + + pp = new CBotCall(name, rExec, rCompile); + + if (p) p->m_next = pp; + else m_ListCalls = pp; + + return true; +} + + +// transforms the array of pointers to variables +// in a chained list of variables +CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false) +{ + int i = 0; + CBotVar* pVar = NULL; + + while( true ) + { + ppVars[i]; + if ( ppVars[i] == NULL ) break; + + CBotVar* pp = CBotVar::Create(ppVars[i]); + if (bSetVal) pp->Copy(ppVars[i]); + else + if ( ppVars[i]->GivType() == CBotTypPointer ) + pp->SetClass( ppVars[i]->GivClass()); +// copy the pointer according to indirections + if (pVar == NULL) pVar = pp; + else pVar->AddNext(pp); + i++; + } + return pVar; +} + +// is acceptable by a call procedure name +// and given parameters + +CBotTypResult CBotCall::CompileCall(CBotToken* &p, CBotVar** ppVar, CBotCStack* pStack, long& nIdent) +{ + nIdent = 0; + CBotCall* pt = m_ListCalls; + CBotString name = p->GivString(); + + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + CBotVar* pVar = MakeListVars(ppVar); + CBotVar* pVar2 = pVar; + CBotTypResult r = pt->m_rComp(pVar2, m_pUser); + int ret = r.GivType(); + + // if a class is returned, it is actually a pointer + if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer ); + + if ( ret > 20 ) + { + if (pVar2) pStack->SetError(ret, p /*pVar2->GivToken()*/ ); + } + delete pVar; + nIdent = pt->m_nFuncIdent; + return r; + } + pt = pt->m_next; + } + return -1; +} + +void* CBotCall::m_pUser = NULL; + +void CBotCall::SetPUser(void* pUser) +{ + m_pUser = pUser; +} + +bool CBotCall::CheckCall(const char* name) +{ + CBotCall* p = m_ListCalls; + + while ( p != NULL ) + { + if ( name == p->GivName() ) return true; + p = p->m_next; + } + return false; +} + + + +CBotString CBotCall::GivName() +{ + return m_name; +} + +CBotCall* CBotCall::Next() +{ + return m_next; +} + + +int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype) +{ + CBotCall* pt = m_ListCalls; + + if ( nIdent ) while ( pt != NULL ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + goto fund; + } + pt = pt->m_next; + } + + pt = m_ListCalls; + + if ( token != NULL ) + { + CBotString name = token->GivString(); + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + nIdent = pt->m_nFuncIdent; + goto fund; + } + pt = pt->m_next; + } + } + + return -1; + +fund: +#if !STACKRUN + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVarToDelete = pVar; + + // creates a variable to the result + CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); + + CBotVar* pRes = pResult; + int Exception = 0; + int res = pt->m_rExec(pVar, pResult, Exception, pStack->GivPUser()); + + if ( pResult != pRes ) delete pRes; // different result if made + delete pVarToDelete; + + if (res == false) + { + if (Exception!=0) + { + pStack->SetError(Exception, token); + } + delete pResult; + return false; + } + pStack->SetVar(pResult); + + if ( rettype.GivType() > 0 && pResult == NULL ) + { + pStack->SetError(TX_NORETVAL, token); + } + nIdent = pt->m_nFuncIdent; + return true; + +#else + + CBotStack* pile = pStack->AddStackEOX(pt); + if ( pile == EOX ) return true; + + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVarToDelete = pVar; + + // creates a variable to the result + CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); + + pile->SetVar( pVar ); + + CBotStack* pile2 = pile->AddStack(); + pile2->SetVar( pResult ); + + pile->SetError(0, token); // for the position on error + away + return pt->Run( pStack ); + +#endif + +} + +#if STACKRUN + +bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) +{ + CBotCall* pt = m_ListCalls; + + { + CBotString name = token->GivString(); + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + nIdent = pt->m_nFuncIdent; + + CBotStack* pile = pStack->RestoreStackEOX(pt); + if ( pile == NULL ) return true; + + CBotStack* pile2 = pile->RestoreStack(); + return true; + } + pt = pt->m_next; + } + } + + return false; +} + +bool CBotCall::Run(CBotStack* pStack) +{ + CBotStack* pile = pStack->AddStackEOX(this); + if ( pile == EOX ) return true; + CBotVar* pVar = pile->GivVar(); + + CBotStack* pile2 = pile->AddStack(); + CBotVar* pResult = pile2->GivVar(); + CBotVar* pRes = pResult; + + int Exception = 0; + int res = m_rExec(pVar, pResult, Exception, pStack->GivPUser()); + + if (res == false) + { + if (Exception!=0) + { + pStack->SetError(Exception); + } + if ( pResult != pRes ) delete pResult; // different result if made + return false; + } + + if ( pResult != NULL ) pStack->SetCopyVar( pResult ); + if ( pResult != pRes ) delete pResult; // different result if made + + return true; +} + +#endif + +/////////////////////////////////////////////////////////////////////////////////////// + +CBotCallMethode::CBotCallMethode(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) +{ + m_name = name; + m_rExec = rExec; + m_rComp = rCompile; + m_next = NULL; + m_nFuncIdent = CBotVar::NextUniqNum(); +} + +CBotCallMethode::~CBotCallMethode() +{ + delete m_next; + m_next = NULL; +} + +// is acceptable by a call procedure name +// and given parameters + +CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, + CBotVar** ppVar, CBotCStack* pStack, + long& nIdent) +{ + CBotCallMethode* pt = this; + nIdent = 0; + + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVar2 = pVar; + CBotTypResult r = pt->m_rComp(pThis, pVar2); + int ret = r.GivType(); + if ( ret > 20 ) + { + if (pVar2) pStack->SetError(ret, pVar2->GivToken()); + } + delete pVar; + nIdent = pt->m_nFuncIdent; + return r; + } + pt = pt->m_next; + } + return CBotTypResult(-1); +} + + +CBotString CBotCallMethode::GivName() +{ + return m_name; +} + +CBotCallMethode* CBotCallMethode::Next() +{ + return m_next; +} + +void CBotCallMethode::AddNext(CBotCallMethode* pt) +{ + CBotCallMethode* p = this; + while ( p->m_next != NULL ) p = p->m_next; + + p->m_next = pt; +} + + +int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken) +{ + CBotCallMethode* pt = this; + + // search by the identifier + + if ( nIdent ) while ( pt != NULL ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVars, true); + CBotVar* pVarToDelete = pVar; + + // then calls the routine external to the module + + int Exception = 0; + int res = pt->m_rExec(pThis, pVar, pResult, Exception); + pStack->SetVar(pResult); + + if (res == false) + { + if (Exception!=0) + { +// pStack->SetError(Exception, pVar->GivToken()); + pStack->SetError(Exception, pToken); + } + delete pVarToDelete; + return false; + } + delete pVarToDelete; + return true; + } + pt = pt->m_next; + } + + // search by name + + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVars, true); + CBotVar* pVarToDelete = pVar; + + int Exception = 0; + int res = pt->m_rExec(pThis, pVar, pResult, Exception); + pStack->SetVar(pResult); + + if (res == false) + { + if (Exception!=0) + { +// pStack->SetError(Exception, pVar->GivToken()); + pStack->SetError(Exception, pToken); + } + delete pVarToDelete; + return false; + } + delete pVarToDelete; + nIdent = pt->m_nFuncIdent; + return true; + } + pt = pt->m_next; + } + + return -1; +} + +bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + if ( pVar == NULL ) return TX_LOWPARAM; + + int i = 0; + pVar = pVar->GivItemList(); + + while ( pVar != NULL ) + { + i++; + pVar = pVar->GivNext(); + } + + pResult->SetValInt(i); + return true; +} + +CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) +{ + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + if ( pVar->GivType() != CBotTypArrayPointer ) + return CBotTypResult( TX_BADPARAM ); + return CBotTypResult( CBotTypInt ); +} + + +CBotString CBotProgram::m_DebugVarStr = ""; + +bool rCBotDebug( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + pResult->SetValString( CBotProgram::m_DebugVarStr ); + + return true; +} + +CBotTypResult cCBotDebug( CBotVar* &pVar, void* pUser ) +{ + // no parameter + if ( pVar != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // function returns a result "string" + return CBotTypResult( CBotTypString ); +} + + +#include "StringFunctions.cpp" + +void CBotProgram::Init() +{ + CBotToken::DefineNum( "CBotErrOpenPar", 5000) ; // missing the opening parenthesis + CBotToken::DefineNum( "CBotErrClosePar", 5001) ; // missing the closing parenthesis + CBotToken::DefineNum( "CBotErrNotBoolean", 5002) ; // expression must be a boolean + CBotToken::DefineNum( "CBotErrUndefVar", 5003) ; // undeclared variable + CBotToken::DefineNum( "CBotErrBadLeft", 5004) ; // impossible assignment (5 = ...) + CBotToken::DefineNum( "CBotErrNoTerminator", 5005) ;// semicolon expected + CBotToken::DefineNum( "CBotErrCaseOut", 5006) ; // case outside a switch + CBotToken::DefineNum( "CBotErrCloseBlock", 5008) ; // missing " } " + CBotToken::DefineNum( "CBotErrElseWhitoutIf", 5009) ;// else without matching if + CBotToken::DefineNum( "CBotErrOpenBlock", 5010) ; // missing " { " + CBotToken::DefineNum( "CBotErrBadType1", 5011) ; // wrong type for the assignment + CBotToken::DefineNum( "CBotErrRedefVar", 5012) ; // redefinition of the variable + CBotToken::DefineNum( "CBotErrBadType2", 5013) ; // two operands are incompatible + CBotToken::DefineNum( "CBotErrUndefCall", 5014) ; // routine unknown + CBotToken::DefineNum( "CBotErrNoDoubleDots", 5015) ;// " : " expected + CBotToken::DefineNum( "CBotErrBreakOutside", 5017) ;// break outside of a loop + CBotToken::DefineNum( "CBotErrUndefLabel", 5019) ; // unknown label + CBotToken::DefineNum( "CBotErrLabel", 5018) ; // label can not get here + CBotToken::DefineNum( "CBotErrNoCase", 5020) ; // missing " case " + CBotToken::DefineNum( "CBotErrBadNum", 5021) ; // expected number + CBotToken::DefineNum( "CBotErrVoid", 5022) ; // " void " not possble here + CBotToken::DefineNum( "CBotErrNoType", 5023) ; // type declaration expected + CBotToken::DefineNum( "CBotErrNoVar", 5024) ; // variable name expected + CBotToken::DefineNum( "CBotErrNoFunc", 5025) ; // expected function name + CBotToken::DefineNum( "CBotErrOverParam", 5026) ; // too many parameters + CBotToken::DefineNum( "CBotErrRedefFunc", 5027) ; // this function already exists + CBotToken::DefineNum( "CBotErrLowParam", 5028) ; // not enough parameters + CBotToken::DefineNum( "CBotErrBadParam", 5029) ; // mauvais types de paramètres + CBotToken::DefineNum( "CBotErrNbParam", 5030) ; // wrong number of parameters + CBotToken::DefineNum( "CBotErrUndefItem", 5031) ; // element does not exist in the class + CBotToken::DefineNum( "CBotErrUndefClass", 5032) ; // variable is not a class + CBotToken::DefineNum( "CBotErrNoConstruct", 5033) ; // no appropriate constructor + CBotToken::DefineNum( "CBotErrRedefClass", 5034) ; // Class already exists + CBotToken::DefineNum( "CBotErrCloseIndex", 5035) ; // " ] " expected + CBotToken::DefineNum( "CBotErrReserved", 5036) ; // reserved word (for a DefineNum) + +// Here are the list of errors that can be returned by the module +// for the execution + + CBotToken::DefineNum( "CBotErrZeroDiv", 6000) ; // division by zero + CBotToken::DefineNum( "CBotErrNotInit", 6001) ; // uninitialized variable + CBotToken::DefineNum( "CBotErrBadThrow", 6002) ; // throw a negative value + CBotToken::DefineNum( "CBotErrNoRetVal", 6003) ; // function did not return results + CBotToken::DefineNum( "CBotErrNoRun", 6004) ; // active Run () without a function + CBotToken::DefineNum( "CBotErrUndefFunc", 6005) ; // Calling a function that no longer exists + + CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); + + InitStringFunctions(); + + // just a function for various debug + CBotProgram::AddFunction("CBOTDEBUGDD", rCBotDebug, cCBotDebug); + //TODO implement this deletion + // DeleteFile("CbotDebug.txt"); + +} + +void CBotProgram::Free() +{ + CBotToken::Free() ; + CBotCall ::Free() ; + CBotClass::Free() ; +} + diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index fcb825c..36923ae 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -1,1427 +1,1433 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// -// ce fichier dfini les instructions suivantes: -// CBotWhile "while (condition) {instructions}" -// CBotDo "do {instructions} while (condition)" -// CBotFor "for (init, condition, incr) {instructions}" -// CBotSwitch "switch (val) {instructions}" -// CBotCase "case val:" -// CBotBreak "break", "break label", "continu", "continu label" -// CBotTry "try {instructions}" -// CBotCatch "catch (condition) {instructions}" ou "finally" -// CBotThrow "throw execption" - - -#include "CBot.h" - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "while" - -CBotWhile::CBotWhile() -{ - m_Condition = - m_Block = NULL; // NULL pour que delete soit possible sans autre - name = "CBotWhile"; // debug -} - -CBotWhile::~CBotWhile() -{ - delete m_Condition; // libre la condition - delete m_Block; // libre le bloc d'instruction -} - -CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotWhile* inst = new CBotWhile(); // cre l'objet - CBotToken* pp = p; // conserve le ^au token (position dbut) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // enregistre le nom du label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_WHILE)) return NULL; // ne devrait jamais arriver - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // la condition existe - - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - // le bloc d'instruction est ok (il peut tre vide ! - - return pStack->Return(inst, pStk); // rend l'objet qui le demande - } - } - - delete inst; // erreur, libre la place - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile -} - -// excute une instruction "while" - -bool CBotWhile :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // excute la boucle - { // il y a 2 tats possibles (selon reprise) - case 0: - // value la condition - if ( !m_Condition->Execute(pile) ) return false; // interrompu ici ? - - // le rsultat de la condition est sur la pile - - // termine s'il y a une erreur ou si la condition est fausse - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - // la condition est vrai, passe dans le second mode - - if (!pile->SetState(1)) return false; // prt pour la suite - - case 1: - // value le bloc d'instruction associ - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(0, m_label)) continue; // si continue, repasse au test - return pj->BreakReturn(pile, m_label); // transmet le rsultat et libre la pile - } - - // termine s'il y a une erreur - if ( !pile->IsOk() ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - // repasse au test pour recommencer - if (!pile->SetState(0, 0)) return false; - continue; - } -} - -void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // il y a 2 tats possibles (selon reprise) - case 0: - // value la condition - m_Condition->RestoreState(pile, bMain); - return; - - case 1: - // value le bloc d'instruction associ - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - } -} - - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "repeat" - -CBotRepeat::CBotRepeat() -{ - m_NbIter = - m_Block = NULL; // NULL pour que delete soit possible sans autre - name = "CBotRepeat"; // debug -} - -CBotRepeat::~CBotRepeat() -{ - delete m_NbIter; // libre la condition - delete m_Block; // libre le bloc d'instruction -} - -CBotInstr* CBotRepeat::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotRepeat* inst = new CBotRepeat(); // cre l'objet - CBotToken* pp = p; // conserve le ^au token (position dbut) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // enregistre le nom du label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_REPEAT)) return NULL; // ne devrait jamais arriver - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - CBotToken* ppp = p; // conserve le ^au token (position dbut) - if ( NULL != (inst->m_NbIter = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GivType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - // le bloc d'instruction est ok (il peut tre vide ! - - return pStack->Return(inst, pStk); // rend l'objet qui le demande - } - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStk->SetStartError(ppp->GivStart()); - pStk->SetError( TX_BADTYPE, p->GivStart() ); - } - pStack->SetError(TX_ENDOF, p); - } - pStack->SetError(TX_OPENPAR, p->GivStart()); // manque la parenthse - - delete inst; // erreur, libre la place - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile -} - -// excute une instruction "repeat" - -bool CBotRepeat :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // excute la boucle - { // il y a 2 tats possibles (selon reprise) - case 0: - // value le nombre d'itration - if ( !m_NbIter->Execute(pile) ) return false; // interrompu ici ? - - // le rsultat de la condition est sur la pile - - // termine s'il y a une erreur ou si la condition est fausse - int n; - if ( !pile->IsOk() || ( n = pile->GivVal() ) < 1 ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - // met le nombre d'itration +1 dans le "state" - - if (!pile->SetState(n+1)) return false; // prt pour la suite - continue; // passe la suite - - case 1: - // fin normale de la boucle - return pj->Return(pile); // transmet le rsultat et libre la pile - - default: - // value le bloc d'instruction associ - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(pile->GivState()-1, m_label)) continue; // si continue, repasse au test - return pj->BreakReturn(pile, m_label); // transmet le rsultat et libre la pile - } - - // termine s'il y a une erreur - if ( !pile->IsOk() ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - // repasse au test pour recommencer - if (!pile->SetState(pile->GivState()-1, 0)) return false; - continue; - } -} - -void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // il y a 2 tats possibles (selon reprise) - case 0: - // value la condition - m_NbIter->RestoreState(pile, bMain); - return; - - case 1: - // value le bloc d'instruction associ - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - } -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "do" - -CBotDo::CBotDo() -{ - m_Condition = - m_Block = NULL; // NULL pour que delete soit possible sans autre - name = "CBotDo"; // debug -} - -CBotDo::~CBotDo() -{ - delete m_Condition; // libre la condition - delete m_Block; // libre le bloc d'instruction -} - -CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotDo* inst = new CBotDo(); // cre l'objet - - CBotToken* pp = p; // conserve le ^au token (position dbut) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // enregistre le nom du label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_DO)) return NULL; // ne devrait jamais arriver - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - - // cherche un bloc d'instruction aprs le do - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - if (IsOfType(p, ID_WHILE)) - { - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // la condition existe - if (IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); // rend l'objet qui le demande - } - pStk->SetError(TX_ENDOF, p->GivStart()); - } - } - pStk->SetError(TX_WHILE, p->GivStart()); - } - - delete inst; // erreur, libre la place - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile -} - -// excute une instruction "do" - -bool CBotDo :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // excute la boucle - { // il y a 2 tats possibles (selon reprise) - case 0: - // value le bloc d'instruction associ - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(1, m_label)) continue; // si continue, repasse au test - return pj->BreakReturn(pile, m_label); // transmet le rsultat et libre la pile - } - - // termine s'il y a une erreur - if ( !pile->IsOk() ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - if (!pile->SetState(1)) return false; // prt pour la suite - - case 1: - // value la condition - if ( !m_Condition->Execute(pile) ) return false; // interrompu ici ? - - // le rsultat de la condition est sur la pile - - // termine s'il y a une erreur ou si la condition est fausse - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - // repasse au bloc d'instruction pour recommencer - if (!pile->SetState(0, 0)) return false; - continue; - } -} - -void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // il y a 2 tats possibles (selon reprise) - case 0: - // restitue le bloc d'instruction associ - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - - case 1: - // restitue la condition - m_Condition->RestoreState(pile, bMain); - return; - } -} - - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "for" - -CBotFor::CBotFor() -{ - m_Init = - m_Test = - m_Incr = - m_Block = NULL; // NULL pour que delete soit possible sans autre - name = "CBotFor"; // debug -} - -CBotFor::~CBotFor() -{ - delete m_Init; - delete m_Test; - delete m_Incr; - delete m_Block; // libre le bloc d'instruction -} - -CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotFor* inst = new CBotFor(); // cre l'objet - CBotToken* pp = p; // conserve le ^au token (position dbut) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // enregistre le nom du label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_FOR)) return NULL; // ne devrait jamais arriver - - if ( !IsOfType(p, ID_OPENPAR)) // manque la parenthse ? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - return NULL; - } - - CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp - - // compile les instructions pour initialisation - inst->m_Init = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // manque le point-virgule ? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile - } - inst->m_Test = CBotBoolExpr::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // manque le point-virgule ? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile - } - inst->m_Incr = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( IsOfType(p, ID_CLOSEPAR)) // manque la parenthse ? - { - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - if ( pStk->IsOk() ) - return pStack->Return(inst, pStk);; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - } - } - - delete inst; // erreur, libre la place - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile -} - -// excute l'instruction "for" - -bool CBotFor :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this, true); // ajoute un lment la pile (variables locales) - // ou le retrouve en cas de reprise -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // excute la boucle - { // il y a 4 tats possibles (selon reprise) - case 0: - // value l'initialisation - if ( m_Init != NULL && - !m_Init->Execute(pile) ) return false; // interrompu ici ? - if (!pile->SetState(1)) return false; // prt pour la suite - - case 1: - // value la condition - if ( m_Test != NULL ) // pas de condition ? -> vrai ! - { - if (!m_Test->Execute(pile) ) return false; // interrompu ici ? - - // le rsultat de la condition est sur la pile - - // termine s'il y a une erreur ou si la condition est fausse - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - } - - // la condition est vrai, passe la suite - if (!pile->SetState(2)) return false; // prt pour la suite - - case 2: - // value le bloc d'instruction associ - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(3, m_label)) continue; // si continue, passe l'incrmentation - return pj->BreakReturn(pile, m_label); // transmet le rsultat et libre la pile - } - - // termine s'il y a une erreur - if ( !pile->IsOk() ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - if (!pile->SetState(3)) return false; // prt pour la suite - - case 3: - // value l'incrmentation - if ( m_Incr != NULL && - !m_Incr->Execute(pile) ) return false; // interrompu ici ? - - // repasse au test pour recommencer - if (!pile->SetState(1, 0)) return false; // revient au test - continue; - } -} - -void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile (variables locales) - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // il y a 4 tats possibles (selon reprise) - case 0: - // value l'initialisation - if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrompu ici ! - return; - - case 1: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // dfinitions variables - - // value la condition - if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrompu ici ! - return; - - case 2: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // dfinitions variables - - // value le bloc d'instruction associ - if ( m_Block != NULL ) m_Block->RestoreState(pile, true); - return; - - case 3: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // dfinitions variables - - // value l'incrmentation - if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrompu ici ! - return; - } -} - -////////////////////////////////////////////////////////////////////////////////////// -// compile une liste d'expression -// n'est utilis que pour l'instruction for -// dans l'intitialisation et dans l'incrmentation - -CBotListExpression::CBotListExpression() -{ - m_Expr = NULL; - name = "CBotListExpression"; -} - -CBotListExpression::~CBotListExpression() -{ - delete m_Expr; -} - -// cherche une dclaration de variable ou une expression - -static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) -{ - CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // est-ce une dclaration d'un entier ? - if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // ou d'un nombre rel ? - if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // ou d'un boolen ? - if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ou d'une chane ? - if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compile une expression - return i; -} - -CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotListExpression* inst = new CBotListExpression(); - - inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile la premire expression de la liste - if (pStack->IsOk()) - { - while ( IsOfType(p, ID_COMMA) ) // plusieurs instructions ? - { - CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // est-ce une dclaration d'un entier ? - inst->m_Expr->AddNext(i); // ajoute la suite - if ( !pStack->IsOk() ) - { - delete inst; - return NULL; // pas d'objet, l'erreur est sur la pile - } - } - return inst; - } - delete inst; - return NULL; -} - -bool CBotListExpression::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack();// indispensable - CBotInstr* p = m_Expr; // la premire expression - - int state = pile->GivState(); - while (state-->0) p = p->GivNext(); // revient sur l'opration interrompue - - if ( p != NULL ) while (true) - { - if ( !p->Execute(pile) ) return false; - p = p->GivNext(); - if ( p == NULL ) break; - if (!pile->IncState()) return false; // prt pour la suivante - } - return pj->Return(pile); -} - -void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - int state = 0x7000; - - if ( bMain ) - { - pile = pj->RestoreStack(); - if ( pile == NULL ) return; - state = pile->GivState(); - } - - CBotInstr* p = m_Expr; // la premire expression - - while (p != NULL && state-->0) - { - p->RestoreState(pile, false); - p = p->GivNext(); // revient sur l'opration interrompue - } - - if ( p != NULL ) - { - p->RestoreState(pile, bMain); - } -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "switch" - -CBotSwitch::CBotSwitch() -{ - m_Value = - m_Block = NULL; // NULL pour que delete soit possible sans autre - name = "CBotSwitch"; // debug -} - -CBotSwitch::~CBotSwitch() -{ - delete m_Value; // libre la valeur - delete m_Block; // libre le bloc d'instruction -} - - -CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotSwitch* inst = new CBotSwitch(); // cre l'objet - CBotToken* pp = p; // conserve le ^au token (position dbut) - - inst->SetToken(p); - if (!IsOfType(p, ID_SWITCH)) return NULL; // ne devrait jamais arriver - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - if ( NULL != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GivType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - if ( IsOfType(p, ID_OPBLK ) ) - { - IncLvl(); - - while( !IsOfType( p, ID_CLBLK ) ) - { - if ( p->GivType() == ID_CASE || p->GivType() == ID_DEFAULT) - { - CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp - - CBotInstr* i = CBotCase::Compile( p, pStk2 ); - if (i == NULL) - { - delete inst; - return pStack->Return(NULL, pStk2); - } - delete pStk2; - if ( inst->m_Block == NULL ) inst->m_Block = i; - else inst->m_Block->AddNext(i); - continue; - } - - if ( inst->m_Block == NULL ) - { - pStk->SetError(TX_NOCASE, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if ( !pStk->IsOk() ) - { - delete inst; - return pStack->Return(NULL, pStk); - } - inst->m_Block->AddNext(i); - - if ( p == NULL ) - { - pStk->SetError(TX_CLOSEBLK, -1); - delete inst; - return pStack->Return(NULL, pStk); - } - } - DecLvl(); - - if ( inst->m_Block == NULL ) - { - pStk->SetError(TX_NOCASE, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - // le bloc d'instruction est ok - return pStack->Return(inst, pStk); // rend l'objet qui le demande - } - pStk->SetError( TX_OPENBLK, p->GivStart() ); - } - pStk->SetError( TX_CLOSEPAR, p->GivStart() ); - } - pStk->SetError( TX_BADTYPE, p->GivStart() ); - } - } - pStk->SetError( TX_OPENPAR, p->GivStart()); - - delete inst; // erreur, libre la place - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile -} - -// excute une instruction "switch" - -bool CBotSwitch :: Execute(CBotStack* &pj) -{ - CBotStack* pile1 = pj->AddStack(this); // ajoute un lment la pile -// if ( pile1 == EOX ) return true; - - CBotInstr* p = m_Block; // la premire expression - - int state = pile1->GivState(); - if (state == 0) - { - if ( !m_Value->Execute(pile1) ) return false; - pile1->SetState(state = -1); - } - - if ( pile1->IfStep() ) return false; - - if ( state == -1 ) - { - state = 0; - int val = pile1->GivVal(); // rsultat de la valeur - - CBotStack* pile2 = pile1->AddStack(); - while ( p != NULL ) // recherche le case correspondant dans la liste - { - state++; - if ( p->CompCase( pile2, val ) ) break; // trouv le case - p = p->GivNext(); - } - pile2->Delete(); - - if ( p == NULL ) return pj->Return(pile1); // termin si plus rien - - if ( !pile1->SetState(state) ) return false; - } - - p = m_Block; // revient au dbut - while (state-->0) p = p->GivNext(); // avance dans la liste - - while( p != NULL ) - { - if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); - if ( !pile1->IncState() ) return false; - p = p->GivNext(); - } - return pj->Return(pile1); -} - -void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile1 = pj->RestoreStack(this); // ajoute un lment la pile - if ( pile1 == NULL ) return; - - CBotInstr* p = m_Block; // la premire expression - - int state = pile1->GivState(); - if (state == 0) - { - m_Value->RestoreState(pile1, bMain); - return; - } - - if ( state == -1 ) - { - return; - } - -// p = m_Block; // revient au dbut - while ( p != NULL && state-- > 0 ) - { - p->RestoreState(pile1, false); - p = p->GivNext(); // avance dans la liste - } - - if( p != NULL ) - { - p->RestoreState(pile1, true); - return; - } -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "case" -// on est forcment dans un bloc d'instruction "switch" - -CBotCase::CBotCase() -{ - m_Value = NULL; // NULL pour que delete soit possible sans autre - name = "CBotCase"; // debug -} - -CBotCase::~CBotCase() -{ - delete m_Value; // libre la valeur -} - - -CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCase* inst = new CBotCase(); // cre l'objet - CBotToken* pp = p; // conserve le ^au token (position dbut) - - inst->SetToken(p); - if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return NULL; // ne devrait jamais arriver - - if ( pp->GivType() == ID_CASE ) - { - pp = p; - inst->m_Value = CBotExprNum::Compile(p, pStack); - if ( inst->m_Value == NULL ) - { - pStack->SetError( TX_BADNUM, pp ); - delete inst; - return NULL; - } - } - if ( !IsOfType( p, ID_DOTS )) - { - pStack->SetError( TX_MISDOTS, p->GivStart() ); - delete inst; - return NULL; - } - - return inst; -} - -// excution de l'instruction "case" - -bool CBotCase::Execute(CBotStack* &pj) -{ - return true; // l'instruction "case" ne fait rien ! -} - -void CBotCase::RestoreState(CBotStack* &pj, bool bMain) -{ -} - -// routine permettant de trouver le point d'entre "case" -// correspondant la valeur cherche - -bool CBotCase::CompCase(CBotStack* &pile, int val) -{ - if ( m_Value == NULL ) return true; // cas pour "default" - - while (!m_Value->Execute(pile)); // met sur la pile la valeur correpondant (sans interruption) - return (pile->GivVal() == val); // compare avec la valeur cherche -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "break" ou "continu" - -CBotBreak::CBotBreak() -{ - name = "CBotBreak"; // debug -} - -CBotBreak::~CBotBreak() -{ -} - -CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; // conserve le ^au token (position dbut) - int type = p->GivType(); - - if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return NULL; // ne devrait jamais arriver - - if ( !ChkLvl(CBotString(), type ) ) - { - pStack->SetError(TX_BREAK, pp); - return NULL; // pas d'objet, l'erreur est sur la pile - } - - CBotBreak* inst = new CBotBreak(); // cre l'objet - inst->SetToken(pp); // garde l'opration - - pp = p; - if ( IsOfType( p, TokenTypVar ) ) - { - inst->m_label = pp->GivString(); // enregistre le nom du label - if ( !ChkLvl(inst->m_label, type ) ) - { - delete inst; - pStack->SetError(TX_NOLABEL, pp); - return NULL; // pas d'objet, l'erreur est sur la pile - } - } - - if (IsOfType(p, ID_SEP)) - { - return inst; // et le donne qui veux - } - delete inst; - - pStack->SetError(TX_ENDOF, p->GivStart()); - return NULL; // pas d'objet, l'erreur est sur la pile -} - -// excution l'instructino "break" ou "continu" - -bool CBotBreak :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); - return pj->Return(pile); -} - -void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( bMain ) pj->RestoreStack(this); -} - - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "try" - -CBotTry::CBotTry() -{ - m_ListCatch = NULL; - m_FinalInst = - m_Block = NULL; // NULL pour que delete soit possible sans autre - name = "CBotTry"; // debug -} - -CBotTry::~CBotTry() -{ - delete m_ListCatch; // libre la liste - delete m_Block; // libre le bloc d'instruction - delete m_FinalInst; -} - -CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotTry* inst = new CBotTry(); // cre l'objet - CBotToken* pp = p; // conserve le ^au token (position dbut) - - inst->SetToken(p); - if (!IsOfType(p, ID_TRY)) return NULL; // ne devrait jamais arriver - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); - CBotCatch** pn = &inst->m_ListCatch; - - while (pStk->IsOk() && p->GivType() == ID_CATCH) - { - CBotCatch* i = CBotCatch::Compile(p, pStk); - *pn = i; - pn = &i->m_next; - } - - if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) - { - inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); - } - - if (pStk->IsOk()) - { - return pStack->Return(inst, pStk); // rend l'objet qui le demande - } - - delete inst; // erreur, libre la place - return pStack->Return(NULL, pStk); // pas d'objet, l'erreur est sur la pile -} - -// excute l'instruction Try -// gre le retour d'exceptions -// les arrts par suspension -// et les "finaly" - -bool CBotTry :: Execute(CBotStack* &pj) -{ - int val; - - CBotStack* pile1 = pj->AddStack(this); // ajoute un lment la pile -// if ( pile1 == EOX ) return true; - - if ( pile1->IfStep() ) return false; - // ou le retrouve en cas de reprise - CBotStack* pile0 = pj->AddStack2(); // ajoute un lment la pile secondaire - CBotStack* pile2 = pile0->AddStack(); - - if ( pile1->GivState() == 0 ) - { - if ( m_Block->Execute(pile1) ) - { - if ( m_FinalInst == NULL ) return pj->Return(pile1); - pile1->SetState(-2); // passe au final - } - - val = pile1->GivError(); - if ( val == 0 && CBotStack::m_initimer == 0 ) // en mode de step ? - return false; // ne fait pas le catch - - pile1->IncState(); - pile2->SetState(val); // mmorise le numro de l'erreur - pile1->SetError(0); // pour l'instant il n'y a plus d'erreur ! - - if ( val == 0 && CBotStack::m_initimer < 0 ) // en mode de step ? - return false; // ne fait pas le catch - } - - // il y a eu une interruption - // voir de quoi il en retourne - - CBotCatch* pc = m_ListCatch; - int state = (short)pile1->GivState(); // o en tions-nous ? - val = pile2->GivState(); // pour quelle erreur ? - pile0->SetState(1); // marquage pour GetRunPos - - if ( val >= 0 && state > 0 ) while ( pc != NULL ) - { - if ( --state <= 0 ) - { - // demande au bloc catch s'il se sent concern - if ( !pc->TestCatch(pile2, val) ) return false; // suspendu ! - pile1->IncState(); - } - if ( --state <= 0 ) - { - if ( pile2->GivVal() == true ) - { -// pile0->SetState(1); - - if ( !pc->Execute(pile2) ) return false; // excute l'opration - if ( m_FinalInst == NULL ) - return pj->Return(pile2); // termine le try - - pile1->SetState(-2); // passe au final - break; - } - pile1->IncState(); - } - pc = pc->m_next; - } - if ( m_FinalInst != NULL && - pile1->GivState() > 0 && val != 0 ) pile1->SetState(-1);// si arret alors fait le final - - if (pile1->GivState() <= -1) - { -// pile0->SetState(1); - - if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; - if (!pile2->IsOk()) return pj->Return(pile2); // garde cette exception - pile2->SetError(pile1->GivState()==-1 ? val : 0); // remet l'erreur initiale - return pj->Return(pile2); - } - - pile1->SetState(0); // revient l'valuation - pile0->SetState(0); // revient l'valuation - if ( val != 0 && m_ListCatch == NULL && m_FinalInst == NULL ) - return pj->Return(pile2); // termine le try sans exception aucune - - pile1->SetError(val); // remet l'erreur - return false; // ce n'est pas pour nous -} - - -void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - int val; - CBotStack* pile1 = pj->RestoreStack(this); // ajoute un lment la pile - if ( pile1 == NULL ) return; - // ou le retrouve en cas de reprise - CBotStack* pile0 = pj->AddStack2(); // ajoute un lment la pile secondaire - if ( pile0 == NULL ) return; - - CBotStack* pile2 = pile0->RestoreStack(); - if ( pile2 == NULL ) return; - - m_Block->RestoreState(pile1, bMain); - if ( pile0->GivState() == 0 ) - { - return; - } - - // il y a eu une interruption - // voir de quoi il en retourne - - CBotCatch* pc = m_ListCatch; - int state = pile1->GivState(); // o en tions-nous ? - val = pile2->GivState(); // pour quelle erreur ? - - if ( val >= 0 && state > 0 ) while ( pc != NULL ) - { - if ( --state <= 0 ) - { - // demande au bloc catch s'il se sent concern - pc->RestoreCondState(pile2, bMain); // suspendu ! - return; - } - if ( --state <= 0 ) - { - if ( pile2->GivVal() == true ) - { - pc->RestoreState(pile2, bMain); // excute l'opration - return; - } - } - pc = pc->m_next; - } - - if (pile1->GivState() <= -1) - { - m_FinalInst->RestoreState(pile2, bMain); - return; - } -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "catch" - -CBotCatch::CBotCatch() -{ - m_Cond = - m_Block = NULL; // NULL pour que delete soit possible sans autre - m_next = NULL; - - name = "CBotCatch"; // debug -} - -CBotCatch::~CBotCatch() -{ - delete m_Cond; // libre la liste - delete m_Block; // libre le bloc d'instruction - delete m_next; // et la suite -} - -CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCatch* inst = new CBotCatch(); // cre l'objet - pStack->SetStartError(p->GivStart()); - - inst->SetToken(p); - if (!IsOfType(p, ID_CATCH)) return NULL; // ne devrait jamais arriver - - if (IsOfType(p, ID_OPENPAR)) - { - inst->m_Cond = CBotExpression::Compile(p, pStack); - if (( pStack->GivType() < CBotTypLong || - pStack->GivTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); - if ( pStack->IsOk() ) - return inst; // rend l'objet qui le demande - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStack->SetError(TX_BADTYPE, p->GivStart()); - } - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; // erreur, libre la place - return NULL; // pas d'objet, l'erreur est sur la pile -} - -// excution de "catch" - -bool CBotCatch :: Execute(CBotStack* &pj) -{ - if ( m_Block == NULL ) return true; - return m_Block->Execute(pj); // excute le bloc associ -} - -void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); -} - -void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) -{ - m_Cond->RestoreState(pj, bMain); -} - -// routine pour savoir si le catch est faire ou non - -bool CBotCatch :: TestCatch(CBotStack* &pile, int val) -{ - if ( !m_Cond->Execute(pile) ) return false; - - if ( val > 0 || pile->GivType() != CBotTypBoolean ) - { - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - var->SetValInt( pile->GivVal() == val ); - pile->SetVar(var); // remet sur la pile - } - - return true; -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile une instruction "throw" - -CBotThrow::CBotThrow() -{ - m_Value = NULL; // NULL pour que delete soit possible sans autre - - name = "CBotThrow"; // debug -} - -CBotThrow::~CBotThrow() -{ - delete m_Value; -} - -CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) -{ - pStack->SetStartError(p->GivStart()); - - CBotThrow* inst = new CBotThrow(); // cre l'objet - inst->SetToken(p); - - CBotToken* pp = p; // conserve le ^au token (position dbut) - - if (!IsOfType(p, ID_THROW)) return NULL; // ne devrait jamais arriver - - inst->m_Value = CBotExpression::Compile( p, pStack ); - - if (pStack->GivType() < CBotTypLong && pStack->IsOk()) - { - return inst; // rend l'objet qui le demande - } - pStack->SetError(TX_BADTYPE, pp); - - delete inst; // erreur, libre la place - return NULL; // pas d'objet, l'erreur est sur la pile -} - -// excute l'instruction "throw" - -bool CBotThrow :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - if ( pile->GivState() == 0 ) - { - if ( !m_Value->Execute(pile) ) return false; - pile->IncState(); - } - - if ( pile->IfStep() ) return false; - - int val = pile->GivVal(); - if ( val < 0 ) val = TX_BADTHROW; - pile->SetError( val, &m_token ); - return pj->Return( pile ); -} - -void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; - - if ( pile->GivState() == 0 ) - { - m_Value->RestoreState(pile, bMain); - return; - } -} - - - -//////////////////////////////////////////////////////////// - - -CBotStartDebugDD::CBotStartDebugDD() -{ - name = "CBotStartDebugDD"; // debug -} - -CBotStartDebugDD::~CBotStartDebugDD() -{ -} - -CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) -{ - - if (!IsOfType(p, ID_DEBUGDD)) return NULL; // ne devrait jamais arriver - - return new CBotStartDebugDD(); // cre l'objet - -} - -// excute l'instruction "throw" - -bool CBotStartDebugDD :: Execute(CBotStack* &pj) -{ - CBotProgram* p = pj->GivBotCall(); - p->m_bDebugDD = true; - - return true; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/////////////////////////////////////////////////////////////////////// +// This file defined the following statements: +// CBotWhile "while (condition) {instructions}" +// CBotDo "do {instructions} while (condition)" +// CBotFor "for (init, condition, incr) {instructions}" +// CBotSwitch "switch (val) {instructions}" +// CBotCase "case val:" +// CBotBreak "break", "break label", "continu", "continu label" +// CBotTry "try {instructions}" +// CBotCatch "catch (condition) {instructions}" or "finally" +// CBotThrow "throw execption" + + +#include "CBot.h" + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compile an instruction "while" + +CBotWhile::CBotWhile() +{ + m_Condition = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotWhile"; // debug +} + +CBotWhile::~CBotWhile() +{ + delete m_Condition; // frees the condition + delete m_Block; // releases the block instruction +} + +CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotWhile* inst = new CBotWhile(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // records the name of the label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_WHILE)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + // a bit of battery please (??) + + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists + + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + // the statement block is ok (it may be empty! + + return pStack->Return(inst, pStk); // return an object to the application + // makes the object to which the application + } + } + + delete inst; // error, frees the place + return pStack->Return(NULL, pStk); // no object, the error is on the stack +} + +// executes a "while" instruction + +bool CBotWhile :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // the condition is true, pass in the second mode + + if (!pile->SetState(1)) return false; // ready for further + + case 1: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(0, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to the test again + if (!pile->SetState(0, 0)) return false; + continue; + } +} + +void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + m_Condition->RestoreState(pile, bMain); + return; + + case 1: + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + } +} + + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "repeat" + +CBotRepeat::CBotRepeat() +{ + m_NbIter = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotRepeat"; // debug +} + +CBotRepeat::~CBotRepeat() +{ + delete m_NbIter; // frees the condition + delete m_Block; // frees the instruction block +} + +CBotInstr* CBotRepeat::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotRepeat* inst = new CBotRepeat(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_REPEAT)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + CBotToken* ppp = p; // preserves the ^ token (starting position) + if ( NULL != (inst->m_NbIter = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GivType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + // the statement block is ok (it may be empty! + + return pStack->Return(inst, pStk); // return an object to the application + } + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStk->SetStartError(ppp->GivStart()); + pStk->SetError( TX_BADTYPE, p->GivStart() ); + } + pStack->SetError(TX_ENDOF, p); + } + pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack +} + +// execution of intruction "repeat" + +bool CBotRepeat :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the number of iterations + if ( !m_NbIter->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + int n; + if ( !pile->IsOk() || ( n = pile->GivVal() ) < 1 ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // puts the number of iterations +1 to the "state" + + if (!pile->SetState(n+1)) return false; // ready for further + continue; // continue as a result + + case 1: + // normal end of the loop + return pj->Return(pile); // sends the results and releases the stack + + default: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(pile->GivState()-1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to the test again + if (!pile->SetState(pile->GivState()-1, 0)) return false; + continue; + } +} + +void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + m_NbIter->RestoreState(pile, bMain); + return; + + case 1: + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + } +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compile the instruction "do" + +CBotDo::CBotDo() +{ + m_Condition = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotDo"; // debug +} + +CBotDo::~CBotDo() +{ + delete m_Condition; // frees the condition + delete m_Block; // frees the instruction block +} + +CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotDo* inst = new CBotDo(); // creates the object + + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_DO)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + + // looking for a statement block after the do + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + if (IsOfType(p, ID_WHILE)) + { + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists + if (IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError(TX_ENDOF, p->GivStart()); + } + } + pStk->SetError(TX_WHILE, p->GivStart()); + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack +} + +// executes instruction "do" + +bool CBotDo :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + if (!pile->SetState(1)) return false; // ready for further + + case 1: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to instruction block to start + if (!pile->SetState(0, 0)) return false; + continue; + } +} + +void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // restores the assosiated statement's block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + + case 1: + // restores the condition + m_Condition->RestoreState(pile, bMain); + return; + } +} + + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "for" + +CBotFor::CBotFor() +{ + m_Init = + m_Test = + m_Incr = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotFor"; // debug +} + +CBotFor::~CBotFor() +{ + delete m_Init; + delete m_Test; + delete m_Incr; + delete m_Block; // frees the instruction block +} + +CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotFor* inst = new CBotFor(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_FOR)) return NULL; // should never happen + + if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + return NULL; + } + + CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp + + // compiles instructions for initialization + inst->m_Init = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); // no object, the error is on the stack + } + inst->m_Test = CBotBoolExpr::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); // no object, the error is on the stack + } + inst->m_Incr = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? + { + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + if ( pStk->IsOk() ) + return pStack->Return(inst, pStk);; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + } + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack +} + +// execution of instruction "for" + +bool CBotFor :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != NULL && + !m_Init->Execute(pile) ) return false; // interrupted here ? + if (!pile->SetState(1)) return false; // ready for further + + case 1: + // evaluates the condition + if ( m_Test != NULL ) // no strings attached? -> True! + { + if (!m_Test->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + } + + // la condition est vrai, passe à la suite + if (!pile->SetState(2)) return false; // ready for further + + case 2: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + if (!pile->SetState(3)) return false; // ready for further + + case 3: + // evalutate the incrementation + if ( m_Incr != NULL && + !m_Incr->Execute(pile) ) return false; // interrupted here ? + + // returns to the test again + if (!pile->SetState(1, 0)) return false; // returns to the test + continue; + } +} + +void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrupted here ! + return; + + case 1: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variables definitions + + // evaluates the condition + if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrupted here ! + return; + + case 2: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, true); + return; + + case 3: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluate the incrementation + if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrupted here ! + return; + } +} + +////////////////////////////////////////////////////////////////////////////////////// +// compiles a list of expressions +// is used only in "for" statement +// in incrementing and intitialisation + +CBotListExpression::CBotListExpression() +{ + m_Expr = NULL; + name = "CBotListExpression"; +} + +CBotListExpression::~CBotListExpression() +{ + delete m_Expr; +} + +// seeks a declaration of variable or expression + +static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) +{ + CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? + if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? + if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? + if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? + if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compiles an expression + return i; +} + +CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotListExpression* inst = new CBotListExpression(); + + inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list + if (pStack->IsOk()) + { + while ( IsOfType(p, ID_COMMA) ) // more instructions? + { + CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? + inst->m_Expr->AddNext(i); // added after + if ( !pStack->IsOk() ) + { + delete inst; + return NULL; // no object, the error is on the stack + } + } + return inst; + } + delete inst; + return NULL; +} + +bool CBotListExpression::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(); // essential + CBotInstr* p = m_Expr; // the first expression + + int state = pile->GivState(); + while (state-->0) p = p->GivNext(); // returns to the interrupted operation + + if ( p != NULL ) while (true) + { + if ( !p->Execute(pile) ) return false; + p = p->GivNext(); + if ( p == NULL ) break; + if (!pile->IncState()) return false; // ready for next + } + return pj->Return(pile); +} + +void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + int state = 0x7000; + + if ( bMain ) + { + pile = pj->RestoreStack(); + if ( pile == NULL ) return; + state = pile->GivState(); + } + + CBotInstr* p = m_Expr; // the first expression + + while (p != NULL && state-->0) + { + p->RestoreState(pile, false); + p = p->GivNext(); // returns to the interrupted operation + } + + if ( p != NULL ) + { + p->RestoreState(pile, bMain); + } +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "switch" + +CBotSwitch::CBotSwitch() +{ + m_Value = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotSwitch"; // debug +} + +CBotSwitch::~CBotSwitch() +{ + delete m_Value; // frees the value + delete m_Block; // frees the instruction block +} + + +CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotSwitch* inst = new CBotSwitch(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_SWITCH)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + if ( NULL != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GivType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + if ( IsOfType(p, ID_OPBLK ) ) + { + IncLvl(); + + while( !IsOfType( p, ID_CLBLK ) ) + { + if ( p->GivType() == ID_CASE || p->GivType() == ID_DEFAULT) + { + CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp + + CBotInstr* i = CBotCase::Compile( p, pStk2 ); + if (i == NULL) + { + delete inst; + return pStack->Return(NULL, pStk2); + } + delete pStk2; + if ( inst->m_Block == NULL ) inst->m_Block = i; + else inst->m_Block->AddNext(i); + continue; + } + + if ( inst->m_Block == NULL ) + { + pStk->SetError(TX_NOCASE, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + + CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if ( !pStk->IsOk() ) + { + delete inst; + return pStack->Return(NULL, pStk); + } + inst->m_Block->AddNext(i); + + if ( p == NULL ) + { + pStk->SetError(TX_CLOSEBLK, -1); + delete inst; + return pStack->Return(NULL, pStk); + } + } + DecLvl(); + + if ( inst->m_Block == NULL ) + { + pStk->SetError(TX_NOCASE, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + // the statement block is ok + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError( TX_OPENBLK, p->GivStart() ); + } + pStk->SetError( TX_CLOSEPAR, p->GivStart() ); + } + pStk->SetError( TX_BADTYPE, p->GivStart() ); + } + } + pStk->SetError( TX_OPENPAR, p->GivStart()); + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack +} + +// executes instruction "switch" + +bool CBotSwitch :: Execute(CBotStack* &pj) +{ + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GivState(); + if (state == 0) + { + if ( !m_Value->Execute(pile1) ) return false; + pile1->SetState(state = -1); + } + + if ( pile1->IfStep() ) return false; + + if ( state == -1 ) + { + state = 0; + int val = pile1->GivVal(); // result of the value + + CBotStack* pile2 = pile1->AddStack(); + while ( p != NULL ) // search for the corresponding case in a list + { + state++; + if ( p->CompCase( pile2, val ) ) break; // found the case + p = p->GivNext(); + } + pile2->Delete(); + + if ( p == NULL ) return pj->Return(pile1); // completed if nothing + + if ( !pile1->SetState(state) ) return false; + } + + p = m_Block; // returns to the beginning + while (state-->0) p = p->GivNext(); // advance in the list + + while( p != NULL ) + { + if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); + if ( !pile1->IncState() ) return false; + p = p->GivNext(); + } + return pj->Return(pile1); +} + +void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == NULL ) return; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GivState(); + if (state == 0) + { + m_Value->RestoreState(pile1, bMain); + return; + } + + if ( state == -1 ) + { + return; + } + +// p = m_Block; // returns to the beginning + while ( p != NULL && state-- > 0 ) + { + p->RestoreState(pile1, false); + p = p->GivNext(); // advance in the list + } + + if( p != NULL ) + { + p->RestoreState(pile1, true); + return; + } +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "case" +// we are bound to the statement block "switch" + +CBotCase::CBotCase() +{ + m_Value = NULL; // NULL so that delete is not possible further + name = "CBotCase"; // debug +} + +CBotCase::~CBotCase() +{ + delete m_Value; // frees the value +} + + +CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCase* inst = new CBotCase(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return NULL; // should never happen + + if ( pp->GivType() == ID_CASE ) + { + pp = p; + inst->m_Value = CBotExprNum::Compile(p, pStack); + if ( inst->m_Value == NULL ) + { + pStack->SetError( TX_BADNUM, pp ); + delete inst; + return NULL; + } + } + if ( !IsOfType( p, ID_DOTS )) + { + pStack->SetError( TX_MISDOTS, p->GivStart() ); + delete inst; + return NULL; + } + + return inst; +} + +// execution of instruction "case" + +bool CBotCase::Execute(CBotStack* &pj) +{ + return true; // the "case" statement does nothing! +} + +void CBotCase::RestoreState(CBotStack* &pj, bool bMain) +{ +} + +// routine to find the entry point of "case" +// corresponding to the value seen + +bool CBotCase::CompCase(CBotStack* &pile, int val) +{ + if ( m_Value == NULL ) return true; // "default" case + + while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) + return (pile->GivVal() == val); // compared with the given value +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "break" or "continu" + +CBotBreak::CBotBreak() +{ + name = "CBotBreak"; // debug +} + +CBotBreak::~CBotBreak() +{ +} + +CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; // preserves at the ^ token (starting position) + int type = p->GivType(); + + if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return NULL; // should never happen + + if ( !ChkLvl(CBotString(), type ) ) + { + pStack->SetError(TX_BREAK, pp); + return NULL; // no object, the error is on the stack + } + + CBotBreak* inst = new CBotBreak(); // creates the object + inst->SetToken(pp); // keeps the operation + + pp = p; + if ( IsOfType( p, TokenTypVar ) ) + { + inst->m_label = pp->GivString(); // register the name of label + if ( !ChkLvl(inst->m_label, type ) ) + { + delete inst; + pStack->SetError(TX_NOLABEL, pp); + return NULL; // no object, the error is on the stack + } + } + + if (IsOfType(p, ID_SEP)) + { + return inst; // return what it wants + } + delete inst; + + pStack->SetError(TX_ENDOF, p->GivStart()); + return NULL; // no object, the error is on the stack +} + +// execution of statement "break" or "continu" + +bool CBotBreak :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); + return pj->Return(pile); +} + +void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( bMain ) pj->RestoreStack(this); +} + + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "try" + +CBotTry::CBotTry() +{ + m_ListCatch = NULL; + m_FinalInst = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotTry"; // debug +} + +CBotTry::~CBotTry() +{ + delete m_ListCatch; // frees the list + delete m_Block; // frees the instruction block + delete m_FinalInst; +} + +CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotTry* inst = new CBotTry(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_TRY)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); + CBotCatch** pn = &inst->m_ListCatch; + + while (pStk->IsOk() && p->GivType() == ID_CATCH) + { + CBotCatch* i = CBotCatch::Compile(p, pStk); + *pn = i; + pn = &i->m_next; + } + + if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) + { + inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); + } + + if (pStk->IsOk()) + { + return pStack->Return(inst, pStk); // return an object to the application + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack +} + +// execution of instruction Try +// manages the return of exceptions +// stops (judgements) by suspension +// and "finally" + +bool CBotTry :: Execute(CBotStack* &pj) +{ + int val; + + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + if ( pile1->IfStep() ) return false; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack + CBotStack* pile2 = pile0->AddStack(); + + if ( pile1->GivState() == 0 ) + { + if ( m_Block->Execute(pile1) ) + { + if ( m_FinalInst == NULL ) return pj->Return(pile1); + pile1->SetState(-2); // passes final + } + + val = pile1->GivError(); + if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? + return false; // does not make the catch + + pile1->IncState(); + pile2->SetState(val); // stores the error number + pile1->SetError(0); // for now there is are more errors! + + if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? + return false; // does not make the catch + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = (short)pile1->GivState(); // where were we? + val = pile2->GivState(); // what error? + pile0->SetState(1); // marking the GetRunPos + + if ( val >= 0 && state > 0 ) while ( pc != NULL ) + { + if ( --state <= 0 ) + { + // request to the catch block if they feel concerned + // demande au bloc catch s'il se sent concerné + if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! + pile1->IncState(); + } + if ( --state <= 0 ) + { + if ( pile2->GivVal() == true ) + { +// pile0->SetState(1); + + if ( !pc->Execute(pile2) ) return false; // performs the operation + if ( m_FinalInst == NULL ) + return pj->Return(pile2); // ends the try + + pile1->SetState(-2); // passes final + break; + } + pile1->IncState(); + } + pc = pc->m_next; + } + if ( m_FinalInst != NULL && + pile1->GivState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final + + if (pile1->GivState() <= -1) + { +// pile0->SetState(1); + + if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; + if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception + pile2->SetError(pile1->GivState()==-1 ? val : 0); // gives the initial error + return pj->Return(pile2); + } + + pile1->SetState(0); // returns to the evaluation + pile0->SetState(0); // returns to the evaluation + if ( val != 0 && m_ListCatch == NULL && m_FinalInst == NULL ) + return pj->Return(pile2); // ends the try without exception + + pile1->SetError(val); // gives the error + return false; // it's not for us +} + + +void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + int val; + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == NULL ) return; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack + if ( pile0 == NULL ) return; + + CBotStack* pile2 = pile0->RestoreStack(); + if ( pile2 == NULL ) return; + + m_Block->RestoreState(pile1, bMain); + if ( pile0->GivState() == 0 ) + { + return; + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = pile1->GivState(); // where were we ? + val = pile2->GivState(); // what error ? + + if ( val >= 0 && state > 0 ) while ( pc != NULL ) + { + if ( --state <= 0 ) + { + // request to the catch block if they feel concerned + // demande au bloc catch s'il se sent concerné + pc->RestoreCondState(pile2, bMain); // suspend ! + return; + } + if ( --state <= 0 ) + { + if ( pile2->GivVal() == true ) + { + pc->RestoreState(pile2, bMain); // execute the operation + return; + } + } + pc = pc->m_next; + } + + if (pile1->GivState() <= -1) + { + m_FinalInst->RestoreState(pile2, bMain); + return; + } +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "catch" + +CBotCatch::CBotCatch() +{ + m_Cond = + m_Block = NULL; // NULL so that delete is not possible further + m_next = NULL; + + name = "CBotCatch"; // debug +} + +CBotCatch::~CBotCatch() +{ + delete m_Cond; // frees the list + delete m_Block; // frees the instruction block + delete m_next; // and subsequent +} + +CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCatch* inst = new CBotCatch(); // creates the object + pStack->SetStartError(p->GivStart()); + + inst->SetToken(p); + if (!IsOfType(p, ID_CATCH)) return NULL; // should never happen + + if (IsOfType(p, ID_OPENPAR)) + { + inst->m_Cond = CBotExpression::Compile(p, pStack); + if (( pStack->GivType() < CBotTypLong || + pStack->GivTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); + if ( pStack->IsOk() ) + return inst; // return an object to the application + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStack->SetError(TX_BADTYPE, p->GivStart()); + } + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; // error, frees up + return NULL; // no object, the error is on the stack +} + +// execution of "catch" + +bool CBotCatch :: Execute(CBotStack* &pj) +{ + if ( m_Block == NULL ) return true; + return m_Block->Execute(pj); // executes the associated block +} + +void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); +} + +void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) +{ + m_Cond->RestoreState(pj, bMain); +} + +// routine to see if the catch is to do or not + +bool CBotCatch :: TestCatch(CBotStack* &pile, int val) +{ + if ( !m_Cond->Execute(pile) ) return false; + + if ( val > 0 || pile->GivType() != CBotTypBoolean ) + { + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + var->SetValInt( pile->GivVal() == val ); + pile->SetVar(var); // calls on the stack + } + + return true; +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compiles instruction "throw" + +CBotThrow::CBotThrow() +{ + m_Value = NULL; // NULL so that delete is not possible further + + name = "CBotThrow"; // debug +} + +CBotThrow::~CBotThrow() +{ + delete m_Value; +} + +CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) +{ + pStack->SetStartError(p->GivStart()); + + CBotThrow* inst = new CBotThrow(); // creates the object + inst->SetToken(p); + + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if (!IsOfType(p, ID_THROW)) return NULL; // should never happen + + inst->m_Value = CBotExpression::Compile( p, pStack ); + + if (pStack->GivType() < CBotTypLong && pStack->IsOk()) + { + return inst; // return an object to the application + } + pStack->SetError(TX_BADTYPE, pp); + + delete inst; // error, frees up + return NULL; // no object, the error is on the stack +} + +// execution of instruction "throw" + +bool CBotThrow :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + if ( pile->GivState() == 0 ) + { + if ( !m_Value->Execute(pile) ) return false; + pile->IncState(); + } + + if ( pile->IfStep() ) return false; + + int val = pile->GivVal(); + if ( val < 0 ) val = TX_BADTHROW; + pile->SetError( val, &m_token ); + return pj->Return( pile ); +} + +void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; + + if ( pile->GivState() == 0 ) + { + m_Value->RestoreState(pile, bMain); + return; + } +} + + + +//////////////////////////////////////////////////////////// + + +CBotStartDebugDD::CBotStartDebugDD() +{ + name = "CBotStartDebugDD"; // debug +} + +CBotStartDebugDD::~CBotStartDebugDD() +{ +} + +CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) +{ + + if (!IsOfType(p, ID_DEBUGDD)) return NULL; // should never happen + + return new CBotStartDebugDD(); // creates the object + +} + +// execution of instruction "throw" + +bool CBotStartDebugDD :: Execute(CBotStack* &pj) +{ + CBotProgram* p = pj->GivBotCall(); + p->m_bDebugDD = true; + + return true; +} + diff --git a/src/CBot/ClassFILE.cpp b/src/CBot/ClassFILE.cpp index 330e814..b6c944c 100644 --- a/src/CBot/ClassFILE.cpp +++ b/src/CBot/ClassFILE.cpp @@ -12,13 +12,15 @@ // * GNU General Public License for more details. // * // * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/.// ClassFile.cpp +// * along with this program. If not, see http://www.gnu.org/licenses/. + +// ClassFile.cpp // -// dfinition des mthodes pour la classe FILE +// definition of methods for class FILE -// Variables statiques +// Static variables static CBotClass* m_pClassFILE; static CBotProgram* m_pFuncFile; @@ -26,7 +28,7 @@ static int m_CompteurFileOpen = 0; -// Prpare un nom de fichier. +// Prepares a file name. void PrepareFilename(CBotString &filename) //DD! { @@ -35,67 +37,67 @@ void PrepareFilename(CBotString &filename) //DD! pos = filename.ReverseFind('\\'); if ( pos > 0 ) { - filename = filename.Mid(pos+1); // enlve les dossiers + filename = filename.Mid(pos+1); // remove the records (files)?? } pos = filename.ReverseFind('/'); if ( pos > 0 ) { - filename = filename.Mid(pos+1); // aussi ceux avec / + filename = filename.Mid(pos+1); // also those with / } pos = filename.ReverseFind(':'); if ( pos > 0 ) { - filename = filename.Mid(pos+1); // enlve aussi la lettre d'unit C: + filename = filename.Mid(pos+1); // also removes the drive letter C: } filename = CBotString("files\\") + filename; } -// constructeur de la classe -// reois le nom du fichier en paramtre +// constructor of the class +// gets the filename as a parameter -// excution +// execution bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { CBotString mode; - // accepte sans paramtre + // accepts no parameters if ( pVar == NULL ) return TRUE; - // qui doit tre une chane de caractres + // must be a string if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return FALSE; } CBotString filename = pVar->GivValString(); PrepareFilename(filename); //DR - // il peut y avoir un second paramtre + // there may be a second parameter pVar = pVar->GivNext(); if ( pVar != NULL ) { - // rcupre le mode + // recovers the mode mode = pVar->GivValString(); if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return FALSE; } - // pas de 3e paramtre + // no third parameter, only two or one possible if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return FALSE; } } - // enregistre le nom du fichier + // save the file name pVar = pThis->GivItem("filename"); pVar->SetValString(filename); if ( ! mode.IsEmpty() ) { - // ouvre le ficher demand + // open the called file FILE* pFile = fopen( filename, mode ); if ( pFile == NULL ) { Exception = CBotErrFileOpen; return FALSE; } m_CompteurFileOpen ++; - // enregiste le canal du fichier + // save the handle of file pVar = pThis->GivItem("handle"); pVar->SetValInt((long)pFile); } @@ -106,21 +108,21 @@ bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exceptio // compilation CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) { - // accepte sans paramtre + // accepts no parameters if ( pVar == NULL ) return CBotTypResult( 0 ); - // qui doit tre une chaine + // must be a string if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - // il peut y avoir un second paramtre + // there may be a second parameter pVar = pVar->GivNext(); if ( pVar != NULL ) { - // qui doit tre une chaine + // must be a string if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - // pas de 3e paramtre + // no third parameter if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); } @@ -129,15 +131,15 @@ CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) } -// destructeur de la classe +// destructor of the class -// excution +// execution bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { - // rcupre l'lment "handle" + // retrieves the element "handle" pVar = pThis->GivItem("handle"); - // pas ouvert ? pas de problme + // not open? no problem if ( pVar->GivInit() != IS_DEF) return TRUE; FILE* pFile= (FILE*)pVar->GivValInt(); @@ -150,52 +152,52 @@ bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception } -// mthode FILE :: open -// reois le mode r/w en paramtre +// FILE :: open method +// get the r / w mode as a parameter -// excution +// execution bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { - // il doit y avoir un paramtre + // there must be a parameter if ( pVar == NULL ) { Exception = CBotErrLowParam; return FALSE; } - // qui doit tre une chane de caractres + // must be a string if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return FALSE; } - // il peut y avoir un second paramtre + // there may be a second parameter if ( pVar->GivNext() != NULL ) { - // dans ce cas le premier paramtre est le nom du fichier + // in this case the first parameter is the file name CBotString filename = pVar->GivValString(); PrepareFilename(filename); //DR - // enregistre le nom du fichier + // saves the file name CBotVar* pVar2 = pThis->GivItem("filename"); pVar2->SetValString(filename); - // paramtre suivant est le mode + // next parameter is the mode pVar = pVar -> GivNext(); } CBotString mode = pVar->GivValString(); if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return FALSE; } - // pas de 3e paramtre + // No third parameter if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return FALSE; } - // rcupre l'lment "handle" + // retrieves the element "handle" pVar = pThis->GivItem("handle"); - // qui doit pas tre initialis + // which must not be initialized if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return FALSE; } - // reprend le nom du fichier + // contains filename pVar = pThis->GivItem("filename"); CBotString filename = pVar->GivValString(); - PrepareFilename(filename); //DD! (si le nom a t attribu par h.filename = "..."; + PrepareFilename(filename); //DD! (if the name was assigned by h.filename = "..."; - // ouvre le ficher demand + // open requsted file FILE* pFile = fopen( filename, mode ); if ( pFile == NULL ) //DR { @@ -205,7 +207,7 @@ bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) m_CompteurFileOpen ++; - // enregiste le canal du fichier + // saves the handle of file pVar = pThis->GivItem("handle"); pVar->SetValInt((long)pFile); @@ -216,39 +218,39 @@ bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) // compilation CBotTypResult cfopen (CBotVar* pThis, CBotVar* &pVar) { - // il doit y avoir un paramtre + // there must be a parameter if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); - // qui doit tre une chaine + // must be a string if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - // il peut y avoir un second paramtre + // there may be a second parameter pVar = pVar->GivNext(); if ( pVar != NULL ) { - // qui doit tre une chaine + // must be a string if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - // pas de 3e paramtre + // no third parameter if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); } - // le rsultat est de type bool + // the result is of type bool return CBotTypResult(CBotTypBoolean); //DR } -// mthode FILE :: close +// FILE :: close method -// excution +// execution bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { - // il ne doit pas y avoir de paramtre + // it should not be any parameter if ( pVar != NULL ) return CBotErrOverParam; - // rcupre l'lment "handle" + // retrieves the element "handle" pVar = pThis->GivItem("handle"); if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } @@ -265,27 +267,27 @@ bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) // compilation CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) { - // il ne doit pas y avoir de paramtre + // it should not be any parameter if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - // la fonction retourne un rsultat "void" + // function returns a result "void" return CBotTypResult( 0 ); } -// mthode FILE :: writeln +// FILE :: writeln method -// excution +// execution bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { - // il doit y avoir un paramtre + // there must be a parameter if ( pVar == NULL ) { Exception = CBotErrLowParam; return FALSE; } - // qui doit tre une chane de caractres + // must be a string if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return FALSE; } CBotString param = pVar->GivValString(); - // rcupre l'lment "handle" + //retrieves the element "handle" pVar = pThis->GivItem("handle"); if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } @@ -294,7 +296,7 @@ bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) int res = fputs(param+CBotString("\n"), pFile); - // en cas d'erreur gnre une exception + // on error throws an exception if ( res < 0 ) { Exception = CBotErrWrite; return FALSE; } return TRUE; @@ -303,28 +305,28 @@ bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) // compilation CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) { - // il doit y avoir un paramtre + // there must be a parameter if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); - // qui doit tre une chane de caractres + // must be a string if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - // pas d'autre paramtre + // no other parameter if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - // la fonction retourne un rsultat void + // function returns "void" result return CBotTypResult( 0 ); } -// mthode FILE :: readln +// FILE :: readln method -// excution +// execution bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { - // il ne doit pas y avoir de paramtre + // there shouldn't be any parameter if ( pVar != NULL ) { Exception = CBotErrOverParam; return FALSE; } - // rcupre l'lment "handle" + //retrieves the element "handle" pVar = pThis->GivItem("handle"); if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } @@ -339,7 +341,7 @@ bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) for ( i = 0 ; i < 2000 ; i++ ) if (chaine[i] == '\n') chaine[i] = 0; - // en cas d'erreur gnre une exception + // on error throws an exception if ( ferror(pFile) ) { Exception = CBotErrRead; return FALSE; } pResult->SetValString( chaine ); @@ -350,22 +352,22 @@ bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) // compilation CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) { - // il ne doit pas y avoir de paramtre + // there shouldn't be any parameter if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - // la fonction retourne un rsultat "string" + // function return "string" result return CBotTypResult( CBotTypString ); } -// mthode FILE :: readln +// FILE :: readln method -// excution +// execution bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { - // il ne doit pas y avoir de paramtre + // there shouldn't be any parameter if ( pVar != NULL ) { Exception = CBotErrOverParam; return FALSE; } - // rcupre l'lment "handle" + // retrieves the element "handle" pVar = pThis->GivItem("handle"); if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } @@ -380,10 +382,10 @@ bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) // compilation CBotTypResult cfeof (CBotVar* pThis, CBotVar* &pVar) { - // il ne doit pas y avoir de paramtre + // there shouldn't be any parameter if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - // la fonction retourne un rsultat booleen + // function return boolean result return CBotTypResult( CBotTypBoolean ); } @@ -393,25 +395,25 @@ CBotTypResult cfeof (CBotVar* pThis, CBotVar* &pVar) void InitClassFILE() { -// cre une classe pour la gestion des fichiers -// l'utilisation en est la suivante: +// creates a class for file management +// the usage is as follows: // file canal( "NomFichier.txt" ) -// canal.open( "r" ); // ouvre en lecture -// s = canal.readln( ); // lit une ligne -// canal.close(); // referme le fichier +// canal.open( "r" ); // open reading +// s = canal.readln( ); // reads a line +// canal.close(); // closes the file - // cre la classe FILE + // create class FILE m_pClassFILE = new CBotClass("file", NULL); - // ajoute le composant ".filename" + // add the component ".filename" m_pClassFILE->AddItem("filename", CBotTypString); - // ajoute le composant ".handle" + // add the component ".handle" m_pClassFILE->AddItem("handle", CBotTypInt, PR_PRIVATE); - // dfini un constructeur et un destructeur + // define a constructor and destructor m_pClassFILE->AddFunction("file", rfconstruct, cfconstruct ); m_pClassFILE->AddFunction("~file", rfdestruct, NULL ); - // dfini les mthodes associes + // defined associated methods m_pClassFILE->AddFunction("open", rfopen, cfopen ); m_pClassFILE->AddFunction("close", rfclose, cfclose ); m_pClassFILE->AddFunction("writeln", rfwrite, cfwrite ); @@ -421,6 +423,6 @@ void InitClassFILE() m_pFuncFile = new CBotProgram( ); CBotStringArray ListFonctions; m_pFuncFile->Compile( "public file openfile(string name, string mode) {return new file(name, mode);}", ListFonctions); - m_pFuncFile->SetIdent(-2); // identificateur spcial pour RestoreState dans cette fonction + m_pFuncFile->SetIdent(-2); // restoreState as a special identifier for this function } -- cgit v1.2.3-1-g7c22 From a1c83c7d0a45fd6cbd8cbd1662aa0a0f0c7e1396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopacki?= Date: Tue, 7 Aug 2012 12:46:19 +0200 Subject: CBot library comments further translations --- src/CBot/CBotDll.h | 790 ++++----- src/CBot/CBotIf.cpp | 320 ++-- src/CBot/CBotVar.cpp | 4493 +++++++++++++++++++++++++------------------------- 3 files changed, 2827 insertions(+), 2776 deletions(-) diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index f2e2297..47388a6 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -12,7 +12,10 @@ // * GNU General Public License for more details. // * // * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/.//////////////////////////////////////////////////////////////////////// +// * along with this program. If not, see http://www.gnu.org/licenses/. +//////////////////////////////////////////////////////////////////////// + +#pragma once #ifndef _CBOTDLL_H_ #define _CBOTDLL_H_ /** @@ -39,7 +42,7 @@ class CBotFunction; // user functions class CBotVar; // variables class CBotVarClass; // instance of class class CBotVarPointer; // pointer to an instance of class -class CBotCall; // fonctions +class CBotCall; // functions class CBotCallMethode; // methods class CBotDefParam; // parameter list class CBotCStack; // stack @@ -84,79 +87,80 @@ class CBotTypResult { public: /** - * \brief CBotTypResult constructor of an object + * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) * \param type type of created result, see CBotType */ CBotTypResult(int type); + // for simple types (CBotTypInt à CBotTypString) - /** - * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) - * \param type type of created result, see CBotType - * \param name - */ - // pour les types simples (CBotTypInt CBotTypString) + CBotTypResult(int type, const char* name); - // pour les types pointeur et classe intrinsic + // for pointer types and intrinsic classes + CBotTypResult(int type, CBotClass* pClass); - // idem partir de l'instance d'une classe + // for the instance of a class + CBotTypResult(int type, CBotTypResult elem); - // pour les tableaux de variables - + // for arrays of variables + CBotTypResult(const CBotTypResult& typ); - // pour les assignations + // for assignments + CBotTypResult(); - // pour par dfaut + // for default + ~CBotTypResult(); int GivType(int mode = 0) const; - // rend le type CBotTyp* du rsultat + // returns type CBotType* as a result void SetType(int n); - // modifie le type + // modifies a type CBotClass* GivClass() const; - // rend le pointeur la classe (pour les CBotTypClass, CBotTypPointer) + // makes the pointer to the class (for CBotTypClass, CBotTypPointer) int GivLimite() const; - // rend la taille limite du tableau (CBotTypArray) + // returns limit size of table (CBotTypArray) void SetLimite(int n); - // fixe une limite au tableau + // set limit to the table void SetArray(int* max ); - // idem avec une liste de dimension (tableaux de tableaux) + // set limits for a list of dimensions (arrays of arrays) CBotTypResult& GivTypElem() const; - // rend le type des lments du tableau (CBotTypArray) + // returns type of array elements (CBotTypArray) + // rend le type des éléments du tableau (CBotTypArray) bool Compare(const CBotTypResult& typ) const; - // compare si les types sont compatibles + // compares whether the types are compatible bool Eq(int type) const; - // compare le type + // compare type + + CBotTypResult& operator=(const CBotTypResult& src); + // copy a complete type in another - CBotTypResult& - operator=(const CBotTypResult& src); - // copie un type complet dans un autre private: int m_type; - CBotTypResult* m_pNext; // pour les types de types - CBotClass* m_pClass; // pour les drivs de classe - int m_limite; // limitation des tableaux + CBotTypResult* m_pNext; // for the types of type + CBotClass* m_pClass; // for the derivatives of class + int m_limite; // limits of tables friend class CBotVarClass; friend class CBotVarPointer; }; /* -// pour dfinir un rsultat en sortie, utiliser par exemple +// to define a result as output, using for example - // pour rendre un simple Float + // to return a simple Float return CBotTypResult( CBotTypFloat ); - // pour rendre un tableau de string + // to return a string array return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); - // pour rendre un tableau de tableau de "point" + // to return un array of array of "point" class CBotTypResult typPoint( CBotTypIntrinsic, "point" ); CBotTypResult arrPoint( CBotTypArray, typPoint ); return CBotTypResult( CBotTypArray, arrPoint ); @@ -164,87 +168,88 @@ private: //////////////////////////////////////////////////////////////////////// -// Gestion des erreurs compilation et excution +// Error Handling of compilation and execution //////////////////////////////////////////////////////////////////////// -// voici la liste des erreurs pouvant tre retournes par le module -// pour la compilation +// Here are the list of errors that can be returned by the module +// for compilation -#define CBotErrOpenPar 5000 // manque la parenthse ouvrante -#define CBotErrClosePar 5001 // manque la parenthse fermante -#define CBotErrNotBoolean 5002 // l'expression doit tre un boolean -#define CBotErrUndefVar 5003 // variable non dclare -#define CBotErrBadLeft 5004 // assignation impossible ( 5 = ... ) -#define CBotErrNoTerminator 5005 // point-virgule attendu -#define CBotErrCaseOut 5006 // case en dehors d'un switch +#define CBotErrOpenPar 5000 // missing the opening parenthesis +#define CBotErrClosePar 5001 // missing the closing parenthesis +#define CBotErrNotBoolean 5002 // expression must be a boolean +#define CBotErrUndefVar 5003 // undeclared variable +#define CBotErrBadLeft 5004 // assignment impossible ( 5 = ... ) +#define CBotErrNoTerminator 5005 // semicolon expected +#define CBotErrCaseOut 5006 // case outside a switch // CBotErrNoTerm 5007, plus utile -#define CBotErrCloseBlock 5008 // manque " } " -#define CBotErrElseWhitoutIf 5009 // else sans if correspondant -#define CBotErrOpenBlock 5010 // manque " { " -#define CBotErrBadType1 5011 // mauvais type pour l'assignation -#define CBotErrRedefVar 5012 // redfinition de la variable -#define CBotErrBadType2 5013 // 2 oprandes de type incompatibles -#define CBotErrUndefCall 5014 // routine inconnue -#define CBotErrNoDoubleDots 5015 // " : " attendu +#define CBotErrCloseBlock 5008 // missing " } " +#define CBotErrElseWhitoutIf 5009 // else without matching if +#define CBotErrOpenBlock 5010 // missing " { " +#define CBotErrBadType1 5011 // wrong type for the assignment +#define CBotErrRedefVar 5012 // redefinition of the variable +#define CBotErrBadType2 5013 // Two operands are incompatible +#define CBotErrUndefCall 5014 // routine undefined +#define CBotErrNoDoubleDots 5015 // " : " expected // CBotErrWhile 5016, plus utile -#define CBotErrBreakOutside 5017 // break en dehors d'une boucle -#define CBotErrUndefLabel 5019 // label inconnu -#define CBotErrLabel 5018 // label ne peut se mettre ici -#define CBotErrNoCase 5020 // manque " case " -#define CBotErrBadNum 5021 // nombre attendu -#define CBotErrVoid 5022 // " void " pas possible ici -#define CBotErrNoType 5023 // dclaration de type attendue -#define CBotErrNoVar 5024 // nom de variable attendu -#define CBotErrNoFunc 5025 // nom de fonction attendu -#define CBotErrOverParam 5026 // trop de paramtres -#define CBotErrRedefFunc 5027 // cette fonction existe dj -#define CBotErrLowParam 5028 // pas assez de paramtres -#define CBotErrBadParam 5029 // mauvais types de paramtres -#define CBotErrNbParam 5030 // mauvais nombre de paramtres -#define CBotErrUndefItem 5031 // lment n'existe pas dans la classe -#define CBotErrUndefClass 5032 // variable n'est pas une classe -#define CBotErrNoConstruct 5033 // pas de constructeur appropri -#define CBotErrRedefClass 5034 // classe existe dj -#define CBotErrCloseIndex 5035 // " ] " attendu -#define CBotErrReserved 5036 // mot rserv (par un DefineNum) -#define CBotErrBadNew 5037 // mauvais paramtre pour new -#define CBotErrOpenIndex 5038 // " [ " attendu -#define CBotErrBadString 5039 // chane de caractre attendue -#define CBotErrBadIndex 5040 // mauvais type d'index "[ false ]" -#define CBotErrPrivate 5041 // lment protg -#define CBotErrNoPublic 5042 // manque le mot "public" - -// voici la liste des erreurs pouvant tre retournes par le module -// pour l'excution - -#define CBotErrZeroDiv 6000 // division par zro -#define CBotErrNotInit 6001 // variable non initialise -#define CBotErrBadThrow 6002 // throw d'une valeur ngative -#define CBotErrNoRetVal 6003 // fonction n'a pas retourn de rsultat -#define CBotErrNoRun 6004 // Run() sans fonction active -#define CBotErrUndefFunc 6005 // appel d'une fonction qui n'existe plus -#define CBotErrNotClass 6006 // cette classe n'existe pas -#define CBotErrNull 6007 // pointeur null -#define CBotErrNan 6008 // calcul avec un NAN -#define CBotErrOutArray 6009 // index hors du tableau -#define CBotErrStackOver 6010 // dpassement de la pile -#define CBotErrDeletedPtr 6011 // pointeur un objet dtruit - -#define CBotErrFileOpen 6012 // ouverture du fichier impossible -#define CBotErrNotOpen 6013 // canal pas ouvert -#define CBotErrRead 6014 // erreur la lecture -#define CBotErrWrite 6015 // erreur l'criture - -// d'autres valeurs peuvent tre rendues -// par exemple les exceptions rendues par les routines externes -// et les " throw " avec un nombre quelconque. +#define CBotErrBreakOutside 5017 // break outside of a loop +#define CBotErrUndefLabel 5019 // label udnefined +#define CBotErrLabel 5018 // label ne peut se mettre ici (label can not get here) +#define CBotErrNoCase 5020 // missing " case " +#define CBotErrBadNum 5021 // expected number +#define CBotErrVoid 5022 // " void " not possible here +#define CBotErrNoType 5023 // type declaration expected +#define CBotErrNoVar 5024 // variable name expected +#define CBotErrNoFunc 5025 // expected function name +#define CBotErrOverParam 5026 // too many parameters +#define CBotErrRedefFunc 5027 // this function already exists +#define CBotErrLowParam 5028 // not enough parameters +#define CBotErrBadParam 5029 // wrong types of parameters +#define CBotErrNbParam 5030 // wrong number of parameters +#define CBotErrUndefItem 5031 // element does not exist in the class +#define CBotErrUndefClass 5032 // variable is not a class +#define CBotErrNoConstruct 5033 // no appropriate constructor +#define CBotErrRedefClass 5034 // class already exists +#define CBotErrCloseIndex 5035 // " ] " expected +#define CBotErrReserved 5036 // reserved word (for a DefineNum) +#define CBotErrBadNew 5037 // wrong setting for new +#define CBotErrOpenIndex 5038 // " [ " expected +#define CBotErrBadString 5039 // expected string +#define CBotErrBadIndex 5040 // wrong index type "[ false ]" +#define CBotErrPrivate 5041 // protected item +#define CBotErrNoPublic 5042 // missing word "public" + +// here is the list of errors that can be returned by the module +// for the execution + +#define CBotErrZeroDiv 6000 // division by zero +#define CBotErrNotInit 6001 // uninitialized variable +#define CBotErrBadThrow 6002 // throw a negative value +#define CBotErrNoRetVal 6003 // function did not return results +#define CBotErrNoRun 6004 // Run() without active function +#define CBotErrUndefFunc 6005 // calling a function that no longer exists +#define CBotErrNotClass 6006 // this class does not exist +#define CBotErrNull 6007 // null pointer +#define CBotErrNan 6008 // calculation with a NAN +#define CBotErrOutArray 6009 // index out of array +#define CBotErrStackOver 6010 // stack overflow +#define CBotErrDeletedPtr 6011 // pointer to an object destroyed + +#define CBotErrFileOpen 6012 // cannot open the file +#define CBotErrNotOpen 6013 // channel not open +#define CBotErrRead 6014 // error while reading +#define CBotErrWrite 6015 // writing error + + +// other values ​​may be returned +// for example exceptions returned by external routines +// and " throw " with any number. //////////////////////////////////////////////////////////////////////// // -// car CString fait partie de MFC pas utilis ici. +// as part of MFC CString not used here. // -// ( toutes les fonctions ne sont pas encore implmentes ) +// ( all functions are not implemented yet ) /** \brief CBotString Class used to work on strings */ class CBotString @@ -320,27 +325,27 @@ private: }; -// idem avec la gestion en tableau +// Class used to array management class CBotStringArray : public CBotString { private: - int m_nSize; // nombre d'lments - int m_nMaxSize; // taille rserve - CBotString* m_pData; // ^aux donnes + int m_nSize; // number of elements + int m_nMaxSize; // reserved size + CBotString* m_pData; // ^data public: CBotStringArray(); ~CBotStringArray(); void SetSize(int nb); - int GivSize(); + int GivSize(); void Add(const CBotString& str); CBotString& operator[](int nIndex); CBotString& ElementAt(int nIndex); }; -// diffrents mode pour GetPosition +// different modes for GetPosition enum CBotGet { GetPosExtern = 1, @@ -350,44 +355,41 @@ enum CBotGet }; //////////////////////////////////////////////////////////////////// -// classe principale grant un programme CBot +// main class managing CBot program // class CBotProgram { private: - CBotFunction* m_Prog; // les fonctions dfinies par l'utilisateur - CBotFunction* m_pRun; // la fonction de base pour l'excution - CBotClass* m_pClass; // les classes dfinies dans cette partie - CBotStack* m_pStack; // la pile d'excution - CBotVar* m_pInstance; // instance de la classe parent + CBotFunction* m_Prog; // the user-defined functions + CBotFunction* m_pRun; // the basic function for the execution + CBotClass* m_pClass; // classes defined in this part + CBotStack* m_pStack; // execution stack + CBotVar* m_pInstance; // instance of the parent class friend class CBotFunction; int m_ErrorCode; int m_ErrorStart; int m_ErrorEnd; - long m_Ident; // identificateur associ + long m_Ident; // associated identifier public: - static - CBotString m_DebugVarStr; // a fin de debug - bool m_bDebugDD; // idem dclanchable par robot - - bool m_bCompileClass; + static CBotString m_DebugVarStr; // end of a debug + bool m_bDebugDD; // idem déclanchable par robot \TODO ??? + bool m_bCompileClass; public: - static - void Init(); - // initialise le module (dfini les mots clefs pour les erreurs) - // doit tre fait une fois (et une seule) au tout dbut + static void Init(); + // initializes the module (defined keywords for errors) + // should be done once (and only one) at the beginning static void Free(); - // libre les zones mmoires statiques + // frees the static memory areas static int GivVersion(); - // donne la version de la librairie CBOT + // gives the version of the library CBOT CBotProgram(); @@ -395,88 +397,87 @@ public: ~CBotProgram(); bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); - // compile le programme donn en texte - // retourne false s'il y a une erreur la compilation - // voir GetCompileError() pour rcuprer l'erreur - // ListFonctions retourne le nom des fonctions dclares extern - // pUser permet de passer un pointeur pour les routines dfinies par AddFunction + // compiles the program given in text + // returns false if an error at compile + // see GetCompileError () to retrieve the error + // ListFonctions returns the names of functions declared as extern + // pUser can pass a pointer to routines defined by AddFunction void SetIdent(long n); - // associe un identificateur avec l'instance CBotProgram + // associates an identifier with the instance CBotProgram long GivIdent(); - // redonne l'identificateur + // gives the identifier int GivError(); bool GetError(int& code, int& start, int& end); bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); - // si true - // donne l'erreur trouve la compilation - // ou l'excution - // start et end dlimite le bloc o se trouve l'erreur - // pProg permet de savoir dans quel "module" s'est produite l'erreur d'excution - static - CBotString GivErrorText(int code); + // if true + // gives the error found in the compilation + // or execution + // delimits the start and end block where the error + // pProg lets you know what "module" has produced runtime error + static CBotString GivErrorText(int code); bool Start(const char* name); - // dfinie quelle fonction doit tre excute - // retourne false si la fontion name n'est pas trouve - // le programme ne fait rien, il faut appeller Run() pour cela + // defines what function should be executed + // returns false if the funtion name is not found + // the program does nothing, we must call Run () for this bool Run(void* pUser = NULL, int timer = -1); - // excute le programme - // retourne false si le programme a t suspendu - // retourne true si le programme s'est termin avec ou sans erreur - // timer = 0 permet de faire une avance pas pas + // executes the program + // returns false if the program was suspended + // returns true if the program ended with or without error + // timer = 0 allows to advance step by step bool GetRunPos(const char* &FunctionName, int &start, int &end); - // donne la position dans le programme en excution - // retourne false si on n'est pas en excution (programme termin) - // FunctionName est un pointeur rendu sur le nom de la fonction - // start et end la position dans le texte du token en traitement + // gives the position in the executing program + // returns false if it is not running (program completion) + // FunctionName is a pointer made to the name of the function + // start and end position in the text of the token processing CBotVar* GivStackVars(const char* &FunctionName, int level); - // permet d'obtenir le pointeur aux variables sur la pile d'excution - // level est un paramtre d'entre, 0 pour le dernier niveau, -1, -2, etc pour les autres niveau - // la valeur retourne (CBotVar*) est une liste de variable (ou NULL) - // qui peut tre trait que la liste des paramtres reu par une routine - // FunctionName donne le nom de la fonction o se trouvent ces variables - // FunctionName == NULL signifiant qu'on est plus dans le programme (selon level) + // provides the pointer to the variables on the execution stack + // level is an input parameter, 0 for the last level, -1, -2, etc. for the other levels + // the return value (CBotVar *) is a variable list (or NULL) + // that can be processed as the list of parameters received by a routine + // FunctionName gives the name of the function where are these variables + // FunctionName == NULL means that is more in a program (depending on level) void Stop(); - // arrte l'excution du programme - // quitte donc le mode "suspendu" + // stops execution of the program + // therefore quits "suspend" mode static void SetTimer(int n); - // dfini le nombre de pas (parties d'instructions) faire - // dans Run() avant de rendre la main "false" + // defines the number of steps (parts of instructions) to done + // in Run() before rendering hand "false" \TODO avant de rendre la main "false" static bool AddFunction(const char* name, bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - // cet appel permet d'ajouter de manire externe (**) - // une nouvelle fonction utilisable par le programme CBot + // call this to add externally (**) + // a new function used by the program CBoT static bool DefineNum(const char* name, long val); bool SaveState(FILE* pf); - // sauvegarde l'tat d'excution dans le fichier - // le fichier doit avoir t ouvert avec l'appel fopen de cette dll - // sinon le systme plante + // backup the execution status in the file + // the file must have been opened with the fopen call this dll (\TODO this library??) + // if the system crashes bool RestoreState(FILE* pf); - // rtablie l'tat de l'excution depuis le fichier - // le programme compil doit videmment tre identique + // restores the state of execution from file + // the compiled program must obviously be the same bool GetPosition(const char* name, int& start, int& stop, CBotGet modestart = GetPosExtern, CBotGet modestop = GetPosBloc); - // donne la position d'une routine dans le texte d'origine - // le mode permet de choisir l'lment trouver pour le dbut et la fin - // voir les modes ci-dessus dans CBotGet + // gives the position of a routine in the original text + // the user can select the item to find from the beginning to the end + // see the above modes in CBotGet CBotFunction* GivFunctions(); @@ -484,7 +485,7 @@ public: /////////////////////////////////////////////////////////////////////////////// -// routines pour la gestion d'un fichier (FILE*) +// routines for file management (* FILE) FILE* fOpen(const char* name, const char* mode); int fClose(FILE* filehandle); size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); @@ -494,33 +495,33 @@ public: #if 0 /* (**) Note: - Pour dfinir une fonction externe, il faut procder ainsi: + To define an external function, proceed as follows: - a) dfinir une routine pour la compilation - cette routine reois la liste des paramtres (sans valeurs) - et retourne soit un type de rsultat (CBotTyp... ou 0 = void) - soit un numro d'erreur - b) dfinir une routine pour l'excution - cette rourine reCoit la liste des paramtres (avec valeurs), - une variable pour stocker le rsultat (selon le type donn la compilation) + a) define a routine for compilation + this routine receive list of parameters (no values) + and either returns a result type (CBotTyp... or 0 = void) + or an error number + b) define a routine for the execution + this routine receive list of parameters (with valeurs), + a variable to store the result (according to the given type at compile time) - Par exemple, une routine qui calcule la moyenne d'une liste de paramtres */ + For example, a routine which calculates the mean of a parameter list */ -int cMoyenne(CBotVar* &pVar, CBotString& ClassName) +int cMean(CBotVar* &pVar, CBotString& ClassName) { - if ( pVar == NULL ) return 6001; // il n'y a aucun paramtre ! + if ( pVar == NULL ) return 6001; // there is no parameter! while ( pVar != NULL ) { - if ( pVar->GivType() > CBotTypDouble ) return 6002; // ce n'est pas un nombre + if ( pVar->GivType() > CBotTypDouble ) return 6002; // this is not a number pVar = pVar -> GivNext(); } - return CBotTypFloat; // le type du rsultat pourrait dpendre des paramtres ! + return CBotTypFloat; // the type of the result may depend on the parameters! } -bool rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) +bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) { float total = 0; int nb = 0; @@ -530,70 +531,70 @@ bool rMoyenne(CBotVar* pVar, CBotVar* pResult, int& Exception) pVar = pVar->GivNext(); nb++; } - pResult->SetValFloat(total/nb); // retourne la valeur moyenne + pResult->SetValFloat(total/nb); // returns the mean value - return true; // opration totalement termine + return true; // operation fully completed } #endif ///////////////////////////////////////////////////////////////////////////////// -// Classe pour la gestion des variables +// Class for managing variables -// peuvent tre utile l'exterieur du module -// ( il n'est pour l'instant pas prvu de pouvoir crer ces objets en externe ) +// may be useful to the outside of the module +// ( it is currently not expected to be able to create these objects in outer ) -// rsultats pour GivInit() -#define IS_UNDEF 0 // variable indfinie -#define IS_DEF 1 // variable dfinie -#define IS_NAN 999 // variable dfinie comme tant not a number +// results of GivInit() +#define IS_UNDEF 0 // undefined variable +#define IS_DEF 1 // variable defined +#define IS_NAN 999 // variable defined as not a number -// type de variable SetPrivate / IsPrivate -#define PR_PUBLIC 0 // variable publique +// variable type SetPrivate / IsPrivate +#define PR_PUBLIC 0 // public variable #define PR_READ 1 // read only -#define PR_PROTECT 2 // protected (hritage) -#define PR_PRIVATE 3 // strictement prive +#define PR_PROTECT 2 // protected (inheritance) +#define PR_PRIVATE 3 // strictly private class CBotVar { protected: - CBotToken* m_token; // le token correspondant + CBotToken* m_token; // the corresponding token - CBotVar* m_next; // liste de variables + CBotVar* m_next; // list of variables friend class CBotStack; friend class CBotCStack; friend class CBotInstrCall; friend class CBotProgram; - CBotTypResult m_type; // type de valeur + CBotTypResult m_type; // type of value - int m_binit; // pas initialise ? - CBotVarClass* m_pMyThis; // ^lment this correspondant - void* m_pUserPtr; // ^donnes user s'il y a lieu - bool m_bStatic; // lment static (dans une classe) - int m_mPrivate; // lment public, protected ou private ? + int m_binit; // not initialized? + CBotVarClass* m_pMyThis; // ^ corresponding this element + void* m_pUserPtr; // ^user data if necessary + bool m_bStatic; // static element (in class) + int m_mPrivate; // element public, protected or private? - CBotInstr* m_InitExpr; // expression pour le contenu initial - CBotInstr* m_LimExpr; // liste des limites pour un tableau + CBotInstr* m_InitExpr; // expression for the original content + CBotInstr* m_LimExpr; // list of limits for a table friend class CBotClass; friend class CBotVarClass; friend class CBotVarPointer; friend class CBotVarArray; - long m_ident; // identificateur unique - static long m_identcpt; // compteur + long m_ident; // unique identifier + static long m_identcpt; // counter public: CBotVar(); -virtual ~CBotVar( ); // destructeur +virtual ~CBotVar( ); // destructor static CBotVar* Create( const char* name, CBotTypResult type); - // idem partir du type complet + // creates from a complete type static CBotVar* Create( const char* name, CBotClass* pClass); - // idem pour une instance d'une classe connue + // creates from one instance of a known class static CBotVar* Create( const CBotToken* name, int type ); @@ -608,31 +609,32 @@ virtual ~CBotVar( ); // destructeur void SetUserPtr(void* pUser); - // associe un pointeur utilisateur une instance + // associate a user pointer to an instance virtual void SetIdent(long UniqId); - // associe un identificateur unique une instance - // ( c'est l'utilisateur de s'assurer que l'id est unique) + // associates a unique identifier to an instance + // ( it is used to ensure that the id is unique) void* GivUserPtr(); - // rend le pointeur associ la variable + // makes the pointer associated with the variable - CBotString GivName(); // le nom de la variable, s'il est connu + CBotString GivName(); // the name of the variable, if known //////////////////////////////////////////////////////////////////////////////////// - void SetName(const char* name); // change le nom de la variable + void SetName(const char* name); // changes the name of the variable - int GivType(int mode = 0); // rend le type de base (int) de la variable + int GivType(int mode = 0); // returns the base type (int) of the variable + // TODO check it //////////////////////////////////////////////////////////////////////////////////////// - CBotTypResult GivTypResult(int mode = 0); // rend le type complet de la variable + CBotTypResult GivTypResult(int mode = 0); // returns the complete type of the variable CBotToken* GivToken(); void SetType(CBotTypResult& type); - void SetInit(int bInit); // met la variable dans l'tat IS_UNDEF, IS_DEF, IS_NAN + void SetInit(int bInit); // is the variable in the state IS_UNDEF, IS_DEF, IS_NAN - int GivInit(); // donne l'tat de la variable + int GivInit(); // gives the state of the variable void SetStatic(bool bStatic); bool IsStatic(); @@ -644,51 +646,51 @@ virtual ~CBotVar( ); // destructeur virtual void ConstructorSet(); - void SetVal(CBotVar* var); // remprend une valeur - + void SetVal(CBotVar* var); // remprend une valeur + // TODO remprend value virtual - CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) + CBotVar* GivItem(const char* name); // returns an element of a class according to its name (*) virtual - CBotVar* GivItemRef(int nIdent); // idem partir du n ref - + CBotVar* GivItemRef(int nIdent); // idem à partir du n° ref + // TODO ditto from ref no. virtual CBotVar* GivItem(int row, bool bGrow = false); virtual - CBotVar* GivItemList(); // donne la liste des lments + CBotVar* GivItemList(); // lists the elements - CBotVar* GivStaticVar(); // rend le pointeur la variable si elle est statique + CBotVar* GivStaticVar(); // makes the pointer to the variable if it is static bool IsElemOfClass(const char* name); - // dit si l'lment appartient la classe "name" - // rend true si l'objet est d'une classe fille + // said if the element belongs to the class "name" + // makes true if the object is a subclass - CBotVar* GivNext(); // prochaine variable dans la liste (paramtres) + CBotVar* GivNext(); // next variable in the list (parameters) //////////////////////////////////////////////////////////////////////////////////////////// - void AddNext(CBotVar* pVar); // ajoute dans une liste + void AddNext(CBotVar* pVar); // added to a list virtual - void Copy(CBotVar* pSrc, bool bName = true); // fait une copie de la variable + void Copy(CBotVar* pSrc, bool bName = true); // makes a copy of the variable virtual void SetValInt(int val, const char* name = NULL); - // initialise avec une valeur entire (#) + // initialized with an integer value (#) ///////////////////////////////////////////////////////////////////////////////// - virtual void SetValFloat(float val); // initialise avec une valeur relle (#) + virtual void SetValFloat(float val); // initialized with a real value (#) //////////////////////////////////////////////////////////////////////////////// - virtual void SetValString(const char* p);// initialise avec une valeur chane (#) + virtual void SetValString(const char* p);// initialized with a string value (#) //////////////////////////////////////////////////////////////////////////////// - virtual int GivValInt(); // demande la valeur entire (#) + virtual int GivValInt(); // request the full value (#) //////////////////////////////////////////////////////////////////////// - virtual float GivValFloat(); // demande la valeur relle (#) + virtual float GivValFloat(); // gets real value (#) /////////////////////////////////////////////////////////////////////// virtual - CBotString GivValString(); // demande la valeur chane (#) + CBotString GivValString(); // request the string value (#) /////////////////////////////////////////////////////////////////////// virtual void SetClass(CBotClass* pClass); @@ -701,11 +703,11 @@ virtual ~CBotVar( ); // destructeur // virtual void SetIndirection(CBotVar* pVar); virtual void Add(CBotVar* left, CBotVar* right); // addition - virtual void Sub(CBotVar* left, CBotVar* right); // soustraction + virtual void Sub(CBotVar* left, CBotVar* right); // subtraction virtual void Mul(CBotVar* left, CBotVar* right); // multiplication virtual int Div(CBotVar* left, CBotVar* right); // division - virtual int Modulo(CBotVar* left, CBotVar* right); // reste de division - virtual void Power(CBotVar* left, CBotVar* right); // puissance + virtual int Modulo(CBotVar* left, CBotVar* right); // remainder of division + virtual void Power(CBotVar* left, CBotVar* right); // power virtual bool Lo(CBotVar* left, CBotVar* right); virtual bool Hi(CBotVar* left, CBotVar* right); @@ -745,98 +747,98 @@ virtual ~CBotVar( ); // destructeur }; /* NOTE (#) - les mthodes SetValInt() SetValFloat() et SetValString() - ne peuvent tes appelles qu'avec des objets respectivement entier, relle ou chane - toujours s'assurer du type de la variable avant d'appeller ces mthodes + methods SetValInt() SetValFloat() et SetValString() + can be called with objects which are respectively integer, real or string + Always be sure of the type of the variable before calling these methods if ( pVar->GivType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! - les mthodes GivValInt(), GivValFloat() et GivValString() - font des conversions de valeur, - GivValString() fonctionne sur des nombres (rend la chane correspondante) - par contre il ne faut pas faire de GivValInt() avec une variable de type chane ! + methods GivValInt(), GivValFloat() et GivValString() + use value conversions, + GivValString() works on numbers (makes the corresponding string) + but do not make GivValInt () with a string variable! */ //////////////////////////////////////////////////////////////////////// -// Gestion des classes +// management of classes //////////////////////////////////////////////////////////////////////// -// classe pour dfinir de nouvelle classes dans le language CBOT -// par exemple pour dfinir la classe CPoint (x,y) +// class to define new classes in the language CBOT +// for example to define the class CPoint (x, y) class CBotClass { private: static - CBotClass* m_ExClass; // liste des classes existante un moment donn - CBotClass* m_ExNext; // pour cette liste gnrale - CBotClass* m_ExPrev; // pour cette liste gnrale + CBotClass* m_ExClass; // list of classes existing at a given time + CBotClass* m_ExNext; // for this general list + CBotClass* m_ExPrev; // for this general list private: - CBotClass* m_pParent; // classe parent - CBotString m_name; // nom de cette classe-ci - int m_nbVar; // nombre de variables dans la chane - CBotVar* m_pVar; // contenu de la classe - bool m_bIntrinsic; // classe intrinsque - CBotClass* m_next; // chaine les classe - CBotCallMethode* m_pCalls; // liste des mthodes dfinie en externe - CBotFunction* m_pMethod; // liste des mthodes compiles + CBotClass* m_pParent; // parent class + CBotString m_name; // name of this class + int m_nbVar; // number of variables in the chain + CBotVar* m_pVar; // content of the class + bool m_bIntrinsic; // intrinsic class + CBotClass* m_next; // the string class + CBotCallMethode* m_pCalls; // list of methods defined in external + CBotFunction* m_pMethod; // compiled list of methods void (*m_rMaj) ( CBotVar* pThis, void* pUser ); friend class CBotVarClass; - int m_cptLock; // pour Lock / UnLock - int m_cptOne; // pour rentrance Lock - CBotProgram* m_ProgInLock[5];// processus en attente pour synchro + int m_cptLock; // for Lock / UnLock + int m_cptOne; // Lock for reentrancy + CBotProgram* m_ProgInLock[5];// processes waiting for sync public: - bool m_IsDef; // marque si est dfinie ou pas encore + bool m_IsDef; // mark if is set or not CBotClass( const char* name, - CBotClass* pParent, bool bIntrinsic = false ); // constructeur - // Ds qu'une classe est cre, elle est connue - // partout dans CBot - // le mode intrinsic donne une classe qui n'est pas gre par des pointeurs + CBotClass* pParent, bool bIntrinsic = false ); // constructor + // Once a class is created, it is known + // around CBoT + // intrinsic mode gives a class that is not managed by pointers - ~CBotClass( ); // destructeur + ~CBotClass( ); // destructor bool AddFunction(const char* name, bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - // cet appel permet d'ajouter de manire externe (**) - // une nouvelle mthode utilisable par les objets de cette classe + // this call allows to add as external (**) + // new method used by the objects of this class bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); - // dfini la routine qui sera appelle pour mettre jour les lements de la classe + // defines routine to be called to update the elements of the class bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); - // ajoute un lment la classe + // adds an element to the class // bool AddItem(CBotString name, CBotClass* pClass); - // idem pour des lments appartenant pClass + // the same for elements belonging to pClass bool AddItem(CBotVar* pVar); - // idem en passant le pointeur une instance d'une variable - // l'objet est pris tel quel, il ne faut donc pas le dtruire + // adds an item by passing the pointer to an instance of a variable + // the object is taken as is, so do not destroyed - // idem en donnant un lment de type CBotVar + // adds an element by giving an element of type CBotVar void AddNext(CBotClass* pClass); - CBotString GivName(); // rend le nom de la classe - CBotClass* GivParent(); // donne la classe pre (ou NULL) + CBotString GivName(); // gives the name of the class + CBotClass* GivParent(); // gives the parent class (or NULL) - // dit si une classe est drive (Extends) d'une autre - // rend true aussi si les classes sont identiques + // true if a class is derived (Extends) of another + // return true also if the classes are identical bool IsChildOf(CBotClass* pClass); static - CBotClass* Find(CBotToken* &pToken); // trouve une classe d'aprs son nom - + CBotClass* Find(CBotToken* &pToken); // trouve une classe d'après son nom + // return a class by it's its name static CBotClass* Find(const char* name); - CBotVar* GivVar(); // rend la liste des variables - CBotVar* GivItem(const char* name); // l'une des variables selon son nom + CBotVar* GivVar(); // return the list of variables + CBotVar* GivItem(const char* name); // one of the variables according to its name CBotVar* GivItemRef(int nIdent); CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, @@ -845,7 +847,7 @@ public: bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); - // compile une classe dclare par l'utilisateur + // compiles a class declared by the user static CBotClass* Compile(CBotToken* &p, CBotCStack* pStack); static @@ -873,82 +875,120 @@ public: }; -#define MAXDEFNUM 1000 // nombre limite des DefineNum +#define MAXDEFNUM 1000 // limited number of DefineNum ///////////////////////////////////////////////////////////////////////////////////// -// gestion des jetons (tokens) - -#define TokenTypKeyWord 1 // un mot clef du language (voir TokenKeyWord) -#define TokenTypNum 2 // un nombre -#define TokenTypString 3 // une chaine -#define TokenTypVar 4 // un nom de variable -#define TokenTypDef 5 // une valeur selon DefineNum +// Token management (tokens) -#define TokenKeyWord 2000 // les mots clefs du langage -#define TokenKeyDeclare 2100 // mots clefs pour dclarations (int, float,..) -#define TokenKeyVal 2200 // les mots reprsentant une "valeur" (true, false, null, nan) -#define TokenKeyOp 2300 // les oprateurs +#define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord) +#define TokenTypNum 2 // number +#define TokenTypString 3 // string +#define TokenTypVar 4 // a variable name +#define TokenTypDef 5 // value according DefineNum +#define TokenKeyWord 2000 // keywords of the language +#define TokenKeyDeclare 2100 // keywords of declarations (int, float,..) +#define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan) +#define TokenKeyOp 2300 // operators +/** \class Responsible for token management */ class CBotToken { private: static - CBotStringArray m_ListKeyWords; // liste des mots clefs du language + CBotStringArray m_ListKeyWords; // list of keywords of language static - int m_ListIdKeyWords[200]; // les codes correspondants + int m_ListIdKeyWords[200]; // the corresponding codes static - CBotStringArray m_ListKeyDefine; // les noms dfinis par un DefineNum + CBotStringArray m_ListKeyDefine; // names defined by a DefineNum static - long m_ListKeyNums[MAXDEFNUM]; // les valeurs associes + long m_ListKeyNums[MAXDEFNUM]; // the ​​associated values private: - CBotToken* m_next; // suivant dans la liste + CBotToken* m_next; // following in the list CBotToken* m_prev; - int m_type; // type de Token - long m_IdKeyWord; // numro du mot clef si c'en est un - // ou valeur du "define" + int m_type; // type of Token + long m_IdKeyWord; // number of the keyword if it is a + // or value of the "define" - CBotString m_Text; // mot trouv comme token - CBotString m_Sep; // sparateurs qui suivent - - int m_start; // position dans le texte d'origine (programme) - int m_end; // itou pour la fin du token + CBotString m_Text; // word found as token + CBotString m_Sep; // following separators + int m_start; // position in the original text (program) + int m_end; // the same for the end of the token + + /** + * \brief Check whether given parameter is a keyword + */ static - int GivKeyWords(const char* w); // est-ce un mot clef ? + int GivKeyWords(const char* w); // is it a keyword? static bool GivKeyDefNum(const char* w, CBotToken* &token); + /** + * \brief Loads the list of keywords + */ static - void LoadKeyWords(); // fait la liste des mots clefs + void LoadKeyWords(); public: + /** + * \brief Constructors + */ CBotToken(); CBotToken(const CBotToken* pSrc); CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0); CBotToken(const char* mot, const char* sep = NULL); - // constructeur - ~CBotToken(); // destructeur - int GivType(); // rend le type du token + /** + * \brief Destructor + */ + ~CBotToken(); + /** + * \brief Returns the type of token + */ + int GivType(); - CBotString& GivString(); // rend la chaine correspondant ce token + /** + * \brief makes the string corresponding to this token + */ + CBotString& GivString(); - CBotString& GivSep(); // rend le sparateur suivant le token + /** + * \brief makes the following separator token + */ + CBotString& GivSep(); - int GivStart(); // position du dbut dans le texte - int GivEnd(); // position de fin dans le texte + /** + * \brief position of the beginning in the text + */ + int GivStart(); + /** + * \brief end position in the text + */ + int GivEnd(); - CBotToken* GivNext(); // rend le suivant dans la liste - CBotToken* GivPrev(); // rend le Prcdent dans la liste + /** + * \brief gives the next token in the list + */ + CBotToken* GivNext(); + /** + * \brief gives the previous token in a list + */ + CBotToken* GivPrev(); + /** + * \brief transforms the entire program + */ static CBotToken* CompileTokens(const char* p, int& error); - // transforme tout le programme + + /** + * \brief releases the list + */ static - void Delete(CBotToken* pToken); // libre la liste + void Delete(CBotToken* pToken); // libère la liste // fonctions non utiles en export @@ -958,11 +998,17 @@ public: void SetPos(int start, int end); long GivIdKey(); - void AddNext(CBotToken* p); // ajoute un token (une copie) + /** + * \brief adds a token (a copy) + */ + void AddNext(CBotToken* p); + /** + * finds the next token + */ static CBotToken* NextToken(char* &program, int& error, bool first = false); - // trouve le prochain token + const CBotToken& operator=(const CBotToken& src); @@ -974,96 +1020,96 @@ public: #if 0 //////////////////////////////////////////////////////////////////////// -// Exemples d'utilisation -// Dfinition de classes et de fonctions +// Examples of use +// Definition classes and functions -// dfinie la classe globale CPoint +// define the global class CPoint // -------------------------------- m_pClassPoint = new CBotClass("CPoint", NULL); - // ajoute le composant ".x" + // adds the component ".x" m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); - // ajoute le composant ".y" + // adds the component ".y" m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); - // le joueur peut alors utiliser les instructions + // the player can then use the instructions // CPoint position; position.x = 12; position.y = -13.6 -// dfinie la classe CColobotObject +// define class CColobotObject // -------------------------------- -// cette classe gre tous les objets dans le monde de COLOBOT -// le programme utilisateur "main" appartient cette classe +// This class manages all the objects in the world of COLOBOT +// the "main" user program belongs to this class m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); - // ajoute le composant ".position" + // adds the component ".position" m_pClassObject->AddItem("position", m_pClassPoint); - // ajoute le composant ".type" + // adds the component ".type" m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); - // ajoute une dfinition de constante - m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT quivalent la valeur 1 - // ajoute la routine FIND + // adds a definition of constant + m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT equivalent to the value 1 + // adds the FIND routine m_pClassObject->AddFunction( rCompFind, rDoFind ); - // le joueur peut maintenant utiliser les instructions + // the player can now use the instructions // CColobotObject chose; chose = FIND( ROBOT ) -// dfinie la classe CColobotRobot drive de CColobotObject +// define class CColobotRobot derived from CColobotObject // --------------------------------------------------------- -// les programmes "main" associs aux robots font partie de cette classe +// programs "main" associated with robots as a part of this class m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); - // ajoute la routine GOTO + // add routine GOTO m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); - // le joueur peut maintenant faire + // the player can now use // GOTO( FIND ( ROBOT ) ); -// cre une instance de la classe Robot +// creates an instance of the class Robot // ------------------------------------ -// par exemple un nouveau robot qui vient d'tre fabriqu +// for example a new robot which has just been manufactured CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); -// compile le programme main pour ce robot-l +// compiles the program by hand for this robot // ------------------------------------------ CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); - if ( !m_pMonRobot->Compile( LeProgramme ) ) {gestion d'erreur...}; + if ( !m_pMonRobot->Compile( LeProgramme ) ) {error handling ...}; -// construit une pile pour l'interprteur +// build a stack for interpreter // -------------------------------------- CBotStack* pStack = new CBotStack(NULL); -// excute le programme main +// executes the main program // ------------------------- while( false = m_pMonRobot->Execute( "main", pStack )) { - // programme suspendu - // on pourrait passer la main un autre (en sauvegardant pStack pour ce robot-l) + // program suspended + // could be pass a handle to another (safeguarding pstack for the robot one) }; - // programme "main" termin ! + // programme "main" finished ! -// routine implmentant l'instruction GOTO( CPoint pos ) +// routine that implements the GOTO (CPoint pos) bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) { if (pVar->GivType() != CBotTypeClass || pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) - // le paramtre n'est pas de la bonne classe ? - // NB en fait ce contrle est dj fait par la routine pour la compilation + // the parameter is not the right class? + // in fact the control is done to the routine of compilation - m_PosToGo.Copy( pVar ); // garde la position atteindre (object type CBotVar) + m_PosToGo.Copy( pVar ); // keeps the target position (object type CBotVar) - // ou alors + // or so CBotVar* temp; - temp = pVar->GivItem("x"); // trouve forcment pour un object de type "CPoint" + temp = pVar->GivItem("x"); // is necessary for the object of type CPoint ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); m_PosToGo.x = temp->GivValFloat(); - temp = pVar->GivItem("y"); // trouve forcment pour un object de type "CPoint" + temp = pVar->GivItem("y"); // is necessary for the object of type CPoint ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); m_PosToGo.y = temp->GivValFloat(); - return (m_CurentPos == m_PosToGo); // rend true si la position est atteinte - // rend false s'il faut patienter encore + return (m_CurentPos == m_PosToGo); // makes true if the position is reached + // returns false if one had wait yet } #endif diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotIf.cpp index 178992e..c1dc833 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotIf.cpp @@ -1,159 +1,161 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// -// instruction if (condition) opration1 else opration2; - -#include "CBot.h" - -// les divers constructeurs / destructeurs -CBotIf::CBotIf() -{ - m_Condition = - m_Block = - m_BlockElse = NULL; // NULL pour pouvoir faire delete directement - name = "CBotIf"; // debug -} - -CBotIf::~CBotIf() -{ - delete m_Condition; // libre la condition - delete m_Block; // libre le bloc d'instruction1 - delete m_BlockElse; // libre le bloc d'instruction2 -} - -// compilation (routine statique) -// appel lorsque le token "if" a t trouv - -CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; // conserve le ^au token (dbut instruction) - - if (!IsOfType(p, ID_IF)) return NULL; // ne doit jamais arriver - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - CBotIf* inst = new CBotIf(); // cre l'object - inst->SetToken( pp ); - - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // la condition existe bel et bien - - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if ( pStk->IsOk() ) - { - // le bloc d'instruction est ok (peut tre vide) - - // regarde si l'instruction suivante est le token "else" - if (IsOfType(p, ID_ELSE)) - { - // si oui, compile le bloc d'instruction qui suit - inst->m_BlockElse = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if (!pStk->IsOk()) - { - // il n'y a pas de bloc correct aprs le else - // libre l'objet, et transmet l'erreur qui est sur la pile - delete inst; - return pStack->Return(NULL, pStk); - } - } - - // rend l'object correct qui le demande. - return pStack->Return(inst, pStk); - } - } - - // erreur, libre l'objet - delete inst; - // et transmet l'erreur qui se trouve sur la pile. - return pStack->Return(NULL, pStk); -} - - -// excution de l'instruction - -bool CBotIf :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - // selon la reprise, on peut tre dans l'un des 2 tats - if( pile->GivState() == 0 ) - { - // value la condition - if ( !m_Condition->Execute(pile) ) return false; // interrompu ici ? - - // termine s'il y a une erreur - if ( !pile->IsOk() ) - { - return pj->Return(pile); // transmet le rsultat et libre la pile - } - - // passe dans le second tat - if (!pile->SetState(1)) return false; // prt pour la suite - } - - // second tat, value les instructions associes - // le rsultat de la condition est sur la pile - - if ( pile->GivVal() == true ) // condition tait vraie ? - { - if ( m_Block != NULL && // bloc peut tre absent - !m_Block->Execute(pile) ) return false; // interrompu ici ? - } - else - { - if ( m_BlockElse != NULL && // s'il existe un bloc alternatif - !m_BlockElse->Execute(pile) ) return false; // interrompu ici - } - - // transmet le rsultat et libre la pile - return pj->Return(pile); -} - - -void CBotIf :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // ajoute un lment la pile - if ( pile == NULL ) return; - - // selon la reprise, on peut tre dans l'un des 2 tats - if( pile->GivState() == 0 ) - { - // value la condition - m_Condition->RestoreState(pile, bMain); // interrompu ici ! - return; - } - - // second tat, value les instructions associes - // le rsultat de la condition est sur la pile - - if ( pile->GivVal() == true ) // condition tait vraie ? - { - if ( m_Block != NULL ) // bloc peut tre absent - m_Block->RestoreState(pile, bMain); // interrompu ici ! - } - else - { - if ( m_BlockElse != NULL ) // s'il existe un bloc alternatif - m_BlockElse->RestoreState(pile, bMain); // interrompu ici ! - } -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/////////////////////////////////////////////////////////////////////// +// instruction if (condition) operation1 else operation2; + +#include "CBot.h" + +// various constructors / destructors +CBotIf::CBotIf() +{ + m_Condition = + m_Block = + m_BlockElse = NULL; // NULL so that delete is not possible further + name = "CBotIf"; // debug +} + +CBotIf::~CBotIf() +{ + delete m_Condition; // frees the condition + delete m_Block; // frees the block of instruction1 + delete m_BlockElse; // frees the block of instruction2 +} + +// compilation (static routine) +// called when the token "if" has been found + +CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; // preserves at the ^ token (starting instruction) + + if (!IsOfType(p, ID_IF)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + CBotIf* inst = new CBotIf(); // create the object + inst->SetToken( pp ); + + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition does exist + + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if ( pStk->IsOk() ) + { + // the statement block is ok (can be empty) + + // see if the next instruction is the token "else" + if (IsOfType(p, ID_ELSE)) + { + // if so, compiles the following statement block + inst->m_BlockElse = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if (!pStk->IsOk()) + { + // there is no correct block after the else + // frees the object, and transmits the error that is on the stack + delete inst; + return pStack->Return(NULL, pStk); + } + } + + // return the corrent object to the application + return pStack->Return(inst, pStk); + } + } + + // error, frees the object + delete inst; + // and transmits the error that is on the stack. + return pStack->Return(NULL, pStk); +} + + +// execution of the instruction + +bool CBotIf :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or found in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + // according to recovery, it may be in one of two states + if( pile->GivState() == 0 ) + { + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here? + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // returns the results and releases the stack + } + + // passes into the second state + if (!pile->SetState(1)) return false; // ready for further + } + + // second state, evaluates the associated instructions + // the result of the condition is on the stack + + if ( pile->GivVal() == true ) // condition was true? + { + if ( m_Block != NULL && // block may be absent + !m_Block->Execute(pile) ) return false; // interrupted here? + } + else + { + if ( m_BlockElse != NULL && // if there is an alternate block + !m_BlockElse->Execute(pile) ) return false; // interrupted here + } + + // sends the results and releases the stack + return pj->Return(pile); +} + + +void CBotIf :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + // according to recovery, it may be in one of two states + if( pile->GivState() == 0 ) + { + // evaluates the condition + m_Condition->RestoreState(pile, bMain); // interrupted here! + return; + } + + // second state, evaluates the associated instructions + // the result of the condition is on the stack + + if ( pile->GivVal() == true ) // condition was true? + { + if ( m_Block != NULL ) // block may be absent + m_Block->RestoreState(pile, bMain); // interrupted here! + } + else + { + if ( m_BlockElse != NULL ) // if there is an alternate block + m_BlockElse->RestoreState(pile, bMain); // interrupted here! + } +} + diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 9070298..0d9bfb3 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -1,2245 +1,2248 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.//////////////////////////////////////////////////////////////////// -// Dfinition pour la classe CBotVar -// gestion des variables du langage CBot - -// on ne cre jamais d'instance de la class mre CBotVar - - -#include "CBot.h" -#include -#include - -long CBotVar::m_identcpt = 0; - -CBotVar::CBotVar( ) -{ - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = -1; - m_binit = false; - m_ident = 0; - m_bStatic = false; - m_mPrivate = 0; -} - -CBotVarInt::CBotVarInt( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypInt; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; - - m_val = 0; -} - -CBotVarFloat::CBotVarFloat( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypFloat; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; - - m_val = 0; -} - -CBotVarString::CBotVarString( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypString; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; - - m_val.Empty(); -} - -CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_type = CBotTypBoolean; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; - - m_val = 0; -} - -CBotVarClass* CBotVarClass::m_ExClass = NULL; - -CBotVarClass::CBotVarClass( const CBotToken* name, const CBotTypResult& type) -{ -/* -// int nIdent = 0; - InitCBotVarClass( name, type ) //, nIdent ); -} - -CBotVarClass::CBotVarClass( const CBotToken* name, CBotTypResult& type) //, int &nIdent ) -{ - InitCBotVarClass( name, type ); //, nIdent ); -} - -void CBotVarClass::InitCBotVarClass( const CBotToken* name, CBotTypResult& type ) //, int &nIdent ) -{*/ - if ( !type.Eq(CBotTypClass) && - !type.Eq(CBotTypIntrinsic) && // par comodit accepte ces types - !type.Eq(CBotTypPointer) && - !type.Eq(CBotTypArrayPointer) && - !type.Eq(CBotTypArrayBody)) ASM_TRAP(); - - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = OBJECTCREATED;//NULL; - m_InitExpr = NULL; - m_LimExpr = NULL; - m_pVar = NULL; - m_type = type; - if ( type.Eq(CBotTypArrayPointer) ) m_type.SetType( CBotTypArrayBody ); - else if ( !type.Eq(CBotTypArrayBody) ) m_type.SetType( CBotTypClass ); - // type officel pour cet object - - m_pClass = NULL; - m_pParent = NULL; - m_binit = false; - m_bStatic = false; - m_mPrivate = 0; - m_bConstructor = false; - m_CptUse = 0; - m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); - - // se place tout seul dans la liste - if (m_ExClass) m_ExClass->m_ExPrev = this; - m_ExNext = m_ExClass; - m_ExPrev = NULL; - m_ExClass = this; - - CBotClass* pClass = type.GivClass(); - CBotClass* pClass2 = pClass->GivParent(); - if ( pClass2 != NULL ) - { - // cre galement une instance dans la classe pre - m_pParent = new CBotVarClass(name, CBotTypResult(type.GivType(),pClass2) ); //, nIdent); - } - - SetClass( pClass ); //, nIdent ); - -} - -CBotVarClass::~CBotVarClass( ) -{ - if ( m_CptUse != 0 ) - ASM_TRAP(); - - if ( m_pParent ) delete m_pParent; - m_pParent = NULL; - - // libre l'objet indirect s'il y a lieu -// if ( m_Indirect != NULL ) -// m_Indirect->DecrementUse(); - - // retire la classe de la liste - if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; - else m_ExClass = m_ExNext; - - if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; - m_ExPrev = NULL; - m_ExNext = NULL; - - delete m_pVar; -} - -void CBotVarClass::ConstructorSet() -{ - m_bConstructor = true; -} - - -CBotVar::~CBotVar( ) -{ - delete m_token; - delete m_next; -} - -void CBotVar::debug() -{ - const char* p = (const char*) m_token->GivString(); - CBotString s = (const char*) GivValString(); - const char* v = (const char*) s; - - if ( m_type.Eq(CBotTypClass) ) - { - CBotVar* pv = ((CBotVarClass*)this)->m_pVar; - while (pv != NULL) - { - pv->debug(); - pv = pv->GivNext(); - } - } -} - -void CBotVar::ConstructorSet() -{ - // nop -} - -void CBotVar::SetUserPtr(void* pUser) -{ - m_pUserPtr = pUser; - if (m_type.Eq(CBotTypPointer) && - ((CBotVarPointer*)this)->m_pVarClass != NULL ) - ((CBotVarPointer*)this)->m_pVarClass->SetUserPtr(pUser); -} - -void CBotVar::SetIdent(long n) -{ - if (m_type.Eq(CBotTypPointer) && - ((CBotVarPointer*)this)->m_pVarClass != NULL ) - ((CBotVarPointer*)this)->m_pVarClass->SetIdent(n); -} - -void CBotVar::SetUniqNum(long n) -{ - m_ident = n; - - if ( n == 0 ) ASM_TRAP(); -} - -long CBotVar::NextUniqNum() -{ - if (++m_identcpt < 10000) m_identcpt = 10000; - return m_identcpt; -} - -long CBotVar::GivUniqNum() -{ - return m_ident; -} - - -void* CBotVar::GivUserPtr() -{ - return m_pUserPtr; -} - -bool CBotVar::Save1State(FILE* pf) -{ - // cette routine "virtual" ne doit jamais tre appelle, - // il doit y avoir une routine pour chaque classe fille (CBotVarInt, CBotVarFloat, etc) - // ( voir le type dans m_type ) - ASM_TRAP(); - return false; -} - -void CBotVar::Maj(void* pUser, bool bContinu) -{ -/* if (!bContinu && m_pMyThis != NULL) - m_pMyThis->Maj(pUser, true);*/ -} - - -// cre une variable selon son type - -CBotVar* CBotVar::Create(const CBotToken* name, int type ) -{ - CBotTypResult t(type); - return Create(name, t); -} - -CBotVar* CBotVar::Create(const CBotToken* name, CBotTypResult type) -{ - switch (type.GivType()) - { - case CBotTypShort: - case CBotTypInt: - return new CBotVarInt(name); - case CBotTypFloat: - return new CBotVarFloat(name); - case CBotTypBoolean: - return new CBotVarBoolean(name); - case CBotTypString: - return new CBotVarString(name); - case CBotTypPointer: - case CBotTypNullPointer: - return new CBotVarPointer(name, type); - case CBotTypIntrinsic: - return new CBotVarClass(name, type); - - case CBotTypClass: - // cre une nouvelle instance d'une classe - // et retourne le POINTER sur cette instance - { - CBotVarClass* instance = new CBotVarClass(name, type); - CBotVarPointer* pointer = new CBotVarPointer(name, type); - pointer->SetPointer( instance ); - return pointer; - } - - case CBotTypArrayPointer: - return new CBotVarArray(name, type); - - case CBotTypArrayBody: - { - CBotVarClass* instance = new CBotVarClass(name, type); - CBotVarArray* array = new CBotVarArray(name, type); - array->SetPointer( instance ); - - CBotVar* pv = array; - while (type.Eq(CBotTypArrayBody)) - { - type = type.GivTypElem(); - pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] - } - - return array; - } - } - - ASM_TRAP(); - return NULL; -} - -CBotVar* CBotVar::Create( CBotVar* pVar ) -{ - CBotVar* p = Create(pVar->m_token->GivString(), pVar->GivTypResult(2)); - return p; -} - - -CBotVar* CBotVar::Create( const char* n, CBotTypResult type) -{ - CBotToken name(n); - - switch (type.GivType()) - { - case CBotTypShort: - case CBotTypInt: - return new CBotVarInt(&name); - case CBotTypFloat: - return new CBotVarFloat(&name); - case CBotTypBoolean: - return new CBotVarBoolean(&name); - case CBotTypString: - return new CBotVarString(&name); - case CBotTypPointer: - case CBotTypNullPointer: - { - CBotVarPointer* p = new CBotVarPointer(&name, type); -// p->SetClass(type.GivClass()); - return p; - } - case CBotTypIntrinsic: - { - CBotVarClass* p = new CBotVarClass(&name, type); -// p->SetClass(type.GivClass()); - return p; - } - - case CBotTypClass: - // cre une nouvelle instance d'une classe - // et retourne le POINTER sur cette instance - { - CBotVarClass* instance = new CBotVarClass(&name, type); - CBotVarPointer* pointer = new CBotVarPointer(&name, type); - pointer->SetPointer( instance ); -// pointer->SetClass( type.GivClass() ); - return pointer; - } - - case CBotTypArrayPointer: - return new CBotVarArray(&name, type); - - case CBotTypArrayBody: - { - CBotVarClass* instance = new CBotVarClass(&name, type); - CBotVarArray* array = new CBotVarArray(&name, type); - array->SetPointer( instance ); - - CBotVar* pv = array; - while (type.Eq(CBotTypArrayBody)) - { - type = type.GivTypElem(); - pv = ((CBotVarArray*)pv)->GivItem(0, true); // cre au moins l'lment [0] - } - - return array; - } - } - - ASM_TRAP(); - return NULL; -} - -CBotVar* CBotVar::Create( const char* name, int type, CBotClass* pClass) -{ - CBotToken token( name, "" ); - CBotVar* pVar = Create( &token, type ); - - if ( type == CBotTypPointer && pClass == NULL ) // pointeur "null" ? - return pVar; - - if ( type == CBotTypClass || type == CBotTypPointer || - type == CBotTypIntrinsic ) - { - if (pClass == NULL) - { - delete pVar; - return NULL; - } - pVar->SetClass( pClass ); - } - return pVar; -} - -CBotVar* CBotVar::Create( const char* name, CBotClass* pClass) -{ - CBotToken token( name, "" ); - CBotVar* pVar = Create( &token, CBotTypResult( CBotTypClass, pClass ) ); -// pVar->SetClass( pClass ); - return pVar; -} - -CBotTypResult CBotVar::GivTypResult(int mode) -{ - CBotTypResult r = m_type; - - if ( mode == 1 && m_type.Eq(CBotTypClass) ) - r.SetType(CBotTypPointer); - if ( mode == 2 && m_type.Eq(CBotTypClass) ) - r.SetType(CBotTypIntrinsic); - - return r; -} - -int CBotVar::GivType(int mode) -{ - if ( mode == 1 && m_type.Eq(CBotTypClass) ) - return CBotTypPointer; - if ( mode == 2 && m_type.Eq(CBotTypClass) ) - return CBotTypIntrinsic; - return m_type.GivType(); -} - -void CBotVar::SetType(CBotTypResult& type) -{ - m_type = type; -} - - -int CBotVar::GivInit() -{ - if ( m_type.Eq(CBotTypClass) ) return IS_DEF; // toujours dfini ! - - return m_binit; -} - -void CBotVar::SetInit(int bInit) -{ - m_binit = bInit; - if ( bInit == 2 ) m_binit = IS_DEF; // cas spcial - - if ( m_type.Eq(CBotTypPointer) && bInit == 2 ) - { - CBotVarClass* instance = GivPointer(); - if ( instance == NULL ) - { - instance = new CBotVarClass(NULL, m_type); -// instance->SetClass(((CBotVarPointer*)this)->m_pClass); - SetPointer(instance); - } - instance->SetInit(1); - } - - if ( m_type.Eq(CBotTypClass) || m_type.Eq(CBotTypIntrinsic) ) - { - CBotVar* p = ((CBotVarClass*)this)->m_pVar; - while( p != NULL ) - { - p->SetInit( bInit ); - p->m_pMyThis = (CBotVarClass*)this; - p = p->GivNext(); - } - } -} - -CBotString CBotVar::GivName() -{ - return m_token->GivString(); -} - -void CBotVar::SetName(const char* name) -{ - m_token->SetString(name); -} - -CBotToken* CBotVar::GivToken() -{ - return m_token; -} - -CBotVar* CBotVar::GivItem(const char* name) -{ - ASM_TRAP(); - return NULL; -} - -CBotVar* CBotVar::GivItemRef(int nIdent) -{ - ASM_TRAP(); - return NULL; -} - -CBotVar* CBotVar::GivItemList() -{ - ASM_TRAP(); - return NULL; -} - -CBotVar* CBotVar::GivItem(int row, bool bGrow) -{ - ASM_TRAP(); - return NULL; -} - -// dit si une variable appartient une classe donne -bool CBotVar::IsElemOfClass(const char* name) -{ - CBotClass* pc = NULL; - - if ( m_type.Eq(CBotTypPointer) ) - { - pc = ((CBotVarPointer*)this)->m_pClass; - } - if ( m_type.Eq(CBotTypClass) ) - { - pc = ((CBotVarClass*)this)->m_pClass; - } - - while ( pc != NULL ) - { - if ( pc->GivName() == name ) return true; - pc = pc->GivParent(); - } - - return false; -} - - -CBotVar* CBotVar::GivStaticVar() -{ - // rend le pointeur la variable si elle est statique - if ( m_bStatic == 0 || m_pMyThis == NULL ) return this; - - CBotClass* pClass = m_pMyThis->GivClass(); - return pClass->GivItem( m_token->GivString() ); -} - - -CBotVar* CBotVar::GivNext() -{ - return m_next; -} - -void CBotVar::AddNext(CBotVar* pVar) -{ - CBotVar* p = this; - while (p->m_next != NULL) p = p->m_next; - - p->m_next = pVar; -} - -void CBotVar::SetVal(CBotVar* var) -{ - switch (/*var->*/GivType()) - { - case CBotTypBoolean: - SetValInt(var->GivValInt()); - break; - case CBotTypInt: - SetValInt(var->GivValInt(), ((CBotVarInt*)var)->m_defnum); - break; - case CBotTypFloat: - SetValFloat(var->GivValFloat()); - break; - case CBotTypString: - SetValString(var->GivValString()); - break; - case CBotTypPointer: - case CBotTypNullPointer: - case CBotTypArrayPointer: - SetPointer(var->GivPointer()); - break; - case CBotTypClass: - { - delete ((CBotVarClass*)this)->m_pVar; - ((CBotVarClass*)this)->m_pVar = NULL; - Copy(var, false); - } - break; - default: - ASM_TRAP(); - } - - m_binit = var->m_binit; // copie l'tat nan s'il y a -} - -void CBotVar::SetStatic(bool bStatic) -{ - m_bStatic = bStatic; -} - -void CBotVar::SetPrivate(int mPrivate) -{ - m_mPrivate = mPrivate; -} - -bool CBotVar::IsStatic() -{ - return m_bStatic; -} - -bool CBotVar::IsPrivate(int mode) -{ - return m_mPrivate >= mode; -} - -int CBotVar::GivPrivate() -{ - return m_mPrivate; -} - - -void CBotVar::SetPointer(CBotVar* pVarClass) -{ - ASM_TRAP(); -} - -CBotVarClass* CBotVar::GivPointer() -{ - ASM_TRAP(); - return NULL; -} - -// toutes ces fonctions doivent tre dfinies dans les classes filles -// drives de la classe CBotVar - -int CBotVar::GivValInt() -{ - ASM_TRAP(); - return 0; -} - -float CBotVar::GivValFloat() -{ - ASM_TRAP(); - return 0; -} - -void CBotVar::SetValInt(int c, const char* s) -{ - ASM_TRAP(); -} - -void CBotVar::SetValFloat(float c) -{ - ASM_TRAP(); -} - -void CBotVar::Mul(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::Power(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -int CBotVar::Div(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return 0; -} - -int CBotVar::Modulo(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return 0; -} - -void CBotVar::Add(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::Sub(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -bool CBotVar::Lo(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return false; -} - -bool CBotVar::Hi(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return false; -} - -bool CBotVar::Ls(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return false; -} - -bool CBotVar::Hs(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return false; -} - -bool CBotVar::Eq(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return false; -} - -bool CBotVar::Ne(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); - return false; -} - -void CBotVar::And(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::Or(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::XOr(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::ASR(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::SR(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::SL(CBotVar* left, CBotVar* right) -{ - ASM_TRAP(); -} - -void CBotVar::Neg() -{ - ASM_TRAP(); -} - -void CBotVar::Not() -{ - ASM_TRAP(); -} - -void CBotVar::Inc() -{ - ASM_TRAP(); -} -void CBotVar::Dec() -{ - ASM_TRAP(); -} - -void CBotVar::Copy(CBotVar* pSrc, bool bName) -{ - ASM_TRAP(); -} - -void CBotVar::SetValString(const char* p) -{ - ASM_TRAP(); -} - -CBotString CBotVar::GivValString() -{ - ASM_TRAP(); - return CBotString(); -} - -void CBotVar::SetClass(CBotClass* pClass) -{ - ASM_TRAP(); -} - -CBotClass* CBotVar::GivClass() -{ - ASM_TRAP(); - return NULL; -} - -/* -void CBotVar::SetIndirection(CBotVar* pVar) -{ - // nop, uniquement pour CBotVarPointer::SetIndirection -} -*/ - -////////////////////////////////////////////////////////////////////////////////////// - -// copie une variable dans une autre -void CBotVarInt::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarInt* p = (CBotVarInt*)pSrc; - - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; - m_pMyThis = NULL; - m_pUserPtr = p->m_pUserPtr; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; - - m_defnum = p->m_defnum; -} - - - - -void CBotVarInt::SetValInt(int val, const char* defnum) -{ - m_val = val; - m_binit = true; - m_defnum = defnum; -} - - - -void CBotVarInt::SetValFloat(float val) -{ - m_val = (int)val; - m_binit = true; -} - -int CBotVarInt::GivValInt() -{ - return m_val; -} - -float CBotVarInt::GivValFloat() -{ - return (float)m_val; -} - -CBotString CBotVarInt::GivValString() -{ - if ( !m_defnum.IsEmpty() ) return m_defnum; - - CBotString res; - - if ( !m_binit ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } - - char buffer[300]; - sprintf(buffer, "%d", m_val); - res = buffer; - - return res; -} - - -void CBotVarInt::Mul(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() * right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::Power(CBotVar* left, CBotVar* right) -{ - m_val = (int) pow( (double) left->GivValInt() , (double) right->GivValInt() ); - m_binit = true; -} - -int CBotVarInt::Div(CBotVar* left, CBotVar* right) -{ - int r = right->GivValInt(); - if ( r != 0 ) - { - m_val = left->GivValInt() / r; - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) -{ - int r = right->GivValInt(); - if ( r != 0 ) - { - m_val = left->GivValInt() % r; - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -void CBotVarInt::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() + right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::Sub(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() - right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::XOr(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() ^ right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::And(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() & right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::Or(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() | right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::SL(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() << right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::ASR(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() >> right->GivValInt(); - m_binit = true; -} - -void CBotVarInt::SR(CBotVar* left, CBotVar* right) -{ - int source = left->GivValInt(); - int shift = right->GivValInt(); - if (shift>=1) source &= 0x7fffffff; - m_val = source >> shift; - m_binit = true; -} - -void CBotVarInt::Neg() -{ - m_val = -m_val; -} - -void CBotVarInt::Not() -{ - m_val = ~m_val; -} - -void CBotVarInt::Inc() -{ - m_val++; - m_defnum.Empty(); -} - -void CBotVarInt::Dec() -{ - m_val--; - m_defnum.Empty(); -} - -bool CBotVarInt::Lo(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() < right->GivValInt(); -} - -bool CBotVarInt::Hi(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() > right->GivValInt(); -} - -bool CBotVarInt::Ls(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() <= right->GivValInt(); -} - -bool CBotVarInt::Hs(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() >= right->GivValInt(); -} - -bool CBotVarInt::Eq(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() == right->GivValInt(); -} - -bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() != right->GivValInt(); -} - - -////////////////////////////////////////////////////////////////////////////////////// - -// copie une variable dans une autre -void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarFloat* p = (CBotVarFloat*)pSrc; - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; -} - - - - -void CBotVarFloat::SetValInt(int val, const char* s) -{ - m_val = (float)val; - m_binit = true; -} - -void CBotVarFloat::SetValFloat(float val) -{ - m_val = val; - m_binit = true; -} - -int CBotVarFloat::GivValInt() -{ - return (int)m_val; -} - -float CBotVarFloat::GivValFloat() -{ - return m_val; -} - -CBotString CBotVarFloat::GivValString() -{ - CBotString res; - - if ( !m_binit ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } - - char buffer[300]; - sprintf(buffer, "%.2f", m_val); - res = buffer; - - return res; -} - - -void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValFloat() * right->GivValFloat(); - m_binit = true; -} - -void CBotVarFloat::Power(CBotVar* left, CBotVar* right) -{ - m_val = (float)pow( left->GivValFloat() , right->GivValFloat() ); - m_binit = true; -} - -int CBotVarFloat::Div(CBotVar* left, CBotVar* right) -{ - float r = right->GivValFloat(); - if ( r != 0 ) - { - m_val = left->GivValFloat() / r; - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) -{ - float r = right->GivValFloat(); - if ( r != 0 ) - { - m_val = (float)fmod( left->GivValFloat() , r ); - m_binit = true; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -void CBotVarFloat::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValFloat() + right->GivValFloat(); - m_binit = true; -} - -void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValFloat() - right->GivValFloat(); - m_binit = true; -} - -void CBotVarFloat::Neg() -{ - m_val = -m_val; -} - -void CBotVarFloat::Inc() -{ - m_val++; -} - -void CBotVarFloat::Dec() -{ - m_val--; -} - - -bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) -{ - return left->GivValFloat() < right->GivValFloat(); -} - -bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) -{ - return left->GivValFloat() > right->GivValFloat(); -} - -bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) -{ - return left->GivValFloat() <= right->GivValFloat(); -} - -bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) -{ - return left->GivValFloat() >= right->GivValFloat(); -} - -bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) -{ - return left->GivValFloat() == right->GivValFloat(); -} - -bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) -{ - return left->GivValFloat() != right->GivValFloat(); -} - - -////////////////////////////////////////////////////////////////////////////////////// - -// copie une variable dans une autre -void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarBoolean* p = (CBotVarBoolean*)pSrc; - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; -} - - - - -void CBotVarBoolean::SetValInt(int val, const char* s) -{ - m_val = (bool)val; - m_binit = true; -} - -void CBotVarBoolean::SetValFloat(float val) -{ - m_val = (bool)val; - m_binit = true; -} - -int CBotVarBoolean::GivValInt() -{ - return m_val; -} - -float CBotVarBoolean::GivValFloat() -{ - return (float)m_val; -} - -CBotString CBotVarBoolean::GivValString() -{ - CBotString ret; - - CBotString res; - - if ( !m_binit ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } - - ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); - return ret; -} - -void CBotVarBoolean::And(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() && right->GivValInt(); - m_binit = true; -} -void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() || right->GivValInt(); - m_binit = true; -} - -void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValInt() ^ right->GivValInt(); - m_binit = true; -} - -void CBotVarBoolean::Not() -{ - m_val = m_val ? false : true ; -} - -bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() == right->GivValInt(); -} - -bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) -{ - return left->GivValInt() != right->GivValInt(); -} - -////////////////////////////////////////////////////////////////////////////////////// - -// copie une variable dans une autre -void CBotVarString::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarString* p = (CBotVarString*)pSrc; - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; -} - - -void CBotVarString::SetValString(const char* p) -{ - m_val = p; - m_binit = true; -} - -CBotString CBotVarString::GivValString() -{ - if ( !m_binit ) - { - CBotString res; - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == IS_NAN ) - { - CBotString res; - res.LoadString(TX_NAN); - return res; - } - - return m_val; -} - - -void CBotVarString::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GivValString() + right->GivValString(); - m_binit = true; -} - -bool CBotVarString::Eq(CBotVar* left, CBotVar* right) -{ - return (left->GivValString() == right->GivValString()); -} - -bool CBotVarString::Ne(CBotVar* left, CBotVar* right) -{ - return (left->GivValString() != right->GivValString()); -} - - -bool CBotVarString::Lo(CBotVar* left, CBotVar* right) -{ - return (left->GivValString() == right->GivValString()); -} - -bool CBotVarString::Hi(CBotVar* left, CBotVar* right) -{ - return (left->GivValString() == right->GivValString()); -} - -bool CBotVarString::Ls(CBotVar* left, CBotVar* right) -{ - return (left->GivValString() == right->GivValString()); -} - -bool CBotVarString::Hs(CBotVar* left, CBotVar* right) -{ - return (left->GivValString() == right->GivValString()); -} - - -//////////////////////////////////////////////////////////////// - -// copie une variable dans une autre -void CBotVarClass::Copy(CBotVar* pSrc, bool bName) -{ - pSrc = pSrc->GivPointer(); // si source donn par un pointeur - - if ( pSrc->GivType() != CBotTypClass ) - ASM_TRAP(); - - CBotVarClass* p = (CBotVarClass*)pSrc; - - if (bName) *m_token = *p->m_token; - - m_type = p->m_type; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_pClass = p->m_pClass; - if ( p->m_pParent ) - { - ASM_TRAP(); "que faire du pParent"; - } - -// m_next = NULL; - m_pUserPtr = p->m_pUserPtr; - m_pMyThis = NULL;//p->m_pMyThis; - m_ItemIdent = p->m_ItemIdent; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; - - delete m_pVar; - m_pVar = NULL; - - CBotVar* pv = p->m_pVar; - while( pv != NULL ) - { - CBotVar* pn = CBotVar::Create(pv); - pn->Copy( pv ); - if ( m_pVar == NULL ) m_pVar = pn; - else m_pVar->AddNext(pn); - - pv = pv->GivNext(); - } -} - -void CBotVarClass::SetItemList(CBotVar* pVar) -{ - delete m_pVar; - m_pVar = pVar; // remplace le pointeur existant -} - -void CBotVarClass::SetIdent(long n) -{ - m_ItemIdent = n; -} - -void CBotVarClass::SetClass(CBotClass* pClass)//, int &nIdent) -{ - m_type.m_pClass = pClass; - - if ( m_pClass == pClass ) return; - - m_pClass = pClass; - - // initialise les variables associes cette classe - delete m_pVar; - m_pVar = NULL; - - if (pClass == NULL) return; - - CBotVar* pv = pClass->GivVar(); // premier de la liste - while ( pv != NULL ) - { - // cherche les dimensions max du tableau - CBotInstr* p = pv->m_LimExpr; // les diffrentes formules - if ( p != NULL ) - { - CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante - int n = 0; - int max[100]; - - while (p != NULL) - { - while( pile->IsOk() && !p->Execute(pile) ) ; // calcul de la taille sans interruptions - CBotVar* v = pile->GivVar(); // rsultat - max[n] = v->GivValInt(); // valeur - n++; - p = p->GivNext3(); - } - while (n<100) max[n++] = 0; - - pv->m_type.SetArray( max ); // mmorise les limitations - pile->Delete(); - } - - CBotVar* pn = CBotVar::Create( pv ); // une copie - pn->SetStatic(pv->IsStatic()); - pn->SetPrivate(pv->GivPrivate()); - - if ( pv->m_InitExpr != NULL ) // expression pour l'initialisation ? - { -#if STACKMEM - CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante - - while(pile->IsOk() && !pv->m_InitExpr->Execute(pile, pn)); // value l'expression sans timer - - pile->Delete(); -#else - CBotStack* pile = new CBotStack(NULL); // une pile indpendante - while(!pv->m_InitExpr->Execute(pile)); // value l'expression sans timer - pn->SetVal( pile->GivVar() ) ; - delete pile; -#endif - } - -// pn->SetUniqNum(CBotVar::NextUniqNum()); // numrote les lments - pn->SetUniqNum(pv->GivUniqNum()); //++nIdent - pn->m_pMyThis = this; - - if ( m_pVar == NULL) m_pVar = pn; - else m_pVar->AddNext( pn ); - pv = pv->GivNext(); - } -} - -CBotClass* CBotVarClass::GivClass() -{ - return m_pClass; -} - - -void CBotVarClass::Maj(void* pUser, bool bContinu) -{ -/* if (!bContinu && m_pMyThis != NULL) - m_pMyThis->Maj(pUser, true);*/ - - // une routine de mise jour existe-elle ? - - if ( m_pClass->m_rMaj == NULL ) return; - - // rcupre le pointeur user selon la classe - // ou selon le paramtre pass au CBotProgram::Run() - - if ( m_pUserPtr != NULL) pUser = m_pUserPtr; - if ( pUser == OBJECTDELETED || - pUser == OBJECTCREATED ) return; - m_pClass->m_rMaj( this, pUser ); -} - -CBotVar* CBotVarClass::GivItem(const char* name) -{ - CBotVar* p = m_pVar; - - while ( p != NULL ) - { - if ( p->GivName() == name ) return p; - p = p->GivNext(); - } - - if ( m_pParent != NULL ) return m_pParent->GivItem(name); - return NULL; -} - -CBotVar* CBotVarClass::GivItemRef(int nIdent) -{ - CBotVar* p = m_pVar; - - while ( p != NULL ) - { - if ( p->GivUniqNum() == nIdent ) return p; - p = p->GivNext(); - } - - if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); - return NULL; -} - -// pour la gestion d'un tableau -// bExtend permet d'agrandir le tableau, mais pas au dela de la taille fixe par SetArray() - -CBotVar* CBotVarClass::GivItem(int n, bool bExtend) -{ - CBotVar* p = m_pVar; - - if ( n < 0 ) return NULL; - if ( n > MAXARRAYSIZE ) return NULL; - - if ( m_type.GivLimite() >= 0 && n >= m_type.GivLimite() ) return NULL; - - if ( p == NULL && bExtend ) - { - p = CBotVar::Create("", m_type.GivTypElem()); - m_pVar = p; - } - - if ( n == 0 ) return p; - - while ( n-- > 0 ) - { - if ( p->m_next == NULL ) - { - if ( bExtend ) p->m_next = CBotVar::Create("", m_type.GivTypElem()); - if ( p->m_next == NULL ) return NULL; - } - p = p->m_next; - } - - return p; -} - -CBotVar* CBotVarClass::GivItemList() -{ - return m_pVar; -} - - -CBotString CBotVarClass::GivValString() -{ -// if ( m_Indirect != NULL) return m_Indirect->GivValString(); - - CBotString res; - - if ( m_pClass != NULL ) // pas utilis pour un array - { - res = m_pClass->GivName() + CBotString("( "); - - CBotVarClass* my = this; - while ( my != NULL ) - { - CBotVar* pv = my->m_pVar; - while ( pv != NULL ) - { - res += pv->GivName() + CBotString("="); - - if ( pv->IsStatic() ) - { - CBotVar* pvv = my->m_pClass->GivItem(pv->GivName()); - res += pvv->GivValString(); - } - else - { - res += pv->GivValString(); - } - pv = pv->GivNext(); - if ( pv != NULL ) res += ", "; - } - my = my->m_pParent; - if ( my != NULL ) - { - res += ") extends "; - res += my->m_pClass->GivName(); - res += " ("; - } - } - } - else - { - res = "( "; - - CBotVar* pv = m_pVar; - while ( pv != NULL ) - { - res += pv->GivValString(); - if ( pv->GivNext() != NULL ) res += ", "; - pv = pv->GivNext(); - } - } - - res += " )"; - return res; -} - -void CBotVarClass::IncrementUse() -{ - m_CptUse++; -} - -void CBotVarClass::DecrementUse() -{ - m_CptUse--; - if ( m_CptUse == 0 ) - { - // s'il y en a un, appel le destructeur - // mais seulement si un constructeur avait t appel. - if ( m_bConstructor ) - { - m_CptUse++; // ne revient pas dans le destructeur - - // m_error est static dans le stack - // sauve la valeur pour la remettre ensuite - int err, start, end; - CBotStack* pile = NULL; - err = pile->GivError(start,end); // pile == NULL a ne derange pas !! - - pile = CBotStack::FirstStack(); // efface l'erreur - CBotVar* ppVars[1]; - ppVars[0] = NULL; - - CBotVar* pThis = CBotVar::Create("this", CBotTypNullPointer); - pThis->SetPointer(this); - CBotVar* pResult = NULL; - - CBotString nom = "~" + m_pClass->GivName(); - long ident = 0; - - while ( pile->IsOk() && !m_pClass->ExecuteMethode(ident, nom, pThis, ppVars, pResult, pile, NULL)) ; // attend la fin - - pile->ResetError(err, start,end); - - pile->Delete(); - delete pThis; - m_CptUse--; - } - - delete this; // s'auto-dtruit !! - } -} - -CBotVarClass* CBotVarClass::GivPointer() -{ - return this; -} - - -// trouve une instance selon son numro unique - -CBotVarClass* CBotVarClass::Find(long id) -{ - CBotVarClass* p = m_ExClass; - - while ( p != NULL ) - { - if ( p->m_ItemIdent == id ) return p; - p = p->m_ExNext; - } - - return NULL; -} - -bool CBotVarClass::Eq(CBotVar* left, CBotVar* right) -{ - CBotVar* l = left->GivItemList(); - CBotVar* r = right->GivItemList(); - - while ( l != NULL && r != NULL ) - { - if ( l->Ne(l, r) ) return false; - l = l->GivNext(); - r = r->GivNext(); - } - - // devrait toujours arriv simultanment au bout (mmes classes) - return l == r; -} - -bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) -{ - CBotVar* l = left->GivItemList(); - CBotVar* r = right->GivItemList(); - - while ( l != NULL && r != NULL ) - { - if ( l->Ne(l, r) ) return true; - l = l->GivNext(); - r = r->GivNext(); - } - - // devrait toujours arriv simultanment au bout (mmes classes) - return l != r; -} - -///////////////////////////////////////////////////////////////////////////// -// gestion des tableaux de variables - -CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type ) -{ - if ( !type.Eq(CBotTypArrayPointer) && - !type.Eq(CBotTypArrayBody)) ASM_TRAP(); - - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - - m_type = type; - m_type.SetType(CBotTypArrayPointer); - m_binit = false; - - m_pInstance = NULL; // la liste des lments du tableau -} - -CBotVarArray::~CBotVarArray() -{ - if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); // une rfrence en moins -} - -// copie une variable dans une autre -void CBotVarArray::Copy(CBotVar* pSrc, bool bName) -{ - if ( pSrc->GivType() != CBotTypArrayPointer ) - ASM_TRAP(); - - CBotVarArray* p = (CBotVarArray*)pSrc; - - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; - m_pInstance = p->GivPointer(); - - if ( m_pInstance != NULL ) - m_pInstance->IncrementUse(); // une rfrence en plus - - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; -} - -void CBotVarArray::SetPointer(CBotVar* pVarClass) -{ - m_binit = true; // init, mme sur un pointeur null - - if ( m_pInstance == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter - // car le dcrment peut dtruire l'object - - if ( pVarClass != NULL ) - { - if ( pVarClass->GivType() == CBotTypArrayPointer ) - pVarClass = pVarClass->GivPointer(); // le vrai pointeur l'objet - - if ( !pVarClass->m_type.Eq(CBotTypClass) && - !pVarClass->m_type.Eq(CBotTypArrayBody)) - ASM_TRAP(); - - ((CBotVarClass*)pVarClass)->IncrementUse(); // une rfrence en plus - } - - if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); - m_pInstance = (CBotVarClass*)pVarClass; -} - - -CBotVarClass* CBotVarArray::GivPointer() -{ - if ( m_pInstance == NULL ) return NULL; - return m_pInstance->GivPointer(); -} - -CBotVar* CBotVarArray::GivItem(int n, bool bExtend) -{ - if ( m_pInstance == NULL ) - { - if ( !bExtend ) return NULL; - // cre une instance pour le tableau - - CBotVarClass* instance = new CBotVarClass(NULL, m_type); - SetPointer( instance ); - } - return m_pInstance->GivItem(n, bExtend); -} - -CBotVar* CBotVarArray::GivItemList() -{ - if ( m_pInstance == NULL) return NULL; - return m_pInstance->GivItemList(); -} - -CBotString CBotVarArray::GivValString() -{ - if ( m_pInstance == NULL ) return ( CBotString( "Null pointer" ) ) ; - return m_pInstance->GivValString(); -} - -bool CBotVarArray::Save1State(FILE* pf) -{ - if ( !WriteType(pf, m_type) ) return false; - return SaveVar(pf, m_pInstance); // sauve l'instance qui gre le tableau -} - - -///////////////////////////////////////////////////////////////////////////// -// gestion des pointeurs une instance donne - -CBotVarPointer::CBotVarPointer(const CBotToken* name, CBotTypResult& type ) -{ - if ( !type.Eq(CBotTypPointer) && - !type.Eq(CBotTypNullPointer) && - !type.Eq(CBotTypClass) && // par commodit accepte Class et Intrinsic - !type.Eq(CBotTypIntrinsic) ) ASM_TRAP(); - - m_token = new CBotToken(name); - m_next = NULL; - m_pMyThis = NULL; - m_pUserPtr = NULL; - - m_type = type; - if ( !type.Eq(CBotTypNullPointer) ) - m_type.SetType(CBotTypPointer); // quoi qu'il en soit, c'est un pointeur - m_binit = false; - m_pClass = NULL; - m_pVarClass = NULL; // sera dfini par un SetPointer() - - SetClass(type.GivClass() ); -} - -CBotVarPointer::~CBotVarPointer() -{ - if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); // une rfrence en moins -} - - -void CBotVarPointer::Maj(void* pUser, bool bContinu) -{ -/* if ( !bContinu && m_pMyThis != NULL ) - m_pMyThis->Maj(pUser, false);*/ - - if ( m_pVarClass != NULL) m_pVarClass->Maj(pUser, false); -} - -CBotVar* CBotVarPointer::GivItem(const char* name) -{ - if ( m_pVarClass == NULL) // pas d'instance existant ? - return m_pClass->GivItem(name); // rend le pointeur dans la classe elle-mme - - return m_pVarClass->GivItem(name); -} - -CBotVar* CBotVarPointer::GivItemRef(int nIdent) -{ - if ( m_pVarClass == NULL) // pas d'instance existant ? - return m_pClass->GivItemRef(nIdent);// rend le pointeur dans la classe elle-mme - - return m_pVarClass->GivItemRef(nIdent); -} - -CBotVar* CBotVarPointer::GivItemList() -{ - if ( m_pVarClass == NULL) return NULL; - return m_pVarClass->GivItemList(); -} - -CBotString CBotVarPointer::GivValString() -{ - CBotString s = "Pointer to "; - if ( m_pVarClass == NULL ) s = "Null pointer" ; - else s += m_pVarClass->GivValString(); - return s; -} - - -void CBotVarPointer::ConstructorSet() -{ - if ( m_pVarClass != NULL) m_pVarClass->ConstructorSet(); -} - -// initialise le pointeur vers l'instance d'une classe - -void CBotVarPointer::SetPointer(CBotVar* pVarClass) -{ - m_binit = true; // init, mme sur un pointeur null - - if ( m_pVarClass == pVarClass) return; // spcial, ne pas dcrmenter et rincrmenter - // car le dcrment peut dtruire l'object - - if ( pVarClass != NULL ) - { - if ( pVarClass->GivType() == CBotTypPointer ) - pVarClass = pVarClass->GivPointer(); // le vrai pointeur l'objet - -// if ( pVarClass->GivType() != CBotTypClass ) - if ( !pVarClass->m_type.Eq(CBotTypClass) ) - ASM_TRAP(); - - ((CBotVarClass*)pVarClass)->IncrementUse(); // une rfrence en plus - m_pClass = ((CBotVarClass*)pVarClass)->m_pClass; - m_pUserPtr = pVarClass->m_pUserPtr; // pas vraiment indispensable - m_type = CBotTypResult(CBotTypPointer, m_pClass); // un pointeur de quel genre - } - - if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); - m_pVarClass = (CBotVarClass*)pVarClass; - -} - -CBotVarClass* CBotVarPointer::GivPointer() -{ - if ( m_pVarClass == NULL ) return NULL; - return m_pVarClass->GivPointer(); -} - -void CBotVarPointer::SetIdent(long n) -{ - if ( m_pVarClass == NULL ) return; - m_pVarClass->SetIdent( n ); -} - -long CBotVarPointer::GivIdent() -{ - if ( m_pVarClass == NULL ) return 0; - return m_pVarClass->m_ItemIdent; -} - - -void CBotVarPointer::SetClass(CBotClass* pClass) -{ -// int nIdent = 0; - m_type.m_pClass = m_pClass = pClass; - if ( m_pVarClass != NULL ) m_pVarClass->SetClass(pClass); //, nIdent); -} - -CBotClass* CBotVarPointer::GivClass() -{ - if ( m_pVarClass != NULL ) return m_pVarClass->GivClass(); - - return m_pClass; -} - - -bool CBotVarPointer::Save1State(FILE* pf) -{ - if ( m_pClass ) - { - if (!WriteString(pf, m_pClass->GivName())) return false; // nom de la classe - } - else - { - if (!WriteString(pf, "")) return false; - } - - if (!WriteLong(pf, GivIdent())) return false; // la rfrence unique - - // sauve aussi une copie de l'instance - return SaveVar(pf, GivPointer()); -} - -// copie une variable dans une autre -void CBotVarPointer::Copy(CBotVar* pSrc, bool bName) -{ - if ( pSrc->GivType() != CBotTypPointer && - pSrc->GivType() != CBotTypNullPointer) - ASM_TRAP(); - - CBotVarPointer* p = (CBotVarPointer*)pSrc; - - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; -// m_pVarClass = p->m_pVarClass; - m_pVarClass = p->GivPointer(); - - if ( m_pVarClass != NULL ) - m_pVarClass->IncrementUse(); // une rfrence en plus - - m_pClass = p->m_pClass; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = NULL; - m_pMyThis = NULL;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // garde le mme idendificateur (par dfaut) - if (m_ident == 0 ) m_ident = p->m_ident; -} - -bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right) -{ - CBotVarClass* l = left->GivPointer(); - CBotVarClass* r = right->GivPointer(); - - if ( l == r ) return true; - if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return true; - if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return true; - return false; -} - -bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) -{ - CBotVarClass* l = left->GivPointer(); - CBotVarClass* r = right->GivPointer(); - - if ( l == r ) return false; - if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return false; - if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return false; - return true; -} - - - -/////////////////////////////////////////////////////// -// gestion des types de rsultats - - -CBotTypResult::CBotTypResult(int type) -{ - m_type = type; - m_pNext = NULL; - m_pClass = NULL; - m_limite = -1; -} - -CBotTypResult::CBotTypResult(int type, const char* name) -{ - m_type = type; - m_pNext = NULL; - m_pClass = NULL; - m_limite = -1; - - if ( type == CBotTypPointer || - type == CBotTypClass || - type == CBotTypIntrinsic ) - { - m_pClass = CBotClass::Find(name); - if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; - } -} - -CBotTypResult::CBotTypResult(int type, CBotClass* pClass) -{ - m_type = type; - m_pNext = NULL; - m_pClass = pClass; - m_limite = -1; - - if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; -} - -CBotTypResult::CBotTypResult(int type, CBotTypResult elem) -{ - m_type = type; - m_pNext = NULL; - m_pClass = NULL; - m_limite = -1; - - if ( type == CBotTypArrayPointer || - type == CBotTypArrayBody ) - m_pNext = new CBotTypResult( elem ); -} - -CBotTypResult::CBotTypResult(const CBotTypResult& typ) -{ - m_type = typ.m_type; - m_pClass = typ.m_pClass; - m_pNext = NULL; - m_limite = typ.m_limite; - - if ( typ.m_pNext ) - m_pNext = new CBotTypResult( *typ.m_pNext ); -} - -CBotTypResult::CBotTypResult() -{ - m_type = 0; - m_limite = -1; - m_pNext = NULL; - m_pClass = NULL; -} - -CBotTypResult::~CBotTypResult() -{ - delete m_pNext; -} - -int CBotTypResult::GivType(int mode) const -{ -#ifdef _DEBUG - if ( m_type == CBotTypPointer || - m_type == CBotTypClass || - m_type == CBotTypIntrinsic ) - - if ( m_pClass == NULL ) ASM_TRAP(); - - - if ( m_type == CBotTypArrayPointer ) - if ( m_pNext == NULL ) ASM_TRAP(); -#endif - if ( mode == 3 && m_type == CBotTypNullPointer ) return CBotTypPointer; - return m_type; -} - -void CBotTypResult::SetType(int n) -{ - m_type = n; -} - -CBotClass* CBotTypResult::GivClass() const -{ - return m_pClass; -} - -CBotTypResult& CBotTypResult::GivTypElem() const -{ - return *m_pNext; -} - -int CBotTypResult::GivLimite() const -{ - return m_limite; -} - -void CBotTypResult::SetLimite(int n) -{ - m_limite = n; -} - -void CBotTypResult::SetArray( int* max ) -{ - m_limite = *max; - if (m_limite < 1) m_limite = -1; - - if ( m_pNext != NULL ) // dernire dimension ? - { - m_pNext->SetArray( max+1 ); - } -} - - - -bool CBotTypResult::Compare(const CBotTypResult& typ) const -{ - if ( m_type != typ.m_type ) return false; - - if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); - - if ( m_type == CBotTypPointer || - m_type == CBotTypClass || - m_type == CBotTypIntrinsic ) - { - return m_pClass == typ.m_pClass; - } - - return true; -} - -bool CBotTypResult::Eq(int type) const -{ - return m_type == type; -} - -CBotTypResult& - CBotTypResult::operator=(const CBotTypResult& src) -{ - m_type = src.m_type; - m_limite = src.m_limite; - m_pClass = src.m_pClass; - m_pNext = NULL; - if ( src.m_pNext != NULL ) - { - m_pNext = new CBotTypResult(*src.m_pNext); - } - return *this; -} - - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +//////////////////////////////////////////////////////////////////// +// Definition for the class CBotVar +// variables management of the language CBoT + +// it never creates an instance of the class mother CBotVar + +#include "CBot.h" +#include +#include + +long CBotVar::m_identcpt = 0; + +CBotVar::CBotVar( ) +{ + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = -1; + m_binit = false; + m_ident = 0; + m_bStatic = false; + m_mPrivate = 0; +} + +CBotVarInt::CBotVarInt( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypInt; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; + + m_val = 0; +} + +CBotVarFloat::CBotVarFloat( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypFloat; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; + + m_val = 0; +} + +CBotVarString::CBotVarString( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypString; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; + + m_val.Empty(); +} + +CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_type = CBotTypBoolean; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; + + m_val = 0; +} + +CBotVarClass* CBotVarClass::m_ExClass = NULL; + +CBotVarClass::CBotVarClass( const CBotToken* name, const CBotTypResult& type) +{ +/* +// int nIdent = 0; + InitCBotVarClass( name, type ) //, nIdent ); +} + +CBotVarClass::CBotVarClass( const CBotToken* name, CBotTypResult& type) //, int &nIdent ) +{ + InitCBotVarClass( name, type ); //, nIdent ); +} + +void CBotVarClass::InitCBotVarClass( const CBotToken* name, CBotTypResult& type ) //, int &nIdent ) +{*/ + if ( !type.Eq(CBotTypClass) && + !type.Eq(CBotTypIntrinsic) && // by convenience there accepts these types + !type.Eq(CBotTypPointer) && + !type.Eq(CBotTypArrayPointer) && + !type.Eq(CBotTypArrayBody)) ASM_TRAP(); + + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = OBJECTCREATED;//NULL; + m_InitExpr = NULL; + m_LimExpr = NULL; + m_pVar = NULL; + m_type = type; + if ( type.Eq(CBotTypArrayPointer) ) m_type.SetType( CBotTypArrayBody ); + else if ( !type.Eq(CBotTypArrayBody) ) m_type.SetType( CBotTypClass ); + // officel type for this object + + m_pClass = NULL; + m_pParent = NULL; + m_binit = false; + m_bStatic = false; + m_mPrivate = 0; + m_bConstructor = false; + m_CptUse = 0; + m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); + + // se place tout seul dans la liste + // TODO stands alone in the list (stands only in a list) + if (m_ExClass) m_ExClass->m_ExPrev = this; + m_ExNext = m_ExClass; + m_ExPrev = NULL; + m_ExClass = this; + + CBotClass* pClass = type.GivClass(); + CBotClass* pClass2 = pClass->GivParent(); + if ( pClass2 != NULL ) + { + // also creates an instance of the parent class + m_pParent = new CBotVarClass(name, CBotTypResult(type.GivType(),pClass2) ); //, nIdent); + } + + SetClass( pClass ); //, nIdent ); + +} + +CBotVarClass::~CBotVarClass( ) +{ + if ( m_CptUse != 0 ) + ASM_TRAP(); + + if ( m_pParent ) delete m_pParent; + m_pParent = NULL; + + // frees the indirect object if necessary +// if ( m_Indirect != NULL ) +// m_Indirect->DecrementUse(); + + // removes the class list + if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; + else m_ExClass = m_ExNext; + + if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; + m_ExPrev = NULL; + m_ExNext = NULL; + + delete m_pVar; +} + +void CBotVarClass::ConstructorSet() +{ + m_bConstructor = true; +} + + +CBotVar::~CBotVar( ) +{ + delete m_token; + delete m_next; +} + +void CBotVar::debug() +{ + const char* p = (const char*) m_token->GivString(); + CBotString s = (const char*) GivValString(); + const char* v = (const char*) s; + + if ( m_type.Eq(CBotTypClass) ) + { + CBotVar* pv = ((CBotVarClass*)this)->m_pVar; + while (pv != NULL) + { + pv->debug(); + pv = pv->GivNext(); + } + } +} + +void CBotVar::ConstructorSet() +{ + // nop +} + +void CBotVar::SetUserPtr(void* pUser) +{ + m_pUserPtr = pUser; + if (m_type.Eq(CBotTypPointer) && + ((CBotVarPointer*)this)->m_pVarClass != NULL ) + ((CBotVarPointer*)this)->m_pVarClass->SetUserPtr(pUser); +} + +void CBotVar::SetIdent(long n) +{ + if (m_type.Eq(CBotTypPointer) && + ((CBotVarPointer*)this)->m_pVarClass != NULL ) + ((CBotVarPointer*)this)->m_pVarClass->SetIdent(n); +} + +void CBotVar::SetUniqNum(long n) +{ + m_ident = n; + + if ( n == 0 ) ASM_TRAP(); +} + +long CBotVar::NextUniqNum() +{ + if (++m_identcpt < 10000) m_identcpt = 10000; + return m_identcpt; +} + +long CBotVar::GivUniqNum() +{ + return m_ident; +} + + +void* CBotVar::GivUserPtr() +{ + return m_pUserPtr; +} + +bool CBotVar::Save1State(FILE* pf) +{ + // this routine "virtual" must never be called, + // there must be a routine for each of the subclasses (CBotVarInt, CBotVarFloat, etc) + // ( see the type in m_type ) + ASM_TRAP(); + return false; +} + +void CBotVar::Maj(void* pUser, bool bContinu) +{ +/* if (!bContinu && m_pMyThis != NULL) + m_pMyThis->Maj(pUser, true);*/ +} + + +// creates a variable depending on its type + +CBotVar* CBotVar::Create(const CBotToken* name, int type ) +{ + CBotTypResult t(type); + return Create(name, t); +} + +CBotVar* CBotVar::Create(const CBotToken* name, CBotTypResult type) +{ + switch (type.GivType()) + { + case CBotTypShort: + case CBotTypInt: + return new CBotVarInt(name); + case CBotTypFloat: + return new CBotVarFloat(name); + case CBotTypBoolean: + return new CBotVarBoolean(name); + case CBotTypString: + return new CBotVarString(name); + case CBotTypPointer: + case CBotTypNullPointer: + return new CBotVarPointer(name, type); + case CBotTypIntrinsic: + return new CBotVarClass(name, type); + + case CBotTypClass: + // creates a new instance of a class + // and returns the POINTER on this instance + { + CBotVarClass* instance = new CBotVarClass(name, type); + CBotVarPointer* pointer = new CBotVarPointer(name, type); + pointer->SetPointer( instance ); + return pointer; + } + + case CBotTypArrayPointer: + return new CBotVarArray(name, type); + + case CBotTypArrayBody: + { + CBotVarClass* instance = new CBotVarClass(name, type); + CBotVarArray* array = new CBotVarArray(name, type); + array->SetPointer( instance ); + + CBotVar* pv = array; + while (type.Eq(CBotTypArrayBody)) + { + type = type.GivTypElem(); + pv = ((CBotVarArray*)pv)->GivItem(0, true); // creates at least the element [0] + } + + return array; + } + } + + ASM_TRAP(); + return NULL; +} + +CBotVar* CBotVar::Create( CBotVar* pVar ) +{ + CBotVar* p = Create(pVar->m_token->GivString(), pVar->GivTypResult(2)); + return p; +} + + +CBotVar* CBotVar::Create( const char* n, CBotTypResult type) +{ + CBotToken name(n); + + switch (type.GivType()) + { + case CBotTypShort: + case CBotTypInt: + return new CBotVarInt(&name); + case CBotTypFloat: + return new CBotVarFloat(&name); + case CBotTypBoolean: + return new CBotVarBoolean(&name); + case CBotTypString: + return new CBotVarString(&name); + case CBotTypPointer: + case CBotTypNullPointer: + { + CBotVarPointer* p = new CBotVarPointer(&name, type); +// p->SetClass(type.GivClass()); + return p; + } + case CBotTypIntrinsic: + { + CBotVarClass* p = new CBotVarClass(&name, type); +// p->SetClass(type.GivClass()); + return p; + } + + case CBotTypClass: + // creates a new instance of a class + // and returns the POINTER on this instance + { + CBotVarClass* instance = new CBotVarClass(&name, type); + CBotVarPointer* pointer = new CBotVarPointer(&name, type); + pointer->SetPointer( instance ); +// pointer->SetClass( type.GivClass() ); + return pointer; + } + + case CBotTypArrayPointer: + return new CBotVarArray(&name, type); + + case CBotTypArrayBody: + { + CBotVarClass* instance = new CBotVarClass(&name, type); + CBotVarArray* array = new CBotVarArray(&name, type); + array->SetPointer( instance ); + + CBotVar* pv = array; + while (type.Eq(CBotTypArrayBody)) + { + type = type.GivTypElem(); + pv = ((CBotVarArray*)pv)->GivItem(0, true); // creates at least the element [0] + } + + return array; + } + } + + ASM_TRAP(); + return NULL; +} + +CBotVar* CBotVar::Create( const char* name, int type, CBotClass* pClass) +{ + CBotToken token( name, "" ); + CBotVar* pVar = Create( &token, type ); + + if ( type == CBotTypPointer && pClass == NULL ) // pointer "null" ? + return pVar; + + if ( type == CBotTypClass || type == CBotTypPointer || + type == CBotTypIntrinsic ) + { + if (pClass == NULL) + { + delete pVar; + return NULL; + } + pVar->SetClass( pClass ); + } + return pVar; +} + +CBotVar* CBotVar::Create( const char* name, CBotClass* pClass) +{ + CBotToken token( name, "" ); + CBotVar* pVar = Create( &token, CBotTypResult( CBotTypClass, pClass ) ); +// pVar->SetClass( pClass ); + return pVar; +} + +CBotTypResult CBotVar::GivTypResult(int mode) +{ + CBotTypResult r = m_type; + + if ( mode == 1 && m_type.Eq(CBotTypClass) ) + r.SetType(CBotTypPointer); + if ( mode == 2 && m_type.Eq(CBotTypClass) ) + r.SetType(CBotTypIntrinsic); + + return r; +} + +int CBotVar::GivType(int mode) +{ + if ( mode == 1 && m_type.Eq(CBotTypClass) ) + return CBotTypPointer; + if ( mode == 2 && m_type.Eq(CBotTypClass) ) + return CBotTypIntrinsic; + return m_type.GivType(); +} + +void CBotVar::SetType(CBotTypResult& type) +{ + m_type = type; +} + + +int CBotVar::GivInit() +{ + if ( m_type.Eq(CBotTypClass) ) return IS_DEF; // always set! + + return m_binit; +} + +void CBotVar::SetInit(int bInit) +{ + m_binit = bInit; + if ( bInit == 2 ) m_binit = IS_DEF; // cas spécial + + if ( m_type.Eq(CBotTypPointer) && bInit == 2 ) + { + CBotVarClass* instance = GivPointer(); + if ( instance == NULL ) + { + instance = new CBotVarClass(NULL, m_type); +// instance->SetClass(((CBotVarPointer*)this)->m_pClass); + SetPointer(instance); + } + instance->SetInit(1); + } + + if ( m_type.Eq(CBotTypClass) || m_type.Eq(CBotTypIntrinsic) ) + { + CBotVar* p = ((CBotVarClass*)this)->m_pVar; + while( p != NULL ) + { + p->SetInit( bInit ); + p->m_pMyThis = (CBotVarClass*)this; + p = p->GivNext(); + } + } +} + +CBotString CBotVar::GivName() +{ + return m_token->GivString(); +} + +void CBotVar::SetName(const char* name) +{ + m_token->SetString(name); +} + +CBotToken* CBotVar::GivToken() +{ + return m_token; +} + +CBotVar* CBotVar::GivItem(const char* name) +{ + ASM_TRAP(); + return NULL; +} + +CBotVar* CBotVar::GivItemRef(int nIdent) +{ + ASM_TRAP(); + return NULL; +} + +CBotVar* CBotVar::GivItemList() +{ + ASM_TRAP(); + return NULL; +} + +CBotVar* CBotVar::GivItem(int row, bool bGrow) +{ + ASM_TRAP(); + return NULL; +} + +// check if a variable belongs to a given class +bool CBotVar::IsElemOfClass(const char* name) +{ + CBotClass* pc = NULL; + + if ( m_type.Eq(CBotTypPointer) ) + { + pc = ((CBotVarPointer*)this)->m_pClass; + } + if ( m_type.Eq(CBotTypClass) ) + { + pc = ((CBotVarClass*)this)->m_pClass; + } + + while ( pc != NULL ) + { + if ( pc->GivName() == name ) return true; + pc = pc->GivParent(); + } + + return false; +} + + +CBotVar* CBotVar::GivStaticVar() +{ + // makes the pointer to the variable if it is static + if ( m_bStatic == 0 || m_pMyThis == NULL ) return this; + + CBotClass* pClass = m_pMyThis->GivClass(); + return pClass->GivItem( m_token->GivString() ); +} + + +CBotVar* CBotVar::GivNext() +{ + return m_next; +} + +void CBotVar::AddNext(CBotVar* pVar) +{ + CBotVar* p = this; + while (p->m_next != NULL) p = p->m_next; + + p->m_next = pVar; +} + +void CBotVar::SetVal(CBotVar* var) +{ + switch (/*var->*/GivType()) + { + case CBotTypBoolean: + SetValInt(var->GivValInt()); + break; + case CBotTypInt: + SetValInt(var->GivValInt(), ((CBotVarInt*)var)->m_defnum); + break; + case CBotTypFloat: + SetValFloat(var->GivValFloat()); + break; + case CBotTypString: + SetValString(var->GivValString()); + break; + case CBotTypPointer: + case CBotTypNullPointer: + case CBotTypArrayPointer: + SetPointer(var->GivPointer()); + break; + case CBotTypClass: + { + delete ((CBotVarClass*)this)->m_pVar; + ((CBotVarClass*)this)->m_pVar = NULL; + Copy(var, false); + } + break; + default: + ASM_TRAP(); + } + + m_binit = var->m_binit; // copie l'état nan s'il y a +} + +void CBotVar::SetStatic(bool bStatic) +{ + m_bStatic = bStatic; +} + +void CBotVar::SetPrivate(int mPrivate) +{ + m_mPrivate = mPrivate; +} + +bool CBotVar::IsStatic() +{ + return m_bStatic; +} + +bool CBotVar::IsPrivate(int mode) +{ + return m_mPrivate >= mode; +} + +int CBotVar::GivPrivate() +{ + return m_mPrivate; +} + + +void CBotVar::SetPointer(CBotVar* pVarClass) +{ + ASM_TRAP(); +} + +CBotVarClass* CBotVar::GivPointer() +{ + ASM_TRAP(); + return NULL; +} + +// All these functions must be defined in the subclasses +// derived from class CBotVar + +int CBotVar::GivValInt() +{ + ASM_TRAP(); + return 0; +} + +float CBotVar::GivValFloat() +{ + ASM_TRAP(); + return 0; +} + +void CBotVar::SetValInt(int c, const char* s) +{ + ASM_TRAP(); +} + +void CBotVar::SetValFloat(float c) +{ + ASM_TRAP(); +} + +void CBotVar::Mul(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::Power(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +int CBotVar::Div(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return 0; +} + +int CBotVar::Modulo(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return 0; +} + +void CBotVar::Add(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::Sub(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +bool CBotVar::Lo(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return false; +} + +bool CBotVar::Hi(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return false; +} + +bool CBotVar::Ls(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return false; +} + +bool CBotVar::Hs(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return false; +} + +bool CBotVar::Eq(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return false; +} + +bool CBotVar::Ne(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); + return false; +} + +void CBotVar::And(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::Or(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::XOr(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::ASR(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::SR(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::SL(CBotVar* left, CBotVar* right) +{ + ASM_TRAP(); +} + +void CBotVar::Neg() +{ + ASM_TRAP(); +} + +void CBotVar::Not() +{ + ASM_TRAP(); +} + +void CBotVar::Inc() +{ + ASM_TRAP(); +} +void CBotVar::Dec() +{ + ASM_TRAP(); +} + +void CBotVar::Copy(CBotVar* pSrc, bool bName) +{ + ASM_TRAP(); +} + +void CBotVar::SetValString(const char* p) +{ + ASM_TRAP(); +} + +CBotString CBotVar::GivValString() +{ + ASM_TRAP(); + return CBotString(); +} + +void CBotVar::SetClass(CBotClass* pClass) +{ + ASM_TRAP(); +} + +CBotClass* CBotVar::GivClass() +{ + ASM_TRAP(); + return NULL; +} + +/* +void CBotVar::SetIndirection(CBotVar* pVar) +{ + // nop, only CBotVarPointer::SetIndirection +} +*/ + +////////////////////////////////////////////////////////////////////////////////////// + +// copy a variable in to another +void CBotVarInt::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarInt* p = (CBotVarInt*)pSrc; + + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; + m_pMyThis = NULL; + m_pUserPtr = p->m_pUserPtr; + + // identificator is the same (by défaut) + if (m_ident == 0 ) m_ident = p->m_ident; + + m_defnum = p->m_defnum; +} + + + + +void CBotVarInt::SetValInt(int val, const char* defnum) +{ + m_val = val; + m_binit = true; + m_defnum = defnum; +} + + + +void CBotVarInt::SetValFloat(float val) +{ + m_val = (int)val; + m_binit = true; +} + +int CBotVarInt::GivValInt() +{ + return m_val; +} + +float CBotVarInt::GivValFloat() +{ + return (float)m_val; +} + +CBotString CBotVarInt::GivValString() +{ + if ( !m_defnum.IsEmpty() ) return m_defnum; + + CBotString res; + + if ( !m_binit ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } + + char buffer[300]; + sprintf(buffer, "%d", m_val); + res = buffer; + + return res; +} + + +void CBotVarInt::Mul(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() * right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::Power(CBotVar* left, CBotVar* right) +{ + m_val = (int) pow( (double) left->GivValInt() , (double) right->GivValInt() ); + m_binit = true; +} + +int CBotVarInt::Div(CBotVar* left, CBotVar* right) +{ + int r = right->GivValInt(); + if ( r != 0 ) + { + m_val = left->GivValInt() / r; + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) +{ + int r = right->GivValInt(); + if ( r != 0 ) + { + m_val = left->GivValInt() % r; + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +void CBotVarInt::Add(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() + right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::Sub(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() - right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::XOr(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() ^ right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::And(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() & right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::Or(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() | right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::SL(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() << right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::ASR(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() >> right->GivValInt(); + m_binit = true; +} + +void CBotVarInt::SR(CBotVar* left, CBotVar* right) +{ + int source = left->GivValInt(); + int shift = right->GivValInt(); + if (shift>=1) source &= 0x7fffffff; + m_val = source >> shift; + m_binit = true; +} + +void CBotVarInt::Neg() +{ + m_val = -m_val; +} + +void CBotVarInt::Not() +{ + m_val = ~m_val; +} + +void CBotVarInt::Inc() +{ + m_val++; + m_defnum.Empty(); +} + +void CBotVarInt::Dec() +{ + m_val--; + m_defnum.Empty(); +} + +bool CBotVarInt::Lo(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() < right->GivValInt(); +} + +bool CBotVarInt::Hi(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() > right->GivValInt(); +} + +bool CBotVarInt::Ls(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() <= right->GivValInt(); +} + +bool CBotVarInt::Hs(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() >= right->GivValInt(); +} + +bool CBotVarInt::Eq(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() == right->GivValInt(); +} + +bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() != right->GivValInt(); +} + + +////////////////////////////////////////////////////////////////////////////////////// + +// copy a variable into another +void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarFloat* p = (CBotVarFloat*)pSrc; + + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + + + + +void CBotVarFloat::SetValInt(int val, const char* s) +{ + m_val = (float)val; + m_binit = true; +} + +void CBotVarFloat::SetValFloat(float val) +{ + m_val = val; + m_binit = true; +} + +int CBotVarFloat::GivValInt() +{ + return (int)m_val; +} + +float CBotVarFloat::GivValFloat() +{ + return m_val; +} + +CBotString CBotVarFloat::GivValString() +{ + CBotString res; + + if ( !m_binit ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } + + char buffer[300]; + sprintf(buffer, "%.2f", m_val); + res = buffer; + + return res; +} + + +void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValFloat() * right->GivValFloat(); + m_binit = true; +} + +void CBotVarFloat::Power(CBotVar* left, CBotVar* right) +{ + m_val = (float)pow( left->GivValFloat() , right->GivValFloat() ); + m_binit = true; +} + +int CBotVarFloat::Div(CBotVar* left, CBotVar* right) +{ + float r = right->GivValFloat(); + if ( r != 0 ) + { + m_val = left->GivValFloat() / r; + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) +{ + float r = right->GivValFloat(); + if ( r != 0 ) + { + m_val = (float)fmod( left->GivValFloat() , r ); + m_binit = true; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +void CBotVarFloat::Add(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValFloat() + right->GivValFloat(); + m_binit = true; +} + +void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValFloat() - right->GivValFloat(); + m_binit = true; +} + +void CBotVarFloat::Neg() +{ + m_val = -m_val; +} + +void CBotVarFloat::Inc() +{ + m_val++; +} + +void CBotVarFloat::Dec() +{ + m_val--; +} + + +bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) +{ + return left->GivValFloat() < right->GivValFloat(); +} + +bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) +{ + return left->GivValFloat() > right->GivValFloat(); +} + +bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) +{ + return left->GivValFloat() <= right->GivValFloat(); +} + +bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) +{ + return left->GivValFloat() >= right->GivValFloat(); +} + +bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) +{ + return left->GivValFloat() == right->GivValFloat(); +} + +bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) +{ + return left->GivValFloat() != right->GivValFloat(); +} + + +////////////////////////////////////////////////////////////////////////////////////// + +// copy a variable into another +void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarBoolean* p = (CBotVarBoolean*)pSrc; + + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + + + + +void CBotVarBoolean::SetValInt(int val, const char* s) +{ + m_val = (bool)val; + m_binit = true; +} + +void CBotVarBoolean::SetValFloat(float val) +{ + m_val = (bool)val; + m_binit = true; +} + +int CBotVarBoolean::GivValInt() +{ + return m_val; +} + +float CBotVarBoolean::GivValFloat() +{ + return (float)m_val; +} + +CBotString CBotVarBoolean::GivValString() +{ + CBotString ret; + + CBotString res; + + if ( !m_binit ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } + + ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); + return ret; +} + +void CBotVarBoolean::And(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() && right->GivValInt(); + m_binit = true; +} +void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() || right->GivValInt(); + m_binit = true; +} + +void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValInt() ^ right->GivValInt(); + m_binit = true; +} + +void CBotVarBoolean::Not() +{ + m_val = m_val ? false : true ; +} + +bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() == right->GivValInt(); +} + +bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) +{ + return left->GivValInt() != right->GivValInt(); +} + +////////////////////////////////////////////////////////////////////////////////////// + +// copy a variable into another +void CBotVarString::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarString* p = (CBotVarString*)pSrc; + + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + + +void CBotVarString::SetValString(const char* p) +{ + m_val = p; + m_binit = true; +} + +CBotString CBotVarString::GivValString() +{ + if ( !m_binit ) + { + CBotString res; + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == IS_NAN ) + { + CBotString res; + res.LoadString(TX_NAN); + return res; + } + + return m_val; +} + + +void CBotVarString::Add(CBotVar* left, CBotVar* right) +{ + m_val = left->GivValString() + right->GivValString(); + m_binit = true; +} + +bool CBotVarString::Eq(CBotVar* left, CBotVar* right) +{ + return (left->GivValString() == right->GivValString()); +} + +bool CBotVarString::Ne(CBotVar* left, CBotVar* right) +{ + return (left->GivValString() != right->GivValString()); +} + + +bool CBotVarString::Lo(CBotVar* left, CBotVar* right) +{ + return (left->GivValString() == right->GivValString()); +} + +bool CBotVarString::Hi(CBotVar* left, CBotVar* right) +{ + return (left->GivValString() == right->GivValString()); +} + +bool CBotVarString::Ls(CBotVar* left, CBotVar* right) +{ + return (left->GivValString() == right->GivValString()); +} + +bool CBotVarString::Hs(CBotVar* left, CBotVar* right) +{ + return (left->GivValString() == right->GivValString()); +} + + +//////////////////////////////////////////////////////////////// + +// copy a variable into another +void CBotVarClass::Copy(CBotVar* pSrc, bool bName) +{ + pSrc = pSrc->GivPointer(); // if source given by a pointer + + if ( pSrc->GivType() != CBotTypClass ) + ASM_TRAP(); + + CBotVarClass* p = (CBotVarClass*)pSrc; + + if (bName) *m_token = *p->m_token; + + m_type = p->m_type; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_pClass = p->m_pClass; + if ( p->m_pParent ) + { + ASM_TRAP(); "que faire du pParent"; + } + +// m_next = NULL; + m_pUserPtr = p->m_pUserPtr; + m_pMyThis = NULL;//p->m_pMyThis; + m_ItemIdent = p->m_ItemIdent; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; + + delete m_pVar; + m_pVar = NULL; + + CBotVar* pv = p->m_pVar; + while( pv != NULL ) + { + CBotVar* pn = CBotVar::Create(pv); + pn->Copy( pv ); + if ( m_pVar == NULL ) m_pVar = pn; + else m_pVar->AddNext(pn); + + pv = pv->GivNext(); + } +} + +void CBotVarClass::SetItemList(CBotVar* pVar) +{ + delete m_pVar; + m_pVar = pVar; // replaces the existing pointer +} + +void CBotVarClass::SetIdent(long n) +{ + m_ItemIdent = n; +} + +void CBotVarClass::SetClass(CBotClass* pClass)//, int &nIdent) +{ + m_type.m_pClass = pClass; + + if ( m_pClass == pClass ) return; + + m_pClass = pClass; + + // initializes the variables associated with this class + delete m_pVar; + m_pVar = NULL; + + if (pClass == NULL) return; + + CBotVar* pv = pClass->GivVar(); // first on a list + while ( pv != NULL ) + { + // seeks the maximum dimensions of the table + CBotInstr* p = pv->m_LimExpr; // the different formulas + if ( p != NULL ) + { + CBotStack* pile = CBotStack::FirstStack(); // an independent stack + int n = 0; + int max[100]; + + while (p != NULL) + { + while( pile->IsOk() && !p->Execute(pile) ) ; // calculate size without interruptions + CBotVar* v = pile->GivVar(); // result + max[n] = v->GivValInt(); // value + n++; + p = p->GivNext3(); + } + while (n<100) max[n++] = 0; + + pv->m_type.SetArray( max ); // stores the limitations + pile->Delete(); + } + + CBotVar* pn = CBotVar::Create( pv ); // a copy + pn->SetStatic(pv->IsStatic()); + pn->SetPrivate(pv->GivPrivate()); + + if ( pv->m_InitExpr != NULL ) // expression for initialization? + { +#if STACKMEM + CBotStack* pile = CBotStack::FirstStack(); // an independent stack + + while(pile->IsOk() && !pv->m_InitExpr->Execute(pile, pn)); // evaluates the expression without timer + + pile->Delete(); +#else + CBotStack* pile = new CBotStack(NULL); // an independent stack + while(!pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer + pn->SetVal( pile->GivVar() ) ; + delete pile; +#endif + } + +// pn->SetUniqNum(CBotVar::NextUniqNum()); // enumerate elements + pn->SetUniqNum(pv->GivUniqNum()); //++nIdent + pn->m_pMyThis = this; + + if ( m_pVar == NULL) m_pVar = pn; + else m_pVar->AddNext( pn ); + pv = pv->GivNext(); + } +} + +CBotClass* CBotVarClass::GivClass() +{ + return m_pClass; +} + + +void CBotVarClass::Maj(void* pUser, bool bContinu) +{ +/* if (!bContinu && m_pMyThis != NULL) + m_pMyThis->Maj(pUser, true);*/ + + // an update routine exist? + + if ( m_pClass->m_rMaj == NULL ) return; + + // retrieves the user pointer according to the class + // or according to the parameter passed to CBotProgram::Run() + + if ( m_pUserPtr != NULL) pUser = m_pUserPtr; + if ( pUser == OBJECTDELETED || + pUser == OBJECTCREATED ) return; + m_pClass->m_rMaj( this, pUser ); +} + +CBotVar* CBotVarClass::GivItem(const char* name) +{ + CBotVar* p = m_pVar; + + while ( p != NULL ) + { + if ( p->GivName() == name ) return p; + p = p->GivNext(); + } + + if ( m_pParent != NULL ) return m_pParent->GivItem(name); + return NULL; +} + +CBotVar* CBotVarClass::GivItemRef(int nIdent) +{ + CBotVar* p = m_pVar; + + while ( p != NULL ) + { + if ( p->GivUniqNum() == nIdent ) return p; + p = p->GivNext(); + } + + if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); + return NULL; +} + +// for the management of an array +// bExtend can enlarge the table, but not beyond the threshold size of SetArray () + +CBotVar* CBotVarClass::GivItem(int n, bool bExtend) +{ + CBotVar* p = m_pVar; + + if ( n < 0 ) return NULL; + if ( n > MAXARRAYSIZE ) return NULL; + + if ( m_type.GivLimite() >= 0 && n >= m_type.GivLimite() ) return NULL; + + if ( p == NULL && bExtend ) + { + p = CBotVar::Create("", m_type.GivTypElem()); + m_pVar = p; + } + + if ( n == 0 ) return p; + + while ( n-- > 0 ) + { + if ( p->m_next == NULL ) + { + if ( bExtend ) p->m_next = CBotVar::Create("", m_type.GivTypElem()); + if ( p->m_next == NULL ) return NULL; + } + p = p->m_next; + } + + return p; +} + +CBotVar* CBotVarClass::GivItemList() +{ + return m_pVar; +} + + +CBotString CBotVarClass::GivValString() +{ +// if ( m_Indirect != NULL) return m_Indirect->GivValString(); + + CBotString res; + + if ( m_pClass != NULL ) // not used for an array + { + res = m_pClass->GivName() + CBotString("( "); + + CBotVarClass* my = this; + while ( my != NULL ) + { + CBotVar* pv = my->m_pVar; + while ( pv != NULL ) + { + res += pv->GivName() + CBotString("="); + + if ( pv->IsStatic() ) + { + CBotVar* pvv = my->m_pClass->GivItem(pv->GivName()); + res += pvv->GivValString(); + } + else + { + res += pv->GivValString(); + } + pv = pv->GivNext(); + if ( pv != NULL ) res += ", "; + } + my = my->m_pParent; + if ( my != NULL ) + { + res += ") extends "; + res += my->m_pClass->GivName(); + res += " ("; + } + } + } + else + { + res = "( "; + + CBotVar* pv = m_pVar; + while ( pv != NULL ) + { + res += pv->GivValString(); + if ( pv->GivNext() != NULL ) res += ", "; + pv = pv->GivNext(); + } + } + + res += " )"; + return res; +} + +void CBotVarClass::IncrementUse() +{ + m_CptUse++; +} + +void CBotVarClass::DecrementUse() +{ + m_CptUse--; + if ( m_CptUse == 0 ) + { + // if there is one, call the destructor + // but only if a constructor had been called. + if ( m_bConstructor ) + { + m_CptUse++; // does not return to the destructor + + // m_error is static in the stack + // saves the value for return + int err, start, end; + CBotStack* pile = NULL; + err = pile->GivError(start,end); // stack == NULL it does not bother! + + pile = CBotStack::FirstStack(); // clears the error + CBotVar* ppVars[1]; + ppVars[0] = NULL; + + CBotVar* pThis = CBotVar::Create("this", CBotTypNullPointer); + pThis->SetPointer(this); + CBotVar* pResult = NULL; + + CBotString nom = "~" + m_pClass->GivName(); + long ident = 0; + + while ( pile->IsOk() && !m_pClass->ExecuteMethode(ident, nom, pThis, ppVars, pResult, pile, NULL)) ; // waits for the end + + pile->ResetError(err, start,end); + + pile->Delete(); + delete pThis; + m_CptUse--; + } + + delete this; // self-destructs! + } +} + +CBotVarClass* CBotVarClass::GivPointer() +{ + return this; +} + + +// makes an instance according to its unique number + +CBotVarClass* CBotVarClass::Find(long id) +{ + CBotVarClass* p = m_ExClass; + + while ( p != NULL ) + { + if ( p->m_ItemIdent == id ) return p; + p = p->m_ExNext; + } + + return NULL; +} + +bool CBotVarClass::Eq(CBotVar* left, CBotVar* right) +{ + CBotVar* l = left->GivItemList(); + CBotVar* r = right->GivItemList(); + + while ( l != NULL && r != NULL ) + { + if ( l->Ne(l, r) ) return false; + l = l->GivNext(); + r = r->GivNext(); + } + + // should always arrived simultaneously at the end (same classes) + return l == r; +} + +bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) +{ + CBotVar* l = left->GivItemList(); + CBotVar* r = right->GivItemList(); + + while ( l != NULL && r != NULL ) + { + if ( l->Ne(l, r) ) return true; + l = l->GivNext(); + r = r->GivNext(); + } + + // should always arrived simultaneously at the end (same classes) + return l != r; +} + +///////////////////////////////////////////////////////////////////////////// +// management of arrays + +CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type ) +{ + if ( !type.Eq(CBotTypArrayPointer) && + !type.Eq(CBotTypArrayBody)) ASM_TRAP(); + + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + + m_type = type; + m_type.SetType(CBotTypArrayPointer); + m_binit = false; + + m_pInstance = NULL; // the list of the array elements +} + +CBotVarArray::~CBotVarArray() +{ + if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); // the lowest reference +} + +// copy a variable into another +void CBotVarArray::Copy(CBotVar* pSrc, bool bName) +{ + if ( pSrc->GivType() != CBotTypArrayPointer ) + ASM_TRAP(); + + CBotVarArray* p = (CBotVarArray*)pSrc; + + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; + m_pInstance = p->GivPointer(); + + if ( m_pInstance != NULL ) + m_pInstance->IncrementUse(); // a reference increase + + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +void CBotVarArray::SetPointer(CBotVar* pVarClass) +{ + m_binit = true; // init, even on a null pointer + + if ( m_pInstance == pVarClass) return; // Special, not decrement and reincrement + // because the decrement can destroy the object + + if ( pVarClass != NULL ) + { + if ( pVarClass->GivType() == CBotTypArrayPointer ) + pVarClass = pVarClass->GivPointer(); // the real pointer to the object + + if ( !pVarClass->m_type.Eq(CBotTypClass) && + !pVarClass->m_type.Eq(CBotTypArrayBody)) + ASM_TRAP(); + + ((CBotVarClass*)pVarClass)->IncrementUse(); // incement the reference + } + + if ( m_pInstance != NULL ) m_pInstance->DecrementUse(); + m_pInstance = (CBotVarClass*)pVarClass; +} + + +CBotVarClass* CBotVarArray::GivPointer() +{ + if ( m_pInstance == NULL ) return NULL; + return m_pInstance->GivPointer(); +} + +CBotVar* CBotVarArray::GivItem(int n, bool bExtend) +{ + if ( m_pInstance == NULL ) + { + if ( !bExtend ) return NULL; + // creates an instance of the table + + CBotVarClass* instance = new CBotVarClass(NULL, m_type); + SetPointer( instance ); + } + return m_pInstance->GivItem(n, bExtend); +} + +CBotVar* CBotVarArray::GivItemList() +{ + if ( m_pInstance == NULL) return NULL; + return m_pInstance->GivItemList(); +} + +CBotString CBotVarArray::GivValString() +{ + if ( m_pInstance == NULL ) return ( CBotString( "Null pointer" ) ) ; + return m_pInstance->GivValString(); +} + +bool CBotVarArray::Save1State(FILE* pf) +{ + if ( !WriteType(pf, m_type) ) return false; + return SaveVar(pf, m_pInstance); // saves the instance that manages the table +} + + +///////////////////////////////////////////////////////////////////////////// +// gestion des pointeurs à une instance donnée +// TODO management of pointers to a given instance + +CBotVarPointer::CBotVarPointer(const CBotToken* name, CBotTypResult& type ) +{ + if ( !type.Eq(CBotTypPointer) && + !type.Eq(CBotTypNullPointer) && + !type.Eq(CBotTypClass) && // for convenience accepts Class and Intrinsic + !type.Eq(CBotTypIntrinsic) ) ASM_TRAP(); + + m_token = new CBotToken(name); + m_next = NULL; + m_pMyThis = NULL; + m_pUserPtr = NULL; + + m_type = type; + if ( !type.Eq(CBotTypNullPointer) ) + m_type.SetType(CBotTypPointer); // anyway, this is a pointer + m_binit = false; + m_pClass = NULL; + m_pVarClass = NULL; // will be defined by a SetPointer() + + SetClass(type.GivClass() ); +} + +CBotVarPointer::~CBotVarPointer() +{ + if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); // decrement reference +} + + +void CBotVarPointer::Maj(void* pUser, bool bContinu) +{ +/* if ( !bContinu && m_pMyThis != NULL ) + m_pMyThis->Maj(pUser, false);*/ + + if ( m_pVarClass != NULL) m_pVarClass->Maj(pUser, false); +} + +CBotVar* CBotVarPointer::GivItem(const char* name) +{ + if ( m_pVarClass == NULL) // no existing instance? + return m_pClass->GivItem(name); // makes the pointer in the class itself + + return m_pVarClass->GivItem(name); +} + +CBotVar* CBotVarPointer::GivItemRef(int nIdent) +{ + if ( m_pVarClass == NULL) // no existing instance? + return m_pClass->GivItemRef(nIdent);// makes the pointer to the class itself + + return m_pVarClass->GivItemRef(nIdent); +} + +CBotVar* CBotVarPointer::GivItemList() +{ + if ( m_pVarClass == NULL) return NULL; + return m_pVarClass->GivItemList(); +} + +CBotString CBotVarPointer::GivValString() +{ + CBotString s = "Pointer to "; + if ( m_pVarClass == NULL ) s = "Null pointer" ; + else s += m_pVarClass->GivValString(); + return s; +} + + +void CBotVarPointer::ConstructorSet() +{ + if ( m_pVarClass != NULL) m_pVarClass->ConstructorSet(); +} + +// initializes the pointer to the instance of a class + +void CBotVarPointer::SetPointer(CBotVar* pVarClass) +{ + m_binit = true; // init, even on a null pointer + + if ( m_pVarClass == pVarClass) return; // special, not decrement and reincrement + // because the decrement can destroy the object + + if ( pVarClass != NULL ) + { + if ( pVarClass->GivType() == CBotTypPointer ) + pVarClass = pVarClass->GivPointer(); // the real pointer to the object + +// if ( pVarClass->GivType() != CBotTypClass ) + if ( !pVarClass->m_type.Eq(CBotTypClass) ) + ASM_TRAP(); + + ((CBotVarClass*)pVarClass)->IncrementUse(); // increment the reference + m_pClass = ((CBotVarClass*)pVarClass)->m_pClass; + m_pUserPtr = pVarClass->m_pUserPtr; // not really necessary + m_type = CBotTypResult(CBotTypPointer, m_pClass); // what kind of a pointer + } + + if ( m_pVarClass != NULL ) m_pVarClass->DecrementUse(); + m_pVarClass = (CBotVarClass*)pVarClass; + +} + +CBotVarClass* CBotVarPointer::GivPointer() +{ + if ( m_pVarClass == NULL ) return NULL; + return m_pVarClass->GivPointer(); +} + +void CBotVarPointer::SetIdent(long n) +{ + if ( m_pVarClass == NULL ) return; + m_pVarClass->SetIdent( n ); +} + +long CBotVarPointer::GivIdent() +{ + if ( m_pVarClass == NULL ) return 0; + return m_pVarClass->m_ItemIdent; +} + + +void CBotVarPointer::SetClass(CBotClass* pClass) +{ +// int nIdent = 0; + m_type.m_pClass = m_pClass = pClass; + if ( m_pVarClass != NULL ) m_pVarClass->SetClass(pClass); //, nIdent); +} + +CBotClass* CBotVarPointer::GivClass() +{ + if ( m_pVarClass != NULL ) return m_pVarClass->GivClass(); + + return m_pClass; +} + + +bool CBotVarPointer::Save1State(FILE* pf) +{ + if ( m_pClass ) + { + if (!WriteString(pf, m_pClass->GivName())) return false; // name of the class + } + else + { + if (!WriteString(pf, "")) return false; + } + + if (!WriteLong(pf, GivIdent())) return false; // the unique reference + + // also saves the proceedings copies + return SaveVar(pf, GivPointer()); +} + +// copy a variable into another +void CBotVarPointer::Copy(CBotVar* pSrc, bool bName) +{ + if ( pSrc->GivType() != CBotTypPointer && + pSrc->GivType() != CBotTypNullPointer) + ASM_TRAP(); + + CBotVarPointer* p = (CBotVarPointer*)pSrc; + + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; +// m_pVarClass = p->m_pVarClass; + m_pVarClass = p->GivPointer(); + + if ( m_pVarClass != NULL ) + m_pVarClass->IncrementUse(); // incerement the reference + + m_pClass = p->m_pClass; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = NULL; + m_pMyThis = NULL;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right) +{ + CBotVarClass* l = left->GivPointer(); + CBotVarClass* r = right->GivPointer(); + + if ( l == r ) return true; + if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return true; + if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return true; + return false; +} + +bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) +{ + CBotVarClass* l = left->GivPointer(); + CBotVarClass* r = right->GivPointer(); + + if ( l == r ) return false; + if ( l == NULL && r->GivUserPtr() == OBJECTDELETED ) return false; + if ( r == NULL && l->GivUserPtr() == OBJECTDELETED ) return false; + return true; +} + + + +/////////////////////////////////////////////////////// +// management of results types + + +CBotTypResult::CBotTypResult(int type) +{ + m_type = type; + m_pNext = NULL; + m_pClass = NULL; + m_limite = -1; +} + +CBotTypResult::CBotTypResult(int type, const char* name) +{ + m_type = type; + m_pNext = NULL; + m_pClass = NULL; + m_limite = -1; + + if ( type == CBotTypPointer || + type == CBotTypClass || + type == CBotTypIntrinsic ) + { + m_pClass = CBotClass::Find(name); + if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; + } +} + +CBotTypResult::CBotTypResult(int type, CBotClass* pClass) +{ + m_type = type; + m_pNext = NULL; + m_pClass = pClass; + m_limite = -1; + + if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; +} + +CBotTypResult::CBotTypResult(int type, CBotTypResult elem) +{ + m_type = type; + m_pNext = NULL; + m_pClass = NULL; + m_limite = -1; + + if ( type == CBotTypArrayPointer || + type == CBotTypArrayBody ) + m_pNext = new CBotTypResult( elem ); +} + +CBotTypResult::CBotTypResult(const CBotTypResult& typ) +{ + m_type = typ.m_type; + m_pClass = typ.m_pClass; + m_pNext = NULL; + m_limite = typ.m_limite; + + if ( typ.m_pNext ) + m_pNext = new CBotTypResult( *typ.m_pNext ); +} + +CBotTypResult::CBotTypResult() +{ + m_type = 0; + m_limite = -1; + m_pNext = NULL; + m_pClass = NULL; +} + +CBotTypResult::~CBotTypResult() +{ + delete m_pNext; +} + +int CBotTypResult::GivType(int mode) const +{ +#ifdef _DEBUG + if ( m_type == CBotTypPointer || + m_type == CBotTypClass || + m_type == CBotTypIntrinsic ) + + if ( m_pClass == NULL ) ASM_TRAP(); + + + if ( m_type == CBotTypArrayPointer ) + if ( m_pNext == NULL ) ASM_TRAP(); +#endif + if ( mode == 3 && m_type == CBotTypNullPointer ) return CBotTypPointer; + return m_type; +} + +void CBotTypResult::SetType(int n) +{ + m_type = n; +} + +CBotClass* CBotTypResult::GivClass() const +{ + return m_pClass; +} + +CBotTypResult& CBotTypResult::GivTypElem() const +{ + return *m_pNext; +} + +int CBotTypResult::GivLimite() const +{ + return m_limite; +} + +void CBotTypResult::SetLimite(int n) +{ + m_limite = n; +} + +void CBotTypResult::SetArray( int* max ) +{ + m_limite = *max; + if (m_limite < 1) m_limite = -1; + + if ( m_pNext != NULL ) // last dimension? + { + m_pNext->SetArray( max+1 ); + } +} + + + +bool CBotTypResult::Compare(const CBotTypResult& typ) const +{ + if ( m_type != typ.m_type ) return false; + + if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); + + if ( m_type == CBotTypPointer || + m_type == CBotTypClass || + m_type == CBotTypIntrinsic ) + { + return m_pClass == typ.m_pClass; + } + + return true; +} + +bool CBotTypResult::Eq(int type) const +{ + return m_type == type; +} + +CBotTypResult& + CBotTypResult::operator=(const CBotTypResult& src) +{ + m_type = src.m_type; + m_limite = src.m_limite; + m_pClass = src.m_pClass; + m_pNext = NULL; + if ( src.m_pNext != NULL ) + { + m_pNext = new CBotTypResult(*src.m_pNext); + } + return *this; +} + + -- cgit v1.2.3-1-g7c22 From 0844a0f7bdcd3184ee60aaf0e943b65c73c8c283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopacki?= Date: Tue, 7 Aug 2012 15:46:04 +0200 Subject: Further CBot translations --- src/CBot/CBot.cpp | 7927 ++++++++++++++++++++++++------------------------ src/CBot/CBot.h | 3465 +++++++++++---------- src/CBot/CBotClass.cpp | 1762 +++++------ 3 files changed, 6578 insertions(+), 6576 deletions(-) diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index fb25049..62d9fb7 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -1,3963 +1,3964 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// - -// compilation of various instructions -// compile all routines are static -// and return an object according to what was found as instruction - -// compiler principle: -// compile the routines return an object of the class corresponding to the operation found -// this is always a subclass of CBotInstr. -// (CBotInstr objects are never used directly) - - -// compiles if the routine returns NULL is that the statement is false -// or misunderstood. -// the error is then on the stack CBotCStack :: Isok () is false - - - -#include "CBot.h" - -CBotInstr::CBotInstr() -{ - name = "CBotInstr"; - m_next = NULL; - m_next2b = NULL; - m_next3 = NULL; - m_next3b = NULL; -} - -CBotInstr::~CBotInstr() -{ - delete m_next; - delete m_next2b; - delete m_next3; - delete m_next3b; -} - -// counter of nested loops, -// to determine the break and continue valid -// list of labels used - - -int CBotInstr::m_LoopLvl = 0; -CBotStringArray CBotInstr::m_labelLvl = CBotStringArray(); - -// adds a level with a label -void CBotInstr::IncLvl(CBotString& label) -{ - m_labelLvl.SetSize(m_LoopLvl+1); - m_labelLvl[m_LoopLvl] = label; - m_LoopLvl++; -} - -// adds a level (switch statement) -void CBotInstr::IncLvl() -{ - m_labelLvl.SetSize(m_LoopLvl+1); - m_labelLvl[m_LoopLvl] = "#SWITCH"; - m_LoopLvl++; -} - -// free a level -void CBotInstr::DecLvl() -{ - m_LoopLvl--; - m_labelLvl[m_LoopLvl].Empty(); -} - -// control validity of break and continue -bool CBotInstr::ChkLvl(const CBotString& label, int type) -{ - int i = m_LoopLvl; - while (--i>=0) - { - if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; - if (label.IsEmpty()) return true; - if (m_labelLvl[i] == label) return true; - } - return false; -} - -bool CBotInstr::IsOfClass(CBotString n) -{ - return name == n; -} - - -//////////////////////////////////////////////////////////////////////////// -// database management class CBotInstr - -// set the token corresponding to the instruction - -void CBotInstr::SetToken(CBotToken* p) -{ - m_token = *p; -} - -// return the type of the token assicated with the instruction - -int CBotInstr::GivTokenType() -{ - return m_token.GivType(); -} - -// return associated token - -CBotToken* CBotInstr::GivToken() -{ - return &m_token; -} - -// adds the statement following the other - -void CBotInstr::AddNext(CBotInstr* n) -{ - CBotInstr* p = this; - while (p->m_next != NULL) p = p->m_next; - p->m_next = n; -} - -void CBotInstr::AddNext3(CBotInstr* n) -{ - CBotInstr* p = this; - while (p->m_next3 != NULL) p = p->m_next3; - p->m_next3 = n; -} - -void CBotInstr::AddNext3b(CBotInstr* n) -{ - CBotInstr* p = this; - while (p->m_next3b != NULL) p = p->m_next3b; - p->m_next3b = n; -} - -// returns next statement - -CBotInstr* CBotInstr::GivNext() -{ - return m_next; -} - -CBotInstr* CBotInstr::GivNext3() -{ - return m_next3; -} - -CBotInstr* CBotInstr::GivNext3b() -{ - return m_next3b; -} - -/////////////////////////////////////////////////////////////////////////// -// compile an instruction which can be -// while, do, try, throw, if, for, switch, break, continue, return -// int, float, boolean, string, -// declaration of an instance of a class -// arbitrary expression - - -CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - - if (p == NULL) return NULL; - - int type = p->GivType(); // what is the next token - - // is it a lable? - if (IsOfType(pp, TokenTypVar) && - IsOfType(pp, ID_DOTS)) - { - type = pp->GivType(); - // these instructions accept only lable - if (!IsOfTypeList(pp, ID_WHILE, ID_FOR, ID_DO, ID_REPEAT, 0)) - { - pStack->SetError(TX_LABEL, pp->GivStart()); - return NULL; - } - } - - // call routine corresponding to the compilation token found - switch (type) - { - case ID_WHILE: - return CBotWhile::Compile(p, pStack); - - case ID_FOR: - return CBotFor::Compile(p, pStack); - - case ID_DO: - return CBotDo::Compile(p, pStack); - - case ID_REPEAT: - return CBotRepeat::Compile(p, pStack); - - case ID_BREAK: - case ID_CONTINUE: - return CBotBreak::Compile(p, pStack); - - case ID_SWITCH: - return CBotSwitch::Compile(p, pStack); - - case ID_TRY: - return CBotTry::Compile(p, pStack); - - case ID_THROW: - return CBotThrow::Compile(p, pStack); - - case ID_DEBUGDD: - return CBotStartDebugDD::Compile(p, pStack); - - case ID_INT: - return CBotInt::Compile(p, pStack); - - case ID_FLOAT: - return CBotFloat::Compile(p, pStack); - - case ID_STRING: - return CBotIString::Compile(p, pStack); - - case ID_BOOLEAN: - case ID_BOOL: - return CBotBoolean::Compile(p, pStack); - - case ID_IF: - return CBotIf::Compile(p, pStack); - - case ID_RETURN: - return CBotReturn::Compile(p, pStack); - - case ID_ELSE: - pStack->SetStartError(p->GivStart()); - pStack->SetError(TX_ELSEWITHOUTIF, p->GivEnd()); - return NULL; - - case ID_CASE: - pStack->SetStartError(p->GivStart()); - pStack->SetError(TX_OUTCASE, p->GivEnd()); - return NULL; - } - - pStack->SetStartError(p->GivStart()); - - // should not be a reserved word DefineNum - if (p->GivType() == TokenTypDef) - { - pStack->SetError(TX_RESERVED, p); - return NULL; - } - - // this might be an instance of class definnition - CBotToken* ppp = p; - if (IsOfType(ppp, TokenTypVar)) - { - if (CBotClass::Find(p) != NULL) - { - // yes, compiles the declaration of the instance - return CBotClassInst::Compile(p, pStack); - } - } - - // this can be an arythmetic instruction - CBotInstr* inst = CBotExpression::Compile(p, pStack); - if (IsOfType(p, ID_SEP)) - { - return inst; - } - pStack->SetError(TX_ENDOF, p->GivStart()); - delete inst; - return NULL; -} - -bool CBotInstr::Execute(CBotStack* &pj) -{ - CBotString ClassManquante = name; - ASM_TRAP(); // should never go through this routine - // but use the routines of the subclasses - return false; -} - -bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) -{ - if (!Execute(pj)) return false; - pVar->SetVal(pj->GivVar()); - return true; -} - -void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotString ClassManquante = name; - ASM_TRAP(); // should never go through this routine - // but use the routines of the subclasses -} - - -bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - ASM_TRAP(); // dad do not know, see the girls - return false; -} - -bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) -{ - ASM_TRAP(); // dad do not know, see the girls - return false; -} - -void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - ASM_TRAP(); // dad do not know, see the girls -} - -// this routine is defined only for the subclass CBotCase -// this allows to make the call on all instructions CompCase -// to see if it's a case to the desired value. - -bool CBotInstr::CompCase(CBotStack* &pj, int val) -{ - return false; -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a statement block " { i ; i ; } " - -// this class have no constructor because there is never an instance of this -// class (TODO what about default constructor?) -// the object returned by Compile is usually of type CBotListInstr - - -CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - pStack->SetStartError(p->GivStart()); - - if (IsOfType(p, ID_OPBLK)) - { - CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); - - if (IsOfType(p, ID_CLBLK)) - { - return inst; - } - - pStack->SetError(TX_CLOSEBLK, p->GivStart()); // missing parenthesis - delete inst; - return NULL; - } - - pStack->SetError(TX_OPENBLK, p->GivStart()); - return NULL; -} - -CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - // is this a new block - if (p->GivType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); - - // otherwise, look for a single statement instead - - // to handle the case with local definition instruction (*) - CBotCStack* pStk = pStack->TokenStack(p, bLocal); - - return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction - pStk); -} - -// (*) is the case in the following statement -// if (1 == 1) int x = 0; -// where the variable x is known only in the block following the if - - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a list of instructions separated by semicolons - -CBotListInstr::CBotListInstr() -{ - m_Instr = NULL; - name = "CBotListInstr"; -} - -CBotListInstr::~CBotListInstr() -{ - delete m_Instr; -} - -CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - CBotCStack* pStk = pStack->TokenStack(p, bLocal); // variables are local - - CBotListInstr* inst = new CBotListInstr(); - - while (true) - { - if (p == NULL) break; - - if (IsOfType(p, ID_SEP)) continue; // empty statement ignored - if (p->GivType() == ID_CLBLK) break; - - if (IsOfType(p, 0)) - { - pStack->SetError(TX_CLOSEBLK, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - - CBotInstr* i = CBotBlock::CompileBlkOrInst(p, pStk); // compiles next - - if (!pStk->IsOk()) - { - delete inst; - return pStack->Return(NULL, pStk); - } - - if (inst->m_Instr == NULL) inst->m_Instr = i; - else inst->m_Instr->AddNext(i); // added a result - } - return pStack->Return(inst, pStk); -} - -// executes a set of instructions - -bool CBotListInstr::Execute(CBotStack* &pj) -{ - - CBotStack* pile = pj->AddStack(this, true); //needed for SetState() - if (pile->StackOver() ) return pj->Return( pile); - - - CBotInstr* p = m_Instr; // the first expression - - int state = pile->GivState(); - while (state-->0) p = p->GivNext(); // returns to the interrupted operation - - if (p != NULL) while (true) - { - if (!p->Execute(pile)) return false; - p = p->GivNext(); - if (p == NULL) break; - if (!pile->IncState()) ;//return false; // ready for next - } - - return pj->Return(pile); -} - -void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == NULL) return; - - CBotInstr* p = m_Instr; // the first expression - - int state = pile->GivState(); - while ( p != NULL && state-- > 0) - { - p->RestoreState(pile, false); - p = p->GivNext(); // returns to the interrupted operation - } - - if (p != NULL) p->RestoreState(pile, true); -} - -////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// compilation of an element to the left of an assignment - -CBotLeftExprVar::CBotLeftExprVar() -{ - name = "CBotLeftExprVar"; - m_typevar = -1; - m_nIdent = 0; -} - -CBotLeftExprVar::~CBotLeftExprVar() -{ -} - -CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) -{ - // verifies that the token is a variable name - if (p->GivType() != TokenTypVar) - { - pStack->SetError( TX_NOVAR, p->GivStart()); - return NULL; - } - - CBotLeftExprVar* inst = new CBotLeftExprVar(); - inst->SetToken(p); - p = p->GivNext(); - - return inst; -} - -// creates a variable and assigns the result to the stack -bool CBotLeftExprVar::Execute(CBotStack* &pj) -{ - CBotVar* var1; - CBotVar* var2; - - var1 = CBotVar::Create(m_token.GivString(), m_typevar); - var1->SetUniqNum(m_nIdent); // with the unique identifier - pj->AddVar(var1); // place it on the stack - - var2 = pj->GivVar(); // result on the stack - if (var2) var1->SetVal(var2); // do the assignment - - return true; -} - -void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotVar* var1; - - var1 = pj->FindVar(m_token.GivString()); - if (var1 == NULL) ASM_TRAP(); - - var1->SetUniqNum(m_nIdent); // with the unique identifier -} - -////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// defining an array of any type -// int a[12]; -// point x[]; - -CBotInstArray::CBotInstArray() -{ - m_var = NULL; - m_listass = NULL; - name = "CBotInstArray"; -} - -CBotInstArray::~CBotInstArray() -{ - delete m_var; - delete m_listass; -} - - -CBotInstr* CBotInstArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) -{ - CBotCStack* pStk = pStack->TokenStack(p); - - CBotInstArray* inst = new CBotInstArray(); - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - - // determinse the expression is valid for the item on the left side - if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable? - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - CBotInstr* i; - while (IsOfType(p, ID_OPBRK)) - { - if (p->GivType() != ID_CLBRK) - i = CBotExpression::Compile(p, pStk); // expression for the value - else - i = new CBotEmpty(); // if no special formula - - inst->AddNext3b(i); // construct a list - type = CBotTypResult(CBotTypArrayPointer, type); - - if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK )) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - } - - CBotVar* var = CBotVar::Create(vartoken, type); // create an instance - inst->m_typevar = type; - - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); // place it on the stack - - if (IsOfType(p, ID_ASS)) // with an assignment - { - inst->m_listass = CBotListArray::Compile(p, pStk, type.GivTypElem()); - } - - if (pStk->IsOk()) return pStack->Return(inst, pStk); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - - -// executes the definition of an array - -bool CBotInstArray::Execute(CBotStack* &pj) -{ - CBotStack* pile1 = pj->AddStack(this); - - CBotStack* pile = pile1; - - if (pile1->GivState() == 0) - { - // seek the maximum dimension of the table - CBotInstr* p = GivNext3b(); // the different formulas - int nb = 0; - - while (p != NULL) - { - pile = pile->AddStack(); // little room to work - nb++; - if (pile->GivState() == 0) - { - if (!p->Execute(pile)) return false; // size calculation //interrupted? - pile->IncState(); - } - p = p->GivNext3b(); - } - - p = GivNext3b(); - pile = pile1; // returns to the stack - int n = 0; - int max[100]; - - while (p != NULL) - { - pile = pile->AddStack(); - CBotVar* v = pile->GivVar(); // result - max[n] = v->GivValInt(); // value - if (max[n]>MAXARRAYSIZE) - { - pile->SetError(TX_OUTARRAY, &m_token); - return pj->Return (pile); - } - n++; - p = p->GivNext3b(); - } - while (n<100) max[n++] = 0; - - m_typevar.SetArray(max); // store the limitations - - // cre simplement un pointeur null - CBotVar* var = CBotVar::Create(m_var->GivToken(), m_typevar); - var->SetPointer(NULL); - var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); - pj->AddVar(var); - -#if STACKMEM - pile1->AddStack()->Delete(); -#else - delete pile1->AddStack(); // need more indices -#endif - pile1->IncState(); - } - - if (pile1->GivState() == 1) - { - if (m_listass != NULL) // there is the assignment for this table - { - CBotVar* pVar = pj->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); - - if (!m_listass->Execute(pile1, pVar)) return false; - } - pile1->IncState(); - } - - if (pile1->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile1 )) return false; - - return pj->Return(pile1); -} - -void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile1 = pj; - - CBotVar* var = pj->FindVar(m_var->GivToken()->GivString()); - if (var != NULL) var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); - - if (bMain) - { - pile1 = pj->RestoreStack(this); - CBotStack* pile = pile1; - if (pile == NULL) return; - - if (pile1->GivState() == 0) - { - // seek the maximum dimension of the table - CBotInstr* p = GivNext3b(); - - while (p != NULL) - { - pile = pile->RestoreStack(); - if (pile == NULL) return; - if (pile->GivState() == 0) - { - p->RestoreState(pile, bMain); - return; - } - p = p->GivNext3b(); - } - } - if (pile1->GivState() == 1 && m_listass != NULL) - { - m_listass->RestoreState(pile1, bMain); - } - - } - - if (m_next2b ) m_next2b->RestoreState( pile1, bMain); -} - -// special case for empty indexes -bool CBotEmpty :: Execute(CBotStack* &pj) -{ - CBotVar* pVar = CBotVar::Create("", CBotTypInt); - pVar->SetValInt(-1); - pj->SetVar(pVar); - return true; -} - -void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) -{ -} - -////////////////////////////////////////////////////////////////////////////////////// -// defining a list table initialization -// int [ ] a [ ] = (( 1, 2, 3 ) , ( 3, 2, 1 )) ; - - -CBotListArray::CBotListArray() -{ - m_expr = NULL; - name = "CBotListArray"; -} - -CBotListArray::~CBotListArray() -{ - delete m_expr; -} - - -CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) -{ - CBotCStack* pStk = pStack->TokenStack(p); - - CBotToken* pp = p; - - if (IsOfType( p, ID_NULL )) - { - CBotInstr* inst = new CBotExprNull (); - inst->SetToken(pp); - return pStack->Return(inst, pStk); // ok with empty element - } - - CBotListArray* inst = new CBotListArray(); - - if (IsOfType( p, ID_OPENPAR )) - { - // each element takes the one after the other - if (type.Eq( CBotTypArrayPointer )) - { - type = type.GivTypElem(); - - pStk->SetStartError(p->GivStart()); - if (NULL == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) )) - { - goto error; - } - - while (IsOfType( p, ID_COMMA )) // other elements? - { - pStk->SetStartError(p->GivStart()); - - CBotInstr* i = CBotListArray::Compile(p, pStk, type); - if (NULL == i) - { - goto error; - } - - inst->m_expr->AddNext3(i); - } - } - else - { - pStk->SetStartError(p->GivStart()); - if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - CBotVar* pv = pStk->GivVar(); // result of the expression - - if (pv == NULL || !TypesCompatibles( type, pv->GivTypResult())) // compatible type? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - - while (IsOfType( p, ID_COMMA )) // other elements? - { - pStk->SetStartError(p->GivStart()); - - CBotInstr* i = CBotTwoOpExpr::Compile(p, pStk) ; - if (NULL == i) - { - goto error; - } - - CBotVar* pv = pStk->GivVar(); // result of the expression - - if (pv == NULL || !TypesCompatibles( type, pv->GivTypResult())) // compatible type? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - inst->m_expr->AddNext3(i); - } - } - - if (!IsOfType(p, ID_CLOSEPAR) ) - { - pStk->SetError(TX_CLOSEPAR, p->GivStart()); - goto error; - } - - return pStack->Return(inst, pStk); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - - -// executes the definition of an array - -bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) -{ - CBotStack* pile1 = pj->AddStack(); - CBotVar* pVar2; - - CBotInstr* p = m_expr; - - int n = 0; - - for (; p != NULL ; n++, p = p->GivNext3()) - { - if (pile1->GivState() > n) continue; - - pVar2 = pVar->GivItem(n, true); - - if (!p->Execute(pile1, pVar2)) return false; // evaluate expression - - pile1->IncState(); - } - - return pj->Return(pile1); -} - -void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) - { - CBotStack* pile = pj->RestoreStack(this); - if (pile == NULL) return; - - CBotInstr* p = m_expr; - - int state = pile->GivState(); - - while(state-- > 0) p = p->GivNext3() ; - - p->RestoreState(pile, bMain); // size calculation //interrupted! - } -} - -////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// definition of an integer variable -// int a, b = 12; - -CBotInt::CBotInt() -{ - m_next = NULL; // for multiple definitions - m_var = - m_expr = NULL; - name = "CBotInt"; -} - -CBotInt::~CBotInt() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) -{ - if (IsOfType(p, ID_OPBRK)) - { - if (!IsOfType(p, ID_CLBRK)) - { - pStack->SetError(TX_CLBRK, p->GivStart()); - return NULL; - } - - CBotInstr* inst = CompileArray(p, pStack, CBotTypResult(CBotTypArrayPointer, type), false); - if (inst != NULL || !pStack->IsOk()) return inst; - } - - // compiles an array declaration - if (first) return NULL ; - - CBotInstr* inst = CBotInstArray::Compile(p, pStack, type); - if (inst == NULL) return NULL; - - if (IsOfType(p, ID_COMMA)) // several definitions - { - if (NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false))) // compiles next one - { - return inst; - } - delete inst; - return NULL; - } - - if (IsOfType(p, ID_SEP)) // end of instruction - { - return inst; - } - - delete inst; - pStack->SetError(TX_ENDOF, p->GivStart()); - return NULL; -} - -CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? NULL : p; // no repetition of the token "int" - - if (!cont && !IsOfType(p, ID_INT)) return NULL; - - CBotInt* inst = (CBotInt*)CompileArray(p, pStack, CBotTypInt); - if (inst != NULL || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotInt(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - - // determines the expression is valid for the item on the left side - if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypInt; - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) - { - delete inst; // type is not CBotInt - p = vartoken; // returns the variable name - - // compiles an array declaration - - CBotInstr* inst2 = CBotInstArray::Compile(p, pStk, CBotTypInt); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - - if (IsOfType(p, ID_COMMA)) // several definition chained - { - if (NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile the next one - { - return pStack->Return(inst2, pStk); - } - } - inst = (CBotInt*)inst2; - goto suite; // no assignment, variable already created - } - - if (IsOfType(p, ID_ASS)) // with an assignment? - { - if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - if (pStk->GivType() >= CBotTypBoolean) // compatible type ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - } - - { - CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// create the variable (evaluated after the assignment) - var->SetInit(inst->m_expr != NULL); // if initialized with assignment - var->SetUniqNum( //set it with a unique number - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); // place it on the stack - } - - if (IsOfType(p, ID_COMMA)) // chained several definitions - { - if (NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile next one - { - return pStack->Return(inst, pStk); - } - } -suite: - if (noskip || IsOfType(p, ID_SEP)) // instruction is completed - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - -// execute the definition of the integer variable - -bool CBotInt::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // essential for SetState() - - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; // initial value // interrupted? - m_var->Execute(pile); // creates and assign the result - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; // other(s) definition(s) - - return pj->Return(pile); // forward below -} - -void CBotInt::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == NULL) return; - - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // initial value // interrupted? - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) m_next2b->RestoreState(pile, bMain); // other(s) definition(s) -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// defining a boolean variable -// int a, b = false; - -CBotBoolean::CBotBoolean() -{ - m_var = - m_expr = NULL; - name = "CBotBoolean"; -} - -CBotBoolean::~CBotBoolean() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? NULL : p; - - if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return NULL; - - CBotBoolean* inst = (CBotBoolean*)CompileArray(p, pStack, CBotTypBoolean); - if (inst != NULL || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotBoolean(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - CBotVar* var = NULL; - - if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypBoolean; - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) - { - delete inst; // type is not CBotInt - p = vartoken; // resutns to the variable name - - // compiles an array declaration - - inst = (CBotBoolean*)CBotInstArray::Compile(p, pStk, CBotTypBoolean); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - goto suite; // no assignment, variable already created - } - - if (IsOfType(p, ID_ASS)) - { - if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - if (!pStk->GivTypResult().Eq(CBotTypBoolean)) - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - } - - var = CBotVar::Create(vartoken, CBotTypBoolean);// create the variable (evaluated after the assignment) - var->SetInit(inst->m_expr != NULL); - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); -suite: - if (IsOfType(p, ID_COMMA)) - { - if (NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip))) - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - -// executes a boolean variable definition - -bool CBotBoolean::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this);//essential for SetState() - - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; - m_var->Execute(pile); - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; - - return pj->Return(pile); -} - -void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == NULL) return; - - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // initial value interrupted? - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) - m_next2b->RestoreState(pile, bMain); // other(s) definition(s) -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// definition of a real/float variable -// int a, b = 12.4; - -CBotFloat::CBotFloat() -{ - m_var = - m_expr = NULL; - name = "CBotFloat"; -} - -CBotFloat::~CBotFloat() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? NULL : p; - - if (!cont && !IsOfType(p, ID_FLOAT)) return NULL; - - CBotFloat* inst = (CBotFloat*)CompileArray(p, pStack, CBotTypFloat); - if (inst != NULL || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotFloat(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - CBotVar* var = NULL; - inst->SetToken(vartoken); - - if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypFloat; - if (pStk->CheckVarLocal(vartoken)) // redefinition of a variable - { - pStk->SetStartError(vartoken->GivStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) - { - delete inst; - p = vartoken; - inst = (CBotFloat*)CBotInstArray::Compile(p, pStk, CBotTypFloat); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - goto suite; // no assignment, variable already created - } - - if (IsOfType(p, ID_ASS)) - { - if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - if (pStk->GivType() >= CBotTypBoolean) - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } - } - - var = CBotVar::Create(vartoken, CBotTypFloat); - var->SetInit(inst->m_expr != NULL); - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); -suite: - if (IsOfType(p, ID_COMMA)) - { - if (NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip))) - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - -// executes the definition of a real variable - -bool CBotFloat::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; - m_var->Execute(pile); - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; - - return pj->Return(pile); -} - -void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == NULL) return; - - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) - m_next2b->RestoreState(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// define a string variable -// int a, b = "salut"; - -CBotIString::CBotIString() -{ - m_var = - m_expr = NULL; - name = "CBotIString"; -} - -CBotIString::~CBotIString() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? NULL : p; - - if (!cont && !IsOfType(p, ID_STRING)) return NULL; - - CBotIString* inst = (CBotIString*)CompileArray(p, pStack, CBotTypString); - if (inst != NULL || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotIString(); - - inst->m_expr = NULL; - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - - if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypString; - if (pStk->CheckVarLocal(vartoken)) - { - pStk->SetStartError(vartoken->GivStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); - goto error; - } - - if (IsOfType(p, ID_ASS)) - { - if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } -/* if (!pStk->GivTypResult().Eq(CBotTypString)) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - }*/ - } - - CBotVar* var = CBotVar::Create(vartoken, CBotTypString); - var->SetInit(inst->m_expr != NULL); - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); - - if (IsOfType(p, ID_COMMA)) - { - if (NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip))) - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - -// executes the definition of the string variable - -bool CBotIString::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if ( pile->GivState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; - m_var->Execute(pile); - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; - - return pj->Return(pile); -} - -void CBotIString::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == NULL) return; - - if ( pile->GivState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) - m_next2b->RestoreState(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a statement such as " x = 123 " ou " z * 5 + 4 " -// with or without assignment - -CBotExpression::CBotExpression() -{ - m_leftop = NULL; - m_rightop = NULL; - name = "CBotExpression"; -} - -CBotExpression::~CBotExpression() -{ - delete m_leftop; - delete m_rightop; -} - -CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - - CBotExpression* inst = new CBotExpression(); - - inst->m_leftop = CBotLeftExpr::Compile(p, pStack); - - inst->SetToken(p); - int OpType = p->GivType(); - - if ( pStack->IsOk() && - IsOfTypeList(p, ID_ASS, ID_ASSADD, ID_ASSSUB, ID_ASSMUL, ID_ASSDIV, ID_ASSMODULO, - ID_ASSAND, ID_ASSXOR, ID_ASSOR, - ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) - { - if (inst->m_leftop == NULL) - { - pStack->SetError(TX_BADLEFT, p->GivEnd()); - delete inst; - return NULL; - } - - inst->m_rightop = CBotExpression::Compile(p, pStack); - if (inst->m_rightop == NULL) - { - delete inst; - return NULL; - } - - CBotTypResult type1 = pStack->GivTypResult(); - - // get the variable assigned to mark - CBotVar* var = NULL; - inst->m_leftop->ExecuteVar(var, pStack); - if (var == NULL) - { - delete inst; - return NULL; - } - - if (OpType != ID_ASS && var->GivInit() != IS_DEF) - { - pStack->SetError(TX_NOTINIT, pp); - delete inst; - return NULL; - } - - CBotTypResult type2 = var->GivTypResult(); - - // what types are acceptable? - switch (OpType) - { - case ID_ASS: - // if (type2 == CBotTypClass) type2 = -1; - if ((type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer)) || - (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) - { -/* CBotClass* c1 = type1.GivClass(); - CBotClass* c2 = type2.GivClass(); - if (!c1->IsChildOf(c2)) type2.SetType(-1); -//- if (!type1.Eq(CBotTypClass)) var->SetPointer(pStack->GivVar()->GivPointer());*/ - var->SetInit(2); - } - else - var->SetInit(true); - - break; - case ID_ASSADD: - if (type2.Eq(CBotTypBoolean) || - type2.Eq(CBotTypPointer) ) type2 = -1; // numbers and strings - break; - case ID_ASSSUB: - case ID_ASSMUL: - case ID_ASSDIV: - case ID_ASSMODULO: - if (type2.GivType() >= CBotTypBoolean) type2 = -1; // numbers only - break; - } - - if (!TypeCompatible(type1, type2, OpType)) - { - pStack->SetError(TX_BADTYPE, &inst->m_token); - delete inst; - return NULL; - } - - return inst; // compatible type? - } - - delete inst; - int start, end, error = pStack->GivError(start, end); - - p = pp; // returns to the top - pStack->SetError(0,0); // forget the error - - CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // tries without assignment - if (i != NULL && error == TX_PRIVATE && p->GivType() == ID_ASS) - pStack->ResetError(error, start, end); - return i; -} - -// executes an expression with assignment - -bool CBotExpression::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - CBotToken* pToken = m_leftop->GivToken(); - CBotVar* pVar = NULL; - - CBotStack* pile1 = pile; - - bool IsInit = true; - CBotVar* result = NULL; - - // must be done before any indexes (stack can be changed) - if (!m_leftop->ExecuteVar(pVar, pile, NULL, false)) return false; // variable before accessing the value on the right - - if ( pile1->GivState()==0) - { - pile1->SetCopyVar(pVar); // keeps the copy on the stack (if interrupted) - pile1->IncState(); - } - - CBotStack* pile2 = pile->AddStack(); - - if ( pile2->GivState()==0) - { - if (m_rightop && !m_rightop->Execute(pile2)) return false; // initial value // interrupted? - pile2->IncState(); - } - - if (pile1->GivState() == 1) - { - if (m_token.GivType() != ID_ASS) - { - pVar = pile1->GivVar(); // recovers if interrupted - IsInit = pVar->GivInit(); - if (IsInit == IS_NAN) - { - pile2->SetError(TX_OPNAN, m_leftop->GivToken()); - return pj->Return(pile2); - } - result = CBotVar::Create("", pVar->GivTypResult(2)); - } - - switch (m_token.GivType()) - { - case ID_ASS: - break; - case ID_ASSADD: - result->Add(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSSUB: - result->Sub(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSMUL: - result->Mul(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSDIV: - if (IsInit && - result->Div(pile1->GivVar(), pile2->GivVar())) - pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); - break; - case ID_ASSMODULO: - if (IsInit && - result->Modulo(pile1->GivVar(), pile2->GivVar())) - pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); - break; - case ID_ASSAND: - result->And(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSXOR: - result->XOr(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSOR: - result->Or(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSSL: - result->SL(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSSR: - result->SR(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - case ID_ASSASR: - result->ASR(pile1->GivVar(), pile2->GivVar()); - pile2->SetVar(result); - break; - default: - ASM_TRAP(); - } - if (!IsInit) - pile2->SetError(TX_NOTINIT, m_leftop->GivToken()); - - pile1->IncState(); - } - - if (!m_leftop->Execute( pile2, pile1 )) - return false; - - return pj->Return(pile2); -} - - -void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) - { - CBotToken* pToken = m_leftop->GivToken(); - CBotVar* pVar = NULL; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == NULL) return; - - CBotStack* pile1 = pile; - - if ( pile1->GivState()==0) - { - m_leftop->RestoreStateVar(pile, true); - return; - } - - m_leftop->RestoreStateVar(pile, false); - - CBotStack* pile2 = pile->RestoreStack(); - if (pile2 == NULL) return; - - if ( pile2->GivState()==0) - { - if (m_rightop) m_rightop->RestoreState(pile2, bMain); - return; - } - } -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a statement such as "(condition)" -// the condition must be Boolean - -// this class has no constructor, because there is never an instance of this class -// the object returned by Compile is usually type CBotExpression - - -CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) -{ - pStack->SetStartError(p->GivStart()); - if (IsOfType(p, ID_OPENPAR)) - { - CBotInstr* inst = CBotBoolExpr::Compile(p, pStack); - if (NULL != inst) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - return inst; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); // missing parenthesis - } - delete inst; - } - - pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis - - return NULL; -} - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a statement such as "(condition)" -// the condition must be Boolean -// -// this class has no constructor, because there is never an instance of this -// class -// the object returned by Compile is usually type CBotExpression -// - -CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - pStack->SetStartError(p->GivStart()); - - CBotInstr* inst = CBotTwoOpExpr::Compile(p, pStack); - - if (NULL != inst) - { - if (pStack->GivTypResult().Eq(CBotTypBoolean)) - { - return inst; - } - pStack->SetError(TX_NOTBOOL, p->GivStart()); // is not a boolean - } - - delete inst; - return NULL; -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile either: -// instruction in parentheses (...) -// a unary expression (negative, not) -// variable name -// variables pre and post-incremented or decremented -// a given number DefineNum -// a constant -// procedure call -// new statement -// -// this class has no constructor, because there is never an instance of this class -// the object returned by Compile is the class corresponding to the instruction - - -CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GivStart()); - - // is it an expression in parentheses? - if (IsOfType(p, ID_OPENPAR)) - { - CBotInstr* inst = CBotExpression::Compile(p, pStk); - - if (NULL != inst) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - return pStack->Return(inst, pStk); - } - pStk->SetError(TX_CLOSEPAR, p->GivStart()); - } - delete inst; - return pStack->Return(NULL, pStk); - } - - // is this a unary operation? - CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); - if (inst != NULL || !pStk->IsOk()) - return pStack->Return(inst, pStk); - - // is it a variable name? - if (p->GivType() == TokenTypVar) - { - // this may be a method call without the "this." before - inst = CBotExprVar::CompileMethode(p, pStk); - if (inst != NULL) return pStack->Return(inst, pStk); - - - // is it a procedure call? - inst = CBotInstrCall::Compile(p, pStk); - if (inst != NULL || !pStk->IsOk()) - return pStack->Return(inst, pStk); - - - CBotToken* pvar = p; - // no, it an "ordinaty" variable - inst = CBotExprVar::Compile(p, pStk); - - CBotToken* pp = p; - // post incremented or decremented? - if (IsOfType(p, ID_INC, ID_DEC)) - { - if (pStk->GivType() >= CBotTypBoolean) - { - pStk->SetError(TX_BADTYPE, pp); - delete inst; - return pStack->Return(NULL, pStk); - } - - // recompile the variable for read-only - delete inst; - p = pvar; - inst = CBotExprVar::Compile(p, pStk, PR_READ); - p = p->GivNext(); - - CBotPostIncExpr* i = new CBotPostIncExpr(); - i->SetToken(pp); - i->m_Instr = inst; // associated statement - return pStack->Return(i, pStk); - } - return pStack->Return(inst, pStk); - } - - // pre increpemted or pre decremented? - CBotToken* pp = p; - if (IsOfType(p, ID_INC, ID_DEC)) - { - CBotPreIncExpr* i = new CBotPreIncExpr(); - i->SetToken(pp); - - if (p->GivType() == TokenTypVar) - { - if (NULL != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) - { - if (pStk->GivType() >= CBotTypBoolean) - { - pStk->SetError(TX_BADTYPE, pp); - delete inst; - return pStack->Return(NULL, pStk); - } - return pStack->Return(i, pStk); - } - delete i; - return pStack->Return(NULL, pStk); - } - } - - // is it a number or DefineNum? - if (p->GivType() == TokenTypNum || - p->GivType() == TokenTypDef ) - { - CBotInstr* inst = CBotExprNum::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is this a chaine? - if (p->GivType() == TokenTypString) - { - CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is a "true" or "false" - if (p->GivType() == ID_TRUE || - p->GivType() == ID_FALSE ) - { - CBotInstr* inst = CBotExprBool::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is an object to be created with new - if (p->GivType() == ID_NEW) - { - CBotInstr* inst = CBotNew::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is a null pointer - if (IsOfType(p, ID_NULL)) - { - CBotInstr* inst = new CBotExprNull (); - inst->SetToken(pp); - CBotVar* var = CBotVar::Create("", CBotTypNullPointer); - pStk->SetVar(var); - return pStack->Return(inst, pStk); - } - - // is a number nan - if (IsOfType(p, ID_NAN)) - { - CBotInstr* inst = new CBotExprNan (); - inst->SetToken(pp); - CBotVar* var = CBotVar::Create("", CBotTypInt); - var->SetInit(IS_NAN); - pStk->SetVar(var); - return pStack->Return(inst, pStk); - } - - - return pStack->Return(NULL, pStk); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// Management of pre-and post increment / decrement -// There is no routine Compiles, the object is created directly -// Compiles in CBotParExpr :: - - -CBotPostIncExpr::CBotPostIncExpr() -{ - m_Instr = NULL; - name = "CBotPostIncExpr"; -} - -CBotPostIncExpr::~CBotPostIncExpr() -{ - delete m_Instr; -} - -CBotPreIncExpr::CBotPreIncExpr() -{ - m_Instr = NULL; - name = "CBotPreIncExpr"; -} - -CBotPreIncExpr::~CBotPreIncExpr() -{ - delete m_Instr; -} - -bool CBotPostIncExpr::Execute(CBotStack* &pj) -{ - CBotStack* pile1 = pj->AddStack(this); - CBotStack* pile2 = pile1; - - CBotVar* var1 = NULL; - - // retrieves the variable fields and indexes according - if (!((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true)) return false; - - pile1->SetState(1); - pile1->SetCopyVar(var1); // place le rsultat (avant incrmentation); - - CBotStack* pile3 = pile2->AddStack(this); - if (pile3->IfStep()) return false; - - if (var1->GivInit() == IS_NAN) - { - pile1->SetError(TX_OPNAN, &m_token); - } - - if (var1->GivInit() != IS_DEF) - { - pile1->SetError(TX_NOTINIT, &m_token); - } - - if (GivTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); - - return pj->Return(pile1); // operation done, result on pile2 -} - -void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile1 = pj->RestoreStack(this); - if (pile1 == NULL) return; - - ((CBotExprVar*)m_Instr)->RestoreStateVar(pile1, bMain); - - if (pile1 != NULL) pile1->RestoreStack(this); -} - -bool CBotPreIncExpr::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var1; - - if (pile->GivState() == 0) - { - CBotStack* pile2 = pile; - // retrieves the variable fields and indexes according - // pile2 is modified on return - if (!((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true)) return false; - - if (var1->GivInit() == IS_NAN) - { - pile->SetError(TX_OPNAN, &m_token); - return pj->Return(pile); // operation performed - } - - if (var1->GivInit() != IS_DEF) - { - pile->SetError(TX_NOTINIT, &m_token); - return pj->Return(pile); // operation performed - } - - if (GivTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); // ((CBotVarInt*)var1)->m_val - - pile->IncState(); - } - - if (!m_Instr->Execute(pile)) return false; - return pj->Return(pile); // operation performed -} - - -void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == NULL) return; - - if (pile->GivState() == 0) - { - return; - } - - m_Instr->RestoreState(pile, bMain); -} - - -////////////////////////////////////////////////////////////////////////////////////// -// compile an unary expression -// + -// - -// not -// ! -// ~ - -CBotExprUnaire::CBotExprUnaire() -{ - m_Expr = NULL; - name = "CBotExprUnaire"; -} - -CBotExprUnaire::~CBotExprUnaire() -{ - delete m_Expr; -} - -CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) -{ - int op = p->GivType(); - CBotToken* pp = p; - if (!IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 )) return NULL; - - CBotCStack* pStk = pStack->TokenStack(pp); - - CBotExprUnaire* inst = new CBotExprUnaire(); - inst->SetToken(pp); - - if (NULL != (inst->m_Expr = CBotParExpr::Compile( p, pStk ))) - { - if (op == ID_ADD && pStk->GivType() < CBotTypBoolean) // only with the number - return pStack->Return(inst, pStk); - if (op == ID_SUB && pStk->GivType() < CBotTypBoolean) // only with the numer - return pStack->Return(inst, pStk); - if (op == ID_NOT && pStk->GivType() < CBotTypFloat) // only with an integer - return pStack->Return(inst, pStk); - if (op == ID_LOG_NOT && pStk->GivTypResult().Eq(CBotTypBoolean))// only with boolean - return pStack->Return(inst, pStk); - if (op == ID_TXT_NOT && pStk->GivTypResult().Eq(CBotTypBoolean))// only with boolean - return pStack->Return(inst, pStk); - - pStk->SetError(TX_BADTYPE, &inst->m_token); - } - delete inst; - return pStack->Return(NULL, pStk); -} - -// executes unary expression - -bool CBotExprUnaire::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->GivState() == 0) - { - if (!m_Expr->Execute(pile)) return false; // interrupted ? - pile->IncState(); - } - - CBotStack* pile2 = pile->AddStack(); - if (pile2->IfStep()) return false; - - CBotVar* var = pile->GivVar(); // get the result on the stack - - switch (GivTokenType()) - { - case ID_ADD: - break; - case ID_SUB: - var->Neg(); // change the sign - break; - case ID_NOT: - case ID_LOG_NOT: - case ID_TXT_NOT: - var->Not(); - break; - } - return pj->Return(pile); // forwards below -} - -void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL) return; - - if (pile->GivState() == 0) - { - m_Expr->RestoreState(pile, bMain); // interrupted here! - return; - } -} - -////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// index management for arrays -// array [ expression ] - - -CBotIndexExpr::CBotIndexExpr() -{ - m_expr = NULL; - name = "CBotIndexExpr"; -} - -CBotIndexExpr::~CBotIndexExpr() -{ - delete m_expr; -} - -// finds a field from the instance at compile time - -bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - if (pVar->GivType(1) != CBotTypArrayPointer) - ASM_TRAP(); - - pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // at compile time makes the element [0] - if (pVar == NULL) - { - pile->SetError(TX_OUTARRAY, m_token.GivEnd()); - return false; - } - if (m_next3 != NULL) return m_next3->ExecuteVar(pVar, pile); - return true; -} - -// attention, changes the pointer to the stack intentionally -// place the index calculated on the additional stack - - -bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) -{ - CBotStack* pj = pile; - - if (pVar->GivType(1) != CBotTypArrayPointer) - ASM_TRAP(); - - pile = pile->AddStack(); - - if (pile->GivState() == 0) - { - if (!m_expr->Execute(pile)) return false; - pile->IncState(); - } - // handles array - - CBotVar* p = pile->GivVar(); // result on the stack - - if (p == NULL || p->GivType() > CBotTypDouble) - { - pile->SetError(TX_BADINDEX, prevToken); - return pj->Return(pile); - } - - int n = p->GivValInt(); // position in the table - - pVar = ((CBotVarArray*)pVar)->GivItem(n, bExtend); - if (pVar == NULL) - { - pile->SetError(TX_OUTARRAY, prevToken); - return pj->Return(pile); - } - - pVar->Maj(pile->GivPUser(), true); - - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; - - // does not release the stack - // to avoid recalculation of the index twice where appropriate - return true; -} - -void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - pile = pile->RestoreStack(); - if (pile == NULL) return; - - if (bMain && pile->GivState() == 0) - { - m_expr->RestoreState(pile, true); - return; - } - - if (m_next3) - m_next3->RestoreStateVar(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// field management in an instance (dot operator) -// toto.x - - -CBotFieldExpr::CBotFieldExpr() -{ - name = "CBotFieldExpr"; - m_nIdent = 0; -} - -CBotFieldExpr::~CBotFieldExpr() -{ -} - -void CBotFieldExpr::SetUniqNum(int num) -{ - m_nIdent = num; -} - - -// find a field from the instance at compile - -bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - if (pVar->GivType(1) != CBotTypPointer) - ASM_TRAP(); - - pVar = pVar->GivItemRef(m_nIdent); - if (pVar == NULL) - { - pile->SetError(TX_NOITEM, &m_token); - return false; - } - - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile) ) return false; - - return true; -} - -bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) -{ - CBotStack* pj = pile; - pile = pile->AddStack(this); // changes in output stack - if (pile == EOX) return true; - - - if (pVar->GivType(1) != CBotTypPointer) - ASM_TRAP(); - - CBotVarClass* pItem = pVar->GivPointer(); - if (pItem == NULL) - { - pile->SetError(TX_NULLPT, prevToken); - return pj->Return(pile); - } - if (pItem->GivUserPtr() == OBJECTDELETED) - { - pile->SetError(TX_DELETEDPT, prevToken); - return pj->Return(pile); - } - - if (bStep && pile->IfStep()) return false; - - pVar = pVar->GivItemRef(m_nIdent); - if (pVar == NULL) - { - pile->SetError(TX_NOITEM, &m_token); - return pj->Return(pile); - } - - if (pVar->IsStatic()) - { - // for a static variable, takes it in the class itself - CBotClass* pClass = pItem->GivClass(); - pVar = pClass->GivItem(m_token.GivString()); - } - - // request the update of the element, if applicable - pVar->Maj(pile->GivPUser(), true); - - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; - - // does not release the stack - // to maintain the state SetState () corresponding to step - - return true; -} - -void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) -{ - pj = pj->RestoreStack(this); - if (pj == NULL) return; - - if (m_next3 != NULL) - m_next3->RestoreStateVar(pj, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// compile a left operand for an assignment -CBotLeftExpr::CBotLeftExpr() -{ - name = "CBotLeftExpr"; - m_nIdent = 0; -} - -CBotLeftExpr::~CBotLeftExpr() -{ -} - -// compiles an expression for a left-operand (left of an assignment) -// this can be -// toto -// toto[ 3 ] -// toto.x -// toto.pos.x -// toto[2].pos.x -// toto[1].pos[2].x -// toto[1][2][3] - -CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GivStart()); - - // is it a variable name? - if (p->GivType() == TokenTypVar) - { - CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object - - inst->SetToken(p); - - CBotVar* var; - - if (NULL != (var = pStk->FindVar(p))) // seek if known variable - { - inst->m_nIdent = var->GivUniqNum(); - if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) - { - if ( var->IsPrivate(PR_READ) && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, p); - goto err; - } - // this is an element of the current class - // adds the equivalent of this. before - CBotToken pthis("this"); - inst->SetToken(&pthis); - inst->m_nIdent = -2; // indent for this - - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(p); // keeps the name of the token - inst->AddNext3(i); // add after - - var = pStk->FindVar(pthis); - var = var->GivItem(p->GivString()); - i->SetUniqNum(var->GivUniqNum()); - } - p = p->GivNext(); // next token - - while (true) - { - if (var->GivType() == CBotTypArrayPointer) - { - if (IsOfType( p, ID_OPBRK )) - { - CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); - inst->AddNext3(i); // add to the chain - - var = ((CBotVarArray*)var)->GivItem(0,true); // gets the component [0] - - if (i->m_expr == NULL) - { - pStk->SetError(TX_BADINDEX, p->GivStart()); - goto err; - } - - if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto err; - } - continue; - } - } - - if (var->GivType(1) == CBotTypPointer) // for classes - { - if (IsOfType(p, ID_DOT)) - { - CBotToken* pp = p; - - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(pp); // keeps the name of the token - inst->AddNext3(i); // adds after - - if (p->GivType() == TokenTypVar) // must be a name - { - var = var->GivItem(p->GivString()); // get item correspondent - if (var != NULL) - { - if ( var->IsPrivate(PR_READ) && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, pp); - goto err; - } - - i->SetUniqNum(var->GivUniqNum()); - p = p->GivNext(); // skips the name - continue; - } - pStk->SetError(TX_NOITEM, p); - } - pStk->SetError(TX_DOT, p->GivStart()); - goto err; - } - } - break; - } - - - if (pStk->IsOk()) return (CBotLeftExpr*) pStack->Return(inst, pStk); - } - pStk->SetError(TX_UNDEFVAR, p); -err: - delete inst; - return (CBotLeftExpr*) pStack->Return(NULL, pStk); - } - - return (CBotLeftExpr*) pStack->Return(NULL, pStk); -} - -// runs, is a variable and assigns the result to the stack -bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) -{ - CBotStack* pile = pj->AddStack(); - - CBotVar* var1 = NULL; - CBotVar* var2 = NULL; - // fetch a variable (not copy) - if (!ExecuteVar(var1, array, NULL, false)) return false; - - if (pile->IfStep()) return false; - - if (var1) - { - var2 = pj->GivVar(); // result on the input stack - if (var2) - { - CBotTypResult t1 = var1->GivTypResult(); - CBotTypResult t2 = var2->GivTypResult(); - if (t2.Eq(CBotTypPointer)) - { - CBotClass* c1 = t1.GivClass(); - CBotClass* c2 = t2.GivClass(); - if ( !c2->IsChildOf(c1)) - { - CBotToken* pt = &m_token; - pile->SetError(TX_BADTYPE, pt); - return pj->Return(pile); // operation performed - } - } - var1->SetVal(var2); // do assignment - } - pile->SetCopyVar(var1); // replace the stack with the copy of the variable - // (for name) - } - - return pj->Return(pile); // operation performed -} - -// fetch a variable during compilation - -bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - pVar = pile->FindVar(m_token); - if (pVar == NULL) return false; - - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile) ) return false; - - return true; -} - -// fetch the variable at runtume - -bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) -{ - pile = pile->AddStack(this); - - pVar = pile->FindVar(m_nIdent); - if (pVar == NULL) - { -#ifdef _DEBUG - ASM_TRAP(); -#endif - pile->SetError(2, &m_token); - return false; - } - - if (bStep && m_next3 == NULL && pile->IfStep()) return false; - - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; - - return true; -} - -void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - pile = pile->RestoreStack(this); - if (pile == NULL) return; - - if (m_next3 != NULL) - m_next3->RestoreStateVar(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -// converts a string into integer -// may be of the form 0xabc123 - -long GivNumInt(const char* p) -{ - long num = 0; - while (*p >= '0' && *p <= '9') - { - num = num * 10 + *p - '0'; - p++; - } - if (*p == 'x' || *p == 'X') - { - while (*++p != 0) - { - if (*p >= '0' && *p <= '9') - { - num = num * 16 + *p - '0'; - continue; - } - if (*p >= 'A' && *p <= 'F') - { - num = num * 16 + *p - 'A' + 10; - continue; - } - if (*p >= 'a' && *p <= 'f') - { - num = num * 16 + *p - 'a' + 10; - continue; - } - break; - } - } - return num; -} - -// converts a string into a float number -extern float GivNumFloat(const char* p) -{ - double num = 0; - double div = 10; - bool bNeg = false; - - if (*p == '-') - { - bNeg = true; - p++; - } - while (*p >= '0' && *p <= '9') - { - num = num * 10. + (*p - '0'); - p++; - } - - if (*p == '.') - { - p++; - while (*p >= '0' && *p <= '9') - { - num = num + (*p - '0') / div; - div = div * 10; - p++; - } - } - - int exp = 0; - if (*p == 'e' || *p == 'E') - { - char neg = 0; - p++; - if (*p == '-' || *p == '+') neg = *p++; - - while (*p >= '0' && *p <= '9') - { - exp = exp * 10 + (*p - '0'); - p++; - } - if (neg == '-') exp = -exp; - } - - while (exp > 0) - { - num *= 10.0; - exp--; - } - - while (exp < 0) - { - num /= 10.0; - exp++; - } - - if (bNeg) num = -num; - return (float)num; -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a token representing a number -CBotExprNum::CBotExprNum() -{ - name = "CBotExprNum"; -} - -CBotExprNum::~CBotExprNum() -{ -} - -CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - CBotExprNum* inst = new CBotExprNum(); - - inst->SetToken(p); - CBotString s = p->GivString(); - - inst->m_numtype = CBotTypInt; - if (p->GivType() == TokenTypDef) - { - inst->m_valint = p->GivIdKey(); - } - else - { - if (s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) )) - { - inst->m_numtype = CBotTypFloat; - inst->m_valfloat = GivNumFloat(s); - } - else - { - inst->m_valint = GivNumInt(s); - } - } - - if (pStk->NextToken(p)) - { - CBotVar* var = CBotVar::Create((CBotToken*)NULL, inst->m_numtype); - pStk->SetVar(var); - - return pStack->Return(inst, pStk); - } - delete inst; - return pStack->Return(NULL, pStk); -} - -// execute, returns the corresponding number - -bool CBotExprNum::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var = CBotVar::Create((CBotToken*)NULL, m_numtype); - - CBotString nombre ; - if (m_token.GivType() == TokenTypDef) - { - nombre = m_token.GivString(); - } - - switch (m_numtype) - { - case CBotTypShort: - case CBotTypInt: - var->SetValInt(m_valint, nombre); - break; - case CBotTypFloat: - var->SetValFloat(m_valfloat); - break; - } - pile->SetVar(var); // place on the stack - - return pj->Return(pile); // it's ok -} - -void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a token representing a string - -CBotExprAlpha::CBotExprAlpha() -{ - name = "CBotExprAlpha"; -} - -CBotExprAlpha::~CBotExprAlpha() -{ -} - -CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - CBotExprAlpha* inst = new CBotExprAlpha(); - - inst->SetToken(p); - p = p->GivNext(); - - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); - pStk->SetVar(var); - - return pStack->Return(inst, pStk); -} - -// execute, returns the corresponding string - -bool CBotExprAlpha::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); - - CBotString chaine = m_token.GivString(); - chaine = chaine.Mid(1, chaine.GivLength()-2); // removes the quotes - - var->SetValString(chaine); // value of the number - - pile->SetVar(var); // put on the stack - - return pj->Return(pile); -} - -void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a token representing true or false - -CBotExprBool::CBotExprBool() -{ - name = "CBotExprBool"; -} - -CBotExprBool::~CBotExprBool() -{ -} - -CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - CBotExprBool* inst = NULL; - - if ( p->GivType() == ID_TRUE || - p->GivType() == ID_FALSE ) - { - inst = new CBotExprBool(); - inst->SetToken(p); // stores the operation false or true - p = p->GivNext(); - - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - pStk->SetVar(var); - } - - return pStack->Return(inst, pStk); -} - -// executes, returns true or false - -bool CBotExprBool::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - - if (GivTokenType() == ID_TRUE) var->SetValInt(1); - else var->SetValInt(0); - - pile->SetVar(var); // put on the stack - return pj->Return(pile); // forwards below -} - -void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -// management of the operand "null" - -CBotExprNull::CBotExprNull() -{ - name = "CBotExprNull"; -} - -CBotExprNull::~CBotExprNull() -{ -} - -// executes, returns an empty pointer - -bool CBotExprNull::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypNullPointer); - - var->SetInit(true); // null pointer valid - pile->SetVar(var); // place on the stack - return pj->Return(pile); // forwards below -} - -void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -// management of the operand "nan" - -CBotExprNan::CBotExprNan() -{ - name = "CBotExprNan"; -} - -CBotExprNan::~CBotExprNan() -{ -} - -// executes, returns null pointer - -bool CBotExprNan::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypInt); - - var->SetInit(IS_NAN); // nan - pile->SetVar(var); // put on the stack - return pj->Return(pile); // forward below -} - -void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - -////////////////////////////////////////////////////////////////////////////////////// -// compile a variable name -// check that it is known on the stack -// and it has been initialized - -CBotExprVar::CBotExprVar() -{ - name = "CBotExprVar"; - m_nIdent = 0; -} - -CBotExprVar::~CBotExprVar() -{ -} - -CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) -{ - CBotToken* pDebut = p; - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GivStart()); - - // is it a variable? - if (p->GivType() == TokenTypVar) - { - CBotInstr* inst = new CBotExprVar(); // create the object - - inst->SetToken(p); - - CBotVar* var; - - if (NULL != (var = pStk->FindVar(p))) // seek if known variable - { - int ident = var->GivUniqNum(); - ((CBotExprVar*)inst)->m_nIdent = ident; // identifies variable by its number - - if (ident > 0 && ident < 9000) - { - if ( var->IsPrivate(privat) && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, p); - goto err; - } - - // This is an element of the current class - // ads the equivalent of this. before - /// \TODO need to be fixed revised and fixed after adding unit - ///tests - CBotToken token("this"); - inst->SetToken(&token); - ((CBotExprVar*)inst)->m_nIdent = -2; // identificator for this - - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(p); // keeps the name of the token - i->SetUniqNum(ident); - inst->AddNext3(i); // added after - } - - p = p->GivNext(); // next token - - while (true) - { - if (var->GivType() == CBotTypArrayPointer) - { - if (IsOfType( p, ID_OPBRK )) // check if there is an aindex - { - CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); // compile the formula - inst->AddNext3(i); // add to the chain - - var = ((CBotVarArray*)var)->GivItem(0,true); // gets the component [0] - - if (i->m_expr == NULL) - { - pStk->SetError(TX_BADINDEX, p->GivStart()); - goto err; - } - if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto err; - } - continue; - } - } - if (var->GivType(1) == CBotTypPointer) // for classes - { - if (IsOfType(p, ID_DOT)) - { - CBotToken* pp = p; - - if (p->GivType() == TokenTypVar) // must be a name - { - if (p->GivNext()->GivType() == ID_OPENPAR) // a method call? - { - CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); - if (!pStk->IsOk()) goto err; - inst->AddNext3(i); // added after - return pStack->Return(inst, pStk); - } - else - { - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(pp); // keeps the name of the token - inst->AddNext3(i); // add after - var = var->GivItem(p->GivString()); // get item correspondent - if (var != NULL) - { - i->SetUniqNum(var->GivUniqNum()); - if ( var->IsPrivate() && - !pStk->GivBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, pp); - goto err; - } - } - } - - - if (var != NULL) - { - p = p->GivNext(); // skips the name - continue; - } - pStk->SetError(TX_NOITEM, p); - goto err; - } - pStk->SetError(TX_DOT, p->GivStart()); - goto err; - } - } - - break; - } - - pStk->SetCopyVar(var); // place the copy of the variable on the stack (for type) - if (pStk->IsOk()) return pStack->Return(inst, pStk); - } - pStk->SetError(TX_UNDEFVAR, p); -err: - delete inst; - return pStack->Return(NULL, pStk); - } - - return pStack->Return(NULL, pStk); -} - -CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(pp->GivStart()); - - // is it a variable ? - if (pp->GivType() == TokenTypVar) - { - CBotToken pthis("this"); - CBotVar* var = pStk->FindVar(pthis); - if (var == 0) return pStack->Return(NULL, pStk); - - CBotInstr* inst = new CBotExprVar(); - - // this is an element of the current class - // adds the equivalent of this. before - - inst->SetToken(&pthis); - ((CBotExprVar*)inst)->m_nIdent = -2; // ident for this - - CBotToken* pp = p; - - if (pp->GivType() == TokenTypVar) - { - if (pp->GivNext()->GivType() == ID_OPENPAR) // a method call? - { - CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); - if (pStk->IsOk()) - { - inst->AddNext3(i); // add after - p = pp; // previous instruction - return pStack->Return(inst, pStk); - } - pStk->SetError(0,0); // the error is not adressed here - } - } - delete inst; - } - return pStack->Return(NULL, pStk); -} - - -// execute, making the value of a variable - -bool CBotExprVar::Execute(CBotStack* &pj) -{ - CBotVar* pVar = NULL; - CBotStack* pile = pj->AddStack(this); - - CBotStack* pile1 = pile; - - if (pile1->GivState() == 0) - { - if (!ExecuteVar(pVar, pile, NULL, true)) return false; // Get the variable fields and indexes according - - if (pVar) pile1->SetCopyVar(pVar); // place a copy on the stack - else - { - return pj->Return(pile1); - } - pile1->IncState(); - } - - pVar = pile1->GivVar(); - - if (pVar == NULL) - { - return pj->Return(pile1); - } - - if (pVar->GivInit() == IS_UNDEF) - { - CBotToken* pt = &m_token; - while (pt->GivNext() != NULL) pt = pt->GivNext(); - pile1->SetError(TX_NOTINIT, pt); - return pj->Return(pile1); - } - return pj->Return(pile1); // operation completed -} - -void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == NULL) return; - - CBotStack* pile1 = pile; - - if (pile1->GivState() == 0) - { - RestoreStateVar(pile, bMain); // retrieves the variable fields and indexes according - return; - } -} - -// fetch a variable at runtime - -bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) -{ - CBotStack* pile = pj; - pj = pj->AddStack(this); - - if (bStep && m_nIdent>0 && pj->IfStep()) return false; - - pVar = pj->FindVar(m_nIdent, true); // tries with the variable update if necessary - if (pVar == NULL) - { -#ifdef _DEBUG - ASM_TRAP(); -#endif - pj->SetError(1, &m_token); - return false; - } - if ( m_next3 != NULL && - !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) - return false; // field of an instance, table, methode - - return pile->ReturnKeep(pj); // does not put on stack but get the result if a method was called -} - - -// fetch variable at runtime - -void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) -{ - pj = pj->RestoreStack(this); - if (pj == NULL) return; - - if (m_next3 != NULL) - m_next3->RestoreStateVar(pj, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - -// compile a list of parameters - -CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) -{ - bool first = true; - CBotInstr* ret = NULL; // to return to the list - - CBotCStack* pile = pStack; - int i = 0; - - if (IsOfType(p, ID_OPENPAR)) - { - int start, end; - if (!IsOfType(p, ID_CLOSEPAR)) while (true) - { - start = p->GivStart(); - pile = pile->TokenStack(); // keeps the result on the stack - - if (first) pStack->SetStartError(start); - first = false; - - CBotInstr* param = CBotExpression::Compile(p, pile); - end = p->GivStart(); - - if (!pile->IsOk()) - { - return pStack->Return(NULL, pile); - } - - if (ret == NULL) ret = param; - else ret->AddNext(param); // construct the list - - if (param != NULL) - { - if (pile->GivTypResult().Eq(99)) - { - delete pStack->TokenStack(); - pStack->SetError(TX_VOID, p->GivStart()); - return NULL; - } - ppVars[i] = pile->GivVar(); - ppVars[i]->GivToken()->SetPos(start, end); - i++; - - if (IsOfType(p, ID_COMMA)) continue; // skips the comma - if (IsOfType(p, ID_CLOSEPAR)) break; - } - - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - delete pStack->TokenStack(); - return NULL; - } - } - ppVars[i] = NULL; - return ret; -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a method call - -CBotInstrMethode::CBotInstrMethode() -{ - m_Parameters = NULL; - m_MethodeIdent = 0; - name = "CBotInstrMethode"; -} - -CBotInstrMethode::~CBotInstrMethode() -{ - delete m_Parameters; -} - -CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* var) -{ - CBotInstrMethode* inst = new CBotInstrMethode(); - inst->SetToken(p); // corresponding token - - if (NULL != var) - { - CBotToken* pp = p; - p = p->GivNext(); - - if (p->GivType() == ID_OPENPAR) - { - inst->m_NomMethod = pp->GivString(); - - // compiles the list of parameters - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStack, ppVars); - - if (pStack->IsOk()) - { - CBotClass* pClass = var->GivClass(); // pointer to the class - inst->m_ClassName = pClass->GivName(); // name of the class - CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, - pStack, inst->m_MethodeIdent); - delete pStack->TokenStack(); // release parameters on the stack - inst->m_typRes = r; - - if (inst->m_typRes.GivType() > 20) - { - pStack->SetError(inst->m_typRes.GivType(), pp); - delete inst; - return NULL; - } - // put the result on the stack to have something - if (inst->m_typRes.GivType() > 0) - { - CBotVar* pResult = CBotVar::Create("", inst->m_typRes); - if (inst->m_typRes.Eq(CBotTypClass)) - { - pResult->SetClass(inst->m_typRes.GivClass()); - } - pStack->SetVar(pResult); - } - return inst; - } - delete inst; - return NULL; - } - } - pStack->SetError(1234, p); - delete inst; - return NULL; -} - -// execute the method call - -bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) -{ - CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // a place for the copy of This - - if (pVar->GivPointer() == NULL) - { - pj->SetError(TX_NULLPT, prevToken); - } - - if (pile1->IfStep()) return false; - - CBotStack* pile2 = pile1->AddStack(); // for the next parameters - - if ( pile1->GivState() == 0) - { - CBotVar* pThis = CBotVar::Create(pVar); - pThis->Copy(pVar); - // this value should be taken before the evaluation parameters - // Test.Action (Test = Other); - // action must act on the value before test = Other! - - pThis->SetName("this"); - pThis->SetUniqNum(-2); - pile1->AddVar(pThis); - pile1->IncState(); - } - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != NULL) while ( true) - { - if (pile2->GivState() == 0) - { - if (!p->Execute(pile2)) return false; // interrupted here? - if (!pile2->SetState(1)) return false; // special mark to recognize parameters - } - ppVars[i++] = pile2->GivVar(); // construct the list of pointers - pile2 = pile2->AddStack(); // space on the stack for the result - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pThis = pile1->FindVar(-2); - CBotVar* pResult = NULL; - if (m_typRes.GivType() > 0) pResult = CBotVar::Create("", m_typRes); - if (m_typRes.Eq(CBotTypClass)) - { - pResult->SetClass(m_typRes.GivClass()); - } - CBotVar* pRes = pResult; - - if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, - pResult, pile2, GivToken())) return false; - if (pRes != pResult) delete pRes; - - pVar = NULL; // does not return value for this - return pj->Return(pile2); // release the entire stack -} - -void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - if (!bMain) return; - - CBotVar* ppVars[1000]; - CBotStack* pile1 = pile->RestoreStack(this); // place for the copy of This - if (pile1 == NULL) return; - - CBotStack* pile2 = pile1->RestoreStack(); // and for the parameters coming - if (pile2 == NULL) return; - - CBotVar* pThis = pile1->FindVar("this"); - pThis->SetUniqNum(-2); - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != NULL) while ( true) - { - if (pile2->GivState() == 0) - { - p->RestoreState(pile2, true); // interrupted here! - return; - } - ppVars[i++] = pile2->GivVar(); // construct the list of pointers - pile2 = pile2->RestoreStack(); - if (pile2 == NULL) return; - - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pResult = NULL; - - CBotVar* pRes = pResult; - - pClass->RestoreMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, pile2); -} - - -bool CBotInstrMethode::Execute(CBotStack* &pj) -{ - CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // place for the copy of This - - if (pile1->IfStep()) return false; - - CBotStack* pile2 = pile1->AddStack(); // and for the parameters coming - - if ( pile1->GivState() == 0) - { - CBotVar* pThis = pile1->CopyVar(m_token); - // this value should be taken before the evaluation parameters - // Test.Action (Test = Other); - // Action must act on the value before test = Other! - pThis->SetName("this"); - pile1->AddVar(pThis); - pile1->IncState(); - } - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - if (p != NULL) while ( true) - { - if (pile2->GivState() == 0) - { - if (!p->Execute(pile2)) return false; // interrupted here? - if (!pile2->SetState(1)) return false; // special mark to recognize parameters - } - ppVars[i++] = pile2->GivVar(); // construct the list of pointers - pile2 = pile2->AddStack(); // space on the stack for the results - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pThis = pile1->FindVar("this"); - CBotVar* pResult = NULL; - if (m_typRes.GivType()>0) pResult = CBotVar::Create("", m_typRes); - if (m_typRes.Eq(CBotTypClass)) - { - pResult->SetClass(m_typRes.GivClass()); - } - CBotVar* pRes = pResult; - - if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interupted - - // set the new value of this in place of the old variable - CBotVar* old = pile1->FindVar(m_token); - old->Copy(pThis, false); - - if (pRes != pResult) delete pRes; - - return pj->Return(pile2); // release the entire stack -} - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile an instruction "new" - -CBotNew::CBotNew() -{ - name = "CBotNew"; - m_Parameters = NULL; - m_nMethodeIdent = 0; -} - -CBotNew::~CBotNew() -{ -} - -CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - if (!IsOfType(p, ID_NEW)) return NULL; - - // verifies that the token is a class name - if (p->GivType() != TokenTypVar) return NULL; - - CBotClass* pClass = CBotClass::Find(p); - if (pClass == NULL) - { - pStack->SetError(TX_BADNEW, p); - return NULL; - } - - CBotNew* inst = new CBotNew(); - inst->SetToken(pp); - - inst->m_vartoken = p; - p = p->GivNext(); - - // creates the object on the "job" - // with a pointer to the object - CBotVar* pVar = CBotVar::Create("", pClass); - - // do the call of the creator - CBotCStack* pStk = pStack->TokenStack(); - { - // check if there are parameters - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStk, ppVars); - if (!pStk->IsOk()) goto error; - - // constructor exist? - CBotTypResult r = pClass->CompileMethode(pClass->GivName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // release extra stack - int typ = r.GivType(); - - // if there is no constructor, and no parameters either, it's ok - if (typ == TX_UNDEFCALL && inst->m_Parameters == NULL) typ = 0; - pVar->SetInit(true); // mark the instance as init - - if (typ>20) - { - pStk->SetError(typ, inst->m_vartoken.GivEnd()); - goto error; - } - - // if the constructor does not exist, but there are parameters - if (typ<0 && inst->m_Parameters != NULL) - { - pStk->SetError(TX_NOCONST, &inst->m_vartoken); - goto error; - } - - // makes pointer to the object on the stack - pStk->SetVar(pVar); - return pStack->Return(inst, pStk); - } -error: - delete inst; - return pStack->Return(NULL, pStk); -} - -// executes instruction "new" - -bool CBotNew::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); //main stack - - if (pile->IfStep()) return false; - - CBotStack* pile1 = pj->AddStack2(); //secondary stack - - CBotVar* pThis = NULL; - - CBotToken* pt = &m_vartoken; - CBotClass* pClass = CBotClass::Find(pt); - - // create the variable "this" pointer type to the stack - - if ( pile->GivState()==0) - { - // create an instance of the requested class - // and initialize the pointer to that object - - - pThis = CBotVar::Create("this", pClass); - pThis->SetUniqNum(-2) ; - - pile1->SetVar(pThis); // place on stack1 - pile->IncState(); - } - - // fetch the this pointer if it was interrupted - if ( pThis == NULL) - { - pThis = pile1->GivVar(); // find the pointer - } - - // is there an assignment or parameters (constructor) - if ( pile->GivState()==1) - { - // evaluates the constructor of the instance - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != NULL) while ( true) - { - pile2 = pile2->AddStack(); // space on the stack for the result - if (pile2->GivState() == 0) - { - if (!p->Execute(pile2)) return false; // interrupted here? - pile2->SetState(1); - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - // create a variable for the result - CBotVar* pResult = NULL; // constructos still void - - if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrupt - - pThis->ConstructorSet(); // indicates that the constructor has been called - } - - return pj->Return(pile1); // passes below -} - -void CBotNew::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); //primary stack - if (pile == NULL) return; - - CBotStack* pile1 = pj->AddStack2(); //secondary stack - - CBotToken* pt = &m_vartoken; - CBotClass* pClass = CBotClass::Find(pt); - - // create the variable "this" pointer type to the object - - if ( pile->GivState()==0) - { - return; - } - - CBotVar* pThis = pile1->GivVar(); // find the pointer - pThis->SetUniqNum(-2); - - // is ther an assignment or parameters (constructor) - if ( pile->GivState()==1) - { - // evaluates the constructor of the instance - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != NULL) while ( true) - { - pile2 = pile2->RestoreStack(); // space on the stack for the result - if (pile2 == NULL) return; - - if (pile2->GivState() == 0) - { - p->RestoreState(pile2, bMain); // interrupt here! - return; - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GivString(), pThis, - ppVars, pile2) ; // interrupt here! - } -} - -///////////////////////////////////////////////////////////// -// check if two results are consistent to make an operation - -bool TypeCompatible(CBotTypResult& type1, CBotTypResult& type2, int op) -{ - int t1 = type1.GivType(); - int t2 = type2.GivType(); - - int max = (t1 > t2) ? t1 : t2; - - if (max == 99) return false; // result is void? - - // special case for strin concatenation - if (op == ID_ADD && max >= CBotTypString) return true; - if (op == ID_ASSADD && max >= CBotTypString) return true; - if (op == ID_ASS && t1 == CBotTypString) return true; - - if (max >= CBotTypBoolean) - { - if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; - if (t2 != t1) return false; - if (t1 == CBotTypArrayPointer) return type1.Compare(type2); - if (t1 == CBotTypPointer || - t1 == CBotTypClass || - t1 == CBotTypIntrinsic ) - { - CBotClass* c1 = type1.GivClass(); - CBotClass* c2 = type2.GivClass(); - - return c1->IsChildOf(c2) || c2->IsChildOf(c1); - // accept the case in reverse - // the transaction will be denied at runtime if the pointer is not - // compatible - } - - return true; - } - - type1.SetType(max); - type2.SetType(max); - return true; -} - -// check if two variables are compatible for parameter passing - -bool TypesCompatibles(const CBotTypResult& type1, const CBotTypResult& type2) -{ - int t1 = type1.GivType(); - int t2 = type2.GivType(); - - if (t1 == CBotTypIntrinsic) t1 = CBotTypClass; - if (t2 == CBotTypIntrinsic) t2 = CBotTypClass; - - int max = (t1 > t2) ? t1 : t2; - - if (max == 99) return false; // result is void? - - if (max >= CBotTypBoolean) - { - if (t2 != t1) return false; - - if (max == CBotTypArrayPointer) - return TypesCompatibles(type1.GivTypElem(), type2.GivTypElem()); - - if (max == CBotTypClass || max == CBotTypPointer) - return type1.GivClass() == type2.GivClass() ; - - return true ; - } - return true; -} - - -///////////////////////////////////////////////////////////////////////////////////// -// file management - -// necessary because it is not possible to do the fopen in the main program -// fwrite and fread in a dll or using the FILE * returned. - - -FILE* fOpen(const char* name, const char* mode) -{ - return fopen(name, mode); -} - -int fClose(FILE* filehandle) -{ - return fclose(filehandle); -} - -size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle) -{ - return fwrite(buffer, elemsize, length, filehandle); -} - -size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle) -{ - return fread(buffer, elemsize, length, filehandle); -} - -size_t fWrite(const void *buffer, size_t length, FILE* filehandle) -{ - return fwrite(buffer, 1, length, filehandle); -} - -size_t fRead(void *buffer, size_t length, FILE* filehandle) -{ - return fread(buffer, 1, length, filehandle); -} - - -//////////////////////////////////////// - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. +/////////////////////////////////////////////////////////////////////// + +// compilation of various instructions +// compile all routines as static +// and return an object according to what was found as instruction + +// compiler principle: +// compile the routines return an object of the class corresponding to the operation found +// this is always a subclass of CBotInstr. +// (CBotInstr objects are never used directly) + + +// compiles if the routine returns NULL is that the statement is false +// or misunderstood. +// the error is then on the stack CBotCStack :: Isok () is false + + + +#include "CBot.h" + +CBotInstr::CBotInstr() +{ + name = "CBotInstr"; + m_next = NULL; + m_next2b = NULL; + m_next3 = NULL; + m_next3b = NULL; +} + +CBotInstr::~CBotInstr() +{ + delete m_next; + delete m_next2b; + delete m_next3; + delete m_next3b; +} + +// counter of nested loops, +// to determine the break and continue valid +// list of labels used + + +int CBotInstr::m_LoopLvl = 0; +CBotStringArray CBotInstr::m_labelLvl = CBotStringArray(); + +// adds a level with a label +void CBotInstr::IncLvl(CBotString& label) +{ + m_labelLvl.SetSize(m_LoopLvl+1); + m_labelLvl[m_LoopLvl] = label; + m_LoopLvl++; +} + +// adds a level (switch statement) +void CBotInstr::IncLvl() +{ + m_labelLvl.SetSize(m_LoopLvl+1); + m_labelLvl[m_LoopLvl] = "#SWITCH"; + m_LoopLvl++; +} + +// free a level +void CBotInstr::DecLvl() +{ + m_LoopLvl--; + m_labelLvl[m_LoopLvl].Empty(); +} + +// control validity of break and continue +bool CBotInstr::ChkLvl(const CBotString& label, int type) +{ + int i = m_LoopLvl; + while (--i>=0) + { + if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; + if (label.IsEmpty()) return true; + if (m_labelLvl[i] == label) return true; + } + return false; +} + +bool CBotInstr::IsOfClass(CBotString n) +{ + return name == n; +} + + +//////////////////////////////////////////////////////////////////////////// +// database management class CBotInstr + +// set the token corresponding to the instruction + +void CBotInstr::SetToken(CBotToken* p) +{ + m_token = *p; +} + +// return the type of the token assicated with the instruction + +int CBotInstr::GivTokenType() +{ + return m_token.GivType(); +} + +// return associated token + +CBotToken* CBotInstr::GivToken() +{ + return &m_token; +} + +// adds the statement following the other + +void CBotInstr::AddNext(CBotInstr* n) +{ + CBotInstr* p = this; + while (p->m_next != NULL) p = p->m_next; + p->m_next = n; +} + +void CBotInstr::AddNext3(CBotInstr* n) +{ + CBotInstr* p = this; + while (p->m_next3 != NULL) p = p->m_next3; + p->m_next3 = n; +} + +void CBotInstr::AddNext3b(CBotInstr* n) +{ + CBotInstr* p = this; + while (p->m_next3b != NULL) p = p->m_next3b; + p->m_next3b = n; +} + +// returns next statement + +CBotInstr* CBotInstr::GivNext() +{ + return m_next; +} + +CBotInstr* CBotInstr::GivNext3() +{ + return m_next3; +} + +CBotInstr* CBotInstr::GivNext3b() +{ + return m_next3b; +} + +/////////////////////////////////////////////////////////////////////////// +// compile an instruction which can be +// while, do, try, throw, if, for, switch, break, continue, return +// int, float, boolean, string, +// declaration of an instance of a class +// arbitrary expression + + +CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + + if (p == NULL) return NULL; + + int type = p->GivType(); // what is the next token + + // is it a lable? + if (IsOfType(pp, TokenTypVar) && + IsOfType(pp, ID_DOTS)) + { + type = pp->GivType(); + // these instructions accept only lable + if (!IsOfTypeList(pp, ID_WHILE, ID_FOR, ID_DO, ID_REPEAT, 0)) + { + pStack->SetError(TX_LABEL, pp->GivStart()); + return NULL; + } + } + + // call routine corresponding to the compilation token found + switch (type) + { + case ID_WHILE: + return CBotWhile::Compile(p, pStack); + + case ID_FOR: + return CBotFor::Compile(p, pStack); + + case ID_DO: + return CBotDo::Compile(p, pStack); + + case ID_REPEAT: + return CBotRepeat::Compile(p, pStack); + + case ID_BREAK: + case ID_CONTINUE: + return CBotBreak::Compile(p, pStack); + + case ID_SWITCH: + return CBotSwitch::Compile(p, pStack); + + case ID_TRY: + return CBotTry::Compile(p, pStack); + + case ID_THROW: + return CBotThrow::Compile(p, pStack); + + case ID_DEBUGDD: + return CBotStartDebugDD::Compile(p, pStack); + + case ID_INT: + return CBotInt::Compile(p, pStack); + + case ID_FLOAT: + return CBotFloat::Compile(p, pStack); + + case ID_STRING: + return CBotIString::Compile(p, pStack); + + case ID_BOOLEAN: + case ID_BOOL: + return CBotBoolean::Compile(p, pStack); + + case ID_IF: + return CBotIf::Compile(p, pStack); + + case ID_RETURN: + return CBotReturn::Compile(p, pStack); + + case ID_ELSE: + pStack->SetStartError(p->GivStart()); + pStack->SetError(TX_ELSEWITHOUTIF, p->GivEnd()); + return NULL; + + case ID_CASE: + pStack->SetStartError(p->GivStart()); + pStack->SetError(TX_OUTCASE, p->GivEnd()); + return NULL; + } + + pStack->SetStartError(p->GivStart()); + + // should not be a reserved word DefineNum + if (p->GivType() == TokenTypDef) + { + pStack->SetError(TX_RESERVED, p); + return NULL; + } + + // this might be an instance of class definnition + CBotToken* ppp = p; + if (IsOfType(ppp, TokenTypVar)) + { + if (CBotClass::Find(p) != NULL) + { + // yes, compiles the declaration of the instance + return CBotClassInst::Compile(p, pStack); + } + } + + // this can be an arythmetic instruction + CBotInstr* inst = CBotExpression::Compile(p, pStack); + if (IsOfType(p, ID_SEP)) + { + return inst; + } + pStack->SetError(TX_ENDOF, p->GivStart()); + delete inst; + return NULL; +} + +bool CBotInstr::Execute(CBotStack* &pj) +{ + CBotString ClassManquante = name; + ASM_TRAP(); // should never go through this routine + // but use the routines of the subclasses + return false; +} + +bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) +{ + if (!Execute(pj)) return false; + pVar->SetVal(pj->GivVar()); + return true; +} + +void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotString ClassManquante = name; + ASM_TRAP(); // should never go through this routine + // but use the routines of the subclasses +} + + +bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + ASM_TRAP(); // dad do not know, see the girls + return false; +} + +bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) +{ + ASM_TRAP(); // dad do not know, see the girls + return false; +} + +void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + ASM_TRAP(); // dad do not know, see the girls +} + +// this routine is defined only for the subclass CBotCase +// this allows to make the call on all instructions CompCase +// to see if it's a case to the desired value. + +bool CBotInstr::CompCase(CBotStack* &pj, int val) +{ + return false; +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compiles a statement block " { i ; i ; } " + +// this class have no constructor because there is never an instance of this +// class (TODO what about default constructor?) +// the object returned by Compile is usually of type CBotListInstr + + +CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + pStack->SetStartError(p->GivStart()); + + if (IsOfType(p, ID_OPBLK)) + { + CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); + + if (IsOfType(p, ID_CLBLK)) + { + return inst; + } + + pStack->SetError(TX_CLOSEBLK, p->GivStart()); // missing parenthesis + delete inst; + return NULL; + } + + pStack->SetError(TX_OPENBLK, p->GivStart()); + return NULL; +} + +CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + // is this a new block + if (p->GivType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); + + // otherwise, look for a single statement instead + + // to handle the case with local definition instruction (*) + CBotCStack* pStk = pStack->TokenStack(p, bLocal); + + return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction + pStk); +} + +// (*) is the case in the following statement +// if (1 == 1) int x = 0; +// where the variable x is known only in the block following the if + + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compiles a list of instructions separated by semicolons + +CBotListInstr::CBotListInstr() +{ + m_Instr = NULL; + name = "CBotListInstr"; +} + +CBotListInstr::~CBotListInstr() +{ + delete m_Instr; +} + +CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + CBotCStack* pStk = pStack->TokenStack(p, bLocal); // variables are local + + CBotListInstr* inst = new CBotListInstr(); + + while (true) + { + if (p == NULL) break; + + if (IsOfType(p, ID_SEP)) continue; // empty statement ignored + if (p->GivType() == ID_CLBLK) break; + + if (IsOfType(p, 0)) + { + pStack->SetError(TX_CLOSEBLK, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + + CBotInstr* i = CBotBlock::CompileBlkOrInst(p, pStk); // compiles next + + if (!pStk->IsOk()) + { + delete inst; + return pStack->Return(NULL, pStk); + } + + if (inst->m_Instr == NULL) inst->m_Instr = i; + else inst->m_Instr->AddNext(i); // added a result + } + return pStack->Return(inst, pStk); +} + +// executes a set of instructions + +bool CBotListInstr::Execute(CBotStack* &pj) +{ + + CBotStack* pile = pj->AddStack(this, true); //needed for SetState() + if (pile->StackOver() ) return pj->Return( pile); + + + CBotInstr* p = m_Instr; // the first expression + + int state = pile->GivState(); + while (state-->0) p = p->GivNext(); // returns to the interrupted operation + + if (p != NULL) while (true) + { + if (!p->Execute(pile)) return false; + p = p->GivNext(); + if (p == NULL) break; + if (!pile->IncState()) ;//return false; // ready for next + } + + return pj->Return(pile); +} + +void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == NULL) return; + + CBotInstr* p = m_Instr; // the first expression + + int state = pile->GivState(); + while ( p != NULL && state-- > 0) + { + p->RestoreState(pile, false); + p = p->GivNext(); // returns to the interrupted operation + } + + if (p != NULL) p->RestoreState(pile, true); +} + +////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// compilation of an element to the left of an assignment + +CBotLeftExprVar::CBotLeftExprVar() +{ + name = "CBotLeftExprVar"; + m_typevar = -1; + m_nIdent = 0; +} + +CBotLeftExprVar::~CBotLeftExprVar() +{ +} + +CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) +{ + // verifies that the token is a variable name + if (p->GivType() != TokenTypVar) + { + pStack->SetError( TX_NOVAR, p->GivStart()); + return NULL; + } + + CBotLeftExprVar* inst = new CBotLeftExprVar(); + inst->SetToken(p); + p = p->GivNext(); + + return inst; +} + +// creates a variable and assigns the result to the stack +bool CBotLeftExprVar::Execute(CBotStack* &pj) +{ + CBotVar* var1; + CBotVar* var2; + + var1 = CBotVar::Create(m_token.GivString(), m_typevar); + var1->SetUniqNum(m_nIdent); // with the unique identifier + pj->AddVar(var1); // place it on the stack + + var2 = pj->GivVar(); // result on the stack + if (var2) var1->SetVal(var2); // do the assignment + + return true; +} + +void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotVar* var1; + + var1 = pj->FindVar(m_token.GivString()); + if (var1 == NULL) ASM_TRAP(); + + var1->SetUniqNum(m_nIdent); // with the unique identifier +} + +////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// defining an array of any type +// int a[12]; +// point x[]; + +CBotInstArray::CBotInstArray() +{ + m_var = NULL; + m_listass = NULL; + name = "CBotInstArray"; +} + +CBotInstArray::~CBotInstArray() +{ + delete m_var; + delete m_listass; +} + + +CBotInstr* CBotInstArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) +{ + CBotCStack* pStk = pStack->TokenStack(p); + + CBotInstArray* inst = new CBotInstArray(); + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + + // determinse the expression is valid for the item on the left side + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable? + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + CBotInstr* i; + while (IsOfType(p, ID_OPBRK)) + { + if (p->GivType() != ID_CLBRK) + i = CBotExpression::Compile(p, pStk); // expression for the value + else + i = new CBotEmpty(); // if no special formula + + inst->AddNext3b(i); // construct a list + type = CBotTypResult(CBotTypArrayPointer, type); + + if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK )) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + } + + CBotVar* var = CBotVar::Create(vartoken, type); // create an instance + inst->m_typevar = type; + + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); // place it on the stack + + if (IsOfType(p, ID_ASS)) // with an assignment + { + inst->m_listass = CBotListArray::Compile(p, pStk, type.GivTypElem()); + } + + if (pStk->IsOk()) return pStack->Return(inst, pStk); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + + +// executes the definition of an array + +bool CBotInstArray::Execute(CBotStack* &pj) +{ + CBotStack* pile1 = pj->AddStack(this); + + CBotStack* pile = pile1; + + if (pile1->GivState() == 0) + { + // seek the maximum dimension of the table + CBotInstr* p = GivNext3b(); // the different formulas + int nb = 0; + + while (p != NULL) + { + pile = pile->AddStack(); // little room to work + nb++; + if (pile->GivState() == 0) + { + if (!p->Execute(pile)) return false; // size calculation //interrupted? + pile->IncState(); + } + p = p->GivNext3b(); + } + + p = GivNext3b(); + pile = pile1; // returns to the stack + int n = 0; + int max[100]; + + while (p != NULL) + { + pile = pile->AddStack(); + CBotVar* v = pile->GivVar(); // result + max[n] = v->GivValInt(); // value + if (max[n]>MAXARRAYSIZE) + { + pile->SetError(TX_OUTARRAY, &m_token); + return pj->Return (pile); + } + n++; + p = p->GivNext3b(); + } + while (n<100) max[n++] = 0; + + m_typevar.SetArray(max); // store the limitations + + // create simply a NULL pointer + CBotVar* var = CBotVar::Create(m_var->GivToken(), m_typevar); + var->SetPointer(NULL); + var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); + pj->AddVar(var); + +#if STACKMEM + pile1->AddStack()->Delete(); +#else + delete pile1->AddStack(); // need more indices +#endif + pile1->IncState(); + } + + if (pile1->GivState() == 1) + { + if (m_listass != NULL) // there is the assignment for this table + { + CBotVar* pVar = pj->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); + + if (!m_listass->Execute(pile1, pVar)) return false; + } + pile1->IncState(); + } + + if (pile1->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile1 )) return false; + + return pj->Return(pile1); +} + +void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile1 = pj; + + CBotVar* var = pj->FindVar(m_var->GivToken()->GivString()); + if (var != NULL) var->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); + + if (bMain) + { + pile1 = pj->RestoreStack(this); + CBotStack* pile = pile1; + if (pile == NULL) return; + + if (pile1->GivState() == 0) + { + // seek the maximum dimension of the table + CBotInstr* p = GivNext3b(); + + while (p != NULL) + { + pile = pile->RestoreStack(); + if (pile == NULL) return; + if (pile->GivState() == 0) + { + p->RestoreState(pile, bMain); + return; + } + p = p->GivNext3b(); + } + } + if (pile1->GivState() == 1 && m_listass != NULL) + { + m_listass->RestoreState(pile1, bMain); + } + + } + + if (m_next2b ) m_next2b->RestoreState( pile1, bMain); +} + +// special case for empty indexes +bool CBotEmpty :: Execute(CBotStack* &pj) +{ + CBotVar* pVar = CBotVar::Create("", CBotTypInt); + pVar->SetValInt(-1); + pj->SetVar(pVar); + return true; +} + +void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) +{ +} + +////////////////////////////////////////////////////////////////////////////////////// +// defining a list table initialization +// int [ ] a [ ] = (( 1, 2, 3 ) , ( 3, 2, 1 )) ; + + +CBotListArray::CBotListArray() +{ + m_expr = NULL; + name = "CBotListArray"; +} + +CBotListArray::~CBotListArray() +{ + delete m_expr; +} + + +CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) +{ + CBotCStack* pStk = pStack->TokenStack(p); + + CBotToken* pp = p; + + if (IsOfType( p, ID_NULL )) + { + CBotInstr* inst = new CBotExprNull (); + inst->SetToken(pp); + return pStack->Return(inst, pStk); // ok with empty element + } + + CBotListArray* inst = new CBotListArray(); + + if (IsOfType( p, ID_OPENPAR )) + { + // each element takes the one after the other + if (type.Eq( CBotTypArrayPointer )) + { + type = type.GivTypElem(); + + pStk->SetStartError(p->GivStart()); + if (NULL == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) )) + { + goto error; + } + + while (IsOfType( p, ID_COMMA )) // other elements? + { + pStk->SetStartError(p->GivStart()); + + CBotInstr* i = CBotListArray::Compile(p, pStk, type); + if (NULL == i) + { + goto error; + } + + inst->m_expr->AddNext3(i); + } + } + else + { + pStk->SetStartError(p->GivStart()); + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + CBotVar* pv = pStk->GivVar(); // result of the expression + + if (pv == NULL || !TypesCompatibles( type, pv->GivTypResult())) // compatible type? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + + while (IsOfType( p, ID_COMMA )) // other elements? + { + pStk->SetStartError(p->GivStart()); + + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStk) ; + if (NULL == i) + { + goto error; + } + + CBotVar* pv = pStk->GivVar(); // result of the expression + + if (pv == NULL || !TypesCompatibles( type, pv->GivTypResult())) // compatible type? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + inst->m_expr->AddNext3(i); + } + } + + if (!IsOfType(p, ID_CLOSEPAR) ) + { + pStk->SetError(TX_CLOSEPAR, p->GivStart()); + goto error; + } + + return pStack->Return(inst, pStk); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + + +// executes the definition of an array + +bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) +{ + CBotStack* pile1 = pj->AddStack(); + CBotVar* pVar2; + + CBotInstr* p = m_expr; + + int n = 0; + + for (; p != NULL ; n++, p = p->GivNext3()) + { + if (pile1->GivState() > n) continue; + + pVar2 = pVar->GivItem(n, true); + + if (!p->Execute(pile1, pVar2)) return false; // evaluate expression + + pile1->IncState(); + } + + return pj->Return(pile1); +} + +void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) + { + CBotStack* pile = pj->RestoreStack(this); + if (pile == NULL) return; + + CBotInstr* p = m_expr; + + int state = pile->GivState(); + + while(state-- > 0) p = p->GivNext3() ; + + p->RestoreState(pile, bMain); // size calculation //interrupted! + } +} + +////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// definition of an integer variable +// int a, b = 12; + +CBotInt::CBotInt() +{ + m_next = NULL; // for multiple definitions + m_var = + m_expr = NULL; + name = "CBotInt"; +} + +CBotInt::~CBotInt() +{ + delete m_var; + delete m_expr; +} + +CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) +{ + if (IsOfType(p, ID_OPBRK)) + { + if (!IsOfType(p, ID_CLBRK)) + { + pStack->SetError(TX_CLBRK, p->GivStart()); + return NULL; + } + + CBotInstr* inst = CompileArray(p, pStack, CBotTypResult(CBotTypArrayPointer, type), false); + if (inst != NULL || !pStack->IsOk()) return inst; + } + + // compiles an array declaration + if (first) return NULL ; + + CBotInstr* inst = CBotInstArray::Compile(p, pStack, type); + if (inst == NULL) return NULL; + + if (IsOfType(p, ID_COMMA)) // several definitions + { + if (NULL != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false))) // compiles next one + { + return inst; + } + delete inst; + return NULL; + } + + if (IsOfType(p, ID_SEP)) // end of instruction + { + return inst; + } + + delete inst; + pStack->SetError(TX_ENDOF, p->GivStart()); + return NULL; +} + +CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? NULL : p; // no repetition of the token "int" + + if (!cont && !IsOfType(p, ID_INT)) return NULL; + + CBotInt* inst = (CBotInt*)CompileArray(p, pStack, CBotTypInt); + if (inst != NULL || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotInt(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + + // determines the expression is valid for the item on the left side + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypInt; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) + { + delete inst; // type is not CBotInt + p = vartoken; // returns the variable name + + // compiles an array declaration + + CBotInstr* inst2 = CBotInstArray::Compile(p, pStk, CBotTypInt); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + + if (IsOfType(p, ID_COMMA)) // several definition chained + { + if (NULL != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile the next one + { + return pStack->Return(inst2, pStk); + } + } + inst = (CBotInt*)inst2; + goto suite; // no assignment, variable already created + } + + if (IsOfType(p, ID_ASS)) // with an assignment? + { + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + if (pStk->GivType() >= CBotTypBoolean) // compatible type ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + } + + { + CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// create the variable (evaluated after the assignment) + var->SetInit(inst->m_expr != NULL); // if initialized with assignment + var->SetUniqNum( //set it with a unique number + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); // place it on the stack + } + + if (IsOfType(p, ID_COMMA)) // chained several definitions + { + if (NULL != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile next one + { + return pStack->Return(inst, pStk); + } + } +suite: + if (noskip || IsOfType(p, ID_SEP)) // instruction is completed + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + +// execute the definition of the integer variable + +bool CBotInt::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // essential for SetState() + + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; // initial value // interrupted? + m_var->Execute(pile); // creates and assign the result + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; // other(s) definition(s) + + return pj->Return(pile); // forward below +} + +void CBotInt::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == NULL) return; + + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // initial value // interrupted? + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) m_next2b->RestoreState(pile, bMain); // other(s) definition(s) +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// defining a boolean variable +// int a, b = false; + +CBotBoolean::CBotBoolean() +{ + m_var = + m_expr = NULL; + name = "CBotBoolean"; +} + +CBotBoolean::~CBotBoolean() +{ + delete m_var; + delete m_expr; +} + +CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? NULL : p; + + if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return NULL; + + CBotBoolean* inst = (CBotBoolean*)CompileArray(p, pStack, CBotTypBoolean); + if (inst != NULL || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotBoolean(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + CBotVar* var = NULL; + + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypBoolean; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) + { + delete inst; // type is not CBotInt + p = vartoken; // resutns to the variable name + + // compiles an array declaration + + inst = (CBotBoolean*)CBotInstArray::Compile(p, pStk, CBotTypBoolean); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + if (IsOfType(p, ID_ASS)) + { + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + if (!pStk->GivTypResult().Eq(CBotTypBoolean)) + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + } + + var = CBotVar::Create(vartoken, CBotTypBoolean);// create the variable (evaluated after the assignment) + var->SetInit(inst->m_expr != NULL); + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); +suite: + if (IsOfType(p, ID_COMMA)) + { + if (NULL != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip))) + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + +// executes a boolean variable definition + +bool CBotBoolean::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this);//essential for SetState() + + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; + + return pj->Return(pile); +} + +void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == NULL) return; + + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // initial value interrupted? + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) + m_next2b->RestoreState(pile, bMain); // other(s) definition(s) +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// definition of a real/float variable +// int a, b = 12.4; + +CBotFloat::CBotFloat() +{ + m_var = + m_expr = NULL; + name = "CBotFloat"; +} + +CBotFloat::~CBotFloat() +{ + delete m_var; + delete m_expr; +} + +CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? NULL : p; + + if (!cont && !IsOfType(p, ID_FLOAT)) return NULL; + + CBotFloat* inst = (CBotFloat*)CompileArray(p, pStack, CBotTypFloat); + if (inst != NULL || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotFloat(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + CBotVar* var = NULL; + inst->SetToken(vartoken); + + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypFloat; + if (pStk->CheckVarLocal(vartoken)) // redefinition of a variable + { + pStk->SetStartError(vartoken->GivStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) + { + delete inst; + p = vartoken; + inst = (CBotFloat*)CBotInstArray::Compile(p, pStk, CBotTypFloat); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + if (IsOfType(p, ID_ASS)) + { + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + if (pStk->GivType() >= CBotTypBoolean) + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } + } + + var = CBotVar::Create(vartoken, CBotTypFloat); + var->SetInit(inst->m_expr != NULL); + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); +suite: + if (IsOfType(p, ID_COMMA)) + { + if (NULL != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip))) + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + +// executes the definition of a real variable + +bool CBotFloat::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; + + return pj->Return(pile); +} + +void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == NULL) return; + + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) + m_next2b->RestoreState(pile, bMain); +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// define a string variable +// int a, b = "salut"; + +CBotIString::CBotIString() +{ + m_var = + m_expr = NULL; + name = "CBotIString"; +} + +CBotIString::~CBotIString() +{ + delete m_var; + delete m_expr; +} + +CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? NULL : p; + + if (!cont && !IsOfType(p, ID_STRING)) return NULL; + + CBotIString* inst = (CBotIString*)CompileArray(p, pStack, CBotTypString); + if (inst != NULL || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotIString(); + + inst->m_expr = NULL; + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + + if (NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = CBotTypString; + if (pStk->CheckVarLocal(vartoken)) + { + pStk->SetStartError(vartoken->GivStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); + goto error; + } + + if (IsOfType(p, ID_ASS)) + { + if (NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } +/* if (!pStk->GivTypResult().Eq(CBotTypString)) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + }*/ + } + + CBotVar* var = CBotVar::Create(vartoken, CBotTypString); + var->SetInit(inst->m_expr != NULL); + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); + + if (IsOfType(p, ID_COMMA)) + { + if (NULL != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip))) + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + +// executes the definition of the string variable + +bool CBotIString::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if ( pile->GivState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; + + return pj->Return(pile); +} + +void CBotIString::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == NULL) return; + + if ( pile->GivState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) + m_next2b->RestoreState(pile, bMain); +} + +////////////////////////////////////////////////////////////////////////////////////////// + + + +////////////////////////////////////////////////////////////////////////////////////// +// compiles a statement such as " x = 123 " ou " z * 5 + 4 " +// with or without assignment + +CBotExpression::CBotExpression() +{ + m_leftop = NULL; + m_rightop = NULL; + name = "CBotExpression"; +} + +CBotExpression::~CBotExpression() +{ + delete m_leftop; + delete m_rightop; +} + +CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + + CBotExpression* inst = new CBotExpression(); + + inst->m_leftop = CBotLeftExpr::Compile(p, pStack); + + inst->SetToken(p); + int OpType = p->GivType(); + + if ( pStack->IsOk() && + IsOfTypeList(p, ID_ASS, ID_ASSADD, ID_ASSSUB, ID_ASSMUL, ID_ASSDIV, ID_ASSMODULO, + ID_ASSAND, ID_ASSXOR, ID_ASSOR, + ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) + { + if (inst->m_leftop == NULL) + { + pStack->SetError(TX_BADLEFT, p->GivEnd()); + delete inst; + return NULL; + } + + inst->m_rightop = CBotExpression::Compile(p, pStack); + if (inst->m_rightop == NULL) + { + delete inst; + return NULL; + } + + CBotTypResult type1 = pStack->GivTypResult(); + + // get the variable assigned to mark + CBotVar* var = NULL; + inst->m_leftop->ExecuteVar(var, pStack); + if (var == NULL) + { + delete inst; + return NULL; + } + + if (OpType != ID_ASS && var->GivInit() != IS_DEF) + { + pStack->SetError(TX_NOTINIT, pp); + delete inst; + return NULL; + } + + CBotTypResult type2 = var->GivTypResult(); + + // what types are acceptable? + switch (OpType) + { + case ID_ASS: + // if (type2 == CBotTypClass) type2 = -1; + if ((type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer)) || + (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) + { +/* CBotClass* c1 = type1.GivClass(); + CBotClass* c2 = type2.GivClass(); + if (!c1->IsChildOf(c2)) type2.SetType(-1); +//- if (!type1.Eq(CBotTypClass)) var->SetPointer(pStack->GivVar()->GivPointer());*/ + var->SetInit(2); + } + else + var->SetInit(true); + + break; + case ID_ASSADD: + if (type2.Eq(CBotTypBoolean) || + type2.Eq(CBotTypPointer) ) type2 = -1; // numbers and strings + break; + case ID_ASSSUB: + case ID_ASSMUL: + case ID_ASSDIV: + case ID_ASSMODULO: + if (type2.GivType() >= CBotTypBoolean) type2 = -1; // numbers only + break; + } + + if (!TypeCompatible(type1, type2, OpType)) + { + pStack->SetError(TX_BADTYPE, &inst->m_token); + delete inst; + return NULL; + } + + return inst; // compatible type? + } + + delete inst; + int start, end, error = pStack->GivError(start, end); + + p = pp; // returns to the top + pStack->SetError(0,0); // forget the error + + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // tries without assignment + if (i != NULL && error == TX_PRIVATE && p->GivType() == ID_ASS) + pStack->ResetError(error, start, end); + return i; +} + +// executes an expression with assignment + +bool CBotExpression::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + CBotToken* pToken = m_leftop->GivToken(); + CBotVar* pVar = NULL; + + CBotStack* pile1 = pile; + + bool IsInit = true; + CBotVar* result = NULL; + + // must be done before any indexes (stack can be changed) + if (!m_leftop->ExecuteVar(pVar, pile, NULL, false)) return false; // variable before accessing the value on the right + + if ( pile1->GivState()==0) + { + pile1->SetCopyVar(pVar); // keeps the copy on the stack (if interrupted) + pile1->IncState(); + } + + CBotStack* pile2 = pile->AddStack(); + + if ( pile2->GivState()==0) + { + if (m_rightop && !m_rightop->Execute(pile2)) return false; // initial value // interrupted? + pile2->IncState(); + } + + if (pile1->GivState() == 1) + { + if (m_token.GivType() != ID_ASS) + { + pVar = pile1->GivVar(); // recovers if interrupted + IsInit = pVar->GivInit(); + if (IsInit == IS_NAN) + { + pile2->SetError(TX_OPNAN, m_leftop->GivToken()); + return pj->Return(pile2); + } + result = CBotVar::Create("", pVar->GivTypResult(2)); + } + + switch (m_token.GivType()) + { + case ID_ASS: + break; + case ID_ASSADD: + result->Add(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSSUB: + result->Sub(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSMUL: + result->Mul(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSDIV: + if (IsInit && + result->Div(pile1->GivVar(), pile2->GivVar())) + pile2->SetError(TX_DIVZERO, &m_token); + pile2->SetVar(result); + break; + case ID_ASSMODULO: + if (IsInit && + result->Modulo(pile1->GivVar(), pile2->GivVar())) + pile2->SetError(TX_DIVZERO, &m_token); + pile2->SetVar(result); + break; + case ID_ASSAND: + result->And(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSXOR: + result->XOr(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSOR: + result->Or(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSSL: + result->SL(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSSR: + result->SR(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + case ID_ASSASR: + result->ASR(pile1->GivVar(), pile2->GivVar()); + pile2->SetVar(result); + break; + default: + ASM_TRAP(); + } + if (!IsInit) + pile2->SetError(TX_NOTINIT, m_leftop->GivToken()); + + pile1->IncState(); + } + + if (!m_leftop->Execute( pile2, pile1 )) + return false; + + return pj->Return(pile2); +} + + +void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) + { + CBotToken* pToken = m_leftop->GivToken(); + CBotVar* pVar = NULL; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == NULL) return; + + CBotStack* pile1 = pile; + + if ( pile1->GivState()==0) + { + m_leftop->RestoreStateVar(pile, true); + return; + } + + m_leftop->RestoreStateVar(pile, false); + + CBotStack* pile2 = pile->RestoreStack(); + if (pile2 == NULL) return; + + if ( pile2->GivState()==0) + { + if (m_rightop) m_rightop->RestoreState(pile2, bMain); + return; + } + } +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compile a statement such as "(condition)" +// the condition must be Boolean + +// this class has no constructor, because there is never an instance of this class +// the object returned by Compile is usually type CBotExpression + + +CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) +{ + pStack->SetStartError(p->GivStart()); + if (IsOfType(p, ID_OPENPAR)) + { + CBotInstr* inst = CBotBoolExpr::Compile(p, pStack); + if (NULL != inst) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + return inst; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); // missing parenthesis + } + delete inst; + } + + pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis + + return NULL; +} + + +////////////////////////////////////////////////////////////////////////////////////// +// compile a statement such as "(condition)" +// the condition must be Boolean +// +// this class has no constructor, because there is never an instance of this +// class +// the object returned by Compile is usually type CBotExpression +// + +CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + pStack->SetStartError(p->GivStart()); + + CBotInstr* inst = CBotTwoOpExpr::Compile(p, pStack); + + if (NULL != inst) + { + if (pStack->GivTypResult().Eq(CBotTypBoolean)) + { + return inst; + } + pStack->SetError(TX_NOTBOOL, p->GivStart()); // is not a boolean + } + + delete inst; + return NULL; +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compile either: +// instruction in parentheses (...) +// a unary expression (negative, not) +// variable name +// variables pre and post-incremented or decremented +// a given number DefineNum +// a constant +// procedure call +// new statement +// +// this class has no constructor, because there is never an instance of this class +// the object returned by Compile is the class corresponding to the instruction + + +CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GivStart()); + + // is it an expression in parentheses? + if (IsOfType(p, ID_OPENPAR)) + { + CBotInstr* inst = CBotExpression::Compile(p, pStk); + + if (NULL != inst) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + return pStack->Return(inst, pStk); + } + pStk->SetError(TX_CLOSEPAR, p->GivStart()); + } + delete inst; + return pStack->Return(NULL, pStk); + } + + // is this a unary operation? + CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); + if (inst != NULL || !pStk->IsOk()) + return pStack->Return(inst, pStk); + + // is it a variable name? + if (p->GivType() == TokenTypVar) + { + // this may be a method call without the "this." before + inst = CBotExprVar::CompileMethode(p, pStk); + if (inst != NULL) return pStack->Return(inst, pStk); + + + // is it a procedure call? + inst = CBotInstrCall::Compile(p, pStk); + if (inst != NULL || !pStk->IsOk()) + return pStack->Return(inst, pStk); + + + CBotToken* pvar = p; + // no, it an "ordinaty" variable + inst = CBotExprVar::Compile(p, pStk); + + CBotToken* pp = p; + // post incremented or decremented? + if (IsOfType(p, ID_INC, ID_DEC)) + { + if (pStk->GivType() >= CBotTypBoolean) + { + pStk->SetError(TX_BADTYPE, pp); + delete inst; + return pStack->Return(NULL, pStk); + } + + // recompile the variable for read-only + delete inst; + p = pvar; + inst = CBotExprVar::Compile(p, pStk, PR_READ); + p = p->GivNext(); + + CBotPostIncExpr* i = new CBotPostIncExpr(); + i->SetToken(pp); + i->m_Instr = inst; // associated statement + return pStack->Return(i, pStk); + } + return pStack->Return(inst, pStk); + } + + // pre increpemted or pre decremented? + CBotToken* pp = p; + if (IsOfType(p, ID_INC, ID_DEC)) + { + CBotPreIncExpr* i = new CBotPreIncExpr(); + i->SetToken(pp); + + if (p->GivType() == TokenTypVar) + { + if (NULL != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) + { + if (pStk->GivType() >= CBotTypBoolean) + { + pStk->SetError(TX_BADTYPE, pp); + delete inst; + return pStack->Return(NULL, pStk); + } + return pStack->Return(i, pStk); + } + delete i; + return pStack->Return(NULL, pStk); + } + } + + // is it a number or DefineNum? + if (p->GivType() == TokenTypNum || + p->GivType() == TokenTypDef ) + { + CBotInstr* inst = CBotExprNum::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is this a chaine? + if (p->GivType() == TokenTypString) + { + CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is a "true" or "false" + if (p->GivType() == ID_TRUE || + p->GivType() == ID_FALSE ) + { + CBotInstr* inst = CBotExprBool::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is an object to be created with new + if (p->GivType() == ID_NEW) + { + CBotInstr* inst = CBotNew::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is a null pointer + if (IsOfType(p, ID_NULL)) + { + CBotInstr* inst = new CBotExprNull (); + inst->SetToken(pp); + CBotVar* var = CBotVar::Create("", CBotTypNullPointer); + pStk->SetVar(var); + return pStack->Return(inst, pStk); + } + + // is a number nan + if (IsOfType(p, ID_NAN)) + { + CBotInstr* inst = new CBotExprNan (); + inst->SetToken(pp); + CBotVar* var = CBotVar::Create("", CBotTypInt); + var->SetInit(IS_NAN); + pStk->SetVar(var); + return pStack->Return(inst, pStk); + } + + + return pStack->Return(NULL, pStk); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// Management of pre-and post increment / decrement +// There is no routine Compiles, the object is created directly +// Compiles in CBotParExpr :: + + +CBotPostIncExpr::CBotPostIncExpr() +{ + m_Instr = NULL; + name = "CBotPostIncExpr"; +} + +CBotPostIncExpr::~CBotPostIncExpr() +{ + delete m_Instr; +} + +CBotPreIncExpr::CBotPreIncExpr() +{ + m_Instr = NULL; + name = "CBotPreIncExpr"; +} + +CBotPreIncExpr::~CBotPreIncExpr() +{ + delete m_Instr; +} + +bool CBotPostIncExpr::Execute(CBotStack* &pj) +{ + CBotStack* pile1 = pj->AddStack(this); + CBotStack* pile2 = pile1; + + CBotVar* var1 = NULL; + + // retrieves the variable fields and indexes according + if (!((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true)) return false; + + pile1->SetState(1); + pile1->SetCopyVar(var1); // places the result (before incrementation); + + CBotStack* pile3 = pile2->AddStack(this); + if (pile3->IfStep()) return false; + + if (var1->GivInit() == IS_NAN) + { + pile1->SetError(TX_OPNAN, &m_token); + } + + if (var1->GivInit() != IS_DEF) + { + pile1->SetError(TX_NOTINIT, &m_token); + } + + if (GivTokenType() == ID_INC) var1->Inc(); + else var1->Dec(); + + return pj->Return(pile1); // operation done, result on pile2 +} + +void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile1 = pj->RestoreStack(this); + if (pile1 == NULL) return; + + ((CBotExprVar*)m_Instr)->RestoreStateVar(pile1, bMain); + + if (pile1 != NULL) pile1->RestoreStack(this); +} + +bool CBotPreIncExpr::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var1; + + if (pile->GivState() == 0) + { + CBotStack* pile2 = pile; + // retrieves the variable fields and indexes according + // pile2 is modified on return + if (!((CBotExprVar*)m_Instr)->ExecuteVar(var1, pile2, NULL, true)) return false; + + if (var1->GivInit() == IS_NAN) + { + pile->SetError(TX_OPNAN, &m_token); + return pj->Return(pile); // operation performed + } + + if (var1->GivInit() != IS_DEF) + { + pile->SetError(TX_NOTINIT, &m_token); + return pj->Return(pile); // operation performed + } + + if (GivTokenType() == ID_INC) var1->Inc(); + else var1->Dec(); // ((CBotVarInt*)var1)->m_val + + pile->IncState(); + } + + if (!m_Instr->Execute(pile)) return false; + return pj->Return(pile); // operation performed +} + + +void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == NULL) return; + + if (pile->GivState() == 0) + { + return; + } + + m_Instr->RestoreState(pile, bMain); +} + + +////////////////////////////////////////////////////////////////////////////////////// +// compile an unary expression +// + +// - +// not +// ! +// ~ + +CBotExprUnaire::CBotExprUnaire() +{ + m_Expr = NULL; + name = "CBotExprUnaire"; +} + +CBotExprUnaire::~CBotExprUnaire() +{ + delete m_Expr; +} + +CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) +{ + int op = p->GivType(); + CBotToken* pp = p; + if (!IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 )) return NULL; + + CBotCStack* pStk = pStack->TokenStack(pp); + + CBotExprUnaire* inst = new CBotExprUnaire(); + inst->SetToken(pp); + + if (NULL != (inst->m_Expr = CBotParExpr::Compile( p, pStk ))) + { + if (op == ID_ADD && pStk->GivType() < CBotTypBoolean) // only with the number + return pStack->Return(inst, pStk); + if (op == ID_SUB && pStk->GivType() < CBotTypBoolean) // only with the numer + return pStack->Return(inst, pStk); + if (op == ID_NOT && pStk->GivType() < CBotTypFloat) // only with an integer + return pStack->Return(inst, pStk); + if (op == ID_LOG_NOT && pStk->GivTypResult().Eq(CBotTypBoolean))// only with boolean + return pStack->Return(inst, pStk); + if (op == ID_TXT_NOT && pStk->GivTypResult().Eq(CBotTypBoolean))// only with boolean + return pStack->Return(inst, pStk); + + pStk->SetError(TX_BADTYPE, &inst->m_token); + } + delete inst; + return pStack->Return(NULL, pStk); +} + +// executes unary expression + +bool CBotExprUnaire::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->GivState() == 0) + { + if (!m_Expr->Execute(pile)) return false; // interrupted ? + pile->IncState(); + } + + CBotStack* pile2 = pile->AddStack(); + if (pile2->IfStep()) return false; + + CBotVar* var = pile->GivVar(); // get the result on the stack + + switch (GivTokenType()) + { + case ID_ADD: + break; + case ID_SUB: + var->Neg(); // change the sign + break; + case ID_NOT: + case ID_LOG_NOT: + case ID_TXT_NOT: + var->Not(); + break; + } + return pj->Return(pile); // forwards below +} + +void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL) return; + + if (pile->GivState() == 0) + { + m_Expr->RestoreState(pile, bMain); // interrupted here! + return; + } +} + +////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// index management for arrays +// array [ expression ] + + +CBotIndexExpr::CBotIndexExpr() +{ + m_expr = NULL; + name = "CBotIndexExpr"; +} + +CBotIndexExpr::~CBotIndexExpr() +{ + delete m_expr; +} + +// finds a field from the instance at compile time + +bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + if (pVar->GivType(1) != CBotTypArrayPointer) + ASM_TRAP(); + + pVar = ((CBotVarArray*)pVar)->GivItem(0, false); // at compile time makes the element [0] + if (pVar == NULL) + { + pile->SetError(TX_OUTARRAY, m_token.GivEnd()); + return false; + } + if (m_next3 != NULL) return m_next3->ExecuteVar(pVar, pile); + return true; +} + +// attention, changes the pointer to the stack intentionally +// place the index calculated on the additional stack + + +bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) +{ + CBotStack* pj = pile; + + if (pVar->GivType(1) != CBotTypArrayPointer) + ASM_TRAP(); + + pile = pile->AddStack(); + + if (pile->GivState() == 0) + { + if (!m_expr->Execute(pile)) return false; + pile->IncState(); + } + // handles array + + CBotVar* p = pile->GivVar(); // result on the stack + + if (p == NULL || p->GivType() > CBotTypDouble) + { + pile->SetError(TX_BADINDEX, prevToken); + return pj->Return(pile); + } + + int n = p->GivValInt(); // position in the table + + pVar = ((CBotVarArray*)pVar)->GivItem(n, bExtend); + if (pVar == NULL) + { + pile->SetError(TX_OUTARRAY, prevToken); + return pj->Return(pile); + } + + pVar->Maj(pile->GivPUser(), true); + + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; + + // does not release the stack + // to avoid recalculation of the index twice where appropriate + return true; +} + +void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + pile = pile->RestoreStack(); + if (pile == NULL) return; + + if (bMain && pile->GivState() == 0) + { + m_expr->RestoreState(pile, true); + return; + } + + if (m_next3) + m_next3->RestoreStateVar(pile, bMain); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// field management in an instance (dot operator) +// toto.x + + +CBotFieldExpr::CBotFieldExpr() +{ + name = "CBotFieldExpr"; + m_nIdent = 0; +} + +CBotFieldExpr::~CBotFieldExpr() +{ +} + +void CBotFieldExpr::SetUniqNum(int num) +{ + m_nIdent = num; +} + + +// find a field from the instance at compile + +bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + if (pVar->GivType(1) != CBotTypPointer) + ASM_TRAP(); + + pVar = pVar->GivItemRef(m_nIdent); + if (pVar == NULL) + { + pile->SetError(TX_NOITEM, &m_token); + return false; + } + + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile) ) return false; + + return true; +} + +bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) +{ + CBotStack* pj = pile; + pile = pile->AddStack(this); // changes in output stack + if (pile == EOX) return true; + + + if (pVar->GivType(1) != CBotTypPointer) + ASM_TRAP(); + + CBotVarClass* pItem = pVar->GivPointer(); + if (pItem == NULL) + { + pile->SetError(TX_NULLPT, prevToken); + return pj->Return(pile); + } + if (pItem->GivUserPtr() == OBJECTDELETED) + { + pile->SetError(TX_DELETEDPT, prevToken); + return pj->Return(pile); + } + + if (bStep && pile->IfStep()) return false; + + pVar = pVar->GivItemRef(m_nIdent); + if (pVar == NULL) + { + pile->SetError(TX_NOITEM, &m_token); + return pj->Return(pile); + } + + if (pVar->IsStatic()) + { + // for a static variable, takes it in the class itself + CBotClass* pClass = pItem->GivClass(); + pVar = pClass->GivItem(m_token.GivString()); + } + + // request the update of the element, if applicable + pVar->Maj(pile->GivPUser(), true); + + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; + + // does not release the stack + // to maintain the state SetState () corresponding to step + + return true; +} + +void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) +{ + pj = pj->RestoreStack(this); + if (pj == NULL) return; + + if (m_next3 != NULL) + m_next3->RestoreStateVar(pj, bMain); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// compile a left operand for an assignment +CBotLeftExpr::CBotLeftExpr() +{ + name = "CBotLeftExpr"; + m_nIdent = 0; +} + +CBotLeftExpr::~CBotLeftExpr() +{ +} + +// compiles an expression for a left-operand (left of an assignment) +// this can be +// toto +// toto[ 3 ] +// toto.x +// toto.pos.x +// toto[2].pos.x +// toto[1].pos[2].x +// toto[1][2][3] + +CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GivStart()); + + // is it a variable name? + if (p->GivType() == TokenTypVar) + { + CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object + + inst->SetToken(p); + + CBotVar* var; + + if (NULL != (var = pStk->FindVar(p))) // seek if known variable + { + inst->m_nIdent = var->GivUniqNum(); + if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) + { + if ( var->IsPrivate(PR_READ) && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, p); + goto err; + } + // this is an element of the current class + // adds the equivalent of this. before + CBotToken pthis("this"); + inst->SetToken(&pthis); + inst->m_nIdent = -2; // indent for this + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(p); // keeps the name of the token + inst->AddNext3(i); // add after + + var = pStk->FindVar(pthis); + var = var->GivItem(p->GivString()); + i->SetUniqNum(var->GivUniqNum()); + } + p = p->GivNext(); // next token + + while (true) + { + if (var->GivType() == CBotTypArrayPointer) + { + if (IsOfType( p, ID_OPBRK )) + { + CBotIndexExpr* i = new CBotIndexExpr(); + i->m_expr = CBotExpression::Compile(p, pStk); + inst->AddNext3(i); // add to the chain + + var = ((CBotVarArray*)var)->GivItem(0,true); // gets the component [0] + + if (i->m_expr == NULL) + { + pStk->SetError(TX_BADINDEX, p->GivStart()); + goto err; + } + + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto err; + } + continue; + } + } + + if (var->GivType(1) == CBotTypPointer) // for classes + { + if (IsOfType(p, ID_DOT)) + { + CBotToken* pp = p; + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(pp); // keeps the name of the token + inst->AddNext3(i); // adds after + + if (p->GivType() == TokenTypVar) // must be a name + { + var = var->GivItem(p->GivString()); // get item correspondent + if (var != NULL) + { + if ( var->IsPrivate(PR_READ) && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, pp); + goto err; + } + + i->SetUniqNum(var->GivUniqNum()); + p = p->GivNext(); // skips the name + continue; + } + pStk->SetError(TX_NOITEM, p); + } + pStk->SetError(TX_DOT, p->GivStart()); + goto err; + } + } + break; + } + + + if (pStk->IsOk()) return (CBotLeftExpr*) pStack->Return(inst, pStk); + } + pStk->SetError(TX_UNDEFVAR, p); +err: + delete inst; + return (CBotLeftExpr*) pStack->Return(NULL, pStk); + } + + return (CBotLeftExpr*) pStack->Return(NULL, pStk); +} + +// runs, is a variable and assigns the result to the stack +bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) +{ + CBotStack* pile = pj->AddStack(); + + CBotVar* var1 = NULL; + CBotVar* var2 = NULL; + // fetch a variable (not copy) + if (!ExecuteVar(var1, array, NULL, false)) return false; + + if (pile->IfStep()) return false; + + if (var1) + { + var2 = pj->GivVar(); // result on the input stack + if (var2) + { + CBotTypResult t1 = var1->GivTypResult(); + CBotTypResult t2 = var2->GivTypResult(); + if (t2.Eq(CBotTypPointer)) + { + CBotClass* c1 = t1.GivClass(); + CBotClass* c2 = t2.GivClass(); + if ( !c2->IsChildOf(c1)) + { + CBotToken* pt = &m_token; + pile->SetError(TX_BADTYPE, pt); + return pj->Return(pile); // operation performed + } + } + var1->SetVal(var2); // do assignment + } + pile->SetCopyVar(var1); // replace the stack with the copy of the variable + // (for name) + } + + return pj->Return(pile); // operation performed +} + +// fetch a variable during compilation + +bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + pVar = pile->FindVar(m_token); + if (pVar == NULL) return false; + + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile) ) return false; + + return true; +} + +// fetch the variable at runtume + +bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) +{ + pile = pile->AddStack(this); + + pVar = pile->FindVar(m_nIdent); + if (pVar == NULL) + { +#ifdef _DEBUG + ASM_TRAP(); +#endif + pile->SetError(2, &m_token); + return false; + } + + if (bStep && m_next3 == NULL && pile->IfStep()) return false; + + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; + + return true; +} + +void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + pile = pile->RestoreStack(this); + if (pile == NULL) return; + + if (m_next3 != NULL) + m_next3->RestoreStateVar(pile, bMain); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +// converts a string into integer +// may be of the form 0xabc123 + +long GivNumInt(const char* p) +{ + long num = 0; + while (*p >= '0' && *p <= '9') + { + num = num * 10 + *p - '0'; + p++; + } + if (*p == 'x' || *p == 'X') + { + while (*++p != 0) + { + if (*p >= '0' && *p <= '9') + { + num = num * 16 + *p - '0'; + continue; + } + if (*p >= 'A' && *p <= 'F') + { + num = num * 16 + *p - 'A' + 10; + continue; + } + if (*p >= 'a' && *p <= 'f') + { + num = num * 16 + *p - 'a' + 10; + continue; + } + break; + } + } + return num; +} + +// converts a string into a float number +extern float GivNumFloat(const char* p) +{ + double num = 0; + double div = 10; + bool bNeg = false; + + if (*p == '-') + { + bNeg = true; + p++; + } + while (*p >= '0' && *p <= '9') + { + num = num * 10. + (*p - '0'); + p++; + } + + if (*p == '.') + { + p++; + while (*p >= '0' && *p <= '9') + { + num = num + (*p - '0') / div; + div = div * 10; + p++; + } + } + + int exp = 0; + if (*p == 'e' || *p == 'E') + { + char neg = 0; + p++; + if (*p == '-' || *p == '+') neg = *p++; + + while (*p >= '0' && *p <= '9') + { + exp = exp * 10 + (*p - '0'); + p++; + } + if (neg == '-') exp = -exp; + } + + while (exp > 0) + { + num *= 10.0; + exp--; + } + + while (exp < 0) + { + num /= 10.0; + exp++; + } + + if (bNeg) num = -num; + return (float)num; +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compiles a token representing a number +CBotExprNum::CBotExprNum() +{ + name = "CBotExprNum"; +} + +CBotExprNum::~CBotExprNum() +{ +} + +CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + CBotExprNum* inst = new CBotExprNum(); + + inst->SetToken(p); + CBotString s = p->GivString(); + + inst->m_numtype = CBotTypInt; + if (p->GivType() == TokenTypDef) + { + inst->m_valint = p->GivIdKey(); + } + else + { + if (s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) )) + { + inst->m_numtype = CBotTypFloat; + inst->m_valfloat = GivNumFloat(s); + } + else + { + inst->m_valint = GivNumInt(s); + } + } + + if (pStk->NextToken(p)) + { + CBotVar* var = CBotVar::Create((CBotToken*)NULL, inst->m_numtype); + pStk->SetVar(var); + + return pStack->Return(inst, pStk); + } + delete inst; + return pStack->Return(NULL, pStk); +} + +// execute, returns the corresponding number + +bool CBotExprNum::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var = CBotVar::Create((CBotToken*)NULL, m_numtype); + + CBotString nombre ; + if (m_token.GivType() == TokenTypDef) + { + nombre = m_token.GivString(); + } + + switch (m_numtype) + { + case CBotTypShort: + case CBotTypInt: + var->SetValInt(m_valint, nombre); + break; + case CBotTypFloat: + var->SetValFloat(m_valfloat); + break; + } + pile->SetVar(var); // place on the stack + + return pj->Return(pile); // it's ok +} + +void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compile a token representing a string + +CBotExprAlpha::CBotExprAlpha() +{ + name = "CBotExprAlpha"; +} + +CBotExprAlpha::~CBotExprAlpha() +{ +} + +CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + CBotExprAlpha* inst = new CBotExprAlpha(); + + inst->SetToken(p); + p = p->GivNext(); + + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); + pStk->SetVar(var); + + return pStack->Return(inst, pStk); +} + +// execute, returns the corresponding string + +bool CBotExprAlpha::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypString); + + CBotString chaine = m_token.GivString(); + chaine = chaine.Mid(1, chaine.GivLength()-2); // removes the quotes + + var->SetValString(chaine); // value of the number + + pile->SetVar(var); // put on the stack + + return pj->Return(pile); +} + +void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compile a token representing true or false + +CBotExprBool::CBotExprBool() +{ + name = "CBotExprBool"; +} + +CBotExprBool::~CBotExprBool() +{ +} + +CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + CBotExprBool* inst = NULL; + + if ( p->GivType() == ID_TRUE || + p->GivType() == ID_FALSE ) + { + inst = new CBotExprBool(); + inst->SetToken(p); // stores the operation false or true + p = p->GivNext(); + + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + pStk->SetVar(var); + } + + return pStack->Return(inst, pStk); +} + +// executes, returns true or false + +bool CBotExprBool::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + + if (GivTokenType() == ID_TRUE) var->SetValInt(1); + else var->SetValInt(0); + + pile->SetVar(var); // put on the stack + return pj->Return(pile); // forwards below +} + +void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +// management of the operand "null" + +CBotExprNull::CBotExprNull() +{ + name = "CBotExprNull"; +} + +CBotExprNull::~CBotExprNull() +{ +} + +// executes, returns an empty pointer + +bool CBotExprNull::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypNullPointer); + + var->SetInit(true); // null pointer valid + pile->SetVar(var); // place on the stack + return pj->Return(pile); // forwards below +} + +void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +// management of the operand "nan" + +CBotExprNan::CBotExprNan() +{ + name = "CBotExprNan"; +} + +CBotExprNan::~CBotExprNan() +{ +} + +// executes, returns null pointer + +bool CBotExprNan::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypInt); + + var->SetInit(IS_NAN); // nan + pile->SetVar(var); // put on the stack + return pj->Return(pile); // forward below +} + +void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} + +////////////////////////////////////////////////////////////////////////////////////// +// compile a variable name +// check that it is known on the stack +// and it has been initialized + +CBotExprVar::CBotExprVar() +{ + name = "CBotExprVar"; + m_nIdent = 0; +} + +CBotExprVar::~CBotExprVar() +{ +} + +CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) +{ + CBotToken* pDebut = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GivStart()); + + // is it a variable? + if (p->GivType() == TokenTypVar) + { + CBotInstr* inst = new CBotExprVar(); // create the object + + inst->SetToken(p); + + CBotVar* var; + + if (NULL != (var = pStk->FindVar(p))) // seek if known variable + { + int ident = var->GivUniqNum(); + ((CBotExprVar*)inst)->m_nIdent = ident; // identifies variable by its number + + if (ident > 0 && ident < 9000) + { + if ( var->IsPrivate(privat) && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, p); + goto err; + } + + // This is an element of the current class + // ads the equivalent of this. before + /// \TODO need to be fixed revised and fixed after adding unit + ///tests + CBotToken token("this"); + inst->SetToken(&token); + ((CBotExprVar*)inst)->m_nIdent = -2; // identificator for this + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(p); // keeps the name of the token + i->SetUniqNum(ident); + inst->AddNext3(i); // added after + } + + p = p->GivNext(); // next token + + while (true) + { + if (var->GivType() == CBotTypArrayPointer) + { + if (IsOfType( p, ID_OPBRK )) // check if there is an aindex + { + CBotIndexExpr* i = new CBotIndexExpr(); + i->m_expr = CBotExpression::Compile(p, pStk); // compile the formula + inst->AddNext3(i); // add to the chain + + var = ((CBotVarArray*)var)->GivItem(0,true); // gets the component [0] + + if (i->m_expr == NULL) + { + pStk->SetError(TX_BADINDEX, p->GivStart()); + goto err; + } + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto err; + } + continue; + } + } + if (var->GivType(1) == CBotTypPointer) // for classes + { + if (IsOfType(p, ID_DOT)) + { + CBotToken* pp = p; + + if (p->GivType() == TokenTypVar) // must be a name + { + if (p->GivNext()->GivType() == ID_OPENPAR) // a method call? + { + CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); + if (!pStk->IsOk()) goto err; + inst->AddNext3(i); // added after + return pStack->Return(inst, pStk); + } + else + { + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(pp); // keeps the name of the token + inst->AddNext3(i); // add after + var = var->GivItem(p->GivString()); // get item correspondent + if (var != NULL) + { + i->SetUniqNum(var->GivUniqNum()); + if ( var->IsPrivate() && + !pStk->GivBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, pp); + goto err; + } + } + } + + + if (var != NULL) + { + p = p->GivNext(); // skips the name + continue; + } + pStk->SetError(TX_NOITEM, p); + goto err; + } + pStk->SetError(TX_DOT, p->GivStart()); + goto err; + } + } + + break; + } + + pStk->SetCopyVar(var); // place the copy of the variable on the stack (for type) + if (pStk->IsOk()) return pStack->Return(inst, pStk); + } + pStk->SetError(TX_UNDEFVAR, p); +err: + delete inst; + return pStack->Return(NULL, pStk); + } + + return pStack->Return(NULL, pStk); +} + +CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(pp->GivStart()); + + // is it a variable ? + if (pp->GivType() == TokenTypVar) + { + CBotToken pthis("this"); + CBotVar* var = pStk->FindVar(pthis); + if (var == 0) return pStack->Return(NULL, pStk); + + CBotInstr* inst = new CBotExprVar(); + + // this is an element of the current class + // adds the equivalent of this. before + + inst->SetToken(&pthis); + ((CBotExprVar*)inst)->m_nIdent = -2; // ident for this + + CBotToken* pp = p; + + if (pp->GivType() == TokenTypVar) + { + if (pp->GivNext()->GivType() == ID_OPENPAR) // a method call? + { + CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); + if (pStk->IsOk()) + { + inst->AddNext3(i); // add after + p = pp; // previous instruction + return pStack->Return(inst, pStk); + } + pStk->SetError(0,0); // the error is not adressed here + } + } + delete inst; + } + return pStack->Return(NULL, pStk); +} + + +// execute, making the value of a variable + +bool CBotExprVar::Execute(CBotStack* &pj) +{ + CBotVar* pVar = NULL; + CBotStack* pile = pj->AddStack(this); + + CBotStack* pile1 = pile; + + if (pile1->GivState() == 0) + { + if (!ExecuteVar(pVar, pile, NULL, true)) return false; // Get the variable fields and indexes according + + if (pVar) pile1->SetCopyVar(pVar); // place a copy on the stack + else + { + return pj->Return(pile1); + } + pile1->IncState(); + } + + pVar = pile1->GivVar(); + + if (pVar == NULL) + { + return pj->Return(pile1); + } + + if (pVar->GivInit() == IS_UNDEF) + { + CBotToken* pt = &m_token; + while (pt->GivNext() != NULL) pt = pt->GivNext(); + pile1->SetError(TX_NOTINIT, pt); + return pj->Return(pile1); + } + return pj->Return(pile1); // operation completed +} + +void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == NULL) return; + + CBotStack* pile1 = pile; + + if (pile1->GivState() == 0) + { + RestoreStateVar(pile, bMain); // retrieves the variable fields and indexes according + return; + } +} + +// fetch a variable at runtime + +bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) +{ + CBotStack* pile = pj; + pj = pj->AddStack(this); + + if (bStep && m_nIdent>0 && pj->IfStep()) return false; + + pVar = pj->FindVar(m_nIdent, true); // tries with the variable update if necessary + if (pVar == NULL) + { +#ifdef _DEBUG + ASM_TRAP(); +#endif + pj->SetError(1, &m_token); + return false; + } + if ( m_next3 != NULL && + !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) + return false; // field of an instance, table, methode + + return pile->ReturnKeep(pj); // does not put on stack but get the result if a method was called +} + + +// fetch variable at runtime + +void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) +{ + pj = pj->RestoreStack(this); + if (pj == NULL) return; + + if (m_next3 != NULL) + m_next3->RestoreStateVar(pj, bMain); +} + +////////////////////////////////////////////////////////////////////////////////////////// + +// compile a list of parameters + +CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) +{ + bool first = true; + CBotInstr* ret = NULL; // to return to the list + + CBotCStack* pile = pStack; + int i = 0; + + if (IsOfType(p, ID_OPENPAR)) + { + int start, end; + if (!IsOfType(p, ID_CLOSEPAR)) while (true) + { + start = p->GivStart(); + pile = pile->TokenStack(); // keeps the result on the stack + + if (first) pStack->SetStartError(start); + first = false; + + CBotInstr* param = CBotExpression::Compile(p, pile); + end = p->GivStart(); + + if (!pile->IsOk()) + { + return pStack->Return(NULL, pile); + } + + if (ret == NULL) ret = param; + else ret->AddNext(param); // construct the list + + if (param != NULL) + { + if (pile->GivTypResult().Eq(99)) + { + delete pStack->TokenStack(); + pStack->SetError(TX_VOID, p->GivStart()); + return NULL; + } + ppVars[i] = pile->GivVar(); + ppVars[i]->GivToken()->SetPos(start, end); + i++; + + if (IsOfType(p, ID_COMMA)) continue; // skips the comma + if (IsOfType(p, ID_CLOSEPAR)) break; + } + + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + delete pStack->TokenStack(); + return NULL; + } + } + ppVars[i] = NULL; + return ret; +} + +////////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// compile a method call + +CBotInstrMethode::CBotInstrMethode() +{ + m_Parameters = NULL; + m_MethodeIdent = 0; + name = "CBotInstrMethode"; +} + +CBotInstrMethode::~CBotInstrMethode() +{ + delete m_Parameters; +} + +CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* var) +{ + CBotInstrMethode* inst = new CBotInstrMethode(); + inst->SetToken(p); // corresponding token + + if (NULL != var) + { + CBotToken* pp = p; + p = p->GivNext(); + + if (p->GivType() == ID_OPENPAR) + { + inst->m_NomMethod = pp->GivString(); + + // compiles the list of parameters + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStack, ppVars); + + if (pStack->IsOk()) + { + CBotClass* pClass = var->GivClass(); // pointer to the class + inst->m_ClassName = pClass->GivName(); // name of the class + CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, + pStack, inst->m_MethodeIdent); + delete pStack->TokenStack(); // release parameters on the stack + inst->m_typRes = r; + + if (inst->m_typRes.GivType() > 20) + { + pStack->SetError(inst->m_typRes.GivType(), pp); + delete inst; + return NULL; + } + // put the result on the stack to have something + if (inst->m_typRes.GivType() > 0) + { + CBotVar* pResult = CBotVar::Create("", inst->m_typRes); + if (inst->m_typRes.Eq(CBotTypClass)) + { + pResult->SetClass(inst->m_typRes.GivClass()); + } + pStack->SetVar(pResult); + } + return inst; + } + delete inst; + return NULL; + } + } + pStack->SetError(1234, p); + delete inst; + return NULL; +} + +// execute the method call + +bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) +{ + CBotVar* ppVars[1000]; + CBotStack* pile1 = pj->AddStack(this, true); // a place for the copy of This + + if (pVar->GivPointer() == NULL) + { + pj->SetError(TX_NULLPT, prevToken); + } + + if (pile1->IfStep()) return false; + + CBotStack* pile2 = pile1->AddStack(); // for the next parameters + + if ( pile1->GivState() == 0) + { + CBotVar* pThis = CBotVar::Create(pVar); + pThis->Copy(pVar); + // this value should be taken before the evaluation parameters + // Test.Action (Test = Other); + // action must act on the value before test = Other! + + pThis->SetName("this"); + pThis->SetUniqNum(-2); + pile1->AddVar(pThis); + pile1->IncState(); + } + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != NULL) while ( true) + { + if (pile2->GivState() == 0) + { + if (!p->Execute(pile2)) return false; // interrupted here? + if (!pile2->SetState(1)) return false; // special mark to recognize parameters + } + ppVars[i++] = pile2->GivVar(); // construct the list of pointers + pile2 = pile2->AddStack(); // space on the stack for the result + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pThis = pile1->FindVar(-2); + CBotVar* pResult = NULL; + if (m_typRes.GivType() > 0) pResult = CBotVar::Create("", m_typRes); + if (m_typRes.Eq(CBotTypClass)) + { + pResult->SetClass(m_typRes.GivClass()); + } + CBotVar* pRes = pResult; + + if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, + pResult, pile2, GivToken())) return false; + if (pRes != pResult) delete pRes; + + pVar = NULL; // does not return value for this + return pj->Return(pile2); // release the entire stack +} + +void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + if (!bMain) return; + + CBotVar* ppVars[1000]; + CBotStack* pile1 = pile->RestoreStack(this); // place for the copy of This + if (pile1 == NULL) return; + + CBotStack* pile2 = pile1->RestoreStack(); // and for the parameters coming + if (pile2 == NULL) return; + + CBotVar* pThis = pile1->FindVar("this"); + pThis->SetUniqNum(-2); + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != NULL) while ( true) + { + if (pile2->GivState() == 0) + { + p->RestoreState(pile2, true); // interrupted here! + return; + } + ppVars[i++] = pile2->GivVar(); // construct the list of pointers + pile2 = pile2->RestoreStack(); + if (pile2 == NULL) return; + + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pResult = NULL; + + CBotVar* pRes = pResult; + + pClass->RestoreMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, pile2); +} + + +bool CBotInstrMethode::Execute(CBotStack* &pj) +{ + CBotVar* ppVars[1000]; + CBotStack* pile1 = pj->AddStack(this, true); // place for the copy of This + + if (pile1->IfStep()) return false; + + CBotStack* pile2 = pile1->AddStack(); // and for the parameters coming + + if ( pile1->GivState() == 0) + { + CBotVar* pThis = pile1->CopyVar(m_token); + // this value should be taken before the evaluation parameters + // Test.Action (Test = Other); + // Action must act on the value before test = Other! + pThis->SetName("this"); + pile1->AddVar(pThis); + pile1->IncState(); + } + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + if (p != NULL) while ( true) + { + if (pile2->GivState() == 0) + { + if (!p->Execute(pile2)) return false; // interrupted here? + if (!pile2->SetState(1)) return false; // special mark to recognize parameters + } + ppVars[i++] = pile2->GivVar(); // construct the list of pointers + pile2 = pile2->AddStack(); // space on the stack for the results + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pThis = pile1->FindVar("this"); + CBotVar* pResult = NULL; + if (m_typRes.GivType()>0) pResult = CBotVar::Create("", m_typRes); + if (m_typRes.Eq(CBotTypClass)) + { + pResult->SetClass(m_typRes.GivClass()); + } + CBotVar* pRes = pResult; + + if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interupted + + // set the new value of this in place of the old variable + CBotVar* old = pile1->FindVar(m_token); + old->Copy(pThis, false); + + if (pRes != pResult) delete pRes; + + return pj->Return(pile2); // release the entire stack +} + +/////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// compile an instruction "new" + +CBotNew::CBotNew() +{ + name = "CBotNew"; + m_Parameters = NULL; + m_nMethodeIdent = 0; +} + +CBotNew::~CBotNew() +{ +} + +CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + if (!IsOfType(p, ID_NEW)) return NULL; + + // verifies that the token is a class name + if (p->GivType() != TokenTypVar) return NULL; + + CBotClass* pClass = CBotClass::Find(p); + if (pClass == NULL) + { + pStack->SetError(TX_BADNEW, p); + return NULL; + } + + CBotNew* inst = new CBotNew(); + inst->SetToken(pp); + + inst->m_vartoken = p; + p = p->GivNext(); + + // creates the object on the "job" + // with a pointer to the object + CBotVar* pVar = CBotVar::Create("", pClass); + + // do the call of the creator + CBotCStack* pStk = pStack->TokenStack(); + { + // check if there are parameters + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStk, ppVars); + if (!pStk->IsOk()) goto error; + + // constructor exist? + CBotTypResult r = pClass->CompileMethode(pClass->GivName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); + delete pStk->TokenStack(); // release extra stack + int typ = r.GivType(); + + // if there is no constructor, and no parameters either, it's ok + if (typ == TX_UNDEFCALL && inst->m_Parameters == NULL) typ = 0; + pVar->SetInit(true); // mark the instance as init + + if (typ>20) + { + pStk->SetError(typ, inst->m_vartoken.GivEnd()); + goto error; + } + + // if the constructor does not exist, but there are parameters + if (typ<0 && inst->m_Parameters != NULL) + { + pStk->SetError(TX_NOCONST, &inst->m_vartoken); + goto error; + } + + // makes pointer to the object on the stack + pStk->SetVar(pVar); + return pStack->Return(inst, pStk); + } +error: + delete inst; + return pStack->Return(NULL, pStk); +} + +// executes instruction "new" + +bool CBotNew::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); //main stack + + if (pile->IfStep()) return false; + + CBotStack* pile1 = pj->AddStack2(); //secondary stack + + CBotVar* pThis = NULL; + + CBotToken* pt = &m_vartoken; + CBotClass* pClass = CBotClass::Find(pt); + + // create the variable "this" pointer type to the stack + + if ( pile->GivState()==0) + { + // create an instance of the requested class + // and initialize the pointer to that object + + + pThis = CBotVar::Create("this", pClass); + pThis->SetUniqNum(-2) ; + + pile1->SetVar(pThis); // place on stack1 + pile->IncState(); + } + + // fetch the this pointer if it was interrupted + if ( pThis == NULL) + { + pThis = pile1->GivVar(); // find the pointer + } + + // is there an assignment or parameters (constructor) + if ( pile->GivState()==1) + { + // evaluates the constructor of the instance + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != NULL) while ( true) + { + pile2 = pile2->AddStack(); // space on the stack for the result + if (pile2->GivState() == 0) + { + if (!p->Execute(pile2)) return false; // interrupted here? + pile2->SetState(1); + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + // create a variable for the result + CBotVar* pResult = NULL; // constructos still void + + if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interrupt + + pThis->ConstructorSet(); // indicates that the constructor has been called + } + + return pj->Return(pile1); // passes below +} + +void CBotNew::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); //primary stack + if (pile == NULL) return; + + CBotStack* pile1 = pj->AddStack2(); //secondary stack + + CBotToken* pt = &m_vartoken; + CBotClass* pClass = CBotClass::Find(pt); + + // create the variable "this" pointer type to the object + + if ( pile->GivState()==0) + { + return; + } + + CBotVar* pThis = pile1->GivVar(); // find the pointer + pThis->SetUniqNum(-2); + + // is ther an assignment or parameters (constructor) + if ( pile->GivState()==1) + { + // evaluates the constructor of the instance + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != NULL) while ( true) + { + pile2 = pile2->RestoreStack(); // space on the stack for the result + if (pile2 == NULL) return; + + if (pile2->GivState() == 0) + { + p->RestoreState(pile2, bMain); // interrupt here! + return; + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GivString(), pThis, + ppVars, pile2) ; // interrupt here! + } +} + +///////////////////////////////////////////////////////////// +// check if two results are consistent to make an operation + +bool TypeCompatible(CBotTypResult& type1, CBotTypResult& type2, int op) +{ + int t1 = type1.GivType(); + int t2 = type2.GivType(); + + int max = (t1 > t2) ? t1 : t2; + + if (max == 99) return false; // result is void? + + // special case for strin concatenation + if (op == ID_ADD && max >= CBotTypString) return true; + if (op == ID_ASSADD && max >= CBotTypString) return true; + if (op == ID_ASS && t1 == CBotTypString) return true; + + if (max >= CBotTypBoolean) + { + if ( (op == ID_EQ || op == ID_NE) && + (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && + (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE) && + (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && + (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; + if (t2 != t1) return false; + if (t1 == CBotTypArrayPointer) return type1.Compare(type2); + if (t1 == CBotTypPointer || + t1 == CBotTypClass || + t1 == CBotTypIntrinsic ) + { + CBotClass* c1 = type1.GivClass(); + CBotClass* c2 = type2.GivClass(); + + return c1->IsChildOf(c2) || c2->IsChildOf(c1); + // accept the case in reverse + // the transaction will be denied at runtime if the pointer is not + // compatible + } + + return true; + } + + type1.SetType(max); + type2.SetType(max); + return true; +} + +// check if two variables are compatible for parameter passing + +bool TypesCompatibles(const CBotTypResult& type1, const CBotTypResult& type2) +{ + int t1 = type1.GivType(); + int t2 = type2.GivType(); + + if (t1 == CBotTypIntrinsic) t1 = CBotTypClass; + if (t2 == CBotTypIntrinsic) t2 = CBotTypClass; + + int max = (t1 > t2) ? t1 : t2; + + if (max == 99) return false; // result is void? + + if (max >= CBotTypBoolean) + { + if (t2 != t1) return false; + + if (max == CBotTypArrayPointer) + return TypesCompatibles(type1.GivTypElem(), type2.GivTypElem()); + + if (max == CBotTypClass || max == CBotTypPointer) + return type1.GivClass() == type2.GivClass() ; + + return true ; + } + return true; +} + + +///////////////////////////////////////////////////////////////////////////////////// +// file management + +// necessary because it is not possible to do the fopen in the main program +// fwrite and fread in a dll or using the FILE * returned. + + +FILE* fOpen(const char* name, const char* mode) +{ + return fopen(name, mode); +} + +int fClose(FILE* filehandle) +{ + return fclose(filehandle); +} + +size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle) +{ + return fwrite(buffer, elemsize, length, filehandle); +} + +size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle) +{ + return fread(buffer, elemsize, length, filehandle); +} + +size_t fWrite(const void *buffer, size_t length, FILE* filehandle) +{ + return fwrite(buffer, 1, length, filehandle); +} + +size_t fRead(void *buffer, size_t length, FILE* filehandle) +{ + return fread(buffer, 1, length, filehandle); +} + + +//////////////////////////////////////// + diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 3b7d7e8..ebc8e52 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -1,1733 +1,1732 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.//////////////////////////////////////////////////////////////////// -/** - * \file CBot.h - * \brief Interpreter of the language CBot for COLOBOT game - */ - - - -#include "resource.h" -#include "CBotDll.h" // public definitions -#include "CBotToken.h" // token management - -#define STACKRUN true /// \def return execution directly on a suspended routine -#define STACKMEM true /// \def preserve memory for the execution stack -#define MAXSTACK 990 /// \def stack size reserved - -#define EOX (CBotStack*)-1 /// \def tag special condition - - -// fix for MSVC instruction __asm int 3 (setting a trap) -#if defined(__MINGW32__) || defined(__GNUC__) -#define ASM_TRAP() asm("int $3"); -#else -#define ASM_TRAP() __asm int 3; -#endif - -///////////////////////////////////////////////////////////////////// -// forward declaration - -class CBotCompExpr; // an expression like - // () <= () -class CBotAddExpr; // an expression like - // () + () -class CBotParExpr; // basic type or instruction in parenthesis - // Toto.truc - // 12.5 - // "string" - // ( expression ) -class CBotExprVar; // a variable name as - // Toto - // chose.truc.machin -class CBotWhile; // while (...) {...}; -class CBotIf; // if (...) {...} else {...} -class CBotDefParam; // paramerer list of a function -class CBotRepeat; // repeat (nb) {...} - - - -//////////////////////////////////////////////////////////////////////// -// Management of the execution stack -//////////////////////////////////////////////////////////////////////// - -// actually, externally, the only thing he can do -// is to create an instance of a stack -// to use for routine CBotProgram :: Execute (CBotStack) - - -/**\class CBotStack - * \brief Management of the execution stack. - * \brief Actually the only thing it can do is to create an instance of a stack - * \brief to use for routine CBotProgram :: Execute(CBotStack)*/ -class CBotStack -{ -public: -#if STACKMEM - /** - * \brief FirstStack Allocate first stack - * \return pointer to created stack - */ - static CBotStack * FirstStack(); - - /** \brief Delete Remove current stack */ - void Delete(); -#endif - - /** - * \brief CBotStack Constructor of the stack - * \param ppapa Not used. - */ - CBotStack(CBotStack* ppapa); - - - /** \brief ~CBotStack Destructor */ - ~CBotStack(); - - /** - * \brief StackOver Check if end of stack is reached - * \return true if end of stack - */ - bool StackOver(); - - /** - * \brief GivError Get error number of the stack - * \param [out] start beginning of the stack - * \param [out] end end of stack - * \return error number - */ - int GivError(int& start, int& end); - - /** - * \brief GivError Get error number - * \return eror number - */ - int GivError();// rend le numro d'erreur retourn - - /** - * \brief Reset Reset error at and set user - * \param [in] pUser User of stack - */ - void Reset(void* pUser); - - /** - * \brief SetType Determines the type. - * \param type Type of instruction on the stack. - */ - void SetType(CBotTypResult& type); - - /** - * \brief GivType Get the type of value on the stack. - * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). - * \return Type number. - */ - int GivType(int mode = 0); - - /** - * \brief Gives the type of complete value on the stack. - * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). - * \return Type of an element. - */ - CBotTypResult GivTypResult(int mode = 0); - - /** - * \brief Adds a local variable. - * \param [in] p Variable to be added. - */ - void AddVar(CBotVar* p); - - /** - * \brief Fetch a variable by its token. - * \brief This may be a composite variable - * \param [in] pToken Token upon which search is performed - * \param [in] bUpdate Not used. Probably need to be removed - * \param [in] bModif Not used. Probably need to be removed - * \return Found variable - */ - CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false, - bool bModif = false); - - /** - * \brief Fetch a variable by its token. - * \brief This may be a composite variable - * \param [in] pToken Token upon which search is performed - * \param [in] bUpdate Not used. Probably need to be removed - * \param [in] bModif Not used. Probably need to be removed - * \return Found variable - */ - CBotVar* FindVar(CBotToken& Token, bool bUpdate = false, - bool bModif = false); - - /** - * \brief Fetch variable by its name - * \param [in] name Name of variable to find - * \return Found variable - */ - CBotVar* FindVar(const char* name); - - /** - * \brief Fetch a variable on the stack according to its identification number - * \brief This is faster than comparing names - * \param [in] ident Identifier of a variable - * \param [in] bUpdate Not used. Probably need to be removed - * \param [in] bModif Not used. Probably need to be removed - * \return Found variable - */ - CBotVar* FindVar(long ident, bool bUpdate = false, - bool bModif = false); - - /** - * \brief Find variable by its token and returns a copy of it. - * \param Token Token upon which search is performed - * \param bUpdate Not used. - * \return Found variable, NULL if not found - */ - CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); - - - CBotStack* AddStack(CBotInstr* instr = NULL, bool bBlock = false); // tend le stack - CBotStack* AddStackEOX(CBotCall* instr = NULL, bool bBlock = false); // tend le stack - CBotStack* RestoreStack(CBotInstr* instr = NULL); - CBotStack* RestoreStackEOX(CBotCall* instr = NULL); - - CBotStack* AddStack2(bool bBlock = false); // tend le stack - bool Return(CBotStack* pFils); // transmet le rsultat au dessus - bool ReturnKeep(CBotStack* pFils); // transmet le rsultat sans rduire la pile - bool BreakReturn(CBotStack* pfils, const char* name = NULL); - // en cas de break ventuel - bool IfContinue(int state, const char* name); - // ou de "continue" - - bool IsOk(); - - bool SetState(int n, int lim = -10); // slectionne un tat - int GivState(); // dans quel tat j're ? - bool IncState(int lim = -10); // passe l'tat suivant - bool IfStep(); // faire du pas pas ? - bool Execute(); - - void SetVar( CBotVar* var ); - void SetCopyVar( CBotVar* var ); - CBotVar* GivVar(); - CBotVar* GivCopyVar(); - CBotVar* GivPtVar(); - bool GivRetVar(bool bRet); - long GivVal(); - - void SetStartError(int pos); - void SetError(int n, CBotToken* token = NULL); - void SetPosError(CBotToken* token); - void ResetError(int n, int start, int end); - void SetBreak(int val, const char* name); - - void SetBotCall(CBotProgram* p); - CBotProgram* GivBotCall(bool bFirst = false); - void* GivPUser(); - bool GivBlock(); - - - bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); - void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); - - bool SaveState(FILE* pf); - bool RestoreState(FILE* pf, CBotStack* &pStack); - - static - void SetTimer(int n); - - void GetRunPos(const char* &FunctionName, int &start, int &end); - CBotVar* GivStackVars(const char* &FunctionName, int level); - - int m_temp; - -private: - CBotStack* m_next; - CBotStack* m_next2; - CBotStack* m_prev; - friend class CBotInstArray; - -#ifdef _DEBUG - int m_index; -#endif - int m_state; - int m_step; - static int m_error; - static int m_start; - static int m_end; - static - CBotVar* m_retvar; // rsultat d'un return - - CBotVar* m_var; // rsultat des oprations - CBotVar* m_listVar; // les variables dclares ce niveau - - bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) - bool m_bOver; // limites de la pile ? -// bool m_bDontDelete; // spcial, ne pas dtruire les variables au delete - CBotProgram* m_prog; // les fonctions dfinies par user - - static - int m_initimer; - static - int m_timer; - static - CBotString m_labelBreak; - static - void* m_pUser; - - CBotInstr* m_instr; // l'instruction correspondante - bool m_bFunc; // une entre d'une fonction ? - CBotCall* m_call; // point de reprise dans un call extern - friend class CBotTry; -}; - -// les routines inline doivent tre dclares dans le fichier .h - -inline bool CBotStack::IsOk() -{ - return (m_error == 0); -} - -inline int CBotStack::GivState() -{ - return m_state; -} - -inline int CBotStack::GivError() -{ - return m_error; -} - -//////////////////////////////////////////////////////////////////////// -// Gestion de la pile de compilation -//////////////////////////////////////////////////////////////////////// - - -class CBotCStack -{ -private: - CBotCStack* m_next; - CBotCStack* m_prev; - - static - int m_error; - static - int m_end; - int m_start; - - CBotVar* m_var; // rsultat des oprations - - bool m_bBlock; // fait partie d'un bloc (variables sont locales ce bloc) - CBotVar* m_listVar; - - static - CBotProgram* m_prog; // liste des fonctions compiles - static - CBotTypResult m_retTyp; -// static -// CBotToken* m_retClass; - -public: - CBotCStack(CBotCStack* ppapa); - ~CBotCStack(); - - bool IsOk(); - int GivError(); - int GivError(int& start, int& end); - // rend le numro d'erreur retourn - - void SetType(CBotTypResult& type);// dtermine le type - CBotTypResult GivTypResult(int mode = 0); // donne le type de valeur sur le stack - int GivType(int mode = 0); // donne le type de valeur sur le stack - CBotClass* GivClass(); // donne la classe de la valeur sur le stack - - void AddVar(CBotVar* p); // ajoute une variable locale - CBotVar* FindVar(CBotToken* &p); // trouve une variable - CBotVar* FindVar(CBotToken& Token); - bool CheckVarLocal(CBotToken* &pToken); - CBotVar* CopyVar(CBotToken& Token); // trouve et rend une copie - - CBotCStack* TokenStack(CBotToken* pToken = NULL, bool bBlock = false); - CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); // transmet le rsultat au dessus - CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); // transmet le rsultat au dessus - - void SetVar( CBotVar* var ); - void SetCopyVar( CBotVar* var ); - CBotVar* GivVar(); - - void SetStartError(int pos); - void SetError(int n, int pos); - void SetError(int n, CBotToken* p); - void ResetError(int n, int start, int end); - - void SetRetType(CBotTypResult& type); - CBotTypResult GivRetType(); - -// void SetBotCall(CBotFunction* &pFunc); - void SetBotCall(CBotProgram* p); - CBotProgram* GivBotCall(); - CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); - bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); - - bool NextToken(CBotToken* &p); -}; - - -extern bool SaveVar(FILE* pf, CBotVar* pVar); - - -///////////////////////////////////////////////////////////////////// -// classes defining an instruction -class CBotInstr -{ -private: - static - CBotStringArray - m_labelLvl; -protected: - CBotToken m_token; // keeps the token - CBotString name; // debug - CBotInstr* m_next; // linked command - CBotInstr* m_next2b; // second list definition chain - CBotInstr* m_next3; // third list for indices and fields - CBotInstr* m_next3b; // necessary for reporting tables -/* - for example, the following program - int x[]; x[1] = 4; - int y[x[1]][10], z; - is generated - CBotInstrArray - m_next3b-> CBotEmpty - m_next-> - CBotExpression .... - m_next-> - CBotInstrArray - m_next3b-> CBotExpression ('x') ( m_next3-> CBotIndexExpr ('1') ) - m_next3b-> CBotExpression ('10') - m_next2-> 'z' - m_next->... - -*/ - - static - int m_LoopLvl; - friend class CBotClassInst; - friend class CBotInt; - friend class CBotListArray; - -public: - CBotInstr(); - virtual - ~CBotInstr(); - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - static - CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first = true); - - virtual - bool Execute(CBotStack* &pj); - virtual - bool Execute(CBotStack* &pj, CBotVar* pVar); - virtual - void RestoreState(CBotStack* &pj, bool bMain); - - virtual - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - virtual - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - virtual - void RestoreStateVar(CBotStack* &pile, bool bMain); - - virtual - bool CompCase(CBotStack* &pj, int val); - - void SetToken(CBotToken* p); - int GivTokenType(); - CBotToken* GivToken(); - - void AddNext(CBotInstr* n); - CBotInstr* GivNext(); - void AddNext3(CBotInstr* n); - CBotInstr* GivNext3(); - void AddNext3b(CBotInstr* n); - CBotInstr* GivNext3b(); - - static - void IncLvl(CBotString& label); - static - void IncLvl(); - static - void DecLvl(); - static - bool ChkLvl(const CBotString& label, int type); - - bool IsOfClass(CBotString name); -}; - -class CBotWhile : public CBotInstr -{ -private: - CBotInstr* m_Condition; // la condition - CBotInstr* m_Block; // les instructions - CBotString m_label; // une tiquette s'il y a - -public: - CBotWhile(); - ~CBotWhile(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotRepeat : public CBotInstr -{ -private: - /// Number of iterations - CBotInstr* m_NbIter; - - /// Instructions - CBotInstr* m_Block; - - /// Label - CBotString m_label; // une tiquette s'il y a - -public: - CBotRepeat(); - ~CBotRepeat(); - - /// Static method used for compilation - static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - - /// Execute - bool Execute(CBotStack* &pj); - - /// Restore state - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotDo : public CBotInstr -{ -private: - CBotInstr* m_Block; // les instructions - CBotInstr* m_Condition; // la condition - CBotString m_label; // une tiquette s'il y a - -public: - CBotDo(); - ~CBotDo(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotFor : public CBotInstr -{ -private: - CBotInstr* m_Init; // intruction initiale - CBotInstr* m_Test; // la condition de test - CBotInstr* m_Incr; // instruction pour l'incrment - CBotInstr* m_Block; // les instructions - CBotString m_label; // une tiquette s'il y a - -public: - CBotFor(); - ~CBotFor(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotBreak : public CBotInstr -{ -private: - CBotString m_label; // une tiquette s'il y a - -public: - CBotBreak(); - ~CBotBreak(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotReturn : public CBotInstr -{ -private: - CBotInstr* m_Instr; // le paramtre retourner - -public: - CBotReturn(); - ~CBotReturn(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotSwitch : public CBotInstr -{ -private: - CBotInstr* m_Value; // value chercher - CBotInstr* m_Block; // les instructions - -public: - CBotSwitch(); - ~CBotSwitch(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotCase : public CBotInstr -{ -private: - CBotInstr* m_Value; // valeur comparer - -public: - CBotCase(); - ~CBotCase(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - bool CompCase(CBotStack* &pj, int val); -}; - -class CBotCatch : public CBotInstr -{ -private: - CBotInstr* m_Block; // les instructions - CBotInstr* m_Cond; // la condition - CBotCatch* m_next; // le catch suivant - friend class CBotTry; - -public: - CBotCatch(); - ~CBotCatch(); - static - CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); - bool TestCatch(CBotStack* &pj, int val); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - void RestoreCondState(CBotStack* &pj, bool bMain); -}; - -class CBotTry : public CBotInstr -{ -private: - CBotInstr* m_Block; // les instructions - CBotCatch* m_ListCatch; // les catches - CBotInstr* m_FinalInst; // instruction finale - -public: - CBotTry(); - ~CBotTry(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotThrow : public CBotInstr -{ -private: - CBotInstr* m_Value; // la valeur envoyer - -public: - CBotThrow(); - ~CBotThrow(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotStartDebugDD : public CBotInstr -{ -private: - -public: - CBotStartDebugDD(); - ~CBotStartDebugDD(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); -}; - - -class CBotIf : public CBotInstr -{ -private: - CBotInstr* m_Condition; // la condition - CBotInstr* m_Block; // les instructions - CBotInstr* m_BlockElse; // les instructions - -public: - CBotIf(); - ~CBotIf(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -// dfinition d'un nombre entier - -class CBotInt : public CBotInstr -{ -private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a -/// CBotInstr* m_next; // plusieurs dfinitions enchanes - -public: - CBotInt(); - ~CBotInt(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// dfinition d'un tableau - -class CBotInstArray : public CBotInstr -{ -private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_listass; // liste d'assignations pour le tableau - CBotTypResult - m_typevar; // type d'lments -// CBotString m_ClassName; - -public: - CBotInstArray(); - ~CBotInstArray(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -// dfinition d'une liste d'assignation pour un tableau -// int [ ] a [ ] = ( ( 1, 2, 3 ) , ( 3, 2, 1 ) ) ; - -class CBotListArray : public CBotInstr -{ -private: - CBotInstr* m_expr; // expression pour un lment - // les autres sont chans avec CBotInstr::m_next3; -public: - CBotListArray(); - ~CBotListArray(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - bool Execute(CBotStack* &pj, CBotVar* pVar); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotEmpty : public CBotInstr -{ - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// dfinition d'un boolen - -class CBotBoolean : public CBotInstr -{ -private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a - -public: - CBotBoolean(); - ~CBotBoolean(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -// dfinition d'un nombre rel - -class CBotFloat : public CBotInstr -{ -private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a - -public: - CBotFloat(); - ~CBotFloat(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// dfinition d'un elment string - -class CBotIString : public CBotInstr -{ -private: - CBotInstr* m_var; // la variable initialiser - CBotInstr* m_expr; // la valeur mettre, s'il y a - -public: - CBotIString(); - ~CBotIString(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// dfinition d'un elment dans une classe quelconque - -class CBotClassInst : public CBotInstr -{ -private: - CBotInstr* m_var; // la variable initialiser - CBotClass* m_pClass; // rfrence la classe - CBotInstr* m_Parameters; // les paramtres valuer pour le constructeur - CBotInstr* m_expr; // la valeur mettre, s'il y a - bool m_hasParams; // il y a des paramtres ? - long m_nMethodeIdent; - -public: - CBotClassInst(); - ~CBotClassInst(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = NULL); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotCondition : public CBotInstr -{ -private: - -public: - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); -}; - - -// left oprande -// n'accepte que les expressions pouvant tre gauche d'une assignation - -class CBotLeftExpr : public CBotInstr -{ -private: - long m_nIdent; - -public: - CBotLeftExpr(); - ~CBotLeftExpr(); - static - CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack, CBotStack* array); - - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); - void RestoreStateVar(CBotStack* &pile, bool bMain); -}; - - -// gestion des champs d'une instance - -class CBotFieldExpr : public CBotInstr -{ -private: - friend class CBotExpression; - int m_nIdent; - -public: - CBotFieldExpr(); - ~CBotFieldExpr(); - void SetUniqNum(int num); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - void RestoreStateVar(CBotStack* &pj, bool bMain); -}; - -// gestion des index dans les tableaux - -class CBotIndexExpr : public CBotInstr -{ -private: - CBotInstr* m_expr; // expression pour le calcul de l'index - friend class CBotLeftExpr; - friend class CBotExprVar; - -public: - CBotIndexExpr(); - ~CBotIndexExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - void RestoreStateVar(CBotStack* &pj, bool bMain); -}; - -// une expression du genre -// x = a; -// x * y + 3; - -class CBotExpression : public CBotInstr -{ -private: - CBotLeftExpr* m_leftop; // lment de gauche - CBotInstr* m_rightop; // lment de droite - -public: - CBotExpression(); - ~CBotExpression(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotListExpression : public CBotInstr -{ -private: - CBotInstr* m_Expr; // la 1re expression valuer - -public: - CBotListExpression(); - ~CBotListExpression(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotLogicExpr : public CBotInstr -{ -private: - CBotInstr* m_condition; // test valuer - CBotInstr* m_op1; // lment de gauche - CBotInstr* m_op2; // lment de droite - friend class CBotTwoOpExpr; - -public: - CBotLogicExpr(); - ~CBotLogicExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - - -class CBotBoolExpr : public CBotInstr -{ -private: - -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); -}; - - - -// une expression ventuellement entre parenthses ( ... ) -// il n'y a jamais d'instance de cette classe -// l'objet retourn tant le contenu de la parenthse -class CBotParExpr : public CBotInstr -{ -private: - -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); -}; - -// expression unaire -class CBotExprUnaire : public CBotInstr -{ -private: - CBotInstr* m_Expr; // l'expression valuer -public: - CBotExprUnaire(); - ~CBotExprUnaire(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// toutes les oprations 2 oprandes - -class CBotTwoOpExpr : public CBotInstr -{ -private: - CBotInstr* m_leftop; // lment de gauche - CBotInstr* m_rightop; // lment de droite -public: - CBotTwoOpExpr(); - ~CBotTwoOpExpr(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = NULL); - bool Execute(CBotStack* &pStack); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - - - -// un bloc d'instructions { .... } -class CBotBlock : public CBotInstr -{ -private: - -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - static - CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); -}; - - -// le contenu d'un bloc d'instructions ... ; ... ; ... ; ... ; -class CBotListInstr : public CBotInstr -{ -private: - CBotInstr* m_Instr; // les instructions faire - -public: - CBotListInstr(); - ~CBotListInstr(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotInstrCall : public CBotInstr -{ -private: - CBotInstr* m_Parameters; // les paramtres valuer -// int m_typeRes; // type du rsultat -// CBotString m_RetClassName; // class du rsultat - CBotTypResult - m_typRes; // type complet du rsultat - long m_nFuncIdent; // id de la fonction - -public: - CBotInstrCall(); - ~CBotInstrCall(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// un appel d'une mthode - -class CBotInstrMethode : public CBotInstr -{ -private: - CBotInstr* m_Parameters; // les paramtres valuer -// int m_typeRes; // type du rsultat -// CBotString m_RetClassName; // class du rsultat - CBotTypResult - m_typRes; // type complet du rsultat - - CBotString m_NomMethod; // nom de la mthode - long m_MethodeIdent; // identificateur de la mthode -// long m_nThisIdent; // identificateur pour "this" - CBotString m_ClassName; // nom de la classe - -public: - CBotInstrMethode(); - ~CBotInstrMethode(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); - bool Execute(CBotStack* &pj); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend); - void RestoreStateVar(CBotStack* &pj, bool bMain); -}; - -// expression for the variable name - -class CBotExprVar : public CBotInstr -{ -private: - long m_nIdent; - friend class CBotPostIncExpr; - friend class CBotPreIncExpr; - -public: - CBotExprVar(); - ~CBotExprVar(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); - static - CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); - - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); - bool Execute2Var(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep); - void RestoreStateVar(CBotStack* &pj, bool bMain); -}; - -class CBotPostIncExpr : public CBotInstr -{ -private: - CBotInstr* m_Instr; - friend class CBotParExpr; - -public: - CBotPostIncExpr(); - ~CBotPostIncExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotPreIncExpr : public CBotInstr -{ -private: - CBotInstr* m_Instr; - friend class CBotParExpr; - -public: - CBotPreIncExpr(); - ~CBotPreIncExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotLeftExprVar : public CBotInstr -{ -private: -public: - CBotTypResult - m_typevar; // type de variable dclare - long m_nIdent; // identificateur unique pour cette variable - -public: - CBotLeftExprVar(); - ~CBotLeftExprVar(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotExprBool : public CBotInstr -{ -private: - -public: - CBotExprBool(); - ~CBotExprBool(); - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -class CBotExprNull : public CBotInstr -{ -private: - -public: - CBotExprNull(); - ~CBotExprNull(); - - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotExprNan : public CBotInstr -{ -private: - -public: - CBotExprNan(); - ~CBotExprNan(); - - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -class CBotNew : public CBotInstr -{ -private: - CBotInstr* m_Parameters; // les paramtres valuer - long m_nMethodeIdent; -// long m_nThisIdent; - CBotToken m_vartoken; - -public: - CBotNew(); - ~CBotNew(); - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - -// expression reprsentant un nombre - -class CBotExprNum : public CBotInstr -{ -private: - int m_numtype; // et le type de nombre - long m_valint; // valeur pour un int - float m_valfloat; // valeur pour un float - -public: - CBotExprNum(); - ~CBotExprNum(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - - -// expression reprsentant une chaine de caractres - -class CBotExprAlpha : public CBotInstr -{ -private: - -public: - CBotExprAlpha(); - ~CBotExprAlpha(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); -}; - - -#define MAX(a,b) ((a>b) ? a : b) - - -// classe pour la gestion des nombres entier (int) -class CBotVarInt : public CBotVar -{ -private: - int m_val; // la valeur - CBotString m_defnum; // le nom si donn par DefineNum - friend class CBotVar; - -public: - CBotVarInt( const CBotToken* name ); -// ~CBotVarInt(); - - void SetValInt(int val, const char* s = NULL); - void SetValFloat(float val); - int GivValInt(); - float GivValFloat(); - CBotString GivValString(); - - void Copy(CBotVar* pSrc, bool bName=true); - - - void Add(CBotVar* left, CBotVar* right); // addition - void Sub(CBotVar* left, CBotVar* right); // soustraction - void Mul(CBotVar* left, CBotVar* right); // multiplication - int Div(CBotVar* left, CBotVar* right); // division - int Modulo(CBotVar* left, CBotVar* right); // reste de division - void Power(CBotVar* left, CBotVar* right); // puissance - - bool Lo(CBotVar* left, CBotVar* right); - bool Hi(CBotVar* left, CBotVar* right); - bool Ls(CBotVar* left, CBotVar* right); - bool Hs(CBotVar* left, CBotVar* right); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); - - void XOr(CBotVar* left, CBotVar* right); - void Or(CBotVar* left, CBotVar* right); - void And(CBotVar* left, CBotVar* right); - - void SL(CBotVar* left, CBotVar* right); - void SR(CBotVar* left, CBotVar* right); - void ASR(CBotVar* left, CBotVar* right); - - void Neg(); - void Not(); - void Inc(); - void Dec(); - - bool Save0State(FILE* pf); - bool Save1State(FILE* pf); - -}; - -// classe pour la gestion des nombres rels (float) -class CBotVarFloat : public CBotVar -{ -private: - float m_val; // la valeur - -public: - CBotVarFloat( const CBotToken* name ); -// ~CBotVarFloat(); - - void SetValInt(int val, const char* s = NULL); - void SetValFloat(float val); - int GivValInt(); - float GivValFloat(); - CBotString GivValString(); - - void Copy(CBotVar* pSrc, bool bName=true); - - - void Add(CBotVar* left, CBotVar* right); // addition - void Sub(CBotVar* left, CBotVar* right); // soustraction - void Mul(CBotVar* left, CBotVar* right); // multiplication - int Div(CBotVar* left, CBotVar* right); // division - int Modulo(CBotVar* left, CBotVar* right); // reste de division - void Power(CBotVar* left, CBotVar* right); // puissance - - bool Lo(CBotVar* left, CBotVar* right); - bool Hi(CBotVar* left, CBotVar* right); - bool Ls(CBotVar* left, CBotVar* right); - bool Hs(CBotVar* left, CBotVar* right); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); - - void Neg(); - void Inc(); - void Dec(); - - bool Save1State(FILE* pf); -}; - - -// classe pour la gestion des chanes (String) -class CBotVarString : public CBotVar -{ -private: - CBotString m_val; // la valeur - -public: - CBotVarString( const CBotToken* name ); -// ~CBotVarString(); - - void SetValString(const char* p); - CBotString GivValString(); - - void Copy(CBotVar* pSrc, bool bName=true); - - void Add(CBotVar* left, CBotVar* right); // addition - - bool Lo(CBotVar* left, CBotVar* right); - bool Hi(CBotVar* left, CBotVar* right); - bool Ls(CBotVar* left, CBotVar* right); - bool Hs(CBotVar* left, CBotVar* right); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); - - bool Save1State(FILE* pf); -}; - -// classe pour la gestion des boolean -class CBotVarBoolean : public CBotVar -{ -private: - bool m_val; // la valeur - -public: - CBotVarBoolean( const CBotToken* name ); -// ~CBotVarBoolean(); - - void SetValInt(int val, const char* s = NULL); - void SetValFloat(float val); - int GivValInt(); - float GivValFloat(); - CBotString GivValString(); - - void Copy(CBotVar* pSrc, bool bName=true); - - void And(CBotVar* left, CBotVar* right); - void Or(CBotVar* left, CBotVar* right); - void XOr(CBotVar* left, CBotVar* right); - void Not(); - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); - - bool Save1State(FILE* pf); -}; - - -// classe pour la gestion des instances de classe -class CBotVarClass : public CBotVar -{ -private: - static - CBotVarClass* m_ExClass; // liste des instances existantes un moment donn - CBotVarClass* m_ExNext; // pour cette liste gnrale - CBotVarClass* m_ExPrev; // pour cette liste gnrale - -private: - CBotClass* m_pClass; // la dfinition de la classe - CBotVarClass* m_pParent; // l'instance dans la classe parent - CBotVar* m_pVar; // contenu - friend class CBotVar; // mon papa est un copain - friend class CBotVarPointer; // et le pointeur aussi - int m_CptUse; // compteur d'utilisation - long m_ItemIdent; // identificateur (unique) de l'instance - bool m_bConstructor; // set si un constructeur a t appel - -public: - CBotVarClass( const CBotToken* name, const CBotTypResult& type ); -// CBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - ~CBotVarClass(); -// void InitCBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - - void Copy(CBotVar* pSrc, bool bName=true); - void SetClass(CBotClass* pClass); //, int &nIdent); - CBotClass* GivClass(); - CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) - CBotVar* GivItemRef(int nIdent); - - CBotVar* GivItem(int n, bool bExtend); - CBotVar* GivItemList(); - - CBotString GivValString(); - - bool Save1State(FILE* pf); - void Maj(void* pUser, bool bContinue); - - void IncrementUse(); // une rfrence en plus - void DecrementUse(); // une rfrence en moins - - CBotVarClass* - GivPointer(); - void SetItemList(CBotVar* pVar); - - void SetIdent(long n); - - static CBotVarClass* Find(long id); - - -// CBotVar* GivMyThis(); - - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); - - void ConstructorSet(); -}; - - -// classe pour la gestion des pointeurs une instances de classe -class CBotVarPointer : public CBotVar -{ -private: - CBotVarClass* - m_pVarClass; // contenu - CBotClass* m_pClass; // la classe prvue pour ce pointeur - friend class CBotVar; // mon papa est un copain - -public: - CBotVarPointer( const CBotToken* name, CBotTypResult& type ); - ~CBotVarPointer(); - - void Copy(CBotVar* pSrc, bool bName=true); - void SetClass(CBotClass* pClass); - CBotClass* GivClass(); - CBotVar* GivItem(const char* name); // rend un lment d'une classe selon son nom (*) - CBotVar* GivItemRef(int nIdent); - CBotVar* GivItemList(); - - CBotString GivValString(); - void SetPointer(CBotVar* p); - CBotVarClass* - GivPointer(); - - void SetIdent(long n); // associe un numro d'identification (unique) - long GivIdent(); // donne le numro d'identification associ - void ConstructorSet(); - - bool Save1State(FILE* pf); - void Maj(void* pUser, bool bContinue); - - bool Eq(CBotVar* left, CBotVar* right); - bool Ne(CBotVar* left, CBotVar* right); -}; - - -// classe pour les tableaux - -#define MAXARRAYSIZE 9999 - -class CBotVarArray : public CBotVar -{ -private: - CBotVarClass* - m_pInstance; // instance grant le tableau - - friend class CBotVar; // papa est un copain - -public: - CBotVarArray( const CBotToken* name, CBotTypResult& type ); - ~CBotVarArray(); - - void SetPointer(CBotVar* p); - CBotVarClass* - GivPointer(); - - void Copy(CBotVar* pSrc, bool bName=true); - CBotVar* GivItem(int n, bool bGrow=false); // rend un lment selon son index numrique - // agrandi le tableau si ncessaire si bExtend -// CBotVar* GivItem(const char* name); // rend un lment selon son index litral - CBotVar* GivItemList(); // donne le premier lment de la liste - - CBotString GivValString(); // donne le contenu du tableau dans une chane - - bool Save1State(FILE* pf); -}; - - -extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); - -extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); -extern bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ); - -extern bool WriteWord(FILE* pf, unsigned short w); -extern bool ReadWord(FILE* pf, unsigned short& w); -extern bool ReadLong(FILE* pf, long& w); -extern bool WriteFloat(FILE* pf, float w); -extern bool WriteLong(FILE* pf, long w); -extern bool ReadFloat(FILE* pf, float& w); -extern bool WriteString(FILE* pf, CBotString s); -extern bool ReadString(FILE* pf, CBotString& s); -extern bool WriteType(FILE* pf, CBotTypResult type); -extern bool ReadType(FILE* pf, CBotTypResult& type); - -extern float GivNumFloat( const char* p ); - -#if false -extern void DEBUG( const char* text, int val, CBotStack* pile ); -#endif - -/////////////////////////////////////////// -// classe pour les appels de routines (externes) - -class CBotCall -{ -private: - static - CBotCall* m_ListCalls; - static - void* m_pUser; - long m_nFuncIdent; - -private: - CBotString m_name; - bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; - CBotTypResult - (*m_rComp) (CBotVar* &pVar, void* pUser) ; - CBotCall* m_next; - -public: - CBotCall(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - ~CBotCall(); - - static - bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - - static - CBotTypResult - CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); - static - bool CheckCall(const char* name); - -// static -// int DoCall(CBotToken* &p, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); - static - int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); -#if STACKRUN - bool Run(CBotStack* pStack); - static - bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); -#endif - - CBotString GivName(); - CBotCall* Next(); - - static void SetPUser(void* pUser); - static void Free(); -}; - -// classe grant les mthodes dclares par AddFunction sur une classe - -class CBotCallMethode -{ -private: - CBotString m_name; - bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception); - CBotTypResult - (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); - CBotCallMethode* m_next; - friend class CBotClass; - long m_nFuncIdent; - -public: - CBotCallMethode(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - ~CBotCallMethode(); - - CBotTypResult - CompileCall(const char* name, CBotVar* pThis, - CBotVar** ppVars, CBotCStack* pStack, - long& nIdent); - - int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pFunc); - - CBotString GivName(); - CBotCallMethode* Next(); - void AddNext(CBotCallMethode* p); - -}; - -// une liste de paramtres - -class CBotDefParam -{ -private: - CBotToken m_token; // nom du paramtre - CBotString m_typename; // nom du type - CBotTypResult m_type; // type de paramtre - CBotDefParam* m_next; // paramtre suivant - long m_nIdent; - -public: - CBotDefParam(); - ~CBotDefParam(); - static - CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotVar** ppVars, CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - - void AddNext(CBotDefParam* p); - int GivType(); - CBotTypResult GivTypResult(); - CBotDefParam* GivNext(); - - CBotString GivParamString(); -}; - - -// une dclaration de fonction - -class CBotFunction : CBotInstr -{ -private: - // gestion d'une liste (static) de fonctions publiques - static - CBotFunction* m_listPublic; - CBotFunction* m_nextpublic; - CBotFunction* m_prevpublic; - friend class CBotCStack; -// long m_nThisIdent; - long m_nFuncIdent; - bool m_bSynchro; // mthode synchronise ? - -private: - CBotDefParam* m_Param; // liste des paramtres - CBotInstr* m_Block; // le bloc d'instructions - CBotFunction* m_next; - CBotToken m_retToken; // si retourne un CBotTypClass - CBotTypResult m_retTyp; // type complet du rsultat - - bool m_bPublic; // fonction publique - bool m_bExtern; // fonction extern - CBotString m_MasterClass; // nom de la classe qu'on drive - CBotProgram* m_pProg; - friend class CBotProgram; - friend class CBotClass; - - CBotToken m_extern; // pour la position du mot "extern" - CBotToken m_openpar; - CBotToken m_closepar; - CBotToken m_openblk; - CBotToken m_closeblk; -public: - CBotFunction(); - ~CBotFunction(); - static - CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, bool bLocal = true); - static - CBotFunction* Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass); - bool Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); - void RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); - - void AddNext(CBotFunction* p); - CBotTypResult CompileCall(const char* name, CBotVar** ppVars, long& nIdent); - CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic = true); - - int DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken); - void RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack); - int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass); - void RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass); - bool CheckParam(CBotDefParam* pParam); - - static - void AddPublic(CBotFunction* pfunc); - - CBotString GivName(); - CBotString GivParams(); - bool IsPublic(); - bool IsExtern(); - CBotFunction* Next(); - - bool GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); -}; - - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. +//////////////////////////////////////////////////////////////////// +/** + * \file CBot.h + * \brief Interpreter of the language CBot for COLOBOT game + */ + +#pragma once + +#include "resource.h" +#include "CBotDll.h" // public definitions +#include "CBotToken.h" // token management + +#define STACKRUN true /// \def return execution directly on a suspended routine +#define STACKMEM true /// \def preserve memory for the execution stack +#define MAXSTACK 990 /// \def stack size reserved + +#define EOX (CBotStack*)-1 /// \def tag special condition + + +// fix for MSVC instruction __asm int 3 (setting a trap) +#if defined(__MINGW32__) || defined(__GNUC__) +#define ASM_TRAP() asm("int $3"); +#else +#define ASM_TRAP() __asm int 3; +#endif + +///////////////////////////////////////////////////////////////////// +// forward declaration + +class CBotCompExpr; // an expression like + // () <= () +class CBotAddExpr; // an expression like + // () + () +class CBotParExpr; // basic type or instruction in parenthesis + // Toto.truc + // 12.5 + // "string" + // ( expression ) +class CBotExprVar; // a variable name as + // Toto + // chose.truc.machin +class CBotWhile; // while (...) {...}; +class CBotIf; // if (...) {...} else {...} +class CBotDefParam; // paramerer list of a function +class CBotRepeat; // repeat (nb) {...} + + + +//////////////////////////////////////////////////////////////////////// +// Management of the execution stack +//////////////////////////////////////////////////////////////////////// + +// actually, externally, the only thing it can do +// is to create an instance of a stack +// to use for routine CBotProgram :: Execute (CBotStack) + + +/**\class CBotStack + * \brief Management of the execution stack. + * \brief Actually the only thing it can do is to create an instance of a stack + * \brief to use for routine CBotProgram :: Execute(CBotStack)*/ +class CBotStack +{ +public: +#if STACKMEM + /** + * \brief FirstStack Allocate first stack + * \return pointer to created stack + */ + static CBotStack * FirstStack(); + + /** \brief Delete Remove current stack */ + void Delete(); +#endif + + /** + * \brief CBotStack Constructor of the stack + * \param ppapa Not used. + */ + CBotStack(CBotStack* ppapa); + + + /** \brief ~CBotStack Destructor */ + ~CBotStack(); + + /** + * \brief StackOver Check if end of stack is reached + * \return true if end of stack + */ + bool StackOver(); + + /** + * \brief GivError Get error number of the stack + * \param [out] start beginning of the stack + * \param [out] end end of stack + * \return error number + */ + int GivError(int& start, int& end); + + /** + * \brief GivError Get error number + * \return eror number + */ + int GivError();// rend le numéro d'erreur retourné + + /** + * \brief Reset Reset error at and set user + * \param [in] pUser User of stack + */ + void Reset(void* pUser); + + /** + * \brief SetType Determines the type. + * \param type Type of instruction on the stack. + */ + void SetType(CBotTypResult& type); + + /** + * \brief GivType Get the type of value on the stack. + * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). + * \return Type number. + */ + int GivType(int mode = 0); + + /** + * \brief Gives the type of complete value on the stack. + * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). + * \return Type of an element. + */ + CBotTypResult GivTypResult(int mode = 0); + + /** + * \brief Adds a local variable. + * \param [in] p Variable to be added. + */ + void AddVar(CBotVar* p); + + /** + * \brief Fetch a variable by its token. + * \brief This may be a composite variable + * \param [in] pToken Token upon which search is performed + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Fetch a variable by its token. + * \brief This may be a composite variable + * \param [in] pToken Token upon which search is performed + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(CBotToken& Token, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Fetch variable by its name + * \param [in] name Name of variable to find + * \return Found variable + */ + CBotVar* FindVar(const char* name); + + /** + * \brief Fetch a variable on the stack according to its identification number + * \brief This is faster than comparing names + * \param [in] ident Identifier of a variable + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(long ident, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Find variable by its token and returns a copy of it. + * \param Token Token upon which search is performed + * \param bUpdate Not used. + * \return Found variable, NULL if not found + */ + CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); + + + CBotStack* AddStack(CBotInstr* instr = NULL, bool bBlock = false); // extends the stack + CBotStack* AddStackEOX(CBotCall* instr = NULL, bool bBlock = false); // extends the stack + CBotStack* RestoreStack(CBotInstr* instr = NULL); + CBotStack* RestoreStackEOX(CBotCall* instr = NULL); + + CBotStack* AddStack2(bool bBlock = false); // extends the stack + bool Return(CBotStack* pFils); // transmits the result over + bool ReturnKeep(CBotStack* pFils); // transmits the result without reducing the stack + bool BreakReturn(CBotStack* pfils, const char* name = NULL); + // in case of eventual break + bool IfContinue(int state, const char* name); + // or "continue" + + bool IsOk(); + + bool SetState(int n, int lim = -10); // select a state + int GivState(); // in what state am I? + bool IncState(int lim = -10); // passes to the next state + bool IfStep(); // do step by step + bool Execute(); + + void SetVar( CBotVar* var ); + void SetCopyVar( CBotVar* var ); + CBotVar* GivVar(); + CBotVar* GivCopyVar(); + CBotVar* GivPtVar(); + bool GivRetVar(bool bRet); + long GivVal(); + + void SetStartError(int pos); + void SetError(int n, CBotToken* token = NULL); + void SetPosError(CBotToken* token); + void ResetError(int n, int start, int end); + void SetBreak(int val, const char* name); + + void SetBotCall(CBotProgram* p); + CBotProgram* GivBotCall(bool bFirst = false); + void* GivPUser(); + bool GivBlock(); + + + bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); + void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); + + bool SaveState(FILE* pf); + bool RestoreState(FILE* pf, CBotStack* &pStack); + + static + void SetTimer(int n); + + void GetRunPos(const char* &FunctionName, int &start, int &end); + CBotVar* GivStackVars(const char* &FunctionName, int level); + + int m_temp; + +private: + CBotStack* m_next; + CBotStack* m_next2; + CBotStack* m_prev; + friend class CBotInstArray; + +#ifdef _DEBUG + int m_index; +#endif + int m_state; + int m_step; + static int m_error; + static int m_start; + static int m_end; + static + CBotVar* m_retvar; // result of a return + + CBotVar* m_var; // result of the operations + CBotVar* m_listVar; // variables declared at this level + + bool m_bBlock; // is part of a block (variables are local to this block) + bool m_bOver; // stack limits? +// bool m_bDontDelete; // special, not to destroy the variable during delete + CBotProgram* m_prog; // user-defined functions + + static + int m_initimer; + static + int m_timer; + static + CBotString m_labelBreak; + static + void* m_pUser; + + CBotInstr* m_instr; // the corresponding instruction + bool m_bFunc; // an input of a function? + CBotCall* m_call; // recovery point in a extern call + friend class CBotTry; +}; + +// inline routinees must be declared in file.h + +inline bool CBotStack::IsOk() +{ + return (m_error == 0); +} + +inline int CBotStack::GivState() +{ + return m_state; +} + +inline int CBotStack::GivError() +{ + return m_error; +} + +//////////////////////////////////////////////////////////////////////// +// Management of the stack of compilation +//////////////////////////////////////////////////////////////////////// + + +class CBotCStack +{ +private: + CBotCStack* m_next; + CBotCStack* m_prev; + + static int m_error; + static int m_end; + int m_start; + + CBotVar* m_var; // result of the operations + + bool m_bBlock; // is part of a block (variables are local to this block) + CBotVar* m_listVar; + + static + CBotProgram* m_prog; // list of compiled functions + static + CBotTypResult m_retTyp; +// static +// CBotToken* m_retClass; + +public: + CBotCStack(CBotCStack* ppapa); + ~CBotCStack(); + + bool IsOk(); + int GivError(); + int GivError(int& start, int& end); + // gives error number + + void SetType(CBotTypResult& type);// determines the type + CBotTypResult GivTypResult(int mode = 0); // gives the type of value on the stack + int GivType(int mode = 0); // gives the type of value on the stack + CBotClass* GivClass(); // gives the class of the value on the stack + + void AddVar(CBotVar* p); // adds a local variable + CBotVar* FindVar(CBotToken* &p); // finds a variable + CBotVar* FindVar(CBotToken& Token); + bool CheckVarLocal(CBotToken* &pToken); + CBotVar* CopyVar(CBotToken& Token); // finds and makes a copy + + CBotCStack* TokenStack(CBotToken* pToken = NULL, bool bBlock = false); + CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); // transmits the result upper + CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); // transmits the result upper + + void SetVar( CBotVar* var ); + void SetCopyVar( CBotVar* var ); + CBotVar* GivVar(); + + void SetStartError(int pos); + void SetError(int n, int pos); + void SetError(int n, CBotToken* p); + void ResetError(int n, int start, int end); + + void SetRetType(CBotTypResult& type); + CBotTypResult GivRetType(); + +// void SetBotCall(CBotFunction* &pFunc); + void SetBotCall(CBotProgram* p); + CBotProgram* GivBotCall(); + CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + + bool NextToken(CBotToken* &p); +}; + + +extern bool SaveVar(FILE* pf, CBotVar* pVar); + + +///////////////////////////////////////////////////////////////////// +// class defining an instruction +class CBotInstr +{ +private: + static + CBotStringArray + m_labelLvl; +protected: + CBotToken m_token; // keeps the token + CBotString name; // debug + CBotInstr* m_next; // linked command + CBotInstr* m_next2b; // second list definition chain + CBotInstr* m_next3; // third list for indices and fields + CBotInstr* m_next3b; // necessary for reporting tables +/* + for example, the following program + int x[]; x[1] = 4; + int y[x[1]][10], z; + is generated + CBotInstrArray + m_next3b-> CBotEmpty + m_next-> + CBotExpression .... + m_next-> + CBotInstrArray + m_next3b-> CBotExpression ('x') ( m_next3-> CBotIndexExpr ('1') ) + m_next3b-> CBotExpression ('10') + m_next2-> 'z' + m_next->... + +*/ + + static + int m_LoopLvl; + friend class CBotClassInst; + friend class CBotInt; + friend class CBotListArray; + +public: + CBotInstr(); + virtual + ~CBotInstr(); + + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first = true); + + virtual + bool Execute(CBotStack* &pj); + virtual + bool Execute(CBotStack* &pj, CBotVar* pVar); + virtual + void RestoreState(CBotStack* &pj, bool bMain); + + virtual + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + virtual + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + virtual + void RestoreStateVar(CBotStack* &pile, bool bMain); + + virtual + bool CompCase(CBotStack* &pj, int val); + + void SetToken(CBotToken* p); + int GivTokenType(); + CBotToken* GivToken(); + + void AddNext(CBotInstr* n); + CBotInstr* GivNext(); + void AddNext3(CBotInstr* n); + CBotInstr* GivNext3(); + void AddNext3b(CBotInstr* n); + CBotInstr* GivNext3b(); + + static + void IncLvl(CBotString& label); + static + void IncLvl(); + static + void DecLvl(); + static + bool ChkLvl(const CBotString& label, int type); + + bool IsOfClass(CBotString name); +}; + +class CBotWhile : public CBotInstr +{ +private: + CBotInstr* m_Condition; // condition + CBotInstr* m_Block; // instructions + CBotString m_label; // a label if there is + +public: + CBotWhile(); + ~CBotWhile(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotRepeat : public CBotInstr +{ +private: + /// Number of iterations + CBotInstr* m_NbIter; + + /// Instructions + CBotInstr* m_Block; + + /// Label + CBotString m_label; // a label if there is + +public: + CBotRepeat(); + ~CBotRepeat(); + + /// Static method used for compilation + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /// Execute + bool Execute(CBotStack* &pj); + + /// Restore state + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotDo : public CBotInstr +{ +private: + CBotInstr* m_Block; // instruction + CBotInstr* m_Condition; // conditions + CBotString m_label; // a label if there is + +public: + CBotDo(); + ~CBotDo(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotFor : public CBotInstr +{ +private: + CBotInstr* m_Init; // initial intruction + CBotInstr* m_Test; // test condition + CBotInstr* m_Incr; // instruction for increment + CBotInstr* m_Block; // instructions + CBotString m_label; // a label if there is + +public: + CBotFor(); + ~CBotFor(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotBreak : public CBotInstr +{ +private: + CBotString m_label; // a label if there is + +public: + CBotBreak(); + ~CBotBreak(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotReturn : public CBotInstr +{ +private: + CBotInstr* m_Instr; // paramter of return + +public: + CBotReturn(); + ~CBotReturn(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotSwitch : public CBotInstr +{ +private: + CBotInstr* m_Value; // value to seek + CBotInstr* m_Block; // instructions + +public: + CBotSwitch(); + ~CBotSwitch(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotCase : public CBotInstr +{ +private: + CBotInstr* m_Value; // value to compare + +public: + CBotCase(); + ~CBotCase(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + bool CompCase(CBotStack* &pj, int val); +}; + +class CBotCatch : public CBotInstr +{ +private: + CBotInstr* m_Block; // instructions + CBotInstr* m_Cond; //condition + CBotCatch* m_next; //following catch + friend class CBotTry; + +public: + CBotCatch(); + ~CBotCatch(); + static + CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); + bool TestCatch(CBotStack* &pj, int val); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + void RestoreCondState(CBotStack* &pj, bool bMain); +}; + +class CBotTry : public CBotInstr +{ +private: + CBotInstr* m_Block; // instructions + CBotCatch* m_ListCatch; // catches + CBotInstr* m_FinalInst; // final instruction + +public: + CBotTry(); + ~CBotTry(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotThrow : public CBotInstr +{ +private: + CBotInstr* m_Value; // the value to send + +public: + CBotThrow(); + ~CBotThrow(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotStartDebugDD : public CBotInstr +{ +private: + +public: + CBotStartDebugDD(); + ~CBotStartDebugDD(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); +}; + + +class CBotIf : public CBotInstr +{ +private: + CBotInstr* m_Condition; // condition + CBotInstr* m_Block; // instructions + CBotInstr* m_BlockElse; // instructions + +public: + CBotIf(); + ~CBotIf(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +// definition of an integer + +class CBotInt : public CBotInstr +{ +private: + CBotInstr* m_var; // the variable to initialize + CBotInstr* m_expr; // a value to put, if there is +/// CBotInstr* m_next; // several definitions chained + +public: + CBotInt(); + ~CBotInt(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// definition of an array + +class CBotInstArray : public CBotInstr +{ +private: + CBotInstr* m_var; // the variables to initialize + CBotInstr* m_listass; // list of assignments for array + CBotTypResult + m_typevar; // type of elements +// CBotString m_ClassName; + +public: + CBotInstArray(); + ~CBotInstArray(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +// definition of a assignment list for a table +// int [ ] a [ ] = ( ( 1, 2, 3 ) , ( 3, 2, 1 ) ) ; + +class CBotListArray : public CBotInstr +{ +private: + CBotInstr* m_expr; // an expression for an element + // others are linked with CBotInstr :: m_next3; +public: + CBotListArray(); + ~CBotListArray(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); + bool Execute(CBotStack* &pj, CBotVar* pVar); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotEmpty : public CBotInstr +{ + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// defininition of a boolean + +class CBotBoolean : public CBotInstr +{ +private: + CBotInstr* m_var; // variable to initialise + CBotInstr* m_expr; // a value to put, if there is + +public: + CBotBoolean(); + ~CBotBoolean(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +// definition of a real number + +class CBotFloat : public CBotInstr +{ +private: + CBotInstr* m_var; // variable to initialise + CBotInstr* m_expr; // a value to put, if there is + +public: + CBotFloat(); + ~CBotFloat(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// definition of an element string + +class CBotIString : public CBotInstr +{ +private: + CBotInstr* m_var; // variable to initialise + CBotInstr* m_expr; // a value to put, if there is + +public: + CBotIString(); + ~CBotIString(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// definition of an element of any class + +class CBotClassInst : public CBotInstr +{ +private: + CBotInstr* m_var; // variable to initialise + CBotClass* m_pClass; // reference to the class + CBotInstr* m_Parameters; // parameters to be evaluated for the contructor + CBotInstr* m_expr; // a value to put, if there is + bool m_hasParams; // has it parameters? + long m_nMethodeIdent; + +public: + CBotClassInst(); + ~CBotClassInst(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = NULL); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotCondition : public CBotInstr +{ +private: + +public: + + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); +}; + + +// left operand +// accept the expressions that be to the left of assignment + +class CBotLeftExpr : public CBotInstr +{ +private: + long m_nIdent; + +public: + CBotLeftExpr(); + ~CBotLeftExpr(); + static + CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack, CBotStack* array); + + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + void RestoreStateVar(CBotStack* &pile, bool bMain); +}; + + +// management of the fields of an instance + +class CBotFieldExpr : public CBotInstr +{ +private: + friend class CBotExpression; + int m_nIdent; + +public: + CBotFieldExpr(); + ~CBotFieldExpr(); + void SetUniqNum(int num); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); +}; + +// management of indices of the tables + +class CBotIndexExpr : public CBotInstr +{ +private: + CBotInstr* m_expr; // expression for calculating the index + friend class CBotLeftExpr; + friend class CBotExprVar; + +public: + CBotIndexExpr(); + ~CBotIndexExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); +}; + +// expressions like +// x = a; +// x * y + 3; + +class CBotExpression : public CBotInstr +{ +private: + CBotLeftExpr* m_leftop; // left operand + CBotInstr* m_rightop; // right operant + +public: + CBotExpression(); + ~CBotExpression(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotListExpression : public CBotInstr +{ +private: + CBotInstr* m_Expr; // the first expression to be evaluated + +public: + CBotListExpression(); + ~CBotListExpression(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotLogicExpr : public CBotInstr +{ +private: + CBotInstr* m_condition; // test to evaluate + CBotInstr* m_op1; // left element + CBotInstr* m_op2; // right element + friend class CBotTwoOpExpr; + +public: + CBotLogicExpr(); + ~CBotLogicExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + + +class CBotBoolExpr : public CBotInstr +{ +private: + +public: + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); +}; + + + +// possibly an expression in parentheses ( ... ) +// there is never an instance of this class +// being the object returned inside the parenthesis +class CBotParExpr : public CBotInstr +{ +private: + +public: + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); +}; + +// unary expression +class CBotExprUnaire : public CBotInstr +{ +private: + CBotInstr* m_Expr; // expression to be evaluated +public: + CBotExprUnaire(); + ~CBotExprUnaire(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// all operations with two operands + +class CBotTwoOpExpr : public CBotInstr +{ +private: + CBotInstr* m_leftop; // left element + CBotInstr* m_rightop; // right element +public: + CBotTwoOpExpr(); + ~CBotTwoOpExpr(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = NULL); + bool Execute(CBotStack* &pStack); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + + + +// an instruction block { .... } +class CBotBlock : public CBotInstr +{ +private: + +public: + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + static + CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); +}; + + +// the content of a block of instructions ... ; ... ; ... ; ... ; +class CBotListInstr : public CBotInstr +{ +private: + CBotInstr* m_Instr; // instructions to do + +public: + CBotListInstr(); + ~CBotListInstr(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotInstrCall : public CBotInstr +{ +private: + CBotInstr* m_Parameters; // the parameters to be evaluated +// int m_typeRes; // type of the result +// CBotString m_RetClassName; // class of the result + CBotTypResult + m_typRes; // complete type of the result + long m_nFuncIdent; // id of a function + +public: + CBotInstrCall(); + ~CBotInstrCall(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// a call of method + +class CBotInstrMethode : public CBotInstr +{ +private: + CBotInstr* m_Parameters; // the parameters to be evaluated +// int m_typeRes; // type of the result +// CBotString m_RetClassName; // class of the result + CBotTypResult + m_typRes; // complete type of the result + + CBotString m_NomMethod; // name of the method + long m_MethodeIdent; // identifier of the method +// long m_nThisIdent; // identifier for "this" + CBotString m_ClassName; // name of the class + +public: + CBotInstrMethode(); + ~CBotInstrMethode(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); + bool Execute(CBotStack* &pj); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend); + void RestoreStateVar(CBotStack* &pj, bool bMain); +}; + +// expression for the variable name + +class CBotExprVar : public CBotInstr +{ +private: + long m_nIdent; + friend class CBotPostIncExpr; + friend class CBotPreIncExpr; + +public: + CBotExprVar(); + ~CBotExprVar(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); + static + CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); + + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + bool Execute2Var(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep); + void RestoreStateVar(CBotStack* &pj, bool bMain); +}; + +class CBotPostIncExpr : public CBotInstr +{ +private: + CBotInstr* m_Instr; + friend class CBotParExpr; + +public: + CBotPostIncExpr(); + ~CBotPostIncExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotPreIncExpr : public CBotInstr +{ +private: + CBotInstr* m_Instr; + friend class CBotParExpr; + +public: + CBotPreIncExpr(); + ~CBotPreIncExpr(); +// static +// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotLeftExprVar : public CBotInstr +{ +private: +public: + CBotTypResult + m_typevar; // type of variable declared + long m_nIdent; // unique identifier for that variable + +public: + CBotLeftExprVar(); + ~CBotLeftExprVar(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotExprBool : public CBotInstr +{ +private: + +public: + CBotExprBool(); + ~CBotExprBool(); + + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +class CBotExprNull : public CBotInstr +{ +private: + +public: + CBotExprNull(); + ~CBotExprNull(); + + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotExprNan : public CBotInstr +{ +private: + +public: + CBotExprNan(); + ~CBotExprNan(); + + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +class CBotNew : public CBotInstr +{ +private: + CBotInstr* m_Parameters; // the parameters to be evaluated + long m_nMethodeIdent; +// long m_nThisIdent; + CBotToken m_vartoken; + +public: + CBotNew(); + ~CBotNew(); + + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + +// expression representing a number + +class CBotExprNum : public CBotInstr +{ +private: + int m_numtype; // et the type of number + long m_valint; // value for an int + float m_valfloat; // value for a float + +public: + CBotExprNum(); + ~CBotExprNum(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + + +// expression representing a string + +class CBotExprAlpha : public CBotInstr +{ +private: + +public: + CBotExprAlpha(); + ~CBotExprAlpha(); + static + CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); +}; + + +#define MAX(a,b) ((a>b) ? a : b) + + +// class for the management of integer numbers (int) +class CBotVarInt : public CBotVar +{ +private: + int m_val; // the value + CBotString m_defnum; // the name if given by DefineNum + friend class CBotVar; + +public: + CBotVarInt( const CBotToken* name ); +// ~CBotVarInt(); + + void SetValInt(int val, const char* s = NULL); + void SetValFloat(float val); + int GivValInt(); + float GivValFloat(); + CBotString GivValString(); + + void Copy(CBotVar* pSrc, bool bName=true); + + + void Add(CBotVar* left, CBotVar* right); // addition + void Sub(CBotVar* left, CBotVar* right); // substraction + void Mul(CBotVar* left, CBotVar* right); // multiplication + int Div(CBotVar* left, CBotVar* right); // division + int Modulo(CBotVar* left, CBotVar* right); // remainder of division + void Power(CBotVar* left, CBotVar* right); // power + + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); + + void XOr(CBotVar* left, CBotVar* right); + void Or(CBotVar* left, CBotVar* right); + void And(CBotVar* left, CBotVar* right); + + void SL(CBotVar* left, CBotVar* right); + void SR(CBotVar* left, CBotVar* right); + void ASR(CBotVar* left, CBotVar* right); + + void Neg(); + void Not(); + void Inc(); + void Dec(); + + bool Save0State(FILE* pf); + bool Save1State(FILE* pf); + +}; + +// Class for managing real numbers (float) +class CBotVarFloat : public CBotVar +{ +private: + float m_val; // the value + +public: + CBotVarFloat( const CBotToken* name ); +// ~CBotVarFloat(); + + void SetValInt(int val, const char* s = NULL); + void SetValFloat(float val); + int GivValInt(); + float GivValFloat(); + CBotString GivValString(); + + void Copy(CBotVar* pSrc, bool bName=true); + + + void Add(CBotVar* left, CBotVar* right); // addition + void Sub(CBotVar* left, CBotVar* right); // substraction + void Mul(CBotVar* left, CBotVar* right); // multiplication + int Div(CBotVar* left, CBotVar* right); // division + int Modulo(CBotVar* left, CBotVar* right); // remainder of division + void Power(CBotVar* left, CBotVar* right); // power + + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); + + void Neg(); + void Inc(); + void Dec(); + + bool Save1State(FILE* pf); +}; + + +// class for management of strings (String) +class CBotVarString : public CBotVar +{ +private: + CBotString m_val; // the value + +public: + CBotVarString( const CBotToken* name ); +// ~CBotVarString(); + + void SetValString(const char* p); + CBotString GivValString(); + + void Copy(CBotVar* pSrc, bool bName=true); + + void Add(CBotVar* left, CBotVar* right); // addition + + bool Lo(CBotVar* left, CBotVar* right); + bool Hi(CBotVar* left, CBotVar* right); + bool Ls(CBotVar* left, CBotVar* right); + bool Hs(CBotVar* left, CBotVar* right); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); + + bool Save1State(FILE* pf); +}; + +// class for the management of boolean +class CBotVarBoolean : public CBotVar +{ +private: + bool m_val; // the value + +public: + CBotVarBoolean( const CBotToken* name ); +// ~CBotVarBoolean(); + + void SetValInt(int val, const char* s = NULL); + void SetValFloat(float val); + int GivValInt(); + float GivValFloat(); + CBotString GivValString(); + + void Copy(CBotVar* pSrc, bool bName=true); + + void And(CBotVar* left, CBotVar* right); + void Or(CBotVar* left, CBotVar* right); + void XOr(CBotVar* left, CBotVar* right); + void Not(); + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); + + bool Save1State(FILE* pf); +}; + + +// class management class instances +class CBotVarClass : public CBotVar +{ +private: + static + CBotVarClass* m_ExClass; // list of existing instances at some point + CBotVarClass* m_ExNext; // for this general list + CBotVarClass* m_ExPrev; // for this general list + +private: + CBotClass* m_pClass; // the class definition + CBotVarClass* m_pParent; // the instance of a parent class + CBotVar* m_pVar; // contents + friend class CBotVar; // my daddy is a buddy WHAT? :D(\TODO mon papa est un copain ) + friend class CBotVarPointer; // and also the pointer + int m_CptUse; // counter usage + long m_ItemIdent; // identifier (unique) of an instance + bool m_bConstructor; // set if a constructor has been called + +public: + CBotVarClass( const CBotToken* name, const CBotTypResult& type ); +// CBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); + ~CBotVarClass(); +// void InitCBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); + + void Copy(CBotVar* pSrc, bool bName=true); + void SetClass(CBotClass* pClass); //, int &nIdent); + CBotClass* GivClass(); + CBotVar* GivItem(const char* name); // return an element of a class according to its name (*) + CBotVar* GivItemRef(int nIdent); + + CBotVar* GivItem(int n, bool bExtend); + CBotVar* GivItemList(); + + CBotString GivValString(); + + bool Save1State(FILE* pf); + void Maj(void* pUser, bool bContinue); + + void IncrementUse(); // a reference to incrementation + void DecrementUse(); // a reference to decrementation + + CBotVarClass* + GivPointer(); + void SetItemList(CBotVar* pVar); + + void SetIdent(long n); + + static CBotVarClass* Find(long id); + + +// CBotVar* GivMyThis(); + + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); + + void ConstructorSet(); +}; + + +// class for the management of pointers to a class instances +class CBotVarPointer : public CBotVar +{ +private: + CBotVarClass* + m_pVarClass; // contents + CBotClass* m_pClass; // class provided for this pointer + friend class CBotVar; // my daddy is a buddy + +public: + CBotVarPointer( const CBotToken* name, CBotTypResult& type ); + ~CBotVarPointer(); + + void Copy(CBotVar* pSrc, bool bName=true); + void SetClass(CBotClass* pClass); + CBotClass* GivClass(); + CBotVar* GivItem(const char* name); // return an element of a class according to its name (*) + CBotVar* GivItemRef(int nIdent); + CBotVar* GivItemList(); + + CBotString GivValString(); + void SetPointer(CBotVar* p); + CBotVarClass* + GivPointer(); + + void SetIdent(long n); // associates an identification number (unique) + long GivIdent(); // gives the identification number associated with + void ConstructorSet(); + + bool Save1State(FILE* pf); + void Maj(void* pUser, bool bContinue); + + bool Eq(CBotVar* left, CBotVar* right); + bool Ne(CBotVar* left, CBotVar* right); +}; + + +// classe pour les tableaux + +#define MAXARRAYSIZE 9999 + +class CBotVarArray : public CBotVar +{ +private: + CBotVarClass* + m_pInstance; // instance manager of table + + friend class CBotVar; // my daddy is a buddy + +public: + CBotVarArray( const CBotToken* name, CBotTypResult& type ); + ~CBotVarArray(); + + void SetPointer(CBotVar* p); + CBotVarClass* + GivPointer(); + + void Copy(CBotVar* pSrc, bool bName=true); + CBotVar* GivItem(int n, bool bGrow=false); // makes an element according to its numeric index + // enlarged the table if necessary if bExtend +// CBotVar* GivItem(const char* name); // makes a element by literal index + CBotVar* GivItemList(); // gives the first item in the list + + CBotString GivValString(); // gets the contents of the array into a string + + bool Save1State(FILE* pf); +}; + + +extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); + +extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); +extern bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ); + +extern bool WriteWord(FILE* pf, unsigned short w); +extern bool ReadWord(FILE* pf, unsigned short& w); +extern bool ReadLong(FILE* pf, long& w); +extern bool WriteFloat(FILE* pf, float w); +extern bool WriteLong(FILE* pf, long w); +extern bool ReadFloat(FILE* pf, float& w); +extern bool WriteString(FILE* pf, CBotString s); +extern bool ReadString(FILE* pf, CBotString& s); +extern bool WriteType(FILE* pf, CBotTypResult type); +extern bool ReadType(FILE* pf, CBotTypResult& type); + +extern float GivNumFloat( const char* p ); + +#if false +extern void DEBUG( const char* text, int val, CBotStack* pile ); +#endif + +/////////////////////////////////////////// +// class for routine calls (external) + +class CBotCall +{ +private: + static + CBotCall* m_ListCalls; + static + void* m_pUser; + long m_nFuncIdent; + +private: + CBotString m_name; + bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; + CBotTypResult + (*m_rComp) (CBotVar* &pVar, void* pUser) ; + CBotCall* m_next; + +public: + CBotCall(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + ~CBotCall(); + + static + bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + + static + CBotTypResult + CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); + static + bool CheckCall(const char* name); + +// static +// int DoCall(CBotToken* &p, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); + static + int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); +#if STACKRUN + bool Run(CBotStack* pStack); + static + bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); +#endif + + CBotString GivName(); + CBotCall* Next(); + + static void SetPUser(void* pUser); + static void Free(); +}; + +// class managing the methods declared by AddFunction on a class + +class CBotCallMethode +{ +private: + CBotString m_name; + bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception); + CBotTypResult + (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); + CBotCallMethode* m_next; + friend class CBotClass; + long m_nFuncIdent; + +public: + CBotCallMethode(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); + ~CBotCallMethode(); + + CBotTypResult + CompileCall(const char* name, CBotVar* pThis, + CBotVar** ppVars, CBotCStack* pStack, + long& nIdent); + + int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pFunc); + + CBotString GivName(); + CBotCallMethode* Next(); + void AddNext(CBotCallMethode* p); + +}; + +// a list of parameters + +class CBotDefParam +{ +private: + CBotToken m_token; // name of the parameter + CBotString m_typename; // type name + CBotTypResult m_type; // type of paramteter + CBotDefParam* m_next; // next parameter + long m_nIdent; + +public: + CBotDefParam(); + ~CBotDefParam(); + static + CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); + bool Execute(CBotVar** ppVars, CBotStack* &pj); + void RestoreState(CBotStack* &pj, bool bMain); + + void AddNext(CBotDefParam* p); + int GivType(); + CBotTypResult GivTypResult(); + CBotDefParam* GivNext(); + + CBotString GivParamString(); +}; + + +// a function declaration + +class CBotFunction : CBotInstr +{ +private: + // management of list of (static) public functions + static + CBotFunction* m_listPublic; + CBotFunction* m_nextpublic; + CBotFunction* m_prevpublic; + friend class CBotCStack; +// long m_nThisIdent; + long m_nFuncIdent; + bool m_bSynchro; // synchronized method? + +private: + CBotDefParam* m_Param; // parameter list + CBotInstr* m_Block; // the instruction block + CBotFunction* m_next; + CBotToken m_retToken; // if returns CBotTypClass + CBotTypResult m_retTyp; // complete type of the result + + bool m_bPublic; // public function + bool m_bExtern; // extern function + CBotString m_MasterClass; // name of the class we derive + CBotProgram* m_pProg; + friend class CBotProgram; + friend class CBotClass; + + CBotToken m_extern; // for the position of the word "extern" + CBotToken m_openpar; + CBotToken m_closepar; + CBotToken m_openblk; + CBotToken m_closeblk; +public: + CBotFunction(); + ~CBotFunction(); + static + CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, bool bLocal = true); + static + CBotFunction* Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass); + bool Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); + void RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = NULL); + + void AddNext(CBotFunction* p); + CBotTypResult CompileCall(const char* name, CBotVar** ppVars, long& nIdent); + CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic = true); + + int DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken); + void RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack); + int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass); + void RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass); + bool CheckParam(CBotDefParam* pParam); + + static + void AddPublic(CBotFunction* pfunc); + + CBotString GivName(); + CBotString GivParams(); + bool IsPublic(); + bool IsExtern(); + CBotFunction* Next(); + + bool GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); +}; + + diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index c99185f..692d3c7 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -1,880 +1,882 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// -// Gestion des variables de type classe -// - -#include "CBot.h" - - -CBotClass* CBotClass::m_ExClass = NULL; - -CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic) -{ - m_pParent = pPapa; - m_name = name; - m_pVar = NULL; - m_next = NULL; - m_pCalls = NULL; - m_pMethod = NULL; - m_rMaj = NULL; - m_IsDef = true; - m_bIntrinsic= bIntrinsic; - m_cptLock = 0; - m_cptOne = 0; - m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; - - for ( int j= 0; j< 5 ; j++ ) - { - m_ProgInLock[j] = NULL; - } - - - // se place tout seul dans la liste - if (m_ExClass) m_ExClass->m_ExPrev = this; - m_ExNext = m_ExClass; - m_ExPrev = NULL; - m_ExClass = this; - -} - -CBotClass::~CBotClass() -{ - // retire la classe de la liste - if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; - else m_ExClass = m_ExNext; - - if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; - m_ExPrev = NULL; - m_ExNext = NULL; - - delete m_pVar; - delete m_pCalls; - delete m_pMethod; - - delete m_next; // libre toutes celle de ce niveau -} - - -void CBotClass::Free() -{ - while ( m_ExClass != NULL ) - { - delete m_ExClass; - } -} - -void CBotClass::Purge() -{ - if ( this == NULL ) return; - - delete m_pVar; - m_pVar = NULL; - delete m_pCalls; - m_pCalls = NULL; - delete m_pMethod; - m_pMethod = NULL; - m_IsDef = false; - - m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; - - m_next->Purge(); - m_next = NULL; // n'appartient plus cette chane -} - -bool CBotClass::Lock(CBotProgram* p) -{ - int i = m_cptLock++; - - if ( i == 0 ) - { - m_cptOne = 1; - m_ProgInLock[0] = p; - return true; - } - if ( p == m_ProgInLock[0] ) - { - m_cptOne++; - m_cptLock--; // a dj t compt - return true; - } - - for ( int j = 1 ; j <= i ; j++) - { - if ( p == m_ProgInLock[j] ) - { - m_cptLock--; - return false; // dj en attente - } - } - - if ( i < 5 ) // maxi 5 en attente - { - m_ProgInLock[i] = p; // se place dans la queue - } - else - m_cptLock--; - - return false; -} - -void CBotClass::Unlock() -{ - if ( --m_cptOne > 0 ) return ; - - int i = --m_cptLock; - if ( i<0 ) - { - m_cptLock = 0; - return; - } - - for ( int j= 0; j< i ; j++ ) - { - m_ProgInLock[j] = m_ProgInLock[j+1]; - } - m_ProgInLock[i] = 0; -} - -void CBotClass::FreeLock(CBotProgram* p) -{ - CBotClass* pClass = m_ExClass; - - while ( pClass != NULL ) - { - if ( p == pClass->m_ProgInLock[0] ) - { - pClass->m_cptLock -= pClass->m_cptOne; - pClass->m_cptOne = 0; - } - - for ( int j = 1; j < 5 ; j++ ) - if ( p == pClass->m_ProgInLock[j] ) - pClass->m_cptLock--; - - pClass = pClass->m_ExNext; - } -} - - - -bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) -{ - CBotToken token(name, CBotString()); - CBotClass* pClass = type.GivClass(); - - CBotVar* pVar = CBotVar::Create( name, type ); -/// pVar->SetUniqNum(CBotVar::NextUniqNum()); - pVar->SetPrivate( mPrivate ); - - if ( pClass != NULL ) - { -// pVar->SetClass(pClass); - if ( type.Eq(CBotTypClass) ) - { - // ajoute une instruction new pour initialiser l'object - pVar->m_InitExpr = new CBotNew() ; - CBotToken nom( pClass->GivName() ); - pVar->m_InitExpr->SetToken(&nom); - } - } - return AddItem( pVar ); -} - - -bool CBotClass::AddItem(CBotVar* pVar) -{ - pVar->SetUniqNum(++m_nbVar); - - if ( m_pVar == NULL ) m_pVar = pVar; - else m_pVar->AddNext(pVar); - - return true; -} - -void CBotClass::AddNext(CBotClass* pClass) -{ - CBotClass* p = this; - while (p->m_next != NULL) p = p->m_next; - - p->m_next = pClass; -} - -CBotString CBotClass::GivName() -{ - return m_name; -} - -CBotClass* CBotClass::GivParent() -{ - if ( this == NULL ) return NULL; - return m_pParent; -} - -bool CBotClass::IsChildOf(CBotClass* pClass) -{ - CBotClass* p = this; - while ( p != NULL ) - { - if ( p == pClass ) return true; - p = p->m_pParent; - } - return false; -} - - -CBotVar* CBotClass::GivVar() -{ - return m_pVar; -} - -CBotVar* CBotClass::GivItem(const char* name) -{ - CBotVar* p = m_pVar; - - while ( p != NULL ) - { - if ( p->GivName() == name ) return p; - p = p->GivNext(); - } - if ( m_pParent != NULL ) return m_pParent->GivItem(name); - return NULL; -} - -CBotVar* CBotClass::GivItemRef(int nIdent) -{ - CBotVar* p = m_pVar; - - while ( p != NULL ) - { - if ( p->GivUniqNum() == nIdent ) return p; - p = p->GivNext(); - } - if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); - return NULL; -} - -bool CBotClass::IsIntrinsic() -{ - return m_bIntrinsic; -} - -CBotClass* CBotClass::Find(CBotToken* &pToken) -{ - return Find(pToken->GivString()); -} - -CBotClass* CBotClass::Find(const char* name) -{ - CBotClass* p = m_ExClass; - - while ( p != NULL ) - { - if ( p->GivName() == name ) return p; - p = p->m_ExNext; - } - - return NULL; -} - -bool CBotClass::AddFunction(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) -{ - // mmorise les pointeurs aux deux fonctions - CBotCallMethode* p = m_pCalls; - CBotCallMethode* pp = NULL; - - while ( p != NULL ) - { - if ( name == p->GivName() ) - { - if ( pp == NULL ) m_pCalls = p->m_next; - else pp->m_next = p->m_next; - delete p; - break; - } - pp = p; - p = p->m_next; - } - - p = new CBotCallMethode(name, rExec, rCompile); - - if (m_pCalls == NULL) m_pCalls = p; - else m_pCalls->AddNext(p); // ajoute la liste - - return true; -} - -bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ) -{ - m_rMaj = rMaj; - return true; -} - -// compile une mthode associe une instance de classe -// la mthode peut tre dclare par AddFunction ou par l'utilisateur - -CBotTypResult CBotClass::CompileMethode(const char* name, - CBotVar* pThis, CBotVar** ppParams, - CBotCStack* pStack, long& nIdent) -{ - nIdent = 0; // oublie le prcdent s'il y a lieu - - // recherche dans les mthodes dclares par AddFunction - - CBotTypResult r = m_pCalls->CompileCall(name, pThis, ppParams, pStack, nIdent); - if ( r.GivType() >= 0) return r; - - // recherche dans les mthodes dclares par l'utilisateur - - r = m_pMethod->CompileCall(name, ppParams, nIdent); - if ( r.Eq(TX_UNDEFCALL) && m_pParent != NULL ) - return m_pParent->m_pMethod->CompileCall(name, ppParams, nIdent); - return r; -} - -// excute une mthode - -bool CBotClass::ExecuteMethode(long& nIdent, const char* name, - CBotVar* pThis, CBotVar** ppParams, - CBotVar* &pResult, CBotStack* &pStack, - CBotToken* pToken) -{ - int ret = m_pCalls->DoCall(nIdent, name, pThis, ppParams, pResult, pStack, pToken); - if (ret>=0) return ret; - - ret = m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, this); - return ret; -} - -// rtabli la pile d'excution - -void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, - CBotVar** ppParams, CBotStack* &pStack) -{ - m_pMethod->RestoreCall(nIdent, name, pThis, ppParams, pStack, this); -} - - - - -bool CBotClass::SaveStaticState(FILE* pf) -{ - if (!WriteWord( pf, CBOTVERSION*2)) return false; - - // sauve l'tat des variables statiques dans les classes - CBotClass* p = m_ExClass; - - while ( p != NULL ) - { - if (!WriteWord( pf, 1)) return false; - // enregistre le nom de la classe - if (!WriteString( pf, p->GivName() )) return false; - - CBotVar* pv = p->GivVar(); - while( pv != NULL ) - { - if ( pv->IsStatic() ) - { - if (!WriteWord( pf, 1)) return false; - if (!WriteString( pf, pv->GivName() )) return false; - - if ( !pv->Save0State(pf)) return false; // entte commune - if ( !pv->Save1State(pf) ) return false; // sauve selon la classe fille - if ( !WriteWord( pf, 0)) return false; - } - pv = pv->GivNext(); - } - - if (!WriteWord( pf, 0)) return false; - p = p->m_ExNext; - } - - if (!WriteWord( pf, 0)) return false; - return true; -} - -bool CBotClass::RestoreStaticState(FILE* pf) -{ - CBotString ClassName, VarName; - CBotClass* pClass; - unsigned short w; - - if (!ReadWord( pf, w )) return false; - if ( w != CBOTVERSION*2 ) return false; - - while (true) - { - if (!ReadWord( pf, w )) return false; - if ( w == 0 ) return true; - - if (!ReadString( pf, ClassName )) return false; - pClass = Find(ClassName); - - while (true) - { - if (!ReadWord( pf, w )) return false; - if ( w == 0 ) break; - - CBotVar* pVar = NULL; - CBotVar* pv = NULL; - - if (!ReadString( pf, VarName )) return false; - if ( pClass != NULL ) pVar = pClass->GivItem(VarName); - - if (!CBotVar::RestoreState(pf, pv)) return false; // la variable temp - - if ( pVar != NULL ) pVar->Copy(pv); - delete pv; - } - } - return true; -} - - -///////////////////////////////////////////////////////////////////// - -CBotClassInst::CBotClassInst() -{ - m_next = NULL; - m_var = NULL; - m_Parameters = NULL; - m_expr = NULL; - m_hasParams = false; - m_nMethodeIdent = 0; - name = "CBotClassInst"; -} - -CBotClassInst::~CBotClassInst() -{ - delete m_var; -// delete m_next; // fait par le destructeur de la classe de base ~CBotInstr() -} - -// dfinition de pointeur(s) un objet -// du style -// CPoint A, B ; - -CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) -{ - // cherche la classe correspondante - if ( pClass == NULL ) - { - pStack->SetStartError(p->GivStart()); - pClass = CBotClass::Find(p); - if ( pClass == NULL ) - { - // pas trouv ? c'est bizare - pStack->SetError(TX_NOCLASS, p); - return NULL; - } - p = p->GivNext(); - } - - bool bIntrinsic = pClass->IsIntrinsic(); - CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); - CBotClassInst* inst = (CBotClassInst*)CompileArray(p, pStack, type); - if ( inst != NULL || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(); - - inst = new CBotClassInst(); - /// \TODO Need to be revised and fixed after adding unit tests - CBotToken token(pClass->GivName(), CBotString(), p->GivStart(), p->GivEnd()); - inst->SetToken(&token); - CBotToken* vartoken = p; - - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = type; - if (pStk->CheckVarLocal(vartoken)) // redfinition de la variable - { - pStk->SetStartError(vartoken->GivStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) // avec des indices ? - { - delete inst; // n'est pas de type CBotInt - p = vartoken; // revient sur le nom de la variable - - // compile une dclaration de tableau - - inst = (CBotClassInst*)CBotInstArray::Compile( p, pStk, type ); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - goto suite; // pas d'assignation, variable dj cre - } - - - CBotVar* var; - var = CBotVar::Create(vartoken->GivString(), type); // cre l'instance -// var->SetClass(pClass); - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // lui attribut un numro unique - pStack->AddVar(var); // la place sur la pile - - // regarde s'il y a des paramtres - inst->m_hasParams = (p->GivType() == ID_OPENPAR); - - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStk, ppVars); - if ( !pStk->IsOk() ) goto error; - - // s'il y a des paramtres, fait l'quivalent de l'instruction new - // CPoint A ( 0, 0 ) est quivalent - // CPoint A = new CPoint( 0, 0 ) - -// if ( NULL != inst->m_Parameters ) - if ( inst->m_hasParams ) - { - // le constructeur existe-il ? -// CBotString noname; - CBotTypResult r = pClass->CompileMethode(pClass->GivName(), var, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // libre le supplment de pile - int typ = r.GivType(); - - if (typ == TX_UNDEFCALL) - { - // si le constructeur n'existe pas - if (inst->m_Parameters != NULL) // avec des paramtres - { - pStk->SetError(TX_NOCONST, vartoken); - goto error; - } - typ = 0; - } - - if (typ>20) - { - pStk->SetError(typ, vartoken->GivEnd()); - goto error; - } - - } - - if (IsOfType(p, ID_ASS)) // avec une assignation ? - { - if (inst->m_hasParams) - { - pStk->SetError(TX_ENDOF, p->GivStart()); - goto error; - } - - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - CBotClass* result = pStk->GivClass(); - if ( !pStk->GivTypResult(1).Eq(CBotTypNullPointer) && - ( !pStk->GivTypResult(1).Eq(CBotTypPointer) || - ( result != NULL && !pClass->IsChildOf(result) ))) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } -// if ( !bIntrinsic ) var->SetPointer(pStk->GivVar()->GivPointer()); - if ( !bIntrinsic ) - { - // n'utilise pas le rsultat sur la pile, pour imposer la classe - CBotVar* pvar = CBotVar::Create("", pClass); - var->SetPointer( pvar ); // var dj dclare pointe l'instance - delete pvar; // supprime le second pointeur - } - var->SetInit(true); // marque le pointeur comme init - } - else if (inst->m_hasParams) - { - // cre l'objet sur le "tas" - // avec un pointeur sur cet objet - if ( !bIntrinsic ) - { - CBotVar* pvar = CBotVar::Create("", pClass); - var->SetPointer( pvar ); // var dj dclare pointe l'instance - delete pvar; // supprime le second pointeur - } - var->SetInit(2); // marque le pointeur comme init - } -suite: - if (IsOfType(p, ID_COMMA)) // plusieurs dfinitions enchanes - { - if ( NULL != ( inst->m_next = CBotClassInst::Compile(p, pStk, pClass) )) // compile la suivante - { - return pStack->Return(inst, pStk); - } - } - - if (IsOfType(p, ID_SEP)) // instruction termine - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } - -error: - delete inst; - return pStack->Return(NULL, pStk); -} - -// dclaration de l'instance d'une classe, par exemple: -// CPoint A, B; - -bool CBotClassInst::Execute(CBotStack* &pj) -{ - CBotVar* pThis = NULL; - - CBotStack* pile = pj->AddStack(this);//indispensable pour SetState() -// if ( pile == EOX ) return true; - - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - - bool bIntrincic = pClass->IsIntrinsic(); - - // cre la variable de type pointeur l'objet - - if ( pile->GivState()==0) - { - CBotString name = m_var->m_token.GivString(); - if ( bIntrincic ) - { - pThis = CBotVar::Create(name, CBotTypResult( CBotTypIntrinsic, pClass )); - } - else - { - pThis = CBotVar::Create(name, CBotTypResult( CBotTypPointer, pClass )); - } - - pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // lui attribut un numro unique - pile->AddVar(pThis); // la place sur la pile - pile->IncState(); - } - - if ( pThis == NULL ) pThis = pile->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); - - if ( pile->GivState()<3) - { - // y a-t-il une assignation ou des paramtres (constructeur) - -// CBotVarClass* pInstance = NULL; - - if ( m_expr != NULL ) - { - // value l'expression pour l'assignation - if (!m_expr->Execute(pile)) return false; - - if ( bIntrincic ) - { - CBotVar* pv = pile->GivVar(); - if ( pv == NULL || pv->GivPointer() == NULL ) - { - pile->SetError(TX_NULLPT, &m_token); - return pj->Return(pile); - } - pThis->Copy(pile->GivVar(), false); - } - else - { - CBotVarClass* pInstance; - pInstance = ((CBotVarPointer*)pile->GivVar())->GivPointer(); // valeur pour l'assignation - pThis->SetPointer(pInstance); - } - pThis->SetInit(true); - } - - else if ( m_hasParams ) - { - // value le constructeur d'une instance - - if ( !bIntrincic && pile->GivState() == 1) - { - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - - // cre une instance de la classe demande - - CBotVarClass* pInstance; - pInstance = (CBotVarClass*)CBotVar::Create("", pClass); - pThis->SetPointer(pInstance); - delete pInstance; - - pile->IncState(); - } - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - - if ( p != NULL) while ( true ) - { - pile2 = pile2->AddStack(); // de la place sur la pile pour les rsultats - if ( pile2->GivState() == 0 ) - { - if (!p->Execute(pile2)) return false; // interrompu ici ? - pile2->SetState(1); - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - // cre une variable pour le rsultat - CBotVar* pResult = NULL; // constructeurs toujours void - - if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrompu - - pThis->SetInit(true); - pThis->ConstructorSet(); // signale que le constructeur a t appel - pile->Return(pile2); // libre un bout de pile - -// pInstance = pThis->GivPointer(); - - } - -// if ( !bIntrincic ) pThis->SetPointer(pInstance); // le fait pointer l'instance - - pile->SetState(3); // fini cette partie - } - - if ( pile->IfStep() ) return false; - - if ( m_next2b != NULL && - !m_next2b->Execute(pile)) return false; // autre(s) dfinition(s) - - return pj->Return( pile ); // transmet en dessous -} - - - -void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotVar* pThis = NULL; - - CBotStack* pile = pj; - if ( bMain ) pile = pj->RestoreStack(this); - if ( pile == NULL ) return; - - // cre la variable de type pointeur l'objet - { - CBotString name = m_var->m_token.GivString(); - pThis = pile->FindVar(name); - pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // lui attribut un numro unique - } - - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - bool bIntrincic = pClass->IsIntrinsic(); - - if ( bMain && pile->GivState()<3) - { - // y a-t-il une assignation ou des paramtres (constructeur) - -// CBotVarClass* pInstance = NULL; - - if ( m_expr != NULL ) - { - // value l'expression pour l'assignation - m_expr->RestoreState(pile, bMain); - return; - } - - else if ( m_hasParams ) - { - // value le constructeur d'une instance - - if ( !bIntrincic && pile->GivState() == 1) - { - return; - } - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - - if ( p != NULL) while ( true ) - { - pile2 = pile2->RestoreStack(); // de la place sur la pile pour les rsultats - if ( pile2 == NULL ) return; - - if ( pile2->GivState() == 0 ) - { - p->RestoreState(pile2, bMain); // interrompu ici ? - return; - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - // cre une variable pour le rsultat - CBotVar* pResult = NULL; // constructeurs toujours void - - pClass->RestoreMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, pile2); - return; - } - } - - if ( m_next2b != NULL ) - m_next2b->RestoreState(pile, bMain); // autre(s) dfinition(s) -} - - -// test si un nom de procdure est dj dfini quelque part - -bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) -{ - CBotString name = pToken->GivString(); - - if ( CBotCall::CheckCall(name) ) return true; - - CBotFunction* pp = m_pMethod; - while ( pp != NULL ) - { - if ( pToken->GivString() == pp->GivName() ) - { - // les paramtres sont-ils exactement les mmes ? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->Next(); - } - - return false; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// + +///////////////////////////////////////////////////////////////////// +// Management of variables of class type +// + +#include "CBot.h" + + +CBotClass* CBotClass::m_ExClass = NULL; + +CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic) +{ + m_pParent = pPapa; + m_name = name; + m_pVar = NULL; + m_next = NULL; + m_pCalls = NULL; + m_pMethod = NULL; + m_rMaj = NULL; + m_IsDef = true; + m_bIntrinsic= bIntrinsic; + m_cptLock = 0; + m_cptOne = 0; + m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; + + for ( int j= 0; j< 5 ; j++ ) + { + m_ProgInLock[j] = NULL; + } + + + // is located alone in the list + if (m_ExClass) m_ExClass->m_ExPrev = this; + m_ExNext = m_ExClass; + m_ExPrev = NULL; + m_ExClass = this; + +} + +CBotClass::~CBotClass() +{ + // removes the list of class + if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; + else m_ExClass = m_ExNext; + + if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; + m_ExPrev = NULL; + m_ExNext = NULL; + + delete m_pVar; + delete m_pCalls; + delete m_pMethod; + + delete m_next; // releases all of them on this level +} + + +void CBotClass::Free() +{ + while ( m_ExClass != NULL ) + { + delete m_ExClass; + } +} + +void CBotClass::Purge() +{ + if ( this == NULL ) return; + + delete m_pVar; + m_pVar = NULL; + delete m_pCalls; + m_pCalls = NULL; + delete m_pMethod; + m_pMethod = NULL; + m_IsDef = false; + + m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; + + m_next->Purge(); + m_next = NULL; // no longer belongs to this chain +} + +bool CBotClass::Lock(CBotProgram* p) +{ + int i = m_cptLock++; + + if ( i == 0 ) + { + m_cptOne = 1; + m_ProgInLock[0] = p; + return true; + } + if ( p == m_ProgInLock[0] ) + { + m_cptOne++; + m_cptLock--; // has already been counted + return true; + } + + for ( int j = 1 ; j <= i ; j++) + { + if ( p == m_ProgInLock[j] ) + { + m_cptLock--; + return false; // already pending + } + } + + if ( i < 5 ) // max 5 in query + { + m_ProgInLock[i] = p; // located in a queue + } + else + m_cptLock--; + + return false; +} + +void CBotClass::Unlock() +{ + if ( --m_cptOne > 0 ) return ; + + int i = --m_cptLock; + if ( i<0 ) + { + m_cptLock = 0; + return; + } + + for ( int j= 0; j< i ; j++ ) + { + m_ProgInLock[j] = m_ProgInLock[j+1]; + } + m_ProgInLock[i] = 0; +} + +void CBotClass::FreeLock(CBotProgram* p) +{ + CBotClass* pClass = m_ExClass; + + while ( pClass != NULL ) + { + if ( p == pClass->m_ProgInLock[0] ) + { + pClass->m_cptLock -= pClass->m_cptOne; + pClass->m_cptOne = 0; + } + + for ( int j = 1; j < 5 ; j++ ) + if ( p == pClass->m_ProgInLock[j] ) + pClass->m_cptLock--; + + pClass = pClass->m_ExNext; + } +} + + + +bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) +{ + CBotToken token(name, CBotString()); + CBotClass* pClass = type.GivClass(); + + CBotVar* pVar = CBotVar::Create( name, type ); +/// pVar->SetUniqNum(CBotVar::NextUniqNum()); + pVar->SetPrivate( mPrivate ); + + if ( pClass != NULL ) + { +// pVar->SetClass(pClass); + if ( type.Eq(CBotTypClass) ) + { + // adds a new statement for the object initialization + pVar->m_InitExpr = new CBotNew() ; + CBotToken nom( pClass->GivName() ); + pVar->m_InitExpr->SetToken(&nom); + } + } + return AddItem( pVar ); +} + + +bool CBotClass::AddItem(CBotVar* pVar) +{ + pVar->SetUniqNum(++m_nbVar); + + if ( m_pVar == NULL ) m_pVar = pVar; + else m_pVar->AddNext(pVar); + + return true; +} + +void CBotClass::AddNext(CBotClass* pClass) +{ + CBotClass* p = this; + while (p->m_next != NULL) p = p->m_next; + + p->m_next = pClass; +} + +CBotString CBotClass::GivName() +{ + return m_name; +} + +CBotClass* CBotClass::GivParent() +{ + if ( this == NULL ) return NULL; + return m_pParent; +} + +bool CBotClass::IsChildOf(CBotClass* pClass) +{ + CBotClass* p = this; + while ( p != NULL ) + { + if ( p == pClass ) return true; + p = p->m_pParent; + } + return false; +} + + +CBotVar* CBotClass::GivVar() +{ + return m_pVar; +} + +CBotVar* CBotClass::GivItem(const char* name) +{ + CBotVar* p = m_pVar; + + while ( p != NULL ) + { + if ( p->GivName() == name ) return p; + p = p->GivNext(); + } + if ( m_pParent != NULL ) return m_pParent->GivItem(name); + return NULL; +} + +CBotVar* CBotClass::GivItemRef(int nIdent) +{ + CBotVar* p = m_pVar; + + while ( p != NULL ) + { + if ( p->GivUniqNum() == nIdent ) return p; + p = p->GivNext(); + } + if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); + return NULL; +} + +bool CBotClass::IsIntrinsic() +{ + return m_bIntrinsic; +} + +CBotClass* CBotClass::Find(CBotToken* &pToken) +{ + return Find(pToken->GivString()); +} + +CBotClass* CBotClass::Find(const char* name) +{ + CBotClass* p = m_ExClass; + + while ( p != NULL ) + { + if ( p->GivName() == name ) return p; + p = p->m_ExNext; + } + + return NULL; +} + +bool CBotClass::AddFunction(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) +{ + // stores pointers to the two functions + CBotCallMethode* p = m_pCalls; + CBotCallMethode* pp = NULL; + + while ( p != NULL ) + { + if ( name == p->GivName() ) + { + if ( pp == NULL ) m_pCalls = p->m_next; + else pp->m_next = p->m_next; + delete p; + break; + } + pp = p; + p = p->m_next; + } + + p = new CBotCallMethode(name, rExec, rCompile); + + if (m_pCalls == NULL) m_pCalls = p; + else m_pCalls->AddNext(p); // added to the list + + return true; +} + +bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ) +{ + m_rMaj = rMaj; + return true; +} + +// compiles a method associated with an instance of class +// the method can be declared by the user or AddFunction + +CBotTypResult CBotClass::CompileMethode(const char* name, + CBotVar* pThis, CBotVar** ppParams, + CBotCStack* pStack, long& nIdent) +{ + nIdent = 0; // forget the previous one if necessary + + // find the methods declared by AddFunction + + CBotTypResult r = m_pCalls->CompileCall(name, pThis, ppParams, pStack, nIdent); + if ( r.GivType() >= 0) return r; + + // find the methods declared by user + + r = m_pMethod->CompileCall(name, ppParams, nIdent); + if ( r.Eq(TX_UNDEFCALL) && m_pParent != NULL ) + return m_pParent->m_pMethod->CompileCall(name, ppParams, nIdent); + return r; +} + +// executes a method + +bool CBotClass::ExecuteMethode(long& nIdent, const char* name, + CBotVar* pThis, CBotVar** ppParams, + CBotVar* &pResult, CBotStack* &pStack, + CBotToken* pToken) +{ + int ret = m_pCalls->DoCall(nIdent, name, pThis, ppParams, pResult, pStack, pToken); + if (ret>=0) return ret; + + ret = m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, this); + return ret; +} + +// restored the execution stack + +void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, + CBotVar** ppParams, CBotStack* &pStack) +{ + m_pMethod->RestoreCall(nIdent, name, pThis, ppParams, pStack, this); +} + + + + +bool CBotClass::SaveStaticState(FILE* pf) +{ + if (!WriteWord( pf, CBOTVERSION*2)) return false; + + // saves the state of static variables in classes + CBotClass* p = m_ExClass; + + while ( p != NULL ) + { + if (!WriteWord( pf, 1)) return false; + // save the name of the class + if (!WriteString( pf, p->GivName() )) return false; + + CBotVar* pv = p->GivVar(); + while( pv != NULL ) + { + if ( pv->IsStatic() ) + { + if (!WriteWord( pf, 1)) return false; + if (!WriteString( pf, pv->GivName() )) return false; + + if ( !pv->Save0State(pf)) return false; // common header + if ( !pv->Save1State(pf) ) return false; // saves as the child class + if ( !WriteWord( pf, 0)) return false; + } + pv = pv->GivNext(); + } + + if (!WriteWord( pf, 0)) return false; + p = p->m_ExNext; + } + + if (!WriteWord( pf, 0)) return false; + return true; +} + +bool CBotClass::RestoreStaticState(FILE* pf) +{ + CBotString ClassName, VarName; + CBotClass* pClass; + unsigned short w; + + if (!ReadWord( pf, w )) return false; + if ( w != CBOTVERSION*2 ) return false; + + while (true) + { + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) return true; + + if (!ReadString( pf, ClassName )) return false; + pClass = Find(ClassName); + + while (true) + { + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) break; + + CBotVar* pVar = NULL; + CBotVar* pv = NULL; + + if (!ReadString( pf, VarName )) return false; + if ( pClass != NULL ) pVar = pClass->GivItem(VarName); + + if (!CBotVar::RestoreState(pf, pv)) return false; // the temp variable + + if ( pVar != NULL ) pVar->Copy(pv); + delete pv; + } + } + return true; +} + + +///////////////////////////////////////////////////////////////////// + +CBotClassInst::CBotClassInst() +{ + m_next = NULL; + m_var = NULL; + m_Parameters = NULL; + m_expr = NULL; + m_hasParams = false; + m_nMethodeIdent = 0; + name = "CBotClassInst"; +} + +CBotClassInst::~CBotClassInst() +{ + delete m_var; +// delete m_next; // done by the destructor of the base class ~CBotInstr() +} + +// definition of pointer (s) to an object +// style +// CPoint A, B ; + +CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) +{ + // seeks the corresponding classes + if ( pClass == NULL ) + { + pStack->SetStartError(p->GivStart()); + pClass = CBotClass::Find(p); + if ( pClass == NULL ) + { + // not found? is bizare + pStack->SetError(TX_NOCLASS, p); + return NULL; + } + p = p->GivNext(); + } + + bool bIntrinsic = pClass->IsIntrinsic(); + CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); + CBotClassInst* inst = (CBotClassInst*)CompileArray(p, pStack, type); + if ( inst != NULL || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(); + + inst = new CBotClassInst(); + /// \TODO Need to be revised and fixed after adding unit tests + CBotToken token(pClass->GivName(), CBotString(), p->GivStart(), p->GivEnd()); + inst->SetToken(&token); + CBotToken* vartoken = p; + + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = type; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetStartError(vartoken->GivStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) // with any clues? + { + delete inst; // is not type CBotInt + p = vartoken; // returns to the variable name + + // compiles declaration an array + + inst = (CBotClassInst*)CBotInstArray::Compile( p, pStk, type ); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + + CBotVar* var; + var = CBotVar::Create(vartoken->GivString(), type); // creates the instance +// var->SetClass(pClass); + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // its attribute a unique number + pStack->AddVar(var); // placed on the stack + + // look if there are parameters + inst->m_hasParams = (p->GivType() == ID_OPENPAR); + + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStk, ppVars); + if ( !pStk->IsOk() ) goto error; + + // if there are parameters, is the equivalent to the stament "new" + // CPoint A ( 0, 0 ) is equivalent to + // CPoint A = new CPoint( 0, 0 ) + +// if ( NULL != inst->m_Parameters ) + if ( inst->m_hasParams ) + { + // the constructor is there? +// CBotString noname; + CBotTypResult r = pClass->CompileMethode(pClass->GivName(), var, ppVars, pStk, inst->m_nMethodeIdent); + delete pStk->TokenStack(); // releases the supplement stack + int typ = r.GivType(); + + if (typ == TX_UNDEFCALL) + { + // si le constructeur n'existe pas + if (inst->m_Parameters != NULL) // with parameters + { + pStk->SetError(TX_NOCONST, vartoken); + goto error; + } + typ = 0; + } + + if (typ>20) + { + pStk->SetError(typ, vartoken->GivEnd()); + goto error; + } + + } + + if (IsOfType(p, ID_ASS)) // with a assignment? + { + if (inst->m_hasParams) + { + pStk->SetError(TX_ENDOF, p->GivStart()); + goto error; + } + + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + CBotClass* result = pStk->GivClass(); + if ( !pStk->GivTypResult(1).Eq(CBotTypNullPointer) && + ( !pStk->GivTypResult(1).Eq(CBotTypPointer) || + ( result != NULL && !pClass->IsChildOf(result) ))) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } +// if ( !bIntrinsic ) var->SetPointer(pStk->GivVar()->GivPointer()); + if ( !bIntrinsic ) + { + // does not use the result on the stack, to impose the class + CBotVar* pvar = CBotVar::Create("", pClass); + var->SetPointer( pvar ); // variable already declared instance pointer + delete pvar; // removes the second pointer + } + var->SetInit(true); // marks the pointer as init + } + else if (inst->m_hasParams) + { + // creates the object on the "job" (\TODO "tas") + // with a pointer to the object + if ( !bIntrinsic ) + { + CBotVar* pvar = CBotVar::Create("", pClass); + var->SetPointer( pvar ); // variable already declared instance pointer + delete pvar; // removes the second pointer + } + var->SetInit(2); // marks the pointer as init + } +suite: + if (IsOfType(p, ID_COMMA)) // several chained definitions + { + if ( NULL != ( inst->m_next = CBotClassInst::Compile(p, pStk, pClass) )) // compiles the following + { + return pStack->Return(inst, pStk); + } + } + + if (IsOfType(p, ID_SEP)) // complete instruction + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } + +error: + delete inst; + return pStack->Return(NULL, pStk); +} + +// declaration of the instance of a class, for example: +// CPoint A, B; + +bool CBotClassInst::Execute(CBotStack* &pj) +{ + CBotVar* pThis = NULL; + + CBotStack* pile = pj->AddStack(this);//essential for SetState() +// if ( pile == EOX ) return true; + + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + + bool bIntrincic = pClass->IsIntrinsic(); + + // creates the variable of type pointer to the object + + if ( pile->GivState()==0) + { + CBotString name = m_var->m_token.GivString(); + if ( bIntrincic ) + { + pThis = CBotVar::Create(name, CBotTypResult( CBotTypIntrinsic, pClass )); + } + else + { + pThis = CBotVar::Create(name, CBotTypResult( CBotTypPointer, pClass )); + } + + pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // its attribute as unique number + pile->AddVar(pThis); // place on the stack + pile->IncState(); + } + + if ( pThis == NULL ) pThis = pile->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); + + if ( pile->GivState()<3) + { + // ss there an assignment or parameters (contructor) + +// CBotVarClass* pInstance = NULL; + + if ( m_expr != NULL ) + { + // evaluates the expression for the assignment + if (!m_expr->Execute(pile)) return false; + + if ( bIntrincic ) + { + CBotVar* pv = pile->GivVar(); + if ( pv == NULL || pv->GivPointer() == NULL ) + { + pile->SetError(TX_NULLPT, &m_token); + return pj->Return(pile); + } + pThis->Copy(pile->GivVar(), false); + } + else + { + CBotVarClass* pInstance; + pInstance = ((CBotVarPointer*)pile->GivVar())->GivPointer(); // value for the assignment + pThis->SetPointer(pInstance); + } + pThis->SetInit(true); + } + + else if ( m_hasParams ) + { + // evaluates the constructor of an instance + + if ( !bIntrincic && pile->GivState() == 1) + { + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + + // creates an instance of the requested class + + CBotVarClass* pInstance; + pInstance = (CBotVarClass*)CBotVar::Create("", pClass); + pThis->SetPointer(pInstance); + delete pInstance; + + pile->IncState(); + } + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates the parameters + // and places the values ​​on the stack + // to (can) be interrupted (broken) at any time + + if ( p != NULL) while ( true ) + { + pile2 = pile2->AddStack(); // place on the stack for the results + if ( pile2->GivState() == 0 ) + { + if (!p->Execute(pile2)) return false; // interrupted here? + pile2->SetState(1); + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + // creates a variable for the result + CBotVar* pResult = NULL; // constructor still void + + if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interrupt + + pThis->SetInit(true); + pThis->ConstructorSet(); // indicates that the constructor has been called + pile->Return(pile2); // releases a piece of stack + +// pInstance = pThis->GivPointer(); + + } + +// if ( !bIntrincic ) pThis->SetPointer(pInstance); // a pointer to the instance + + pile->SetState(3); // finished this part + } + + if ( pile->IfStep() ) return false; + + if ( m_next2b != NULL && + !m_next2b->Execute(pile)) return false; // other (s) definition (s) + + return pj->Return( pile ); // transmits below (further) +} + + + +void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotVar* pThis = NULL; + + CBotStack* pile = pj; + if ( bMain ) pile = pj->RestoreStack(this); + if ( pile == NULL ) return; + + // creates the variable of type pointer to the object + { + CBotString name = m_var->m_token.GivString(); + pThis = pile->FindVar(name); + pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // its attribute a unique number + } + + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + bool bIntrincic = pClass->IsIntrinsic(); + + if ( bMain && pile->GivState()<3) + { + // is there an assignment or parameters (constructor) + +// CBotVarClass* pInstance = NULL; + + if ( m_expr != NULL ) + { + // evaluates the expression for the assignment + m_expr->RestoreState(pile, bMain); + return; + } + + else if ( m_hasParams ) + { + // evaluates the constructor of an instance + + if ( !bIntrincic && pile->GivState() == 1) + { + return; + } + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates the parameters + // and the values an the stack + // for the ability to be interrupted at any time (\TODO pour pouvoir être interrompu n'importe quand) + + if ( p != NULL) while ( true ) + { + pile2 = pile2->RestoreStack(); // place on the stack for the results + if ( pile2 == NULL ) return; + + if ( pile2->GivState() == 0 ) + { + p->RestoreState(pile2, bMain); // interrupted here? + return; + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + // creates a variable for the result + CBotVar* pResult = NULL; // constructor still void + + pClass->RestoreMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, pile2); + return; + } + } + + if ( m_next2b != NULL ) + m_next2b->RestoreState(pile, bMain); // other(s) definition(s) +} + + +// test if a procedure name is already defined somewhere + +bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) +{ + CBotString name = pToken->GivString(); + + if ( CBotCall::CheckCall(name) ) return true; + + CBotFunction* pp = m_pMethod; + while ( pp != NULL ) + { + if ( pToken->GivString() == pp->GivName() ) + { + // are their parameters exactly the same? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->Next(); + } + + return false; +} + -- cgit v1.2.3-1-g7c22 From 0919796df7ab9025bb53ef2dc56d0888de8c7e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopacki?= Date: Wed, 8 Aug 2012 02:01:06 +0200 Subject: Transation of comments complete --- src/CBot/CBotAddExpr.cpp | 286 ++-- src/CBot/CBotCompExpr.cpp | 264 ++-- src/CBot/CBotDll.h | 2234 ++++++++++++++-------------- src/CBot/CBotFunction.cpp | 3291 +++++++++++++++++++++--------------------- src/CBot/CBotStack.cpp | 2942 ++++++++++++++++++------------------- src/CBot/CBotString.cpp | 3 +- src/CBot/CBotToken.h | 1 + src/CBot/CBotTwoOpExpr.cpp | 1134 +++++++-------- src/CBot/ClassFILE.cpp | 64 +- src/CBot/StringFunctions.cpp | 870 +++++------ 10 files changed, 5551 insertions(+), 5538 deletions(-) diff --git a/src/CBot/CBotAddExpr.cpp b/src/CBot/CBotAddExpr.cpp index d94946e..8e4ed85 100644 --- a/src/CBot/CBotAddExpr.cpp +++ b/src/CBot/CBotAddExpr.cpp @@ -1,142 +1,144 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////// -// expression du genre Oprande1 + Oprande2 -// Oprande1 - Oprande2 - -#include "CBot.h" - -// divers constructeurs - -CBotAddExpr::CBotAddExpr() -{ - m_leftop = - m_rightop = NULL; // NULL pour pouvoir faire delete sans autre - name = "CBotAddExpr"; // debug -} - -CBotAddExpr::~CBotAddExpr() -{ - delete m_leftop; - delete m_rightop; -} - - -// compile une instruction de type A + B - -CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack) -{ - CBotStack* pStk = pStack->TokenStack(); // un bout de pile svp - - // cherche des instructions qui peuvent convenir gauche de l'opration + ou - - - CBotInstr* left = CBotMulExpr::Compile( p, pStk ); // expression A * B gauche - if (left == NULL) return pStack->Return(NULL, pStk); // si erreur, la transmet - - // est-ce qu'on a le token + ou - ensuite ? - - if ( p->GetType() == ID_ADD || - p->GetType() == ID_SUB) // plus ou moins - { - CBotAddExpr* inst = new CBotAddExpr(); // lment pour opration - inst->SetToken(p); // mmorise l'opration - - int type1, type2; - type1 = pStack->GetType(); // de quel type le premier oprande ? - - p = p->Next(); // saute le token de l'opration - - // cherche des instructions qui peuvent convenir droite - - if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression (...) droite - { - // il y a un second oprande acceptable - - type2 = pStack->GetType(); // de quel type le rsultat ? - - if ( type1 == type2 ) // les rsultats sont-ils compatibles - { - // si ok, enregistre l'oprande dans l'objet - inst->m_leftop = left; - // et rend l'object qui l'a demand - return pStack->Return(inst, pStk); - } - } - - // en cas d'erreur, libre les lments - delete left; - delete inst; - // et transmet l'erreur qui se trouve sur la pile - return pStack->Return(NULL, pStk); - } - - // si on n'a pas affaire une opration + ou - - // rend qui l'a demand, l'oprande (de gauche) trouv - // la place de l'objet "addition" - return pStack->Return(left, pStk); -} - - - - -// fait l'opration d'addition ou de soustraction - -bool CBotAddExpr::Execute(CBotStack* &pStack) -{ - CBotStack* pStk1 = pStack->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pSk1 == EOX ) return TRUE; - - - // selon la reprise, on peut tre dans l'un des 2 tats - - if ( pStk1->GetState() == 0 && // 1er tat, value l'oprande de gauche - !m_leftop->Execute(pStk1) ) return FALSE; // interrompu ici ? - - // passe l'tape suivante - pStk1->SetState(1); // prt pour la suite - - // demande un peu plus de stack pour ne pas toucher le rsultat de gauche - // qui se trouve sur la pile, justement. - - CBotStack* pStk2 = pStk1->AddStack(); // ajoute un lment la pile - // ou le retrouve en cas de reprise - - // 2e tat, value l'oprande de droite - if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrompu ici ? - - int type1 = pStk1->GetType(); // de quels types les rsultats ? - int type2 = pStk2->GetType(); - - // cre une variable temporaire pour y mettre le rsultat - CBotVar* result = new CBotVar( NULL, MAX(type1, type2)); - - // fait l'opration selon la demande - switch (GetTokenType()) - { - case ID_ADD: - result->Add(pStk1->GetVar(), pStk2->GetVar()); // additionne - break; - case ID_SUB: - result->Sub(pStk1->GetVar(), pStk2->GetVar()); // soustrait - break; - } - pStk2->SetVar(result); // met le rsultat sur la pile - - pStk1->Return(pStk2); // libre la pile - return pStack->Return(pStk1); // transmet le rsultat -} - - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/////////////////////////////////////////////////// +// expressions of type Operand1 + Operand2 +// Operand1 - Operand2 + +#include "CBot.h" + +// various constructors + +CBotAddExpr::CBotAddExpr() +{ + m_leftop = + m_rightop = NULL; // NULL to be able to delete without further + name = "CBotAddExpr"; // debug +} + +CBotAddExpr::~CBotAddExpr() +{ + delete m_leftop; + delete m_rightop; +} + + +// compile une instruction de type A + B + +CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack) +{ + CBotStack* pStk = pStack->TokenStack(); // one end of stack please + + // looking statements that may be suitable to the left of the operation + or - + + CBotInstr* left = CBotMulExpr::Compile( p, pStk ); // expression A * B left + if (left == NULL) return pStack->Return(NULL, pStk); // if error, transmit + + // do we have the token + or - next? + + if ( p->GetType() == ID_ADD || + p->GetType() == ID_SUB) // more or less + { + CBotAddExpr* inst = new CBotAddExpr(); // element for operation + inst->SetToken(p); // stores the operation + + int type1, type2; + type1 = pStack->GetType(); // what kind of the first operand? + + p = p->Next(); // skip the token of the operation + + // looking statements that may be suitable for right + + if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression (...) rigth + { + // there is an acceptable second operand + + type2 = pStack->GetType(); // what kind of results? + + if ( type1 == type2 ) // are the results consistent ? + { + // ok so, saves the operand in the object + inst->m_leftop = left; + // and makes the object on demand + return pStack->Return(inst, pStk); + } + } + + // in case of error, free the elements + delete left; + delete inst; + // and transmits the error that is on the stack + return pStack->Return(NULL, pStk); + } + + // if we are not dealing with an operation + or - + // goes to that requested, the operand (left) found + // place the object "addition" + return pStack->Return(left, pStk); +} + + + + +// operation is addition or subtraction + +bool CBotAddExpr::Execute(CBotStack* &pStack) +{ + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or is found in case of recovery +// if ( pSk1 == EOX ) return TRUE; + + + // according to recovery, it may be in one of two states + + if ( pStk1->GetState() == 0 && // first state, evaluates the left operand + !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here? + + // passes to the next step + pStk1->SetState(1); // ready for further + + // requires a little more stack to not touch the result of the left + // which is on the stack, precisely. + + CBotStack* pStk2 = pStk1->AddStack(); // adds an item to the stack + // or is found in case of recovery + + // Second state, evaluates the right operand + if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here? + + int type1 = pStk1->GetType(); // what kind of results? + int type2 = pStk2->GetType(); + + // creates a temporary variable to put the result + CBotVar* result = new CBotVar( NULL, MAX(type1, type2)); + + // is the operation as requested + switch (GetTokenType()) + { + case ID_ADD: + result->Add(pStk1->GetVar(), pStk2->GetVar()); // addition + break; + case ID_SUB: + result->Sub(pStk1->GetVar(), pStk2->GetVar()); // subtraction + break; + } + pStk2->SetVar(result); // puts the result on the stack + + pStk1->Return(pStk2); // frees the stack + return pStack->Return(pStk1); // transmits the result +} + + diff --git a/src/CBot/CBotCompExpr.cpp b/src/CBot/CBotCompExpr.cpp index 0f296d5..8ae507f 100644 --- a/src/CBot/CBotCompExpr.cpp +++ b/src/CBot/CBotCompExpr.cpp @@ -1,131 +1,133 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////// -// expression du genre Oprande1 > Oprande2 -// Oprande1 != Oprande2 -// etc. - -#include "CBot.h" - -// divers constructeurs - -CBotCompExpr::CBotCompExpr() -{ - m_leftop = - m_rightop = NULL; - name = "CBotCompExpr"; -} - -CBotCompExpr::~CBotCompExpr() -{ - delete m_leftop; - delete m_rightop; -} - -fichier plus utilise; - -// compile une instruction de type A < B - -CBotInstr* CBotCompExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->AddStack(); - - CBotInstr* left = CBotAddExpr::Compile( p, pStk ); // expression A + B gauche - if (left == NULL) return pStack->Return(NULL, pStk); // erreur - - if ( p->GetType() == ID_HI || - p->GetType() == ID_LO || - p->GetType() == ID_HS || - p->GetType() == ID_LS || - p->GetType() == ID_EQ || - p->GetType() == ID_NE) // les diverses comparaisons - { - CBotCompExpr* inst = new CBotCompExpr(); // lment pour opration - inst->SetToken(p); // mmorise l'opration - - int type1, type2; - type1 = pStack->GetType(); - - p = p->Next(); - if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression A + B droite - { - type2 = pStack->GetType(); - // les rsultats sont-ils compatibles - if ( type1 == type2 ) - { - inst->m_leftop = left; - pStk->SetVar(new CBotVar(NULL, CBotTypBoolean)); - // le rsultat est un boolean - return pStack->Return(inst, pStk); - } - } - - delete left; - delete inst; - return pStack->Return(NULL, pStk); - } - - return pStack->Return(left, pStk); -} - - -// fait l'opration - -bool CBotCompExpr::Execute(CBotStack* &pStack) -{ - CBotStack* pStk1 = pStack->AddStack(this); -// if ( pStk1 == EOX ) return TRUE; - - if ( pStk1->GetState() == 0 && !m_leftop->Execute(pStk1) ) return FALSE; // interrompu ici ? - - pStk1->SetState(1); // opration termine - - // demande un peu plus de stack pour ne pas toucher le rsultat de gauche - CBotStack* pStk2 = pStk1->AddStack(); - - if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrompu ici ? - - int type1 = pStk1->GetType(); - int type2 = pStk2->GetType(); - - CBotVar* result = new CBotVar( NULL, CBotTypBoolean ); - - switch (GetTokenType()) - { - case ID_LO: - result->Lo(pStk1->GetVar(), pStk2->GetVar()); // infrieur - break; - case ID_HI: - result->Hi(pStk1->GetVar(), pStk2->GetVar()); // suprieur - break; - case ID_LS: - result->Ls(pStk1->GetVar(), pStk2->GetVar()); // infrieur ou gal - break; - case ID_HS: - result->Hs(pStk1->GetVar(), pStk2->GetVar()); // suprieur ou gal - break; - case ID_EQ: - result->Eq(pStk1->GetVar(), pStk2->GetVar()); // gal - break; - case ID_NE: - result->Ne(pStk1->GetVar(), pStk2->GetVar()); // diffrent - break; - } - pStk2->SetVar(result); // met le rsultat sur la pile - - pStk1->Return(pStk2); // libre la pile - return pStack->Return(pStk1); // transmet le rsultat -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/////////////////////////////////////////////////// +// expression of type Opérande1 > Opérande2 +// Opérande1 != Opérande2 +// etc. + +#include "CBot.h" + +// various constructeurs + +CBotCompExpr::CBotCompExpr() +{ + m_leftop = + m_rightop = NULL; + name = "CBotCompExpr"; +} + +CBotCompExpr::~CBotCompExpr() +{ + delete m_leftop; + delete m_rightop; +} + +fichier plus utilise; + +// compile instruction of type A < B + +CBotInstr* CBotCompExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->AddStack(); + + CBotInstr* left = CBotAddExpr::Compile( p, pStk ); // expression A + B left + if (left == NULL) return pStack->Return(NULL, pStk); // error + + if ( p->GetType() == ID_HI || + p->GetType() == ID_LO || + p->GetType() == ID_HS || + p->GetType() == ID_LS || + p->GetType() == ID_EQ || + p->GetType() == ID_NE) // the various comparisons + { + CBotCompExpr* inst = new CBotCompExpr(); // element for operation + inst->SetToken(p); // stores the operation + + int type1, type2; + type1 = pStack->GetType(); + + p = p->Next(); + if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression A + B right + { + type2 = pStack->GetType(); + // are the results compatible + if ( type1 == type2 ) + { + inst->m_leftop = left; + pStk->SetVar(new CBotVar(NULL, CBotTypBoolean)); + // the result is a boolean + return pStack->Return(inst, pStk); + } + } + + delete left; + delete inst; + return pStack->Return(NULL, pStk); + } + + return pStack->Return(left, pStk); +} + + +// perform the operation + +bool CBotCompExpr::Execute(CBotStack* &pStack) +{ + CBotStack* pStk1 = pStack->AddStack(this); +// if ( pStk1 == EOX ) return TRUE; + + if ( pStk1->GetState() == 0 && !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here ? + + pStk1->SetState(1); // finished + + // requires a little more stack to not touch the result of the left + CBotStack* pStk2 = pStk1->AddStack(); + + if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here ? + + int type1 = pStk1->GetType(); + int type2 = pStk2->GetType(); + + CBotVar* result = new CBotVar( NULL, CBotTypBoolean ); + + switch (GetTokenType()) + { + case ID_LO: + result->Lo(pStk1->GetVar(), pStk2->GetVar()); // lower + break; + case ID_HI: + result->Hi(pStk1->GetVar(), pStk2->GetVar()); // higher + break; + case ID_LS: + result->Ls(pStk1->GetVar(), pStk2->GetVar()); // lower or equal + break; + case ID_HS: + result->Hs(pStk1->GetVar(), pStk2->GetVar()); // higher of equal + break; + case ID_EQ: + result->Eq(pStk1->GetVar(), pStk2->GetVar()); // equal + break; + case ID_NE: + result->Ne(pStk1->GetVar(), pStk2->GetVar()); // not equal + break; + } + pStk2->SetVar(result); // puts the result on the stack + + pStk1->Return(pStk2); // frees the stack + return pStack->Return(pStk1); // transmit the result +} + diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 47388a6..269ef94 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -1,1117 +1,1117 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. -//////////////////////////////////////////////////////////////////////// - -#pragma once -#ifndef _CBOTDLL_H_ -#define _CBOTDLL_H_ -/** - * \file CBotDll.h - * \brief Library for interpretation of CBOT language - */ - -#include -#include "resource.h" -#include -#include - - -#define CBOTVERSION 104 - -//////////////////////////////////////////////////////////////////////// -// forward declaration of needed classes - -class CBotToken; // program turned into "tokens -class CBotStack; // for the execution stack -class CBotClass; // class of object -class CBotInstr; // instruction to be executed -class CBotFunction; // user functions -class CBotVar; // variables -class CBotVarClass; // instance of class -class CBotVarPointer; // pointer to an instance of class -class CBotCall; // functions -class CBotCallMethode; // methods -class CBotDefParam; // parameter list -class CBotCStack; // stack - - -//////////////////////////////////////////////////////////////////////// -// Variables management -//////////////////////////////////////////////////////////////////////// - -/** \brief CBotType Defines known types. This types are modeled on Java types. Do not change the order of elements */ -enum CBotType -{ - CBotTypVoid = 0, - CBotTypByte = 1, //n - CBotTypShort = 2, //n - CBotTypChar = 3, //n - CBotTypInt = 4, - CBotTypLong = 5, //n - CBotTypFloat = 6, - CBotTypDouble = 7, //n - CBotTypBoolean = 8, - CBotTypString = 9, - - CBotTypArrayPointer = 10, // array of variables - CBotTypArrayBody = 11, // same but creates an instance - - CBotTypPointer = 12, // pointer to an instance - CBotTypNullPointer = 13, // null pointer is special - CBotTypClass = 15, - CBotTypIntrinsic = 16 // instance of a class intrinsic -}; -//n = not implemented yet - -// for SetUserPtr when deleting an object -#define OBJECTDELETED ((void*)-1) -// value set before initialization -#define OBJECTCREATED ((void*)-2) - - -/** \brief CBotTypResult class to define the complete type of a result*/ -class CBotTypResult -{ -public: - /** - * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) - * \param type type of created result, see CBotType - */ - CBotTypResult(int type); - // for simple types (CBotTypInt à CBotTypString) - - - CBotTypResult(int type, const char* name); - // for pointer types and intrinsic classes - - CBotTypResult(int type, CBotClass* pClass); - // for the instance of a class - - CBotTypResult(int type, CBotTypResult elem); - // for arrays of variables - - CBotTypResult(const CBotTypResult& typ); - // for assignments - - CBotTypResult(); - // for default - - ~CBotTypResult(); - - int GivType(int mode = 0) const; - // returns type CBotType* as a result - - void SetType(int n); - // modifies a type - - CBotClass* GivClass() const; - // makes the pointer to the class (for CBotTypClass, CBotTypPointer) - - int GivLimite() const; - // returns limit size of table (CBotTypArray) - - void SetLimite(int n); - // set limit to the table - - void SetArray(int* max ); - // set limits for a list of dimensions (arrays of arrays) - - CBotTypResult& GivTypElem() const; - // returns type of array elements (CBotTypArray) - // rend le type des éléments du tableau (CBotTypArray) - - bool Compare(const CBotTypResult& typ) const; - // compares whether the types are compatible - bool Eq(int type) const; - // compare type - - CBotTypResult& operator=(const CBotTypResult& src); - // copy a complete type in another - -private: - int m_type; - CBotTypResult* m_pNext; // for the types of type - CBotClass* m_pClass; // for the derivatives of class - int m_limite; // limits of tables - friend class CBotVarClass; - friend class CBotVarPointer; -}; - -/* -// to define a result as output, using for example - - // to return a simple Float - return CBotTypResult( CBotTypFloat ); - - - // to return a string array - return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); - - // to return un array of array of "point" class - CBotTypResult typPoint( CBotTypIntrinsic, "point" ); - CBotTypResult arrPoint( CBotTypArray, typPoint ); - return CBotTypResult( CBotTypArray, arrPoint ); -*/ - - -//////////////////////////////////////////////////////////////////////// -// Error Handling of compilation and execution -//////////////////////////////////////////////////////////////////////// - -// Here are the list of errors that can be returned by the module -// for compilation - -#define CBotErrOpenPar 5000 // missing the opening parenthesis -#define CBotErrClosePar 5001 // missing the closing parenthesis -#define CBotErrNotBoolean 5002 // expression must be a boolean -#define CBotErrUndefVar 5003 // undeclared variable -#define CBotErrBadLeft 5004 // assignment impossible ( 5 = ... ) -#define CBotErrNoTerminator 5005 // semicolon expected -#define CBotErrCaseOut 5006 // case outside a switch -// CBotErrNoTerm 5007, plus utile -#define CBotErrCloseBlock 5008 // missing " } " -#define CBotErrElseWhitoutIf 5009 // else without matching if -#define CBotErrOpenBlock 5010 // missing " { " -#define CBotErrBadType1 5011 // wrong type for the assignment -#define CBotErrRedefVar 5012 // redefinition of the variable -#define CBotErrBadType2 5013 // Two operands are incompatible -#define CBotErrUndefCall 5014 // routine undefined -#define CBotErrNoDoubleDots 5015 // " : " expected -// CBotErrWhile 5016, plus utile -#define CBotErrBreakOutside 5017 // break outside of a loop -#define CBotErrUndefLabel 5019 // label udnefined -#define CBotErrLabel 5018 // label ne peut se mettre ici (label can not get here) -#define CBotErrNoCase 5020 // missing " case " -#define CBotErrBadNum 5021 // expected number -#define CBotErrVoid 5022 // " void " not possible here -#define CBotErrNoType 5023 // type declaration expected -#define CBotErrNoVar 5024 // variable name expected -#define CBotErrNoFunc 5025 // expected function name -#define CBotErrOverParam 5026 // too many parameters -#define CBotErrRedefFunc 5027 // this function already exists -#define CBotErrLowParam 5028 // not enough parameters -#define CBotErrBadParam 5029 // wrong types of parameters -#define CBotErrNbParam 5030 // wrong number of parameters -#define CBotErrUndefItem 5031 // element does not exist in the class -#define CBotErrUndefClass 5032 // variable is not a class -#define CBotErrNoConstruct 5033 // no appropriate constructor -#define CBotErrRedefClass 5034 // class already exists -#define CBotErrCloseIndex 5035 // " ] " expected -#define CBotErrReserved 5036 // reserved word (for a DefineNum) -#define CBotErrBadNew 5037 // wrong setting for new -#define CBotErrOpenIndex 5038 // " [ " expected -#define CBotErrBadString 5039 // expected string -#define CBotErrBadIndex 5040 // wrong index type "[ false ]" -#define CBotErrPrivate 5041 // protected item -#define CBotErrNoPublic 5042 // missing word "public" - -// here is the list of errors that can be returned by the module -// for the execution - -#define CBotErrZeroDiv 6000 // division by zero -#define CBotErrNotInit 6001 // uninitialized variable -#define CBotErrBadThrow 6002 // throw a negative value -#define CBotErrNoRetVal 6003 // function did not return results -#define CBotErrNoRun 6004 // Run() without active function -#define CBotErrUndefFunc 6005 // calling a function that no longer exists -#define CBotErrNotClass 6006 // this class does not exist -#define CBotErrNull 6007 // null pointer -#define CBotErrNan 6008 // calculation with a NAN -#define CBotErrOutArray 6009 // index out of array -#define CBotErrStackOver 6010 // stack overflow -#define CBotErrDeletedPtr 6011 // pointer to an object destroyed - -#define CBotErrFileOpen 6012 // cannot open the file -#define CBotErrNotOpen 6013 // channel not open -#define CBotErrRead 6014 // error while reading -#define CBotErrWrite 6015 // writing error - - -// other values ​​may be returned -// for example exceptions returned by external routines -// and " throw " with any number. - - -//////////////////////////////////////////////////////////////////////// -// -// as part of MFC CString not used here. -// -// ( all functions are not implemented yet ) - -/** \brief CBotString Class used to work on strings */ -class CBotString -{ -public: - CBotString(); - CBotString(const char* p); - CBotString(const CBotString& p); - ~CBotString(); - - void Empty(); - bool IsEmpty() const; - int GivLength(); - int Find(const char c); - int Find(const char* lpsz); - int ReverseFind(const char c); - int ReverseFind(const char* lpsz); - bool LoadString(unsigned int id); - CBotString Mid(int nFirst, int nCount) const; - CBotString Mid(int nFirst) const; - CBotString Mid(int start, int lg=-1); - CBotString Left(int nCount) const; - CBotString Right(int nCount) const; - int Compare(const char* lpsz) const; - void MakeUpper(); - void MakeLower(); - - - /** - * \brief Overloaded oprators to work on CBotString classes - */ - const CBotString& operator=(const CBotString& stringSrc); - const CBotString& operator=(const char ch); - const CBotString& operator=(const char* pString); - const CBotString& operator+(const CBotString& str); - friend CBotString operator+(const CBotString& string, const char* lpsz); - - const CBotString& operator+=(const char ch); - const CBotString& operator+=(const CBotString& str); - bool operator==(const CBotString& str); - bool operator==(const char* p); - bool operator!=(const CBotString& str); - bool operator!=(const char* p); - bool operator>(const CBotString& str); - bool operator>(const char* p); - bool operator>=(const CBotString& str); - bool operator>=(const char* p); - bool operator<(const CBotString& str); - bool operator<(const char* p); - bool operator<=(const CBotString& str); - bool operator<=(const char* p); - - operator const char*() const; // as a C string - - -private: - - /** \brief Pointer to string */ - char* m_ptr; - - /** \brief Length of the string */ - int m_lg; - - /** \brief Keeps the string corresponding to keyword ID */ - static const std::map s_keywordString; - - /** - * \brief MapIdToString maps given ID to its string equivalent - * \param id Provided identifier - * \return string if found, else NullString - */ - static const char * MapIdToString(EID id); -}; - - -// Class used to array management - -class CBotStringArray : public CBotString -{ -private: - int m_nSize; // number of elements - int m_nMaxSize; // reserved size - CBotString* m_pData; // ^data - -public: - CBotStringArray(); - ~CBotStringArray(); - void SetSize(int nb); - int GivSize(); - void Add(const CBotString& str); - CBotString& operator[](int nIndex); - - CBotString& ElementAt(int nIndex); -}; - -// different modes for GetPosition -enum CBotGet -{ - GetPosExtern = 1, - GetPosNom = 2, - GetPosParam = 3, - GetPosBloc = 4 -}; - -//////////////////////////////////////////////////////////////////// -// main class managing CBot program -// - -class CBotProgram -{ -private: - CBotFunction* m_Prog; // the user-defined functions - CBotFunction* m_pRun; // the basic function for the execution - CBotClass* m_pClass; // classes defined in this part - CBotStack* m_pStack; // execution stack - CBotVar* m_pInstance; // instance of the parent class - friend class CBotFunction; - - int m_ErrorCode; - int m_ErrorStart; - int m_ErrorEnd; - - long m_Ident; // associated identifier - -public: - static CBotString m_DebugVarStr; // end of a debug - bool m_bDebugDD; // idem déclanchable par robot \TODO ??? - bool m_bCompileClass; - -public: - static void Init(); - // initializes the module (defined keywords for errors) - // should be done once (and only one) at the beginning - static - void Free(); - // frees the static memory areas - - static - int GivVersion(); - // gives the version of the library CBOT - - - CBotProgram(); - CBotProgram(CBotVar* pInstance); - ~CBotProgram(); - - bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); - // compiles the program given in text - // returns false if an error at compile - // see GetCompileError () to retrieve the error - // ListFonctions returns the names of functions declared as extern - // pUser can pass a pointer to routines defined by AddFunction - - void SetIdent(long n); - // associates an identifier with the instance CBotProgram - - long GivIdent(); - // gives the identifier - - int GivError(); - bool GetError(int& code, int& start, int& end); - bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); - // if true - // gives the error found in the compilation - // or execution - // delimits the start and end block where the error - // pProg lets you know what "module" has produced runtime error - static CBotString GivErrorText(int code); - - - bool Start(const char* name); - // defines what function should be executed - // returns false if the funtion name is not found - // the program does nothing, we must call Run () for this - - bool Run(void* pUser = NULL, int timer = -1); - // executes the program - // returns false if the program was suspended - // returns true if the program ended with or without error - // timer = 0 allows to advance step by step - - bool GetRunPos(const char* &FunctionName, int &start, int &end); - // gives the position in the executing program - // returns false if it is not running (program completion) - // FunctionName is a pointer made to the name of the function - // start and end position in the text of the token processing - - CBotVar* GivStackVars(const char* &FunctionName, int level); - // provides the pointer to the variables on the execution stack - // level is an input parameter, 0 for the last level, -1, -2, etc. for the other levels - // the return value (CBotVar *) is a variable list (or NULL) - // that can be processed as the list of parameters received by a routine - // FunctionName gives the name of the function where are these variables - // FunctionName == NULL means that is more in a program (depending on level) - - void Stop(); - // stops execution of the program - // therefore quits "suspend" mode - - static - void SetTimer(int n); - // defines the number of steps (parts of instructions) to done - // in Run() before rendering hand "false" \TODO avant de rendre la main "false" - - static - bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - // call this to add externally (**) - // a new function used by the program CBoT - - static - bool DefineNum(const char* name, long val); - - bool SaveState(FILE* pf); - // backup the execution status in the file - // the file must have been opened with the fopen call this dll (\TODO this library??) - // if the system crashes - bool RestoreState(FILE* pf); - // restores the state of execution from file - // the compiled program must obviously be the same - - bool GetPosition(const char* name, int& start, int& stop, - CBotGet modestart = GetPosExtern, - CBotGet modestop = GetPosBloc); - // gives the position of a routine in the original text - // the user can select the item to find from the beginning to the end - // see the above modes in CBotGet - - - CBotFunction* GivFunctions(); -}; - - -/////////////////////////////////////////////////////////////////////////////// -// routines for file management (* FILE) - FILE* fOpen(const char* name, const char* mode); - int fClose(FILE* filehandle); - size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); - size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle); - - -#if 0 -/* -(**) Note: - To define an external function, proceed as follows: - - a) define a routine for compilation - this routine receive list of parameters (no values) - and either returns a result type (CBotTyp... or 0 = void) - or an error number - b) define a routine for the execution - this routine receive list of parameters (with valeurs), - a variable to store the result (according to the given type at compile time) - - For example, a routine which calculates the mean of a parameter list */ - -int cMean(CBotVar* &pVar, CBotString& ClassName) -{ - if ( pVar == NULL ) return 6001; // there is no parameter! - - while ( pVar != NULL ) - { - if ( pVar->GivType() > CBotTypDouble ) return 6002; // this is not a number - pVar = pVar -> GivNext(); - } - - return CBotTypFloat; // the type of the result may depend on the parameters! -} - - -bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - float total = 0; - int nb = 0; - while (pVar != NULL) - { - total += pVar->GivValFloat(); - pVar = pVar->GivNext(); - nb++; - } - pResult->SetValFloat(total/nb); // returns the mean value - - return true; // operation fully completed -} - -#endif - -///////////////////////////////////////////////////////////////////////////////// -// Class for managing variables - -// may be useful to the outside of the module -// ( it is currently not expected to be able to create these objects in outer ) - -// results of GivInit() -#define IS_UNDEF 0 // undefined variable -#define IS_DEF 1 // variable defined -#define IS_NAN 999 // variable defined as not a number - -// variable type SetPrivate / IsPrivate -#define PR_PUBLIC 0 // public variable -#define PR_READ 1 // read only -#define PR_PROTECT 2 // protected (inheritance) -#define PR_PRIVATE 3 // strictly private - -class CBotVar -{ -protected: - CBotToken* m_token; // the corresponding token - - CBotVar* m_next; // list of variables - friend class CBotStack; - friend class CBotCStack; - friend class CBotInstrCall; - friend class CBotProgram; - - CBotTypResult m_type; // type of value - - int m_binit; // not initialized? - CBotVarClass* m_pMyThis; // ^ corresponding this element - void* m_pUserPtr; // ^user data if necessary - bool m_bStatic; // static element (in class) - int m_mPrivate; // element public, protected or private? - - CBotInstr* m_InitExpr; // expression for the original content - CBotInstr* m_LimExpr; // list of limits for a table - friend class CBotClass; - friend class CBotVarClass; - friend class CBotVarPointer; - friend class CBotVarArray; - - long m_ident; // unique identifier - static long m_identcpt; // counter - -public: - CBotVar(); -virtual ~CBotVar( ); // destructor - - static - CBotVar* Create( const char* name, CBotTypResult type); - // creates from a complete type - - static - CBotVar* Create( const char* name, CBotClass* pClass); - // creates from one instance of a known class - - static - CBotVar* Create( const CBotToken* name, int type ); - static - CBotVar* Create( const CBotToken* name, CBotTypResult type ); - - static - CBotVar* Create( const char* name, int type, CBotClass* pClass); - - static - CBotVar* Create( CBotVar* pVar ); - - - void SetUserPtr(void* pUser); - // associate a user pointer to an instance - - virtual void SetIdent(long UniqId); - // associates a unique identifier to an instance - // ( it is used to ensure that the id is unique) - - void* GivUserPtr(); - // makes the pointer associated with the variable - - CBotString GivName(); // the name of the variable, if known - //////////////////////////////////////////////////////////////////////////////////// - void SetName(const char* name); // changes the name of the variable - - int GivType(int mode = 0); // returns the base type (int) of the variable - // TODO check it - //////////////////////////////////////////////////////////////////////////////////////// - - CBotTypResult GivTypResult(int mode = 0); // returns the complete type of the variable - - - CBotToken* GivToken(); - void SetType(CBotTypResult& type); - - void SetInit(int bInit); // is the variable in the state IS_UNDEF, IS_DEF, IS_NAN - - int GivInit(); // gives the state of the variable - - void SetStatic(bool bStatic); - bool IsStatic(); - - void SetPrivate(int mPrivate); - bool IsPrivate(int mode = PR_PROTECT); - int GivPrivate(); - - virtual - void ConstructorSet(); - - void SetVal(CBotVar* var); // remprend une valeur - // TODO remprend value - virtual - CBotVar* GivItem(const char* name); // returns an element of a class according to its name (*) - virtual - CBotVar* GivItemRef(int nIdent); // idem à partir du n° ref - // TODO ditto from ref no. - virtual - CBotVar* GivItem(int row, bool bGrow = false); - - virtual - CBotVar* GivItemList(); // lists the elements - - CBotVar* GivStaticVar(); // makes the pointer to the variable if it is static - - bool IsElemOfClass(const char* name); - // said if the element belongs to the class "name" - // makes true if the object is a subclass - - CBotVar* GivNext(); // next variable in the list (parameters) - //////////////////////////////////////////////////////////////////////////////////////////// - - void AddNext(CBotVar* pVar); // added to a list - - virtual - void Copy(CBotVar* pSrc, bool bName = true); // makes a copy of the variable - - virtual void SetValInt(int val, const char* name = NULL); - // initialized with an integer value (#) - ///////////////////////////////////////////////////////////////////////////////// - - virtual void SetValFloat(float val); // initialized with a real value (#) - //////////////////////////////////////////////////////////////////////////////// - - virtual void SetValString(const char* p);// initialized with a string value (#) - //////////////////////////////////////////////////////////////////////////////// - - virtual int GivValInt(); // request the full value (#) - //////////////////////////////////////////////////////////////////////// - - virtual float GivValFloat(); // gets real value (#) - /////////////////////////////////////////////////////////////////////// - - virtual - CBotString GivValString(); // request the string value (#) - /////////////////////////////////////////////////////////////////////// - - virtual void SetClass(CBotClass* pClass); - virtual - CBotClass* GivClass(); - - virtual void SetPointer(CBotVar* p); - virtual - CBotVarClass* GivPointer(); -// virtual void SetIndirection(CBotVar* pVar); - - virtual void Add(CBotVar* left, CBotVar* right); // addition - virtual void Sub(CBotVar* left, CBotVar* right); // subtraction - virtual void Mul(CBotVar* left, CBotVar* right); // multiplication - virtual int Div(CBotVar* left, CBotVar* right); // division - virtual int Modulo(CBotVar* left, CBotVar* right); // remainder of division - virtual void Power(CBotVar* left, CBotVar* right); // power - - virtual bool Lo(CBotVar* left, CBotVar* right); - virtual bool Hi(CBotVar* left, CBotVar* right); - virtual bool Ls(CBotVar* left, CBotVar* right); - virtual bool Hs(CBotVar* left, CBotVar* right); - virtual bool Eq(CBotVar* left, CBotVar* right); - virtual bool Ne(CBotVar* left, CBotVar* right); - - virtual void And(CBotVar* left, CBotVar* right); - virtual void Or(CBotVar* left, CBotVar* right); - virtual void XOr(CBotVar* left, CBotVar* right); - virtual void ASR(CBotVar* left, CBotVar* right); - virtual void SR(CBotVar* left, CBotVar* right); - virtual void SL(CBotVar* left, CBotVar* right); - - virtual void Neg(); - virtual void Not(); - virtual void Inc(); - virtual void Dec(); - - - virtual bool Save0State(FILE* pf); - virtual bool Save1State(FILE* pf); - static bool RestoreState(FILE* pf, CBotVar* &pVar); - - void debug(); - -// virtual -// CBotVar* GivMyThis(); - - virtual - void Maj(void* pUser = NULL, bool bContinue = true); - - void SetUniqNum(long n); - long GivUniqNum(); - static long NextUniqNum(); -}; - -/* NOTE (#) - methods SetValInt() SetValFloat() et SetValString() - can be called with objects which are respectively integer, real or string - Always be sure of the type of the variable before calling these methods - - if ( pVar->GivType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! - - methods GivValInt(), GivValFloat() et GivValString() - use value conversions, - GivValString() works on numbers (makes the corresponding string) - but do not make GivValInt () with a string variable! -*/ - - - -//////////////////////////////////////////////////////////////////////// -// management of classes -//////////////////////////////////////////////////////////////////////// - -// class to define new classes in the language CBOT -// for example to define the class CPoint (x, y) - -class CBotClass -{ -private: - static - CBotClass* m_ExClass; // list of classes existing at a given time - CBotClass* m_ExNext; // for this general list - CBotClass* m_ExPrev; // for this general list - -private: - CBotClass* m_pParent; // parent class - CBotString m_name; // name of this class - int m_nbVar; // number of variables in the chain - CBotVar* m_pVar; // content of the class - bool m_bIntrinsic; // intrinsic class - CBotClass* m_next; // the string class - CBotCallMethode* m_pCalls; // list of methods defined in external - CBotFunction* m_pMethod; // compiled list of methods - void (*m_rMaj) ( CBotVar* pThis, void* pUser ); - friend class CBotVarClass; - int m_cptLock; // for Lock / UnLock - int m_cptOne; // Lock for reentrancy - CBotProgram* m_ProgInLock[5];// processes waiting for sync - -public: - bool m_IsDef; // mark if is set or not - - CBotClass( const char* name, - CBotClass* pParent, bool bIntrinsic = false ); // constructor - // Once a class is created, it is known - // around CBoT - // intrinsic mode gives a class that is not managed by pointers - - ~CBotClass( ); // destructor - - bool AddFunction(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - // this call allows to add as external (**) - // new method used by the objects of this class - - bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); - // defines routine to be called to update the elements of the class - - bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); - // adds an element to the class -// bool AddItem(CBotString name, CBotClass* pClass); - // the same for elements belonging to pClass - bool AddItem(CBotVar* pVar); - // adds an item by passing the pointer to an instance of a variable - // the object is taken as is, so do not destroyed - - - - // adds an element by giving an element of type CBotVar - void AddNext(CBotClass* pClass); - - CBotString GivName(); // gives the name of the class - CBotClass* GivParent(); // gives the parent class (or NULL) - - // true if a class is derived (Extends) of another - // return true also if the classes are identical - bool IsChildOf(CBotClass* pClass); - - static - CBotClass* Find(CBotToken* &pToken); // trouve une classe d'après son nom - // return a class by it's its name - static - CBotClass* Find(const char* name); - - CBotVar* GivVar(); // return the list of variables - CBotVar* GivItem(const char* name); // one of the variables according to its name - CBotVar* GivItemRef(int nIdent); - - CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, - CBotCStack* pStack, long& nIdent); - - bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); - void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); - - // compiles a class declared by the user - static - CBotClass* Compile(CBotToken* &p, CBotCStack* pStack); - static - CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack); - - bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond); - - bool IsIntrinsic(); - void Purge(); - static - void Free(); - - static - bool SaveStaticState(FILE* pf); - - static - bool RestoreStaticState(FILE* pf); - - bool Lock(CBotProgram* p); - void Unlock(); - static - void FreeLock(CBotProgram* p); - - bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); - -}; - -#define MAXDEFNUM 1000 // limited number of DefineNum - -///////////////////////////////////////////////////////////////////////////////////// -// Token management (tokens) - -#define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord) -#define TokenTypNum 2 // number -#define TokenTypString 3 // string -#define TokenTypVar 4 // a variable name -#define TokenTypDef 5 // value according DefineNum - -#define TokenKeyWord 2000 // keywords of the language -#define TokenKeyDeclare 2100 // keywords of declarations (int, float,..) -#define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan) -#define TokenKeyOp 2300 // operators - -/** \class Responsible for token management */ -class CBotToken -{ -private: - static - CBotStringArray m_ListKeyWords; // list of keywords of language - static - int m_ListIdKeyWords[200]; // the corresponding codes - - static - CBotStringArray m_ListKeyDefine; // names defined by a DefineNum - static - long m_ListKeyNums[MAXDEFNUM]; // the ​​associated values - -private: - CBotToken* m_next; // following in the list - CBotToken* m_prev; - int m_type; // type of Token - long m_IdKeyWord; // number of the keyword if it is a - // or value of the "define" - - CBotString m_Text; // word found as token - CBotString m_Sep; // following separators - - int m_start; // position in the original text (program) - int m_end; // the same for the end of the token - - /** - * \brief Check whether given parameter is a keyword - */ - static - int GivKeyWords(const char* w); // is it a keyword? - static - bool GivKeyDefNum(const char* w, CBotToken* &token); - - /** - * \brief Loads the list of keywords - */ - static - void LoadKeyWords(); - -public: - /** - * \brief Constructors - */ - CBotToken(); - CBotToken(const CBotToken* pSrc); - CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0); - CBotToken(const char* mot, const char* sep = NULL); - - /** - * \brief Destructor - */ - ~CBotToken(); - /** - * \brief Returns the type of token - */ - int GivType(); - - /** - * \brief makes the string corresponding to this token - */ - CBotString& GivString(); - - /** - * \brief makes the following separator token - */ - CBotString& GivSep(); - - /** - * \brief position of the beginning in the text - */ - int GivStart(); - /** - * \brief end position in the text - */ - int GivEnd(); - - /** - * \brief gives the next token in the list - */ - CBotToken* GivNext(); - /** - * \brief gives the previous token in a list - */ - CBotToken* GivPrev(); - - /** - * \brief transforms the entire program - */ - static - CBotToken* CompileTokens(const char* p, int& error); - - /** - * \brief releases the list - */ - static - void Delete(CBotToken* pToken); // libère la liste - - - // fonctions non utiles en export - static - bool DefineNum(const char* name, long val); - void SetString(const char* name); - - void SetPos(int start, int end); - long GivIdKey(); - /** - * \brief adds a token (a copy) - */ - void AddNext(CBotToken* p); - - /** - * finds the next token - */ - static - CBotToken* NextToken(char* &program, int& error, bool first = false); - - const CBotToken& - operator=(const CBotToken& src); - - static - void Free(); -}; - - - -#if 0 -//////////////////////////////////////////////////////////////////////// -// Examples of use -// Definition classes and functions - - -// define the global class CPoint -// -------------------------------- - m_pClassPoint = new CBotClass("CPoint", NULL); - // adds the component ".x" - m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); - // adds the component ".y" - m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); - // the player can then use the instructions - // CPoint position; position.x = 12; position.y = -13.6 - -// define class CColobotObject -// -------------------------------- -// This class manages all the objects in the world of COLOBOT -// the "main" user program belongs to this class - m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); - // adds the component ".position" - m_pClassObject->AddItem("position", m_pClassPoint); - // adds the component ".type" - m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); - // adds a definition of constant - m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT equivalent to the value 1 - // adds the FIND routine - m_pClassObject->AddFunction( rCompFind, rDoFind ); - // the player can now use the instructions - // CColobotObject chose; chose = FIND( ROBOT ) - - - -// define class CColobotRobot derived from CColobotObject -// --------------------------------------------------------- -// programs "main" associated with robots as a part of this class - m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); - // add routine GOTO - m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); - // the player can now use - // GOTO( FIND ( ROBOT ) ); - - -// creates an instance of the class Robot -// ------------------------------------ -// for example a new robot which has just been manufactured - CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); - -// compiles the program by hand for this robot -// ------------------------------------------ - CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); - if ( !m_pMonRobot->Compile( LeProgramme ) ) {error handling ...}; - -// build a stack for interpreter -// -------------------------------------- - CBotStack* pStack = new CBotStack(NULL); - -// executes the main program -// ------------------------- - while( false = m_pMonRobot->Execute( "main", pStack )) - { - // program suspended - // could be pass a handle to another (safeguarding pstack for the robot one) - }; - // programme "main" finished ! - - - - -// routine that implements the GOTO (CPoint pos) -bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) -{ - if (pVar->GivType() != CBotTypeClass || - pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) - // the parameter is not the right class? - // in fact the control is done to the routine of compilation - - m_PosToGo.Copy( pVar ); // keeps the target position (object type CBotVar) - - // or so - CBotVar* temp; - temp = pVar->GivItem("x"); // is necessary for the object of type CPoint - ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); - m_PosToGo.x = temp->GivValFloat(); - - temp = pVar->GivItem("y"); // is necessary for the object of type CPoint - ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); - m_PosToGo.y = temp->GivValFloat(); - - return (m_CurentPos == m_PosToGo); // makes true if the position is reached - // returns false if one had wait yet -} - -#endif -#endif //_CBOTDLL_H_ - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. +//////////////////////////////////////////////////////////////////////// + +#pragma once +#ifndef _CBOTDLL_H_ +#define _CBOTDLL_H_ +/** + * \file CBotDll.h + * \brief Library for interpretation of CBOT language + */ + +#include +#include "resource.h" +#include +#include + + +#define CBOTVERSION 104 + +//////////////////////////////////////////////////////////////////////// +// forward declaration of needed classes + +class CBotToken; // program turned into "tokens +class CBotStack; // for the execution stack +class CBotClass; // class of object +class CBotInstr; // instruction to be executed +class CBotFunction; // user functions +class CBotVar; // variables +class CBotVarClass; // instance of class +class CBotVarPointer; // pointer to an instance of class +class CBotCall; // functions +class CBotCallMethode; // methods +class CBotDefParam; // parameter list +class CBotCStack; // stack + + +//////////////////////////////////////////////////////////////////////// +// Variables management +//////////////////////////////////////////////////////////////////////// + +/** \brief CBotType Defines known types. This types are modeled on Java types. Do not change the order of elements */ +enum CBotType +{ + CBotTypVoid = 0, + CBotTypByte = 1, //n + CBotTypShort = 2, //n + CBotTypChar = 3, //n + CBotTypInt = 4, + CBotTypLong = 5, //n + CBotTypFloat = 6, + CBotTypDouble = 7, //n + CBotTypBoolean = 8, + CBotTypString = 9, + + CBotTypArrayPointer = 10, // array of variables + CBotTypArrayBody = 11, // same but creates an instance + + CBotTypPointer = 12, // pointer to an instance + CBotTypNullPointer = 13, // null pointer is special + CBotTypClass = 15, + CBotTypIntrinsic = 16 // instance of a class intrinsic +}; +//n = not implemented yet + +// for SetUserPtr when deleting an object +#define OBJECTDELETED ((void*)-1) +// value set before initialization +#define OBJECTCREATED ((void*)-2) + + +/** \brief CBotTypResult class to define the complete type of a result*/ +class CBotTypResult +{ +public: + /** + * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) + * \param type type of created result, see CBotType + */ + CBotTypResult(int type); + // for simple types (CBotTypInt à CBotTypString) + + + CBotTypResult(int type, const char* name); + // for pointer types and intrinsic classes + + CBotTypResult(int type, CBotClass* pClass); + // for the instance of a class + + CBotTypResult(int type, CBotTypResult elem); + // for arrays of variables + + CBotTypResult(const CBotTypResult& typ); + // for assignments + + CBotTypResult(); + // for default + + ~CBotTypResult(); + + int GivType(int mode = 0) const; + // returns type CBotType* as a result + + void SetType(int n); + // modifies a type + + CBotClass* GivClass() const; + // makes the pointer to the class (for CBotTypClass, CBotTypPointer) + + int GivLimite() const; + // returns limit size of table (CBotTypArray) + + void SetLimite(int n); + // set limit to the table + + void SetArray(int* max ); + // set limits for a list of dimensions (arrays of arrays) + + CBotTypResult& GivTypElem() const; + // returns type of array elements (CBotTypArray) + // rend le type des éléments du tableau (CBotTypArray) + + bool Compare(const CBotTypResult& typ) const; + // compares whether the types are compatible + bool Eq(int type) const; + // compare type + + CBotTypResult& operator=(const CBotTypResult& src); + // copy a complete type in another + +private: + int m_type; + CBotTypResult* m_pNext; // for the types of type + CBotClass* m_pClass; // for the derivatives of class + int m_limite; // limits of tables + friend class CBotVarClass; + friend class CBotVarPointer; +}; + +/* +// to define a result as output, using for example + + // to return a simple Float + return CBotTypResult( CBotTypFloat ); + + + // to return a string array + return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); + + // to return un array of array of "point" class + CBotTypResult typPoint( CBotTypIntrinsic, "point" ); + CBotTypResult arrPoint( CBotTypArray, typPoint ); + return CBotTypResult( CBotTypArray, arrPoint ); +*/ + + +//////////////////////////////////////////////////////////////////////// +// Error Handling of compilation and execution +//////////////////////////////////////////////////////////////////////// + +// Here are the list of errors that can be returned by the module +// for compilation + +#define CBotErrOpenPar 5000 // missing the opening parenthesis +#define CBotErrClosePar 5001 // missing the closing parenthesis +#define CBotErrNotBoolean 5002 // expression must be a boolean +#define CBotErrUndefVar 5003 // undeclared variable +#define CBotErrBadLeft 5004 // assignment impossible ( 5 = ... ) +#define CBotErrNoTerminator 5005 // semicolon expected +#define CBotErrCaseOut 5006 // case outside a switch +// CBotErrNoTerm 5007, plus utile +#define CBotErrCloseBlock 5008 // missing " } " +#define CBotErrElseWhitoutIf 5009 // else without matching if +#define CBotErrOpenBlock 5010 // missing " { " +#define CBotErrBadType1 5011 // wrong type for the assignment +#define CBotErrRedefVar 5012 // redefinition of the variable +#define CBotErrBadType2 5013 // Two operands are incompatible +#define CBotErrUndefCall 5014 // routine undefined +#define CBotErrNoDoubleDots 5015 // " : " expected +// CBotErrWhile 5016, plus utile +#define CBotErrBreakOutside 5017 // break outside of a loop +#define CBotErrUndefLabel 5019 // label udnefined +#define CBotErrLabel 5018 // label ne peut se mettre ici (label can not get here) +#define CBotErrNoCase 5020 // missing " case " +#define CBotErrBadNum 5021 // expected number +#define CBotErrVoid 5022 // " void " not possible here +#define CBotErrNoType 5023 // type declaration expected +#define CBotErrNoVar 5024 // variable name expected +#define CBotErrNoFunc 5025 // expected function name +#define CBotErrOverParam 5026 // too many parameters +#define CBotErrRedefFunc 5027 // this function already exists +#define CBotErrLowParam 5028 // not enough parameters +#define CBotErrBadParam 5029 // wrong types of parameters +#define CBotErrNbParam 5030 // wrong number of parameters +#define CBotErrUndefItem 5031 // element does not exist in the class +#define CBotErrUndefClass 5032 // variable is not a class +#define CBotErrNoConstruct 5033 // no appropriate constructor +#define CBotErrRedefClass 5034 // class already exists +#define CBotErrCloseIndex 5035 // " ] " expected +#define CBotErrReserved 5036 // reserved word (for a DefineNum) +#define CBotErrBadNew 5037 // wrong setting for new +#define CBotErrOpenIndex 5038 // " [ " expected +#define CBotErrBadString 5039 // expected string +#define CBotErrBadIndex 5040 // wrong index type "[ false ]" +#define CBotErrPrivate 5041 // protected item +#define CBotErrNoPublic 5042 // missing word "public" + +// here is the list of errors that can be returned by the module +// for the execution + +#define CBotErrZeroDiv 6000 // division by zero +#define CBotErrNotInit 6001 // uninitialized variable +#define CBotErrBadThrow 6002 // throw a negative value +#define CBotErrNoRetVal 6003 // function did not return results +#define CBotErrNoRun 6004 // Run() without active function +#define CBotErrUndefFunc 6005 // calling a function that no longer exists +#define CBotErrNotClass 6006 // this class does not exist +#define CBotErrNull 6007 // null pointer +#define CBotErrNan 6008 // calculation with a NAN +#define CBotErrOutArray 6009 // index out of array +#define CBotErrStackOver 6010 // stack overflow +#define CBotErrDeletedPtr 6011 // pointer to an object destroyed + +#define CBotErrFileOpen 6012 // cannot open the file +#define CBotErrNotOpen 6013 // channel not open +#define CBotErrRead 6014 // error while reading +#define CBotErrWrite 6015 // writing error + + +// other values ​​may be returned +// for example exceptions returned by external routines +// and " throw " with any number. + + +//////////////////////////////////////////////////////////////////////// +// +// as part of MFC CString not used here. +// +// ( all functions are not implemented yet ) + +/** \brief CBotString Class used to work on strings */ +class CBotString +{ +public: + CBotString(); + CBotString(const char* p); + CBotString(const CBotString& p); + ~CBotString(); + + void Empty(); + bool IsEmpty() const; + int GivLength(); + int Find(const char c); + int Find(const char* lpsz); + int ReverseFind(const char c); + int ReverseFind(const char* lpsz); + bool LoadString(unsigned int id); + CBotString Mid(int nFirst, int nCount) const; + CBotString Mid(int nFirst) const; + CBotString Mid(int start, int lg=-1); + CBotString Left(int nCount) const; + CBotString Right(int nCount) const; + int Compare(const char* lpsz) const; + void MakeUpper(); + void MakeLower(); + + + /** + * \brief Overloaded oprators to work on CBotString classes + */ + const CBotString& operator=(const CBotString& stringSrc); + const CBotString& operator=(const char ch); + const CBotString& operator=(const char* pString); + const CBotString& operator+(const CBotString& str); + friend CBotString operator+(const CBotString& string, const char* lpsz); + + const CBotString& operator+=(const char ch); + const CBotString& operator+=(const CBotString& str); + bool operator==(const CBotString& str); + bool operator==(const char* p); + bool operator!=(const CBotString& str); + bool operator!=(const char* p); + bool operator>(const CBotString& str); + bool operator>(const char* p); + bool operator>=(const CBotString& str); + bool operator>=(const char* p); + bool operator<(const CBotString& str); + bool operator<(const char* p); + bool operator<=(const CBotString& str); + bool operator<=(const char* p); + + operator const char*() const; // as a C string + + +private: + + /** \brief Pointer to string */ + char* m_ptr; + + /** \brief Length of the string */ + int m_lg; + + /** \brief Keeps the string corresponding to keyword ID */ + static const std::map s_keywordString; + + /** + * \brief MapIdToString maps given ID to its string equivalent + * \param id Provided identifier + * \return string if found, else NullString + */ + static const char * MapIdToString(EID id); +}; + + +// Class used to array management + +class CBotStringArray : public CBotString +{ +private: + int m_nSize; // number of elements + int m_nMaxSize; // reserved size + CBotString* m_pData; // ^data + +public: + CBotStringArray(); + ~CBotStringArray(); + void SetSize(int nb); + int GivSize(); + void Add(const CBotString& str); + CBotString& operator[](int nIndex); + + CBotString& ElementAt(int nIndex); +}; + +// different modes for GetPosition +enum CBotGet +{ + GetPosExtern = 1, + GetPosNom = 2, + GetPosParam = 3, + GetPosBloc = 4 +}; + +//////////////////////////////////////////////////////////////////// +// main class managing CBot program +// + +class CBotProgram +{ +private: + CBotFunction* m_Prog; // the user-defined functions + CBotFunction* m_pRun; // the basic function for the execution + CBotClass* m_pClass; // classes defined in this part + CBotStack* m_pStack; // execution stack + CBotVar* m_pInstance; // instance of the parent class + friend class CBotFunction; + + int m_ErrorCode; + int m_ErrorStart; + int m_ErrorEnd; + + long m_Ident; // associated identifier + +public: + static CBotString m_DebugVarStr; // end of a debug + bool m_bDebugDD; // idem déclanchable par robot \TODO ??? + bool m_bCompileClass; + +public: + static void Init(); + // initializes the module (defined keywords for errors) + // should be done once (and only one) at the beginning + static + void Free(); + // frees the static memory areas + + static + int GivVersion(); + // gives the version of the library CBOT + + + CBotProgram(); + CBotProgram(CBotVar* pInstance); + ~CBotProgram(); + + bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = NULL); + // compiles the program given in text + // returns false if an error at compile + // see GetCompileError () to retrieve the error + // ListFonctions returns the names of functions declared as extern + // pUser can pass a pointer to routines defined by AddFunction + + void SetIdent(long n); + // associates an identifier with the instance CBotProgram + + long GivIdent(); + // gives the identifier + + int GivError(); + bool GetError(int& code, int& start, int& end); + bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); + // if true + // gives the error found in the compilation + // or execution + // delimits the start and end block where the error + // pProg lets you know what "module" has produced runtime error + static CBotString GivErrorText(int code); + + + bool Start(const char* name); + // defines what function should be executed + // returns false if the funtion name is not found + // the program does nothing, we must call Run () for this + + bool Run(void* pUser = NULL, int timer = -1); + // executes the program + // returns false if the program was suspended + // returns true if the program ended with or without error + // timer = 0 allows to advance step by step + + bool GetRunPos(const char* &FunctionName, int &start, int &end); + // gives the position in the executing program + // returns false if it is not running (program completion) + // FunctionName is a pointer made to the name of the function + // start and end position in the text of the token processing + + CBotVar* GivStackVars(const char* &FunctionName, int level); + // provides the pointer to the variables on the execution stack + // level is an input parameter, 0 for the last level, -1, -2, etc. for the other levels + // the return value (CBotVar *) is a variable list (or NULL) + // that can be processed as the list of parameters received by a routine + // FunctionName gives the name of the function where are these variables + // FunctionName == NULL means that is more in a program (depending on level) + + void Stop(); + // stops execution of the program + // therefore quits "suspend" mode + + static + void SetTimer(int n); + // defines the number of steps (parts of instructions) to done + // in Run() before rendering hand "false" \TODO avant de rendre la main "false" + + static + bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + // call this to add externally (**) + // a new function used by the program CBoT + + static + bool DefineNum(const char* name, long val); + + bool SaveState(FILE* pf); + // backup the execution status in the file + // the file must have been opened with the fopen call this dll (\TODO this library??) + // if the system crashes + bool RestoreState(FILE* pf); + // restores the state of execution from file + // the compiled program must obviously be the same + + bool GetPosition(const char* name, int& start, int& stop, + CBotGet modestart = GetPosExtern, + CBotGet modestop = GetPosBloc); + // gives the position of a routine in the original text + // the user can select the item to find from the beginning to the end + // see the above modes in CBotGet + + + CBotFunction* GivFunctions(); +}; + + +/////////////////////////////////////////////////////////////////////////////// +// routines for file management (* FILE) + FILE* fOpen(const char* name, const char* mode); + int fClose(FILE* filehandle); + size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); + size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle); + + +#if 0 +/* +(**) Note: + To define an external function, proceed as follows: + + a) define a routine for compilation + this routine receive list of parameters (no values) + and either returns a result type (CBotTyp... or 0 = void) + or an error number + b) define a routine for the execution + this routine receive list of parameters (with valeurs), + a variable to store the result (according to the given type at compile time) + + For example, a routine which calculates the mean of a parameter list */ + +int cMean(CBotVar* &pVar, CBotString& ClassName) +{ + if ( pVar == NULL ) return 6001; // there is no parameter! + + while ( pVar != NULL ) + { + if ( pVar->GivType() > CBotTypDouble ) return 6002; // this is not a number + pVar = pVar -> GivNext(); + } + + return CBotTypFloat; // the type of the result may depend on the parameters! +} + + +bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + float total = 0; + int nb = 0; + while (pVar != NULL) + { + total += pVar->GivValFloat(); + pVar = pVar->GivNext(); + nb++; + } + pResult->SetValFloat(total/nb); // returns the mean value + + return true; // operation fully completed +} + +#endif + +///////////////////////////////////////////////////////////////////////////////// +// Class for managing variables + +// may be useful to the outside of the module +// ( it is currently not expected to be able to create these objects in outer ) + +// results of GivInit() +#define IS_UNDEF 0 // undefined variable +#define IS_DEF 1 // variable defined +#define IS_NAN 999 // variable defined as not a number + +// variable type SetPrivate / IsPrivate +#define PR_PUBLIC 0 // public variable +#define PR_READ 1 // read only +#define PR_PROTECT 2 // protected (inheritance) +#define PR_PRIVATE 3 // strictly private + +class CBotVar +{ +protected: + CBotToken* m_token; // the corresponding token + + CBotVar* m_next; // list of variables + friend class CBotStack; + friend class CBotCStack; + friend class CBotInstrCall; + friend class CBotProgram; + + CBotTypResult m_type; // type of value + + int m_binit; // not initialized? + CBotVarClass* m_pMyThis; // ^ corresponding this element + void* m_pUserPtr; // ^user data if necessary + bool m_bStatic; // static element (in class) + int m_mPrivate; // element public, protected or private? + + CBotInstr* m_InitExpr; // expression for the original content + CBotInstr* m_LimExpr; // list of limits for a table + friend class CBotClass; + friend class CBotVarClass; + friend class CBotVarPointer; + friend class CBotVarArray; + + long m_ident; // unique identifier + static long m_identcpt; // counter + +public: + CBotVar(); +virtual ~CBotVar( ); // destructor + + static + CBotVar* Create( const char* name, CBotTypResult type); + // creates from a complete type + + static + CBotVar* Create( const char* name, CBotClass* pClass); + // creates from one instance of a known class + + static + CBotVar* Create( const CBotToken* name, int type ); + static + CBotVar* Create( const CBotToken* name, CBotTypResult type ); + + static + CBotVar* Create( const char* name, int type, CBotClass* pClass); + + static + CBotVar* Create( CBotVar* pVar ); + + + void SetUserPtr(void* pUser); + // associate a user pointer to an instance + + virtual void SetIdent(long UniqId); + // associates a unique identifier to an instance + // ( it is used to ensure that the id is unique) + + void* GivUserPtr(); + // makes the pointer associated with the variable + + CBotString GivName(); // the name of the variable, if known + //////////////////////////////////////////////////////////////////////////////////// + void SetName(const char* name); // changes the name of the variable + + int GivType(int mode = 0); // returns the base type (int) of the variable + // TODO check it + //////////////////////////////////////////////////////////////////////////////////////// + + CBotTypResult GivTypResult(int mode = 0); // returns the complete type of the variable + + + CBotToken* GivToken(); + void SetType(CBotTypResult& type); + + void SetInit(int bInit); // is the variable in the state IS_UNDEF, IS_DEF, IS_NAN + + int GivInit(); // gives the state of the variable + + void SetStatic(bool bStatic); + bool IsStatic(); + + void SetPrivate(int mPrivate); + bool IsPrivate(int mode = PR_PROTECT); + int GivPrivate(); + + virtual + void ConstructorSet(); + + void SetVal(CBotVar* var); // remprend une valeur + // TODO remprend value + virtual + CBotVar* GivItem(const char* name); // returns an element of a class according to its name (*) + virtual + CBotVar* GivItemRef(int nIdent); // idem à partir du n° ref + // TODO ditto from ref no. + virtual + CBotVar* GivItem(int row, bool bGrow = false); + + virtual + CBotVar* GivItemList(); // lists the elements + + CBotVar* GivStaticVar(); // makes the pointer to the variable if it is static + + bool IsElemOfClass(const char* name); + // said if the element belongs to the class "name" + // makes true if the object is a subclass + + CBotVar* GivNext(); // next variable in the list (parameters) + //////////////////////////////////////////////////////////////////////////////////////////// + + void AddNext(CBotVar* pVar); // added to a list + + virtual + void Copy(CBotVar* pSrc, bool bName = true); // makes a copy of the variable + + virtual void SetValInt(int val, const char* name = NULL); + // initialized with an integer value (#) + ///////////////////////////////////////////////////////////////////////////////// + + virtual void SetValFloat(float val); // initialized with a real value (#) + //////////////////////////////////////////////////////////////////////////////// + + virtual void SetValString(const char* p);// initialized with a string value (#) + //////////////////////////////////////////////////////////////////////////////// + + virtual int GivValInt(); // request the full value (#) + //////////////////////////////////////////////////////////////////////// + + virtual float GivValFloat(); // gets real value (#) + /////////////////////////////////////////////////////////////////////// + + virtual + CBotString GivValString(); // request the string value (#) + /////////////////////////////////////////////////////////////////////// + + virtual void SetClass(CBotClass* pClass); + virtual + CBotClass* GivClass(); + + virtual void SetPointer(CBotVar* p); + virtual + CBotVarClass* GivPointer(); +// virtual void SetIndirection(CBotVar* pVar); + + virtual void Add(CBotVar* left, CBotVar* right); // addition + virtual void Sub(CBotVar* left, CBotVar* right); // subtraction + virtual void Mul(CBotVar* left, CBotVar* right); // multiplication + virtual int Div(CBotVar* left, CBotVar* right); // division + virtual int Modulo(CBotVar* left, CBotVar* right); // remainder of division + virtual void Power(CBotVar* left, CBotVar* right); // power + + virtual bool Lo(CBotVar* left, CBotVar* right); + virtual bool Hi(CBotVar* left, CBotVar* right); + virtual bool Ls(CBotVar* left, CBotVar* right); + virtual bool Hs(CBotVar* left, CBotVar* right); + virtual bool Eq(CBotVar* left, CBotVar* right); + virtual bool Ne(CBotVar* left, CBotVar* right); + + virtual void And(CBotVar* left, CBotVar* right); + virtual void Or(CBotVar* left, CBotVar* right); + virtual void XOr(CBotVar* left, CBotVar* right); + virtual void ASR(CBotVar* left, CBotVar* right); + virtual void SR(CBotVar* left, CBotVar* right); + virtual void SL(CBotVar* left, CBotVar* right); + + virtual void Neg(); + virtual void Not(); + virtual void Inc(); + virtual void Dec(); + + + virtual bool Save0State(FILE* pf); + virtual bool Save1State(FILE* pf); + static bool RestoreState(FILE* pf, CBotVar* &pVar); + + void debug(); + +// virtual +// CBotVar* GivMyThis(); + + virtual + void Maj(void* pUser = NULL, bool bContinue = true); + + void SetUniqNum(long n); + long GivUniqNum(); + static long NextUniqNum(); +}; + +/* NOTE (#) + methods SetValInt() SetValFloat() et SetValString() + can be called with objects which are respectively integer, real or string + Always be sure of the type of the variable before calling these methods + + if ( pVar->GivType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! + + methods GivValInt(), GivValFloat() et GivValString() + use value conversions, + GivValString() works on numbers (makes the corresponding string) + but do not make GivValInt () with a string variable! +*/ + + + +//////////////////////////////////////////////////////////////////////// +// management of classes +//////////////////////////////////////////////////////////////////////// + +// class to define new classes in the language CBOT +// for example to define the class CPoint (x, y) + +class CBotClass +{ +private: + static + CBotClass* m_ExClass; // list of classes existing at a given time + CBotClass* m_ExNext; // for this general list + CBotClass* m_ExPrev; // for this general list + +private: + CBotClass* m_pParent; // parent class + CBotString m_name; // name of this class + int m_nbVar; // number of variables in the chain + CBotVar* m_pVar; // content of the class + bool m_bIntrinsic; // intrinsic class + CBotClass* m_next; // the string class + CBotCallMethode* m_pCalls; // list of methods defined in external + CBotFunction* m_pMethod; // compiled list of methods + void (*m_rMaj) ( CBotVar* pThis, void* pUser ); + friend class CBotVarClass; + int m_cptLock; // for Lock / UnLock + int m_cptOne; // Lock for reentrancy + CBotProgram* m_ProgInLock[5];// processes waiting for sync + +public: + bool m_IsDef; // mark if is set or not + + CBotClass( const char* name, + CBotClass* pParent, bool bIntrinsic = false ); // constructor + // Once a class is created, it is known + // around CBoT + // intrinsic mode gives a class that is not managed by pointers + + ~CBotClass( ); // destructor + + bool AddFunction(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); + // this call allows to add as external (**) + // new method used by the objects of this class + + bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); + // defines routine to be called to update the elements of the class + + bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); + // adds an element to the class +// bool AddItem(CBotString name, CBotClass* pClass); + // the same for elements belonging to pClass + bool AddItem(CBotVar* pVar); + // adds an item by passing the pointer to an instance of a variable + // the object is taken as is, so do not destroyed + + + + // adds an element by giving an element of type CBotVar + void AddNext(CBotClass* pClass); + + CBotString GivName(); // gives the name of the class + CBotClass* GivParent(); // gives the parent class (or NULL) + + // true if a class is derived (Extends) of another + // return true also if the classes are identical + bool IsChildOf(CBotClass* pClass); + + static + CBotClass* Find(CBotToken* &pToken); // trouve une classe d'après son nom + // return a class by it's its name + static + CBotClass* Find(const char* name); + + CBotVar* GivVar(); // return the list of variables + CBotVar* GivItem(const char* name); // one of the variables according to its name + CBotVar* GivItemRef(int nIdent); + + CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, + CBotCStack* pStack, long& nIdent); + + bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); + void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); + + // compiles a class declared by the user + static + CBotClass* Compile(CBotToken* &p, CBotCStack* pStack); + static + CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack); + + bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond); + + bool IsIntrinsic(); + void Purge(); + static + void Free(); + + static + bool SaveStaticState(FILE* pf); + + static + bool RestoreStaticState(FILE* pf); + + bool Lock(CBotProgram* p); + void Unlock(); + static + void FreeLock(CBotProgram* p); + + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + +}; + +#define MAXDEFNUM 1000 // limited number of DefineNum + +///////////////////////////////////////////////////////////////////////////////////// +// Token management (tokens) + +#define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord) +#define TokenTypNum 2 // number +#define TokenTypString 3 // string +#define TokenTypVar 4 // a variable name +#define TokenTypDef 5 // value according DefineNum + +#define TokenKeyWord 2000 // keywords of the language +#define TokenKeyDeclare 2100 // keywords of declarations (int, float,..) +#define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan) +#define TokenKeyOp 2300 // operators + +/** \class Responsible for token management */ +class CBotToken +{ +private: + static + CBotStringArray m_ListKeyWords; // list of keywords of language + static + int m_ListIdKeyWords[200]; // the corresponding codes + + static + CBotStringArray m_ListKeyDefine; // names defined by a DefineNum + static + long m_ListKeyNums[MAXDEFNUM]; // the ​​associated values + +private: + CBotToken* m_next; // following in the list + CBotToken* m_prev; + int m_type; // type of Token + long m_IdKeyWord; // number of the keyword if it is a + // or value of the "define" + + CBotString m_Text; // word found as token + CBotString m_Sep; // following separators + + int m_start; // position in the original text (program) + int m_end; // the same for the end of the token + + /** + * \brief Check whether given parameter is a keyword + */ + static + int GivKeyWords(const char* w); // is it a keyword? + static + bool GivKeyDefNum(const char* w, CBotToken* &token); + + /** + * \brief Loads the list of keywords + */ + static + void LoadKeyWords(); + +public: + /** + * \brief Constructors + */ + CBotToken(); + CBotToken(const CBotToken* pSrc); + CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0); + CBotToken(const char* mot, const char* sep = NULL); + + /** + * \brief Destructor + */ + ~CBotToken(); + /** + * \brief Returns the type of token + */ + int GivType(); + + /** + * \brief makes the string corresponding to this token + */ + CBotString& GivString(); + + /** + * \brief makes the following separator token + */ + CBotString& GivSep(); + + /** + * \brief position of the beginning in the text + */ + int GivStart(); + /** + * \brief end position in the text + */ + int GivEnd(); + + /** + * \brief gives the next token in the list + */ + CBotToken* GivNext(); + /** + * \brief gives the previous token in a list + */ + CBotToken* GivPrev(); + + /** + * \brief transforms the entire program + */ + static + CBotToken* CompileTokens(const char* p, int& error); + + /** + * \brief releases the list + */ + static + void Delete(CBotToken* pToken); // libère la liste + + + // fonctions non utiles en export + static + bool DefineNum(const char* name, long val); + void SetString(const char* name); + + void SetPos(int start, int end); + long GivIdKey(); + /** + * \brief adds a token (a copy) + */ + void AddNext(CBotToken* p); + + /** + * finds the next token + */ + static + CBotToken* NextToken(char* &program, int& error, bool first = false); + + const CBotToken& + operator=(const CBotToken& src); + + static + void Free(); +}; + + + +#if 0 +//////////////////////////////////////////////////////////////////////// +// Examples of use +// Definition classes and functions + + +// define the global class CPoint +// -------------------------------- + m_pClassPoint = new CBotClass("CPoint", NULL); + // adds the component ".x" + m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); + // adds the component ".y" + m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); + // the player can then use the instructions + // CPoint position; position.x = 12; position.y = -13.6 + +// define class CColobotObject +// -------------------------------- +// This class manages all the objects in the world of COLOBOT +// the "main" user program belongs to this class + m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); + // adds the component ".position" + m_pClassObject->AddItem("position", m_pClassPoint); + // adds the component ".type" + m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); + // adds a definition of constant + m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT equivalent to the value 1 + // adds the FIND routine + m_pClassObject->AddFunction( rCompFind, rDoFind ); + // the player can now use the instructions + // CColobotObject chose; chose = FIND( ROBOT ) + + + +// define class CColobotRobot derived from CColobotObject +// --------------------------------------------------------- +// programs "main" associated with robots as a part of this class + m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); + // add routine GOTO + m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); + // the player can now use + // GOTO( FIND ( ROBOT ) ); + + +// creates an instance of the class Robot +// ------------------------------------ +// for example a new robot which has just been manufactured + CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); + +// compiles the program by hand for this robot +// ------------------------------------------ + CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); + if ( !m_pMonRobot->Compile( LeProgramme ) ) {error handling ...}; + +// build a stack for interpreter +// -------------------------------------- + CBotStack* pStack = new CBotStack(NULL); + +// executes the main program +// ------------------------- + while( false = m_pMonRobot->Execute( "main", pStack )) + { + // program suspended + // could be pass a handle to another (safeguarding pstack for the robot one) + }; + // programme "main" finished ! + + + + +// routine that implements the GOTO (CPoint pos) +bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) +{ + if (pVar->GivType() != CBotTypeClass || + pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) + // the parameter is not the right class? + // in fact the control is done to the routine of compilation + + m_PosToGo.Copy( pVar ); // keeps the target position (object type CBotVar) + + // or so + CBotVar* temp; + temp = pVar->GivItem("x"); // is necessary for the object of type CPoint + ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); + m_PosToGo.x = temp->GivValFloat(); + + temp = pVar->GivItem("y"); // is necessary for the object of type CPoint + ASSERT (temp != NULL && temp->GivType() == CBotTypFloat); + m_PosToGo.y = temp->GivValFloat(); + + return (m_CurentPos == m_PosToGo); // makes true if the position is reached + // returns false if one had wait yet +} + +#endif +#endif //_CBOTDLL_H_ + diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 363b939..756c6cb 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -1,1644 +1,1647 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////////////////////////// -// compilation des diverses fonctions dclares par l'utilisateur -// - -#include "CBot.h" - -// les divers constructeurs / destructeurs -// pour librer tout selon l'arbre tabli -CBotFunction::CBotFunction() -{ - m_Param = NULL; // liste des paramtres vide - m_Block = NULL; // le bloc d'instructions - m_next = NULL; // les fonctions peuvent tre chanes - m_bPublic = false; // fonction non publique - m_bExtern = false; // fonction non externe - m_nextpublic = NULL; - m_prevpublic = NULL; - m_pProg = NULL; -// m_nThisIdent = 0; - m_nFuncIdent = 0; - m_bSynchro = false; -} - -CBotFunction* CBotFunction::m_listPublic = NULL; - -CBotFunction::~CBotFunction() -{ - delete m_Param; // liste des paramtres vide - delete m_Block; // le bloc d'instructions - delete m_next; - - // enlve de la liste publique s'il y a lieu - if ( m_bPublic ) - { - if ( m_nextpublic != NULL ) - { - m_nextpublic->m_prevpublic = m_prevpublic; - } - if ( m_prevpublic != NULL) - { - m_prevpublic->m_nextpublic = m_nextpublic; - } - else - { - // si prev = next = null peut ne pas tre dans la liste ! - if ( m_listPublic == this ) m_listPublic = m_nextpublic; - } - } -} - -bool CBotFunction::IsPublic() -{ - return m_bPublic; -} - -bool CBotFunction::IsExtern() -{ - return m_bExtern; -} - -bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop) -{ - start = m_extern.GivStart(); - stop = m_closeblk.GivEnd(); - - if (modestart == GetPosExtern) - { - start = m_extern.GivStart(); - } - if (modestop == GetPosExtern) - { - stop = m_extern.GivEnd(); - } - if (modestart == GetPosNom) - { - start = m_token.GivStart(); - } - if (modestop == GetPosNom) - { - stop = m_token.GivEnd(); - } - if (modestart == GetPosParam) - { - start = m_openpar.GivStart(); - } - if (modestop == GetPosParam) - { - stop = m_closepar.GivEnd(); - } - if (modestart == GetPosBloc) - { - start = m_openblk.GivStart(); - } - if (modestop == GetPosBloc) - { - stop = m_closeblk.GivEnd(); - } - - return true; -} - - -CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) -{ - while ( IsOfType( p, ID_OPBRK ) ) - { - if ( !IsOfType( p, ID_CLBRK ) ) - { - pile->SetError(TX_CLBRK, p->GivStart()); - return CBotTypResult( -1 ); - } - type = CBotTypResult( CBotTypArrayPointer, type ); - } - return type; -} - -CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) -{ - CBotClass* pClass = NULL; - - switch (p->GivType()) - { - case ID_INT: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypInt )); - case ID_FLOAT: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypFloat )); - case ID_BOOLEAN: - case ID_BOOL: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypBoolean )); - case ID_STRING: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypString )); - case ID_VOID: - p = p->GivNext(); - return CBotTypResult( 0 ); - - case TokenTypVar: - pClass = CBotClass::Find(p); - if ( pClass != NULL) - { - p = p->GivNext(); - return ArrayType(p, pile, - pClass->IsIntrinsic() ? - CBotTypResult( CBotTypIntrinsic, pClass ) : - CBotTypResult( CBotTypPointer, pClass ) ); - } - } - return CBotTypResult( -1 ); -} - -// compile une nouvelle fonction -// bLocal permet de mettre la dclaration des paramtres au mme niveau -// que le lments appartenant la classe pour les mthodes -CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, bool bLocal) -{ - CBotToken* pp; - CBotFunction* func = finput; - if ( func == NULL ) func = new CBotFunction(); - - CBotCStack* pStk = pStack->TokenStack(p, bLocal); - -// func->m_nFuncIdent = CBotVar::NextUniqNum(); - - while (true) - { - if ( IsOfType(p, ID_PUBLIC) ) - { - func->m_bPublic = true; - continue; - } - pp = p; - if ( IsOfType(p, ID_EXTERN) ) - { - func->m_extern = pp; // pour la position du mot "extern" - func->m_bExtern = true; -// func->m_bPublic = true; // donc aussi publique! - continue; - } - break; - } - - func->m_retToken = *p; -// CBotClass* pClass; - func->m_retTyp = TypeParam(p, pStk); // type du rsultat - - if (func->m_retTyp.GivType() >= 0) - { - CBotToken* pp = p; - func->m_token = *p; - - if ( IsOfType(p, ID_NOT) ) - { - CBotToken d("~" + p->GivString()); - func->m_token = d; - } - - // un nom de fonction est-il l ? - if (IsOfType(p, TokenTypVar)) - { - if ( IsOfType( p, ID_DBLDOTS ) ) // mthode pour une classe - { - func->m_MasterClass = pp->GivString(); - CBotClass* pClass = CBotClass::Find(pp); - if ( pClass == NULL ) goto bad; - -// pp = p; - func->m_token = *p; - if (!IsOfType(p, TokenTypVar)) goto bad; - - } - func->m_openpar = p; - func->m_Param = CBotDefParam::Compile( p, pStk ); - func->m_closepar = p->GivPrev(); - if (pStk->IsOk()) - { - pStk->SetRetType(func->m_retTyp); // pour savoir de quel type les return - - if (!func->m_MasterClass.IsEmpty()) - { - // rend "this" connu - CBotVar* pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, func->m_MasterClass )); - pThis->SetInit(2); -// pThis->SetUniqNum(func->m_nThisIdent = -2); //CBotVar::NextUniqNum() va pas - pThis->SetUniqNum(-2); - pStk->AddVar(pThis); - - // initialise les variables selon This - // n'enregistre que le pointeur la premire, - // le reste est chain - CBotVar* pv = pThis->GivItemList(); -// int num = 1; - while (pv != NULL) - { - CBotVar* pcopy = CBotVar::Create(pv); -// pcopy->SetInit(2); - pcopy->Copy(pv); - pcopy->SetPrivate(pv->GivPrivate()); -// pcopy->SetUniqNum(pv->GivUniqNum()); //num++); - pStk->AddVar(pcopy); - pv = pv->GivNext(); - } - } - - // et compile le bloc d'instruction qui suit - func->m_openblk = p; - func->m_Block = CBotBlock::Compile(p, pStk, false); - func->m_closeblk = p->GivPrev(); - if ( pStk->IsOk() ) - { - if ( func->m_bPublic ) // fonction publique, la rend connue pour tous - { - CBotFunction::AddPublic(func); - } - return pStack->ReturnFunc(func, pStk); - } - } - } -bad: - pStk->SetError(TX_NOFONC, p); - } - pStk->SetError(TX_NOTYP, p); - if ( finput == NULL ) delete func; - return pStack->ReturnFunc(NULL, pStk); -} - -// pr-compile une nouvelle fonction -CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) -{ - CBotFunction* func = new CBotFunction(); - func->m_nFuncIdent = CBotVar::NextUniqNum(); - - CBotCStack* pStk = pStack->TokenStack(p, true); - - while (true) - { - if ( IsOfType(p, ID_PUBLIC) ) - { - // func->m_bPublic = true; // sera fait en passe 2 - continue; - } - if ( IsOfType(p, ID_EXTERN) ) - { - func->m_bExtern = true; - continue; - } - break; - } - - func->m_retToken = *p; - func->m_retTyp = TypeParam(p, pStack); // type du rsultat - - if (func->m_retTyp.GivType() >= 0) - { - CBotToken* pp = p; - func->m_token = *p; - // un nom de fonction est-il l ? - if (IsOfType(p, TokenTypVar)) - { - if ( IsOfType( p, ID_DBLDOTS ) ) // mthode pour une classe - { - func->m_MasterClass = pp->GivString(); - CBotClass* pClass = CBotClass::Find(pp); - if ( pClass == NULL ) - { - pStk->SetError(TX_NOCLASS, pp); - goto bad; - } - - pp = p; - func->m_token = *p; - if (!IsOfType(p, TokenTypVar)) goto bad; - - } - func->m_Param = CBotDefParam::Compile( p, pStk ); - if (pStk->IsOk()) - { - // regarde si la fonction existe ailleurs - if (( pClass != NULL || !pStack->CheckCall(pp, func->m_Param)) && - ( pClass == NULL || !pClass->CheckCall(pp, func->m_Param)) ) - { - if (IsOfType(p, ID_OPBLK)) - { - int level = 1; - // et saute le bloc d'instructions qui suit - do - { - int type = p->GivType(); - p = p->GivNext(); - if (type == ID_OPBLK) level++; - if (type == ID_CLBLK) level--; - } - while (level > 0 && p != NULL); - - return pStack->ReturnFunc(func, pStk); - } - pStk->SetError(TX_OPENBLK, p); - } - } - pStk->SetError(TX_REDEF, pp); - } -bad: - pStk->SetError(TX_NOFONC, p); - } - pStk->SetError(TX_NOTYP, p); - delete func; - return pStack->ReturnFunc(NULL, pStk); -} - -#ifdef _DEBUG -static int xx = 0; -#endif - -bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) -{ - CBotStack* pile = pj->AddStack(this, 2); // un bout de pile local cette fonction -// if ( pile == EOX ) return true; - - pile->SetBotCall(m_pProg); // bases pour les routines - - if ( pile->GivState() == 0 ) - { - if ( !m_Param->Execute(ppVars, pile) ) return false; // dfini les paramtres - pile->IncState(); - } - - if ( pile->GivState() == 1 && !m_MasterClass.IsEmpty() ) - { - // rend "this" connu - CBotVar* pThis ; - if ( pInstance == NULL ) - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, m_MasterClass )); - pThis->SetInit(2); - } - else - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, m_MasterClass )); - pThis->SetPointer(pInstance); - pThis->SetInit(2); - } - -// pThis->SetUniqNum(m_nThisIdent); - pThis->SetUniqNum(-2); - pile->AddVar(pThis); - - pile->IncState(); - } - - if ( pile->IfStep() ) return false; - - if ( !m_Block->Execute(pile) ) - { - if ( pile->GivError() < 0 ) - pile->SetError( 0 ); - else - return false; - } - - return pj->Return(pile); -} - - -void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) -{ - CBotStack* pile = pj->RestoreStack(this); // un bout de pile local cette fonction - if ( pile == NULL ) return; - CBotStack* pile2 = pile; - - pile->SetBotCall(m_pProg); // bases pour les routines - - if ( pile->GivBlock() < 2 ) - { - CBotStack* pile2 = pile->RestoreStack(NULL); // un bout de pile local cette fonction - if ( pile2 == NULL ) return; - pile->SetState(pile->GivState() + pile2->GivState()); - pile2->Delete(); - } - - m_Param->RestoreState(pile2, true); // les paramtres - - if ( !m_MasterClass.IsEmpty() ) - { - CBotVar* pThis = pile->FindVar("this"); - pThis->SetInit(2); - pThis->SetUniqNum(-2); - } - - m_Block->RestoreState(pile2, true); -} - -void CBotFunction::AddNext(CBotFunction* p) -{ - CBotFunction* pp = this; - while (pp->m_next != NULL) pp = pp->m_next; - - pp->m_next = p; -} - - -CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long& nIdent) -{ - nIdent = 0; - CBotTypResult type; - - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); - return type; -} - - -// trouve une fonction selon son identificateur unique -// si l'identificateur n'est pas trouv, cherche selon le nom et les paramtres - -CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic) -{ - TypeOrError.SetType(TX_UNDEFCALL); // pas de routine de ce nom - CBotFunction* pt; - - if ( nIdent ) - { - if ( this != NULL ) for ( pt = this ; pt != NULL ; pt = pt->m_next ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - TypeOrError = pt->m_retTyp; - return pt; - } - } - - // recherche dans la liste des fonctions publiques - - for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - TypeOrError = pt->m_retTyp; - return pt; - } - } - } - - if ( name == NULL ) return NULL; - - int delta = 99999; // cherche la signature la plus faible - CBotFunction* pFunc = NULL; // la meilleure fonction trouve - - if ( this != NULL ) - { - for ( pt = this ; pt != NULL ; pt = pt->m_next ) - { - if ( pt->m_token.GivString() == name ) - { - int i = 0; - int alpha = 0; // signature des paramtres - // les paramtres sont-ils compatibles ? - CBotDefParam* pv = pt->m_Param; // liste des paramtres attendus - CBotVar* pw = ppVars[i++]; // liste des paramtres fournis - while ( pv != NULL && pw != NULL) - { - if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) - { - if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; - break; - } - int d = pv->GivType() - pw->GivType(2); - alpha += d>0 ? d : -10*d; // perte de qualit, 10 fois plus cher !! - - pv = pv->GivNext(); - pw = ppVars[i++]; - } - if ( pw != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); - continue; // trop de paramtres - } - if ( pv != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); - continue; // pas assez de paramtres - } - - if (alpha == 0) // signature parfaite - { - nIdent = pt->m_nFuncIdent; - TypeOrError = pt->m_retTyp; - return pt; - } - - if ( alpha < delta ) // une meilleur signature ? - { - pFunc = pt; - delta = alpha; - } - } - } - } - - if ( bPublic ) - { - for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) - { - if ( pt->m_token.GivString() == name ) - { - int i = 0; - int alpha = 0; // signature des paramtres - // les paramtres sont-ils compatibles ? - CBotDefParam* pv = pt->m_Param; // liste des paramtres attendus - CBotVar* pw = ppVars[i++]; // liste des paramtres fournis - while ( pv != NULL && pw != NULL) - { - if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) - { - if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; - break; - } - int d = pv->GivType() - pw->GivType(2); - alpha += d>0 ? d : -10*d; // perte de qualit, 10 fois plus cher !! - - pv = pv->GivNext(); - pw = ppVars[i++]; - } - if ( pw != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); - continue; // trop de paramtres - } - if ( pv != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); - continue; // pas assez de paramtres - } - - if (alpha == 0) // signature parfaite - { - nIdent = pt->m_nFuncIdent; - TypeOrError = pt->m_retTyp; - return pt; - } - - if ( alpha < delta ) // une meilleur signature ? - { - pFunc = pt; - delta = alpha; - } - } - } - } - - if ( pFunc != NULL ) - { - nIdent = pFunc->m_nFuncIdent; - TypeOrError = pFunc->m_retTyp; - return pFunc; - } - return NULL; -} - - -// fait un appel une fonction - -int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken) -{ - CBotTypResult type; - CBotFunction* pt = NULL; - - pt = FindLocalOrPublic(nIdent, name, ppVars, type); - - if ( pt != NULL ) - { - CBotStack* pStk1 = pStack->AddStack(pt, 2); // pour mettre "this" -// if ( pStk1 == EOX ) return true; - - pStk1->SetBotCall(pt->m_pProg); // on a peut-tre chang de module - - if ( pStk1->IfStep() ) return false; - - CBotStack* pStk3 = pStk1->AddStack(NULL, true); // paramtres - - // prpare les paramtres sur la pile - - if ( pStk1->GivState() == 0 ) - { - if ( !pt->m_MasterClass.IsEmpty() ) - { - CBotVar* pInstance = m_pProg->m_pInstance; - // rend "this" connu - CBotVar* pThis ; - if ( pInstance == NULL ) - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, pt->m_MasterClass )); - pThis->SetInit(2); - } - else - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, pt->m_MasterClass )); - pThis->SetPointer(pInstance); - pThis->SetInit(2); - } - - pThis->SetUniqNum(-2); - pStk1->AddVar(pThis); - - } - - // initialise les variables selon paramtres - pt->m_Param->Execute(ppVars, pStk3); // ne peut pas tre interrompu - - pStk1->IncState(); - } - - // finalement excute la fonction trouve - - if ( !pStk3->GivRetVar( // remet le rsultat sur la pile - pt->m_Block->Execute(pStk3) )) // GivRetVar dit si c'est interrompu - { - if ( !pStk3->IsOk() && pt->m_pProg != m_pProg ) - { -#ifdef _DEBUG - if ( m_pProg->GivFunctions()->GivName() == "LaCommande" ) return false; -#endif - pStk3->SetPosError(pToken); // indique l'erreur sur l'appel de procdure - } - return false; // interrompu ! - } - - return pStack->Return( pStk3 ); - } - return -1; -} - -void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack) -{ - CBotTypResult type; - CBotFunction* pt = NULL; - CBotStack* pStk1; - CBotStack* pStk3; - - // recherche la fonction pour remettre l'identificateur ok - - pt = FindLocalOrPublic(nIdent, name, ppVars, type); - - if ( pt != NULL ) - { - pStk1 = pStack->RestoreStack(pt); - if ( pStk1 == NULL ) return; - - pStk1->SetBotCall(pt->m_pProg); // on a peut-tre chang de module - - if ( pStk1->GivBlock() < 2 ) - { - CBotStack* pStk2 = pStk1->RestoreStack(NULL); // plus utilis - if ( pStk2 == NULL ) return; - pStk3 = pStk2->RestoreStack(NULL); - if ( pStk3 == NULL ) return; - } - else - { - pStk3 = pStk1->RestoreStack(NULL); - if ( pStk3 == NULL ) return; - } - - // prpare les paramtres sur la pile - - { - if ( !pt->m_MasterClass.IsEmpty() ) - { - CBotVar* pInstance = m_pProg->m_pInstance; - // rend "this" connu - CBotVar* pThis = pStk1->FindVar("this"); - pThis->SetInit(2); - pThis->SetUniqNum(-2); - } - } - - if ( pStk1->GivState() == 0 ) - { - pt->m_Param->RestoreState(pStk3, true); - return; - } - - // initialise les variables selon paramtres - pt->m_Param->RestoreState(pStk3, false); - pt->m_Block->RestoreState(pStk3, true); - } -} - - - -// fait un appel d'une mthode -// note : this est dj sur la pile, le pointeur pThis est juste l pour simplifier - -int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass) -{ - CBotTypResult type; - CBotProgram* pProgCurrent = pStack->GivBotCall(); - - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, false); - - if ( pt != NULL ) - { -// DEBUG( "CBotFunction::DoCall" + pt->GivName(), 0, pStack); - - CBotStack* pStk = pStack->AddStack(pt, 2); -// if ( pStk == EOX ) return true; - - pStk->SetBotCall(pt->m_pProg); // on a peut-tre chang de module - CBotStack* pStk3 = pStk->AddStack(NULL, true); // pour mettre les paramtres passs - - // prpare les paramtres sur la pile - - if ( pStk->GivState() == 0 ) - { - // met la variable "this" sur la pile - CBotVar* pthis = CBotVar::Create("this", CBotTypNullPointer); - pthis->Copy(pThis, false); - pthis->SetUniqNum(-2); // valeur spciale - pStk->AddVar(pthis); - - CBotClass* pClass = pThis->GivClass()->GivParent(); - if ( pClass ) - { - // met la variable "super" sur la pile - CBotVar* psuper = CBotVar::Create("super", CBotTypNullPointer); - psuper->Copy(pThis, false); // en fait identique "this" - psuper->SetUniqNum(-3); // valeur spciale - pStk->AddVar(psuper); - } - // initialise les variables selon paramtres - pt->m_Param->Execute(ppVars, pStk3); // ne peut pas tre interrompu - pStk->IncState(); - } - - if ( pStk->GivState() == 1 ) - { - if ( pt->m_bSynchro ) - { - CBotProgram* pProgBase = pStk->GivBotCall(true); - if ( !pClass->Lock(pProgBase) ) return false; // attend de pouvoir - } - pStk->IncState(); - } - // finalement appelle la fonction trouve - - if ( !pStk3->GivRetVar( // remet le rsultat sur la pile - pt->m_Block->Execute(pStk3) )) // GivRetVar dit si c'est interrompu - { - if ( !pStk3->IsOk() ) - { - if ( pt->m_bSynchro ) - { - pClass->Unlock(); // libre la fonction - } - - if ( pt->m_pProg != pProgCurrent ) - { - pStk3->SetPosError(pToken); // indique l'erreur sur l'appel de procdure - } - } - return false; // interrompu ! - } - - if ( pt->m_bSynchro ) - { - pClass->Unlock(); // libre la fonction - } - - return pStack->Return( pStk3 ); - } - return -1; -} - -void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass) -{ - CBotTypResult type; - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); - - if ( pt != NULL ) - { - CBotStack* pStk = pStack->RestoreStack(pt); - if ( pStk == NULL ) return; - pStk->SetBotCall(pt->m_pProg); // on a peut-tre chang de module - - CBotVar* pthis = pStk->FindVar("this"); - pthis->SetUniqNum(-2); - - CBotStack* pStk3 = pStk->RestoreStack(NULL); // pour mettre les paramtres passs - if ( pStk3 == NULL ) return; - - pt->m_Param->RestoreState(pStk3, true); // les paramtres - - if ( pStk->GivState() > 1 && // vrouillage est effectif ? - pt->m_bSynchro ) - { - CBotProgram* pProgBase = pStk->GivBotCall(true); - pClass->Lock(pProgBase); // vrouille la classe - } - - // finalement appelle la fonction trouve - - pt->m_Block->RestoreState(pStk3, true); // interrompu ! - } -} - -// regarde si la "signature" des paramtres est identique -bool CBotFunction::CheckParam(CBotDefParam* pParam) -{ - CBotDefParam* pp = m_Param; - while ( pp != NULL && pParam != NULL ) - { - CBotTypResult type1 = pp->GivType(); - CBotTypResult type2 = pParam->GivType(); - if ( !type1.Compare(type2) ) return false; - pp = pp->GivNext(); - pParam = pParam->GivNext(); - } - return ( pp == NULL && pParam == NULL ); -} - -CBotString CBotFunction::GivName() -{ - return m_token.GivString(); -} - -CBotString CBotFunction::GivParams() -{ - if ( m_Param == NULL ) return CBotString("()"); - - CBotString params = "( "; - CBotDefParam* p = m_Param; // liste des paramtres - - while (p != NULL) - { - params += p->GivParamString(); - p = p->GivNext(); - if ( p != NULL ) params += ", "; - } - - params += " )"; - return params; -} - -CBotFunction* CBotFunction::Next() -{ - return m_next; -} - -void CBotFunction::AddPublic(CBotFunction* func) -{ - if ( m_listPublic != NULL ) - { - func->m_nextpublic = m_listPublic; - m_listPublic->m_prevpublic = func; - } - m_listPublic = func; -} - - - -///////////////////////////////////////////////////////////////////////// -// gestion des paramtres - - -CBotDefParam::CBotDefParam() -{ - m_next = NULL; - m_nIdent = 0; -} - -CBotDefParam::~CBotDefParam() -{ - delete m_next; -} - - -// compile une liste de paramtres -CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) -{ - // surtout pas de pStack->TokenStack ici - // les variables dclares doivent rester visibles par la suite - - pStack->SetStartError(p->GivStart()); - - if (IsOfType(p, ID_OPENPAR)) - { - CBotDefParam* list = NULL; - - while (!IsOfType(p, ID_CLOSEPAR)) - { - CBotDefParam* param = new CBotDefParam(); - if (list == NULL) list = param; - else list->AddNext(param); // ajoute la liste - - CBotClass* pClass = NULL;//= CBotClass::Find(p); - param->m_typename = p->GivString(); - CBotTypResult type = param->m_type = TypeParam(p, pStack); -// if ( type == CBotTypPointer ) type = CBotTypClass; // il faut crer un nouvel objet - - if (param->m_type.GivType() > 0) - { - CBotToken* pp = p; - param->m_token = *p; - if (pStack->IsOk() && IsOfType(p, TokenTypVar) ) - { - - // variable dj dclare ? - if (pStack->CheckVarLocal(pp)) - { - pStack->SetError(TX_REDEFVAR, pp); - break; - } - - if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody); - CBotVar* var = CBotVar::Create(pp->GivString(), type); // cre la variable -// if ( pClass ) var->SetClass(pClass); - var->SetInit(2); // la marque initialise - param->m_nIdent = CBotVar::NextUniqNum(); - var->SetUniqNum(param->m_nIdent); - pStack->AddVar(var); // la place sur la pile - - if (IsOfType(p, ID_COMMA) || p->GivType() == ID_CLOSEPAR) - continue; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStack->SetError(TX_NOTYP, p); - delete list; - return NULL; - } - return list; - } - pStack->SetError(TX_OPENPAR, p->GivStart()); - return NULL; -} - -void CBotDefParam::AddNext(CBotDefParam* p) -{ - CBotDefParam* pp = this; - while (pp->m_next != NULL) pp = pp->m_next; - - pp->m_next = p; -} - - -bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) -{ - int i = 0; - CBotDefParam* p = this; - - while ( p != NULL ) - { - // cre une variable locale sur la pile - CBotVar* newvar = CBotVar::Create(p->m_token.GivString(), p->m_type); - - // procde ainsi pour faire la transformation des types : - if ( ppVars != NULL && ppVars[i] != NULL ) - { - switch (p->m_type.GivType()) - { - case CBotTypInt: - newvar->SetValInt(ppVars[i]->GivValInt()); - break; - case CBotTypFloat: - newvar->SetValFloat(ppVars[i]->GivValFloat()); - break; - case CBotTypString: - newvar->SetValString(ppVars[i]->GivValString()); - break; - case CBotTypBoolean: - newvar->SetValInt(ppVars[i]->GivValInt()); - break; - case CBotTypIntrinsic: - ((CBotVarClass*)newvar)->Copy(ppVars[i], false); - break; - case CBotTypPointer: - case CBotTypArrayPointer: - { - newvar->SetPointer(ppVars[i]->GivPointer()); - } - break; - default: - ASM_TRAP(); - } - } - newvar->SetUniqNum(p->m_nIdent); - pj->AddVar(newvar); // place la variable - p = p->m_next; - i++; - } - - return true; -} - -void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) -{ - int i = 0; - CBotDefParam* p = this; - - while ( p != NULL ) - { - // cre une variable locale sur la pile - CBotVar* var = pj->FindVar(p->m_token.GivString()); - var->SetUniqNum(p->m_nIdent); - p = p->m_next; - } -} - -int CBotDefParam::GivType() -{ - return m_type.GivType(); -} - -CBotTypResult CBotDefParam::GivTypResult() -{ - return m_type; -} - -CBotDefParam* CBotDefParam::GivNext() -{ - return m_next; -} - -CBotString CBotDefParam::GivParamString() -{ - CBotString param; - - param = m_typename; - param += ' '; - - param += m_token.GivString(); - return param; -} - - - -////////////////////////////////////////////////////////////////////////// -// retour des paramtres - -CBotReturn::CBotReturn() -{ - m_Instr = NULL; - name = "CBotReturn"; // debug -} - -CBotReturn::~CBotReturn() -{ - delete m_Instr; -} - -CBotInstr* CBotReturn::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - - if (!IsOfType(p, ID_RETURN)) return NULL; // ne devrait jamais arriver - - CBotReturn* inst = new CBotReturn(); // cre l'objet - inst->SetToken( pp ); - - CBotTypResult type = pStack->GivRetType(); - - if ( type.GivType() == 0 ) // retourne void ? - { - if ( IsOfType( p, ID_SEP ) ) return inst; - pStack->SetError( TX_BADTYPE, pp ); - return NULL; - } - - inst->m_Instr = CBotExpression::Compile(p, pStack); - if ( pStack->IsOk() ) - { - CBotTypResult retType = pStack->GivTypResult(2); - if (TypeCompatible(retType, type, ID_ASS)) - { - if ( IsOfType( p, ID_SEP ) ) - return inst; - - pStack->SetError(TX_ENDOF, p->GivStart()); - } - pStack->SetError(TX_BADTYPE, p->GivStart()); - } - - delete inst; - return NULL; // pas d'objet, l'erreur est sur la pile -} - -bool CBotReturn::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - if ( pile->GivState() == 0 ) - { - if ( m_Instr != NULL && !m_Instr->Execute(pile) ) return false; // value le rsultat - // le rsultat est sur la pile - pile->IncState(); - } - - if ( pile->IfStep() ) return false; - - pile->SetBreak(3, CBotString()); - return pj->Return(pile); -} - -void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; - - if ( pile->GivState() == 0 ) - { - if ( m_Instr != NULL ) m_Instr->RestoreState(pile, bMain); // value le rsultat - return; - } -} - -//////////////////////////////////////////////////////////////////////////////// -// Les appels ces fonctions - -CBotInstrCall::CBotInstrCall() -{ - m_Parameters = NULL; - m_nFuncIdent = 0; - name = "CBotInstrCall"; -} - -CBotInstrCall::~CBotInstrCall() -{ - delete m_Parameters; -} - -CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotVar* ppVars[1000]; - - int i = 0; - - CBotToken* pp = p; - p = p->GivNext(); - - pStack->SetStartError(p->GivStart()); - CBotCStack* pile = pStack; - - if ( IsOfType(p, ID_OPENPAR) ) - { - int start, end; - CBotInstrCall* inst = new CBotInstrCall(); - inst->SetToken(pp); - - // compile la liste des paramtres - if (!IsOfType(p, ID_CLOSEPAR)) while (true) - { - start = p->GivStart(); - pile = pile->TokenStack(); // garde les rsultats sur la pile - - CBotInstr* param = CBotExpression::Compile(p, pile); - end = p->GivStart(); - if ( inst->m_Parameters == NULL ) inst->m_Parameters = param; - else inst->m_Parameters->AddNext(param); // construit la liste - - if ( !pile->IsOk() ) - { - delete inst; - return pStack->Return(NULL, pile); - } - - if ( param != NULL ) - { - if ( pile->GivTypResult().Eq(99) ) - { - delete pStack->TokenStack(); - pStack->SetError(TX_VOID, p->GivStart()); - delete inst; - return NULL; - } - ppVars[i] = pile->GivVar(); - ppVars[i]->GivToken()->SetPos(start, end); - i++; - - if (IsOfType(p, ID_COMMA)) continue; // saute la virgule - if (IsOfType(p, ID_CLOSEPAR)) break; - } - - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - delete pStack->TokenStack(); - delete inst; - return NULL; - } - ppVars[i] = NULL; - - // la routine est-elle connue ? -// CBotClass* pClass = NULL; - inst->m_typRes = pStack->CompileCall(pp, ppVars, inst->m_nFuncIdent); - if ( inst->m_typRes.GivType() >= 20 ) - { -// if (pVar2!=NULL) pp = pVar2->RetToken(); - pStack->SetError( inst->m_typRes.GivType(), pp ); - delete pStack->TokenStack(); - delete inst; - return NULL; - } - - delete pStack->TokenStack(); - if ( inst->m_typRes.GivType() > 0 ) - { - CBotVar* pRes = CBotVar::Create("", inst->m_typRes); - pStack->SetVar(pRes); // pour connatre le type du rsultat - } - else pStack->SetVar(NULL); // routine retourne void - - return inst; - } - p = pp; - delete pStack->TokenStack(); - return NULL; -} - -bool CBotInstrCall::Execute(CBotStack* &pj) -{ - CBotVar* ppVars[1000]; - CBotStack* pile = pj->AddStack(this); - if ( pile->StackOver() ) return pj->Return( pile ); - - CBotStack* pile1 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - pile = pile->AddStack(); // de la place sur la pile pour les rsultats - if ( pile->GivState() == 0 ) - { - if (!p->Execute(pile)) return false; // interrompu ici ? - pile->SetState(1); // marque spciale pour reconnare les paramtres - } - ppVars[i++] = pile->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return false; - - if ( !pile2->ExecuteCall(m_nFuncIdent, GivToken(), ppVars, m_typRes)) return false; // interrompu - - return pj->Return(pile2); // libre toute la pile -} - -void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; - - CBotStack* pile1 = pile; - - int i = 0; - CBotVar* ppVars[1000]; - CBotInstr* p = m_Parameters; - // value les paramtres - // et place les valeurs sur la pile - // pour pouvoir tre interrompu n'importe quand - if ( p != NULL) while ( true ) - { - pile = pile->RestoreStack(); // de la place sur la pile pour les rsultats - if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) - { - p->RestoreState(pile, bMain); // interrompu ici ! - return; - } - ppVars[i++] = pile->GivVar(); // construit la liste des paramtres - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotStack* pile2 = pile->RestoreStack(); - if ( pile2 == NULL ) return; - - pile2->RestoreCall(m_nFuncIdent, GivToken(), ppVars); -} - -////////////////////////////////////////////////////////////////////////////// -// dclaration des classes par l'utilisateur - -// pr-compile une nouvelle class -// l'analyse est complte l'execption du corps des routines - -CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) -{ - if ( !IsOfType(p, ID_PUBLIC) ) - { - pStack->SetError(TX_NOPUBLIC, p); - return NULL; - } - - if ( !IsOfType(p, ID_CLASS) ) return NULL; - - CBotString name = p->GivString(); - - CBotClass* pOld = CBotClass::Find(name); - if ( pOld != NULL && pOld->m_IsDef ) - { - pStack->SetError( TX_REDEFCLASS, p ); - return NULL; - } - - // un nom pour la classe est-il l ? - if (IsOfType(p, TokenTypVar)) - { - CBotClass* pPapa = NULL; - if ( IsOfType( p, ID_EXTENDS ) ) - { - CBotString name = p->GivString(); - pPapa = CBotClass::Find(name); - - if (!IsOfType(p, TokenTypVar) || pPapa == NULL ) - { - pStack->SetError( TX_NOCLASS, p ); - return NULL; - } - } - CBotClass* classe = (pOld == NULL) ? new CBotClass(name, pPapa) : pOld; - classe->Purge(); // vide les anciennes dfinitions - classe->m_IsDef = false; // dfinition en cours - - if ( !IsOfType( p, ID_OPBLK) ) - { - pStack->SetError(TX_OPENBLK, p); - return NULL; - } - - while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) - { - classe->CompileDefItem(p, pStack, false); - } - - if (pStack->IsOk()) return classe; - } - pStack->SetError(TX_ENDOF, p); - return NULL; -} - -bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) -{ - bool bStatic = false; - int mProtect = PR_PUBLIC; - bool bSynchro = false; - - while (IsOfType(p, ID_SEP)) ; - - CBotTypResult type( -1 ); - - if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; - CBotToken* pBase = p; - - if ( IsOfType(p, ID_STATIC) ) bStatic = true; - if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; - if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; - if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; - if ( IsOfType(p, ID_STATIC) ) bStatic = true; - -// CBotClass* pClass = NULL; - type = TypeParam(p, pStack); // type du rsultat - - if ( type.Eq(-1) ) - { - pStack->SetError(TX_NOTYP, p); - return false; - } - - while (pStack->IsOk()) - { - CBotToken* pp = p; - IsOfType(p, ID_NOT); // saute le ~ ventuel (destructeur) - - if (IsOfType(p, TokenTypVar)) - { - CBotInstr* limites = NULL; - while ( IsOfType( p, ID_OPBRK ) ) // un tableau ? - { - CBotInstr* i = NULL; - - if ( p->GivType() != ID_CLBRK ) - i = CBotExpression::Compile( p, pStack ); // expression pour la valeur - else - i = new CBotEmpty(); // spcial si pas de formule - - type = CBotTypResult(CBotTypArrayPointer, type); - - if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStack->SetError(TX_CLBRK, p->GivStart()); - return false; - } - -/* CBotVar* pv = pStack->GivVar(); - if ( pv->GivType()>= CBotTypBoolean ) - { - pStack->SetError(TX_BADTYPE, p->GivStart()); - return false; - }*/ - - if (limites == NULL) limites = i; - else limites->AddNext3(i); - } - - if ( p->GivType() == ID_OPENPAR ) - { - if ( !bSecond ) - { - p = pBase; - CBotFunction* f = - CBotFunction::Compile1(p, pStack, this); - - if ( f == NULL ) return false; - - if (m_pMethod == NULL) m_pMethod = f; - else m_pMethod->AddNext(f); - } - else - { - // retrouve la mthode prcompile en passe 1 - CBotFunction* pf = m_pMethod; - CBotFunction* prev = NULL; - while ( pf != NULL ) - { - if (pf->GivName() == pp->GivString()) break; - prev = pf; - pf = pf->Next(); - } - - bool bConstructor = (pp->GivString() == GivName()); - CBotCStack* pile = pStack->TokenStack(NULL, true); - - // rend "this" connu - CBotToken TokenThis(CBotString("this"), CBotString()); - CBotVar* pThis = CBotVar::Create(&TokenThis, CBotTypResult( CBotTypClass, this ) ); - pThis->SetUniqNum(-2); - pile->AddVar(pThis); - - if ( m_pParent ) - { - // rend "super" connu - CBotToken TokenSuper(CBotString("super"), CBotString()); - CBotVar* pThis = CBotVar::Create(&TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) ); - pThis->SetUniqNum(-3); - pile->AddVar(pThis); - } - -// int num = 1; - CBotClass* my = this; - while (my != NULL) - { - // place une copie des varibles de la classe (this) sur la pile - CBotVar* pv = my->m_pVar; - while (pv != NULL) - { - CBotVar* pcopy = CBotVar::Create(pv); - pcopy->SetInit(!bConstructor || pv->IsStatic()); - pcopy->SetUniqNum(pv->GivUniqNum()); - pile->AddVar(pcopy); - pv = pv->GivNext(); - } - my = my->m_pParent; - } - - // compile une mthode - p = pBase; - CBotFunction* f = - CBotFunction::Compile(p, pile, NULL/*, false*/); - - if ( f != NULL ) - { - f->m_pProg = pStack->GivBotCall(); - f->m_bSynchro = bSynchro; - // remplace l'lment dans la chane - f->m_next = pf->m_next; - pf->m_next = NULL; - delete pf; - if (prev == NULL) m_pMethod = f; - else prev->m_next = f; - } - pStack->Return(NULL, pile); - } - - return pStack->IsOk(); - } - - // dfinition d'un lment - if (type.Eq(0)) - { - pStack->SetError(TX_ENDOF, p); - return false; - } - - CBotInstr* i = NULL; - if ( IsOfType(p, ID_ASS ) ) - { - if ( type.Eq(CBotTypArrayPointer) ) - { - i = CBotListArray::Compile(p, pStack, type.GivTypElem()); - } - else - { - // il y a une assignation calculer - i = CBotTwoOpExpr::Compile(p, pStack); - } - if ( !pStack->IsOk() ) return false; - } - - - if ( !bSecond ) - { - CBotVar* pv = CBotVar::Create(pp->GivString(), type); - pv -> SetStatic( bStatic ); - pv -> SetPrivate( mProtect ); - - AddItem( pv ); - - pv->m_InitExpr = i; - pv->m_LimExpr = limites; - - - if ( pv->IsStatic() && pv->m_InitExpr != NULL ) - { - CBotStack* pile = CBotStack::FirstStack(); // une pile indpendante - while(pile->IsOk() && !pv->m_InitExpr->Execute(pile)); // value l'expression sans timer - pv->SetVal( pile->GivVar() ) ; - pile->Delete(); - } - } - else - delete i; - - if ( IsOfType(p, ID_COMMA) ) continue; - if ( IsOfType(p, ID_SEP) ) break; - } - pStack->SetError(TX_ENDOF, p); - } - return pStack->IsOk(); -} - - -CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) -{ - if ( !IsOfType(p, ID_PUBLIC) ) return NULL; - if ( !IsOfType(p, ID_CLASS) ) return NULL; - - CBotString name = p->GivString(); - - // un nom pour la classe est-il l ? - if (IsOfType(p, TokenTypVar)) - { - // la classe t cre par Compile1 - CBotClass* pOld = CBotClass::Find(name); - - if ( IsOfType( p, ID_EXTENDS ) ) - { - IsOfType(p, TokenTypVar); // forcment - } - IsOfType( p, ID_OPBLK); // forcment - - while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) - { - pOld->CompileDefItem(p, pStack, true); - } - - pOld->m_IsDef = true; // dfinition termine - if (pStack->IsOk()) return pOld; - } - pStack->SetError(TX_ENDOF, p); - return NULL; -} +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/////////////////////////////////////////////////////////////////////// +// compilation of various functions declared by the user +// + +#include "CBot.h" + +// various constructors / destructors +// \TODO translation:to liberate all according to esteblished tree +// pour libérer tout selon l'arbre établi +CBotFunction::CBotFunction() +{ + m_Param = NULL; // empty parameter list + m_Block = NULL; // the instruction block + m_next = NULL; // functions can be chained + m_bPublic = false; // function not public + m_bExtern = false; // function not extern + m_nextpublic = NULL; + m_prevpublic = NULL; + m_pProg = NULL; +// m_nThisIdent = 0; + m_nFuncIdent = 0; + m_bSynchro = false; +} + +CBotFunction* CBotFunction::m_listPublic = NULL; + +CBotFunction::~CBotFunction() +{ + delete m_Param; // empty parameter list + delete m_Block; // the instruction block + delete m_next; + + // remove public list if there is + if ( m_bPublic ) + { + if ( m_nextpublic != NULL ) + { + m_nextpublic->m_prevpublic = m_prevpublic; + } + if ( m_prevpublic != NULL) + { + m_prevpublic->m_nextpublic = m_nextpublic; + } + else + { + // if prev = next = null may not be in the list! + if ( m_listPublic == this ) m_listPublic = m_nextpublic; + } + } +} + +bool CBotFunction::IsPublic() +{ + return m_bPublic; +} + +bool CBotFunction::IsExtern() +{ + return m_bExtern; +} + +bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop) +{ + start = m_extern.GivStart(); + stop = m_closeblk.GivEnd(); + + if (modestart == GetPosExtern) + { + start = m_extern.GivStart(); + } + if (modestop == GetPosExtern) + { + stop = m_extern.GivEnd(); + } + if (modestart == GetPosNom) + { + start = m_token.GivStart(); + } + if (modestop == GetPosNom) + { + stop = m_token.GivEnd(); + } + if (modestart == GetPosParam) + { + start = m_openpar.GivStart(); + } + if (modestop == GetPosParam) + { + stop = m_closepar.GivEnd(); + } + if (modestart == GetPosBloc) + { + start = m_openblk.GivStart(); + } + if (modestop == GetPosBloc) + { + stop = m_closeblk.GivEnd(); + } + + return true; +} + + +CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) +{ + while ( IsOfType( p, ID_OPBRK ) ) + { + if ( !IsOfType( p, ID_CLBRK ) ) + { + pile->SetError(TX_CLBRK, p->GivStart()); + return CBotTypResult( -1 ); + } + type = CBotTypResult( CBotTypArrayPointer, type ); + } + return type; +} + +CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) +{ + CBotClass* pClass = NULL; + + switch (p->GivType()) + { + case ID_INT: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypInt )); + case ID_FLOAT: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypFloat )); + case ID_BOOLEAN: + case ID_BOOL: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypBoolean )); + case ID_STRING: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypString )); + case ID_VOID: + p = p->GivNext(); + return CBotTypResult( 0 ); + + case TokenTypVar: + pClass = CBotClass::Find(p); + if ( pClass != NULL) + { + p = p->GivNext(); + return ArrayType(p, pile, + pClass->IsIntrinsic() ? + CBotTypResult( CBotTypIntrinsic, pClass ) : + CBotTypResult( CBotTypPointer, pClass ) ); + } + } + return CBotTypResult( -1 ); +} + +// compiles a new function +// bLocal allows of the declaration of parameters on the same level +// as the elements belonging to the class for methods +CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, bool bLocal) +{ + CBotToken* pp; + CBotFunction* func = finput; + if ( func == NULL ) func = new CBotFunction(); + + CBotCStack* pStk = pStack->TokenStack(p, bLocal); + +// func->m_nFuncIdent = CBotVar::NextUniqNum(); + + while (true) + { + if ( IsOfType(p, ID_PUBLIC) ) + { + func->m_bPublic = true; + continue; + } + pp = p; + if ( IsOfType(p, ID_EXTERN) ) + { + func->m_extern = pp; // for the position of the word "extern" + func->m_bExtern = true; +// func->m_bPublic = true; // therefore also public! + continue; + } + break; + } + + func->m_retToken = *p; +// CBotClass* pClass; + func->m_retTyp = TypeParam(p, pStk); // type of the result + + if (func->m_retTyp.GivType() >= 0) + { + CBotToken* pp = p; + func->m_token = *p; + + if ( IsOfType(p, ID_NOT) ) + { + CBotToken d("~" + p->GivString()); + func->m_token = d; + } + + // un nom de fonction est-il là ? + if (IsOfType(p, TokenTypVar)) + { + if ( IsOfType( p, ID_DBLDOTS ) ) // method for a class + { + func->m_MasterClass = pp->GivString(); + CBotClass* pClass = CBotClass::Find(pp); + if ( pClass == NULL ) goto bad; + +// pp = p; + func->m_token = *p; + if (!IsOfType(p, TokenTypVar)) goto bad; + + } + func->m_openpar = p; + func->m_Param = CBotDefParam::Compile( p, pStk ); + func->m_closepar = p->GivPrev(); + if (pStk->IsOk()) + { + pStk->SetRetType(func->m_retTyp); // for knowledge what type returns + + if (!func->m_MasterClass.IsEmpty()) + { + // return "this" known + CBotVar* pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, func->m_MasterClass )); + pThis->SetInit(2); +// pThis->SetUniqNum(func->m_nThisIdent = -2); //CBotVar::NextUniqNum() will not + pThis->SetUniqNum(-2); + pStk->AddVar(pThis); + + // initialize variables acording to This + // only saves the pointer to the first, + // the rest is chained + CBotVar* pv = pThis->GivItemList(); +// int num = 1; + while (pv != NULL) + { + CBotVar* pcopy = CBotVar::Create(pv); +// pcopy->SetInit(2); + pcopy->Copy(pv); + pcopy->SetPrivate(pv->GivPrivate()); +// pcopy->SetUniqNum(pv->GivUniqNum()); //num++); + pStk->AddVar(pcopy); + pv = pv->GivNext(); + } + } + + // and compiles the following instruction block + func->m_openblk = p; + func->m_Block = CBotBlock::Compile(p, pStk, false); + func->m_closeblk = p->GivPrev(); + if ( pStk->IsOk() ) + { + if ( func->m_bPublic ) // public function, return known for all + { + CBotFunction::AddPublic(func); + } + return pStack->ReturnFunc(func, pStk); + } + } + } +bad: + pStk->SetError(TX_NOFONC, p); + } + pStk->SetError(TX_NOTYP, p); + if ( finput == NULL ) delete func; + return pStack->ReturnFunc(NULL, pStk); +} + +// pre-compile a new function +CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) +{ + CBotFunction* func = new CBotFunction(); + func->m_nFuncIdent = CBotVar::NextUniqNum(); + + CBotCStack* pStk = pStack->TokenStack(p, true); + + while (true) + { + if ( IsOfType(p, ID_PUBLIC) ) + { + // func->m_bPublic = true; // will be done in two passes + continue; + } + if ( IsOfType(p, ID_EXTERN) ) + { + func->m_bExtern = true; + continue; + } + break; + } + + func->m_retToken = *p; + func->m_retTyp = TypeParam(p, pStack); // type of the result + + if (func->m_retTyp.GivType() >= 0) + { + CBotToken* pp = p; + func->m_token = *p; + // un nom de fonction est-il là ? + if (IsOfType(p, TokenTypVar)) + { + if ( IsOfType( p, ID_DBLDOTS ) ) // method for a class + { + func->m_MasterClass = pp->GivString(); + CBotClass* pClass = CBotClass::Find(pp); + if ( pClass == NULL ) + { + pStk->SetError(TX_NOCLASS, pp); + goto bad; + } + + pp = p; + func->m_token = *p; + if (!IsOfType(p, TokenTypVar)) goto bad; + + } + func->m_Param = CBotDefParam::Compile( p, pStk ); + if (pStk->IsOk()) + { + // looks if the function exists elsewhere + if (( pClass != NULL || !pStack->CheckCall(pp, func->m_Param)) && + ( pClass == NULL || !pClass->CheckCall(pp, func->m_Param)) ) + { + if (IsOfType(p, ID_OPBLK)) + { + int level = 1; + // and skips the following instruction block + do + { + int type = p->GivType(); + p = p->GivNext(); + if (type == ID_OPBLK) level++; + if (type == ID_CLBLK) level--; + } + while (level > 0 && p != NULL); + + return pStack->ReturnFunc(func, pStk); + } + pStk->SetError(TX_OPENBLK, p); + } + } + pStk->SetError(TX_REDEF, pp); + } +bad: + pStk->SetError(TX_NOFONC, p); + } + pStk->SetError(TX_NOTYP, p); + delete func; + return pStack->ReturnFunc(NULL, pStk); +} + +#ifdef _DEBUG +static int xx = 0; +#endif + +bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) +{ + CBotStack* pile = pj->AddStack(this, 2); // one end of stack local to this function +// if ( pile == EOX ) return true; + + pile->SetBotCall(m_pProg); // bases for routines + + if ( pile->GivState() == 0 ) + { + if ( !m_Param->Execute(ppVars, pile) ) return false; // define parameters + pile->IncState(); + } + + if ( pile->GivState() == 1 && !m_MasterClass.IsEmpty() ) + { + // makes "this" known + CBotVar* pThis ; + if ( pInstance == NULL ) + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, m_MasterClass )); + pThis->SetInit(2); + } + else + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, m_MasterClass )); + pThis->SetPointer(pInstance); + pThis->SetInit(2); + } + +// pThis->SetUniqNum(m_nThisIdent); + pThis->SetUniqNum(-2); + pile->AddVar(pThis); + + pile->IncState(); + } + + if ( pile->IfStep() ) return false; + + if ( !m_Block->Execute(pile) ) + { + if ( pile->GivError() < 0 ) + pile->SetError( 0 ); + else + return false; + } + + return pj->Return(pile); +} + + +void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) +{ + CBotStack* pile = pj->RestoreStack(this); // one end of stack local to this function + if ( pile == NULL ) return; + CBotStack* pile2 = pile; + + pile->SetBotCall(m_pProg); // bases for routines + + if ( pile->GivBlock() < 2 ) + { + CBotStack* pile2 = pile->RestoreStack(NULL); // one end of stack local to this function + if ( pile2 == NULL ) return; + pile->SetState(pile->GivState() + pile2->GivState()); + pile2->Delete(); + } + + m_Param->RestoreState(pile2, true); // parameters + + if ( !m_MasterClass.IsEmpty() ) + { + CBotVar* pThis = pile->FindVar("this"); + pThis->SetInit(2); + pThis->SetUniqNum(-2); + } + + m_Block->RestoreState(pile2, true); +} + +void CBotFunction::AddNext(CBotFunction* p) +{ + CBotFunction* pp = this; + while (pp->m_next != NULL) pp = pp->m_next; + + pp->m_next = p; +} + + +CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long& nIdent) +{ + nIdent = 0; + CBotTypResult type; + + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); + return type; +} + + +// is a function according to its unique identifier +// if the identifier is not found, looking by name and parameters + +CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic) +{ + TypeOrError.SetType(TX_UNDEFCALL); // no routine of the name + CBotFunction* pt; + + if ( nIdent ) + { + if ( this != NULL ) for ( pt = this ; pt != NULL ; pt = pt->m_next ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + TypeOrError = pt->m_retTyp; + return pt; + } + } + + // search the list of public functions + + for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + TypeOrError = pt->m_retTyp; + return pt; + } + } + } + + if ( name == NULL ) return NULL; + + int delta = 99999; // seeks the lowest signature + CBotFunction* pFunc = NULL; // the best function found + + if ( this != NULL ) + { + for ( pt = this ; pt != NULL ; pt = pt->m_next ) + { + if ( pt->m_token.GivString() == name ) + { + int i = 0; + int alpha = 0; // signature of parameters + // parameters are compatible? + CBotDefParam* pv = pt->m_Param; // expected list of parameters + CBotVar* pw = ppVars[i++]; // provided list parameter + while ( pv != NULL && pw != NULL) + { + if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) + { + if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; + break; + } + int d = pv->GivType() - pw->GivType(2); + alpha += d>0 ? d : -10*d; // quality loss, 10 times more expensive! + + pv = pv->GivNext(); + pw = ppVars[i++]; + } + if ( pw != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); + continue; // too many parameters + } + if ( pv != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); + continue; // not enough parameters + } + + if (alpha == 0) // perfect signature + { + nIdent = pt->m_nFuncIdent; + TypeOrError = pt->m_retTyp; + return pt; + } + + if ( alpha < delta ) // a better signature? + { + pFunc = pt; + delta = alpha; + } + } + } + } + + if ( bPublic ) + { + for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) + { + if ( pt->m_token.GivString() == name ) + { + int i = 0; + int alpha = 0; // signature of parameters + // parameters sont-ils compatibles ? + CBotDefParam* pv = pt->m_Param; // list of expected parameters + CBotVar* pw = ppVars[i++]; // list of provided parameters + while ( pv != NULL && pw != NULL) + { + if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) + { + if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; + break; + } + int d = pv->GivType() - pw->GivType(2); + alpha += d>0 ? d : -10*d; // quality loss, 10 times more expensive! + + pv = pv->GivNext(); + pw = ppVars[i++]; + } + if ( pw != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); + continue; // to many parameters + } + if ( pv != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); + continue; // not enough parameters + } + + if (alpha == 0) // perfect signature + { + nIdent = pt->m_nFuncIdent; + TypeOrError = pt->m_retTyp; + return pt; + } + + if ( alpha < delta ) // a better signature? + { + pFunc = pt; + delta = alpha; + } + } + } + } + + if ( pFunc != NULL ) + { + nIdent = pFunc->m_nFuncIdent; + TypeOrError = pFunc->m_retTyp; + return pFunc; + } + return NULL; +} + + +// fait un appel à une fonction + +int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken) +{ + CBotTypResult type; + CBotFunction* pt = NULL; + + pt = FindLocalOrPublic(nIdent, name, ppVars, type); + + if ( pt != NULL ) + { + CBotStack* pStk1 = pStack->AddStack(pt, 2); // to put "this" +// if ( pStk1 == EOX ) return true; + + pStk1->SetBotCall(pt->m_pProg); // it may have changed module + + if ( pStk1->IfStep() ) return false; + + CBotStack* pStk3 = pStk1->AddStack(NULL, true); // parameters + + // preparing parameters on the stack + + if ( pStk1->GivState() == 0 ) + { + if ( !pt->m_MasterClass.IsEmpty() ) + { + CBotVar* pInstance = m_pProg->m_pInstance; + // make "this" known + CBotVar* pThis ; + if ( pInstance == NULL ) + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, pt->m_MasterClass )); + pThis->SetInit(2); + } + else + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, pt->m_MasterClass )); + pThis->SetPointer(pInstance); + pThis->SetInit(2); + } + + pThis->SetUniqNum(-2); + pStk1->AddVar(pThis); + + } + + // initializes the variables as parameters + pt->m_Param->Execute(ppVars, pStk3); // cannot be interrupted + + pStk1->IncState(); + } + + // finally execution of the found function + + if ( !pStk3->GivRetVar( // puts the result on the stack + pt->m_Block->Execute(pStk3) )) // GivRetVar said if it is interrupted + { + if ( !pStk3->IsOk() && pt->m_pProg != m_pProg ) + { +#ifdef _DEBUG + if ( m_pProg->GivFunctions()->GivName() == "LaCommande" ) return false; +#endif + pStk3->SetPosError(pToken); // indicates the error on the procedure call + } + return false; // interrupt ! + } + + return pStack->Return( pStk3 ); + } + return -1; +} + +void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack) +{ + CBotTypResult type; + CBotFunction* pt = NULL; + CBotStack* pStk1; + CBotStack* pStk3; + + // search function to return the ok identifier + + pt = FindLocalOrPublic(nIdent, name, ppVars, type); + + if ( pt != NULL ) + { + pStk1 = pStack->RestoreStack(pt); + if ( pStk1 == NULL ) return; + + pStk1->SetBotCall(pt->m_pProg); // it may have changed module + + if ( pStk1->GivBlock() < 2 ) + { + CBotStack* pStk2 = pStk1->RestoreStack(NULL); // used more + if ( pStk2 == NULL ) return; + pStk3 = pStk2->RestoreStack(NULL); + if ( pStk3 == NULL ) return; + } + else + { + pStk3 = pStk1->RestoreStack(NULL); + if ( pStk3 == NULL ) return; + } + + // preparing parameters on the stack + + { + if ( !pt->m_MasterClass.IsEmpty() ) + { + CBotVar* pInstance = m_pProg->m_pInstance; + // make "this" known + CBotVar* pThis = pStk1->FindVar("this"); + pThis->SetInit(2); + pThis->SetUniqNum(-2); + } + } + + if ( pStk1->GivState() == 0 ) + { + pt->m_Param->RestoreState(pStk3, true); + return; + } + + // initializes the variables as parameters + pt->m_Param->RestoreState(pStk3, false); + pt->m_Block->RestoreState(pStk3, true); + } +} + + + +// makes call of a method +// note: this is already on the stack, the pointer pThis is just to simplify + +int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass) +{ + CBotTypResult type; + CBotProgram* pProgCurrent = pStack->GivBotCall(); + + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, false); + + if ( pt != NULL ) + { +// DEBUG( "CBotFunction::DoCall" + pt->GivName(), 0, pStack); + + CBotStack* pStk = pStack->AddStack(pt, 2); +// if ( pStk == EOX ) return true; + + pStk->SetBotCall(pt->m_pProg); // it may have changed module + CBotStack* pStk3 = pStk->AddStack(NULL, true); // to set parameters passed + + // preparing parameters on the stack + + if ( pStk->GivState() == 0 ) + { + // sets the variable "this" on the stack + CBotVar* pthis = CBotVar::Create("this", CBotTypNullPointer); + pthis->Copy(pThis, false); + pthis->SetUniqNum(-2); // special value + pStk->AddVar(pthis); + + CBotClass* pClass = pThis->GivClass()->GivParent(); + if ( pClass ) + { + // sets the variable "super" on the stack + CBotVar* psuper = CBotVar::Create("super", CBotTypNullPointer); + psuper->Copy(pThis, false); // in fact identical to "this" + psuper->SetUniqNum(-3); // special value + pStk->AddVar(psuper); + } + // initializes the variables as parameters + pt->m_Param->Execute(ppVars, pStk3); // cannot be interrupted + pStk->IncState(); + } + + if ( pStk->GivState() == 1 ) + { + if ( pt->m_bSynchro ) + { + CBotProgram* pProgBase = pStk->GivBotCall(true); + if ( !pClass->Lock(pProgBase) ) return false; // expected to power \TODO attend de pouvoir + } + pStk->IncState(); + } + // finally calls the found function + + if ( !pStk3->GivRetVar( // puts the result on the stack + pt->m_Block->Execute(pStk3) )) // GivRetVar said if it is interrupted + { + if ( !pStk3->IsOk() ) + { + if ( pt->m_bSynchro ) + { + pClass->Unlock(); // release function + } + + if ( pt->m_pProg != pProgCurrent ) + { + pStk3->SetPosError(pToken); // indicates the error on the procedure call + } + } + return false; // interrupt ! + } + + if ( pt->m_bSynchro ) + { + pClass->Unlock(); // release function + } + + return pStack->Return( pStk3 ); + } + return -1; +} + +void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass) +{ + CBotTypResult type; + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); + + if ( pt != NULL ) + { + CBotStack* pStk = pStack->RestoreStack(pt); + if ( pStk == NULL ) return; + pStk->SetBotCall(pt->m_pProg); // it may have changed module + + CBotVar* pthis = pStk->FindVar("this"); + pthis->SetUniqNum(-2); + + CBotStack* pStk3 = pStk->RestoreStack(NULL); // to set parameters passed + if ( pStk3 == NULL ) return; + + pt->m_Param->RestoreState(pStk3, true); // parameters + + if ( pStk->GivState() > 1 && // latching is effective? + pt->m_bSynchro ) + { + CBotProgram* pProgBase = pStk->GivBotCall(true); + pClass->Lock(pProgBase); // locks the class + } + + // finally calls the found function + + pt->m_Block->RestoreState(pStk3, true); // interrupt ! + } +} + +// see if the "signature" of parameters is identical +bool CBotFunction::CheckParam(CBotDefParam* pParam) +{ + CBotDefParam* pp = m_Param; + while ( pp != NULL && pParam != NULL ) + { + CBotTypResult type1 = pp->GivType(); + CBotTypResult type2 = pParam->GivType(); + if ( !type1.Compare(type2) ) return false; + pp = pp->GivNext(); + pParam = pParam->GivNext(); + } + return ( pp == NULL && pParam == NULL ); +} + +CBotString CBotFunction::GivName() +{ + return m_token.GivString(); +} + +CBotString CBotFunction::GivParams() +{ + if ( m_Param == NULL ) return CBotString("()"); + + CBotString params = "( "; + CBotDefParam* p = m_Param; // list of parameters + + while (p != NULL) + { + params += p->GivParamString(); + p = p->GivNext(); + if ( p != NULL ) params += ", "; + } + + params += " )"; + return params; +} + +CBotFunction* CBotFunction::Next() +{ + return m_next; +} + +void CBotFunction::AddPublic(CBotFunction* func) +{ + if ( m_listPublic != NULL ) + { + func->m_nextpublic = m_listPublic; + m_listPublic->m_prevpublic = func; + } + m_listPublic = func; +} + + + +///////////////////////////////////////////////////////////////////////// +// management of parameters + + +CBotDefParam::CBotDefParam() +{ + m_next = NULL; + m_nIdent = 0; +} + +CBotDefParam::~CBotDefParam() +{ + delete m_next; +} + + +// compiles a list of parameters +CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) +{ + // mainly not pStack->TokenStack here + // declared variables must remain visible thereafter + + pStack->SetStartError(p->GivStart()); + + if (IsOfType(p, ID_OPENPAR)) + { + CBotDefParam* list = NULL; + + while (!IsOfType(p, ID_CLOSEPAR)) + { + CBotDefParam* param = new CBotDefParam(); + if (list == NULL) list = param; + else list->AddNext(param); // added to the list + + CBotClass* pClass = NULL;//= CBotClass::Find(p); + param->m_typename = p->GivString(); + CBotTypResult type = param->m_type = TypeParam(p, pStack); +// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object + + if (param->m_type.GivType() > 0) + { + CBotToken* pp = p; + param->m_token = *p; + if (pStack->IsOk() && IsOfType(p, TokenTypVar) ) + { + + // variable already declared? + if (pStack->CheckVarLocal(pp)) + { + pStack->SetError(TX_REDEFVAR, pp); + break; + } + + if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody); + CBotVar* var = CBotVar::Create(pp->GivString(), type); // creates the variable +// if ( pClass ) var->SetClass(pClass); + var->SetInit(2); // mark initialized + param->m_nIdent = CBotVar::NextUniqNum(); + var->SetUniqNum(param->m_nIdent); + pStack->AddVar(var); // place on the stack + + if (IsOfType(p, ID_COMMA) || p->GivType() == ID_CLOSEPAR) + continue; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStack->SetError(TX_NOTYP, p); + delete list; + return NULL; + } + return list; + } + pStack->SetError(TX_OPENPAR, p->GivStart()); + return NULL; +} + +void CBotDefParam::AddNext(CBotDefParam* p) +{ + CBotDefParam* pp = this; + while (pp->m_next != NULL) pp = pp->m_next; + + pp->m_next = p; +} + + +bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) +{ + int i = 0; + CBotDefParam* p = this; + + while ( p != NULL ) + { + // creates a local variable on the stack + CBotVar* newvar = CBotVar::Create(p->m_token.GivString(), p->m_type); + + // serves to make the transformation of types: + if ( ppVars != NULL && ppVars[i] != NULL ) + { + switch (p->m_type.GivType()) + { + case CBotTypInt: + newvar->SetValInt(ppVars[i]->GivValInt()); + break; + case CBotTypFloat: + newvar->SetValFloat(ppVars[i]->GivValFloat()); + break; + case CBotTypString: + newvar->SetValString(ppVars[i]->GivValString()); + break; + case CBotTypBoolean: + newvar->SetValInt(ppVars[i]->GivValInt()); + break; + case CBotTypIntrinsic: + ((CBotVarClass*)newvar)->Copy(ppVars[i], false); + break; + case CBotTypPointer: + case CBotTypArrayPointer: + { + newvar->SetPointer(ppVars[i]->GivPointer()); + } + break; + default: + ASM_TRAP(); + } + } + newvar->SetUniqNum(p->m_nIdent); + pj->AddVar(newvar); // add a variable + p = p->m_next; + i++; + } + + return true; +} + +void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) +{ + int i = 0; + CBotDefParam* p = this; + + while ( p != NULL ) + { + // creates a local variable on the stack + CBotVar* var = pj->FindVar(p->m_token.GivString()); + var->SetUniqNum(p->m_nIdent); + p = p->m_next; + } +} + +int CBotDefParam::GivType() +{ + return m_type.GivType(); +} + +CBotTypResult CBotDefParam::GivTypResult() +{ + return m_type; +} + +CBotDefParam* CBotDefParam::GivNext() +{ + return m_next; +} + +CBotString CBotDefParam::GivParamString() +{ + CBotString param; + + param = m_typename; + param += ' '; + + param += m_token.GivString(); + return param; +} + + + +////////////////////////////////////////////////////////////////////////// +// return parameters + +CBotReturn::CBotReturn() +{ + m_Instr = NULL; + name = "CBotReturn"; // debug +} + +CBotReturn::~CBotReturn() +{ + delete m_Instr; +} + +CBotInstr* CBotReturn::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + + if (!IsOfType(p, ID_RETURN)) return NULL; // should never happen + + CBotReturn* inst = new CBotReturn(); // creates the object + inst->SetToken( pp ); + + CBotTypResult type = pStack->GivRetType(); + + if ( type.GivType() == 0 ) // returned void ? + { + if ( IsOfType( p, ID_SEP ) ) return inst; + pStack->SetError( TX_BADTYPE, pp ); + return NULL; + } + + inst->m_Instr = CBotExpression::Compile(p, pStack); + if ( pStack->IsOk() ) + { + CBotTypResult retType = pStack->GivTypResult(2); + if (TypeCompatible(retType, type, ID_ASS)) + { + if ( IsOfType( p, ID_SEP ) ) + return inst; + + pStack->SetError(TX_ENDOF, p->GivStart()); + } + pStack->SetError(TX_BADTYPE, p->GivStart()); + } + + delete inst; + return NULL; // no object, the error is on the stack +} + +bool CBotReturn::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + if ( pile->GivState() == 0 ) + { + if ( m_Instr != NULL && !m_Instr->Execute(pile) ) return false; // evaluate the result + // the result is on the stack + pile->IncState(); + } + + if ( pile->IfStep() ) return false; + + pile->SetBreak(3, CBotString()); + return pj->Return(pile); +} + +void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; + + if ( pile->GivState() == 0 ) + { + if ( m_Instr != NULL ) m_Instr->RestoreState(pile, bMain); // evaluate the result + return; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Calls of these functions + +CBotInstrCall::CBotInstrCall() +{ + m_Parameters = NULL; + m_nFuncIdent = 0; + name = "CBotInstrCall"; +} + +CBotInstrCall::~CBotInstrCall() +{ + delete m_Parameters; +} + +CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotVar* ppVars[1000]; + + int i = 0; + + CBotToken* pp = p; + p = p->GivNext(); + + pStack->SetStartError(p->GivStart()); + CBotCStack* pile = pStack; + + if ( IsOfType(p, ID_OPENPAR) ) + { + int start, end; + CBotInstrCall* inst = new CBotInstrCall(); + inst->SetToken(pp); + + // compile la list of parameters + if (!IsOfType(p, ID_CLOSEPAR)) while (true) + { + start = p->GivStart(); + pile = pile->TokenStack(); // keeps the results on the stack + + CBotInstr* param = CBotExpression::Compile(p, pile); + end = p->GivStart(); + if ( inst->m_Parameters == NULL ) inst->m_Parameters = param; + else inst->m_Parameters->AddNext(param); // constructs the list + + if ( !pile->IsOk() ) + { + delete inst; + return pStack->Return(NULL, pile); + } + + if ( param != NULL ) + { + if ( pile->GivTypResult().Eq(99) ) + { + delete pStack->TokenStack(); + pStack->SetError(TX_VOID, p->GivStart()); + delete inst; + return NULL; + } + ppVars[i] = pile->GivVar(); + ppVars[i]->GivToken()->SetPos(start, end); + i++; + + if (IsOfType(p, ID_COMMA)) continue; // skips the comma + if (IsOfType(p, ID_CLOSEPAR)) break; + } + + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + delete pStack->TokenStack(); + delete inst; + return NULL; + } + ppVars[i] = NULL; + + // the routine is known? +// CBotClass* pClass = NULL; + inst->m_typRes = pStack->CompileCall(pp, ppVars, inst->m_nFuncIdent); + if ( inst->m_typRes.GivType() >= 20 ) + { +// if (pVar2!=NULL) pp = pVar2->RetToken(); + pStack->SetError( inst->m_typRes.GivType(), pp ); + delete pStack->TokenStack(); + delete inst; + return NULL; + } + + delete pStack->TokenStack(); + if ( inst->m_typRes.GivType() > 0 ) + { + CBotVar* pRes = CBotVar::Create("", inst->m_typRes); + pStack->SetVar(pRes); // for knowing the type of the result + } + else pStack->SetVar(NULL); // routine returns void + + return inst; + } + p = pp; + delete pStack->TokenStack(); + return NULL; +} + +bool CBotInstrCall::Execute(CBotStack* &pj) +{ + CBotVar* ppVars[1000]; + CBotStack* pile = pj->AddStack(this); + if ( pile->StackOver() ) return pj->Return( pile ); + + CBotStack* pile1 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates parameters + // and places the values ​​on the stack + // for allow of interruption at any time + if ( p != NULL) while ( true ) + { + pile = pile->AddStack(); // place on the stack for the results + if ( pile->GivState() == 0 ) + { + if (!p->Execute(pile)) return false; // interrupted here? + pile->SetState(1); // mark as special for reknowed parameters \TODO marque spéciale pour reconnaîre parameters + } + ppVars[i++] = pile->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotStack* pile2 = pile->AddStack(); + if ( pile2->IfStep() ) return false; + + if ( !pile2->ExecuteCall(m_nFuncIdent, GivToken(), ppVars, m_typRes)) return false; // interrupt + + return pj->Return(pile2); // release the entire stack +} + +void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; + + CBotStack* pile1 = pile; + + int i = 0; + CBotVar* ppVars[1000]; + CBotInstr* p = m_Parameters; + // evaluate parameters + // and place the values on the stack + // for allow of interruption at any time + if ( p != NULL) while ( true ) + { + pile = pile->RestoreStack(); // place on the stack for the results + if ( pile == NULL ) return; + if ( pile->GivState() == 0 ) + { + p->RestoreState(pile, bMain); // interrupt here! + return; + } + ppVars[i++] = pile->GivVar(); // constructs the list of parameters + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotStack* pile2 = pile->RestoreStack(); + if ( pile2 == NULL ) return; + + pile2->RestoreCall(m_nFuncIdent, GivToken(), ppVars); +} + +////////////////////////////////////////////////////////////////////////////// +// statement of user classes + +// pre-compile a new class +// analysis is complete except the body of routines + +CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) +{ + if ( !IsOfType(p, ID_PUBLIC) ) + { + pStack->SetError(TX_NOPUBLIC, p); + return NULL; + } + + if ( !IsOfType(p, ID_CLASS) ) return NULL; + + CBotString name = p->GivString(); + + CBotClass* pOld = CBotClass::Find(name); + if ( pOld != NULL && pOld->m_IsDef ) + { + pStack->SetError( TX_REDEFCLASS, p ); + return NULL; + } + + // a name of the class is there? + if (IsOfType(p, TokenTypVar)) + { + CBotClass* pPapa = NULL; + if ( IsOfType( p, ID_EXTENDS ) ) + { + CBotString name = p->GivString(); + pPapa = CBotClass::Find(name); + + if (!IsOfType(p, TokenTypVar) || pPapa == NULL ) + { + pStack->SetError( TX_NOCLASS, p ); + return NULL; + } + } + CBotClass* classe = (pOld == NULL) ? new CBotClass(name, pPapa) : pOld; + classe->Purge(); // emptythe old definitions + classe->m_IsDef = false; // current definition + + if ( !IsOfType( p, ID_OPBLK) ) + { + pStack->SetError(TX_OPENBLK, p); + return NULL; + } + + while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) + { + classe->CompileDefItem(p, pStack, false); + } + + if (pStack->IsOk()) return classe; + } + pStack->SetError(TX_ENDOF, p); + return NULL; +} + +bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) +{ + bool bStatic = false; + int mProtect = PR_PUBLIC; + bool bSynchro = false; + + while (IsOfType(p, ID_SEP)) ; + + CBotTypResult type( -1 ); + + if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; + CBotToken* pBase = p; + + if ( IsOfType(p, ID_STATIC) ) bStatic = true; + if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; + if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; + if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; + if ( IsOfType(p, ID_STATIC) ) bStatic = true; + +// CBotClass* pClass = NULL; + type = TypeParam(p, pStack); // type of the result + + if ( type.Eq(-1) ) + { + pStack->SetError(TX_NOTYP, p); + return false; + } + + while (pStack->IsOk()) + { + CBotToken* pp = p; + IsOfType(p, ID_NOT); // skips ~ eventual (destructor) + + if (IsOfType(p, TokenTypVar)) + { + CBotInstr* limites = NULL; + while ( IsOfType( p, ID_OPBRK ) ) // a table? + { + CBotInstr* i = NULL; + + if ( p->GivType() != ID_CLBRK ) + i = CBotExpression::Compile( p, pStack ); // expression for the value + else + i = new CBotEmpty(); // special if not a formula + + type = CBotTypResult(CBotTypArrayPointer, type); + + if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) + { + pStack->SetError(TX_CLBRK, p->GivStart()); + return false; + } + +/* CBotVar* pv = pStack->GivVar(); + if ( pv->GivType()>= CBotTypBoolean ) + { + pStack->SetError(TX_BADTYPE, p->GivStart()); + return false; + }*/ + + if (limites == NULL) limites = i; + else limites->AddNext3(i); + } + + if ( p->GivType() == ID_OPENPAR ) + { + if ( !bSecond ) + { + p = pBase; + CBotFunction* f = + CBotFunction::Compile1(p, pStack, this); + + if ( f == NULL ) return false; + + if (m_pMethod == NULL) m_pMethod = f; + else m_pMethod->AddNext(f); + } + else + { + // return a method precompiled in pass 1 + CBotFunction* pf = m_pMethod; + CBotFunction* prev = NULL; + while ( pf != NULL ) + { + if (pf->GivName() == pp->GivString()) break; + prev = pf; + pf = pf->Next(); + } + + bool bConstructor = (pp->GivString() == GivName()); + CBotCStack* pile = pStack->TokenStack(NULL, true); + + // make "this" known + CBotToken TokenThis(CBotString("this"), CBotString()); + CBotVar* pThis = CBotVar::Create(&TokenThis, CBotTypResult( CBotTypClass, this ) ); + pThis->SetUniqNum(-2); + pile->AddVar(pThis); + + if ( m_pParent ) + { + // makes "super" known + CBotToken TokenSuper(CBotString("super"), CBotString()); + CBotVar* pThis = CBotVar::Create(&TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) ); + pThis->SetUniqNum(-3); + pile->AddVar(pThis); + } + +// int num = 1; + CBotClass* my = this; + while (my != NULL) + { + // places a copy of variables of a class (this) on a stack + CBotVar* pv = my->m_pVar; + while (pv != NULL) + { + CBotVar* pcopy = CBotVar::Create(pv); + pcopy->SetInit(!bConstructor || pv->IsStatic()); + pcopy->SetUniqNum(pv->GivUniqNum()); + pile->AddVar(pcopy); + pv = pv->GivNext(); + } + my = my->m_pParent; + } + + // compiles a method + p = pBase; + CBotFunction* f = + CBotFunction::Compile(p, pile, NULL/*, false*/); + + if ( f != NULL ) + { + f->m_pProg = pStack->GivBotCall(); + f->m_bSynchro = bSynchro; + // replaces the element in the chain + f->m_next = pf->m_next; + pf->m_next = NULL; + delete pf; + if (prev == NULL) m_pMethod = f; + else prev->m_next = f; + } + pStack->Return(NULL, pile); + } + + return pStack->IsOk(); + } + + // definition of an element + if (type.Eq(0)) + { + pStack->SetError(TX_ENDOF, p); + return false; + } + + CBotInstr* i = NULL; + if ( IsOfType(p, ID_ASS ) ) + { + if ( type.Eq(CBotTypArrayPointer) ) + { + i = CBotListArray::Compile(p, pStack, type.GivTypElem()); + } + else + { + // it has an assignmet to calculate + i = CBotTwoOpExpr::Compile(p, pStack); + } + if ( !pStack->IsOk() ) return false; + } + + + if ( !bSecond ) + { + CBotVar* pv = CBotVar::Create(pp->GivString(), type); + pv -> SetStatic( bStatic ); + pv -> SetPrivate( mProtect ); + + AddItem( pv ); + + pv->m_InitExpr = i; + pv->m_LimExpr = limites; + + + if ( pv->IsStatic() && pv->m_InitExpr != NULL ) + { + CBotStack* pile = CBotStack::FirstStack(); // independent stack + while(pile->IsOk() && !pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer + pv->SetVal( pile->GivVar() ) ; + pile->Delete(); + } + } + else + delete i; + + if ( IsOfType(p, ID_COMMA) ) continue; + if ( IsOfType(p, ID_SEP) ) break; + } + pStack->SetError(TX_ENDOF, p); + } + return pStack->IsOk(); +} + + +CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) +{ + if ( !IsOfType(p, ID_PUBLIC) ) return NULL; + if ( !IsOfType(p, ID_CLASS) ) return NULL; + + CBotString name = p->GivString(); + + // a name for the class is there? + if (IsOfType(p, TokenTypVar)) + { + // the class was created by Compile1 + CBotClass* pOld = CBotClass::Find(name); + + if ( IsOfType( p, ID_EXTENDS ) ) + { + IsOfType(p, TokenTypVar); // necessarily + } + IsOfType( p, ID_OPBLK); // necessarily + + while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) + { + pOld->CompileDefItem(p, pStack, true); + } + + pOld->m_IsDef = true; // complete definition + if (pStack->IsOk()) return pOld; + } + pStack->SetError(TX_ENDOF, p); + return NULL; +} diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 419798f..ddb26c6 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -1,1471 +1,1471 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.////////////////////////////////////////////////////////////////////// - -//Management of the stack - - -#include "CBot.h" -#include -#include - - -#define ITIMER 100 - -//////////////////////////////////////////////////////////////////////////// -// gestion de la pile d'excution -//////////////////////////////////////////////////////////////////////////// - -int CBotStack::m_initimer = ITIMER; -int CBotStack::m_timer = 0; -CBotVar* CBotStack::m_retvar = NULL; -int CBotStack::m_error = 0; -int CBotStack::m_start = 0; -int CBotStack::m_end = 0; -CBotString CBotStack::m_labelBreak=""; -void* CBotStack::m_pUser = NULL; - -#if STACKMEM - -CBotStack* CBotStack::FirstStack() -{ - CBotStack* p; - - long size = sizeof(CBotStack); - size *= (MAXSTACK+10); - - // demande une tranche mmoire pour la pile - p = (CBotStack*)malloc(size); - - // la vide totalement - memset(p, 0, size); - - p-> m_bBlock = true; - m_timer = m_initimer; // met le timer au dbut - - CBotStack* pp = p; - pp += MAXSTACK; - int i; - for ( i = 0 ; i< 10 ; i++ ) - { - pp->m_bOver = true; - pp ++; - } -#ifdef _DEBUG - int n = 1; - pp = p; - for ( i = 0 ; i< MAXSTACK+10 ; i++ ) - { - pp->m_index = n++; - pp ++; - } -#endif - - m_error = 0; // vite des blocages car m_error est static - return p; -} - -CBotStack::CBotStack(CBotStack* ppapa) -{ - // constructor must exist or the destructor is never called! - ASM_TRAP(); -} - -CBotStack::~CBotStack() -{ - ASM_TRAP(); // utiliser Delete() la place -} - -void CBotStack::Delete() -{ - if ( this == NULL || this == EOX ) return; - - m_next->Delete(); - m_next2->Delete(); - - if (m_prev != NULL) - { - if ( m_prev->m_next == this ) - m_prev->m_next = NULL; // enlve de la chane - - if ( m_prev->m_next2 == this ) - m_prev->m_next2 = NULL; // enlve de la chane - } - - delete m_var; - delete m_listVar; - - CBotStack* p = m_prev; - bool bOver = m_bOver; -#ifdef _DEBUG - int n = m_index; -#endif - - // efface le bloc libr - memset(this, 0, sizeof(CBotStack)); - m_bOver = bOver; -#ifdef _DEBUG - m_index = n; -#endif - - if ( p == NULL ) - free( this ); -} - - -// routine optimise -CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) -{ - if (m_next != NULL) - { - return m_next; // reprise dans une pile existante - } - -#ifdef _DEBUG - int n = 0; -#endif - CBotStack* p = this; - do - { - p ++; -#ifdef _DEBUG - n ++; -#endif - } - while ( p->m_prev != NULL ); - - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_instr = instr; - p->m_prog = m_prog; - p->m_step = 0; - p->m_prev = this; - p->m_state = 0; - p->m_call = NULL; - p->m_bFunc = false; - return p; -} - -CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) -{ - if (m_next != NULL) - { - if ( m_next == EOX ) - { - m_next = NULL; - return EOX; - } - return m_next; // reprise dans une pile existante - } - CBotStack* p = AddStack(NULL, bBlock); - p->m_call = instr; - p->m_bFunc = 2; // spcial - return p; -} - -CBotStack* CBotStack::AddStack2(bool bBlock) -{ - if (m_next2 != NULL) - { - m_next2->m_prog = m_prog; // spcial vite un RestoreStack2 - return m_next2; // reprise dans une pile existante - } - - CBotStack* p = this; - do - { - p ++; - } - while ( p->m_prev != NULL ); - - m_next2 = p; // chane l'lment - p->m_prev = this; - p->m_bBlock = bBlock; - p->m_prog = m_prog; - p->m_step = 0; - return p; -} - -bool CBotStack::GivBlock() -{ - return m_bBlock; -} - -bool CBotStack::Return(CBotStack* pfils) -{ - if ( pfils == this ) return true; // spcial - - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable - - m_next->Delete();m_next = NULL; // libre la pile au dessus - m_next2->Delete();m_next2 = NULL; // aussi la seconde pile (catch) - - return (m_error == 0); // interrompu si erreur -} - -bool CBotStack::ReturnKeep(CBotStack* pfils) -{ - if ( pfils == this ) return true; // spcial - - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable - - return (m_error == 0); // interrompu si erreur -} - -bool CBotStack::StackOver() -{ - if (!m_bOver) return false; - m_error = TX_STACKOVER; - return true; -} - -#else - -CBotStack::CBotStack(CBotStack* ppapa) -{ - m_next = NULL; - m_next2 = NULL; - m_prev = ppapa; - - m_bBlock = (ppapa == NULL) ? true : false; - - m_state = 0; - m_step = 1; - - if (ppapa == NULL) m_timer = m_initimer; // met le timer au dbut - - m_listVar = NULL; - m_bDontDelete = false; - - m_var = NULL; - m_prog = NULL; - m_instr = NULL; - m_call = NULL; - m_bFunc = false; -} - -// destructeur -CBotStack::~CBotStack() -{ - if ( m_next != EOX) delete m_next; - delete m_next2; - if (m_prev != NULL && m_prev->m_next == this ) - m_prev->m_next = NULL; // enlve de la chane - - delete m_var; - if ( !m_bDontDelete ) delete m_listVar; -} - -// routine optimiser -CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) -{ - if (m_next != NULL) - { - return m_next; // reprise dans une pile existante - } - CBotStack* p = new CBotStack(this); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_instr = instr; - p->m_prog = m_prog; - p->m_step = 0; - return p; -} - -CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) -{ - if (m_next != NULL) - { - if ( m_next == EOX ) - { - m_next = NULL; - return EOX; - } - return m_next; // reprise dans une pile existante - } - CBotStack* p = new CBotStack(this); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_call = instr; - p->m_prog = m_prog; - p->m_step = 0; - p->m_bFunc = 2; // spcial - return p; -} - -CBotStack* CBotStack::AddStack2(bool bBlock) -{ - if (m_next2 != NULL) - { - m_next2->m_prog = m_prog; // spcial vite un RestoreStack2 - return m_next2; // reprise dans une pile existante - } - - CBotStack* p = new CBotStack(this); - m_next2 = p; // chane l'lment - p->m_bBlock = bBlock; - p->m_prog = m_prog; - p->m_step = 0; - - return p; -} - -bool CBotStack::Return(CBotStack* pfils) -{ - if ( pfils == this ) return true; // spcial - - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruite la variable - - if ( m_next != EOX ) delete m_next; // libre la pile au dessus - delete m_next2;m_next2 = NULL; // aussi la seconde pile (catch) - - return (m_error == 0); // interrompu si erreur -} - -bool CBotStack::StackOver() -{ - return false; // pas de test de dbordement dans cette version -} - -#endif - -void CBotStack::Reset(void* pUser) -{ - m_timer = m_initimer; // remet le timer - m_error = 0; -// m_start = 0; -// m_end = 0; - m_labelBreak.Empty(); - m_pUser = pUser; -} - - - - -CBotStack* CBotStack::RestoreStack(CBotInstr* instr) -{ - if (m_next != NULL) - { - m_next->m_instr = instr; // rinit (si reprise aprs restitution) - m_next->m_prog = m_prog; - return m_next; // reprise dans une pile existante - } - return NULL; -} - -CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) -{ - CBotStack* p = RestoreStack(); - p->m_call = instr; - return p; -} - - - -// routine pour l'excution pas pas -bool CBotStack::IfStep() -{ - if ( m_initimer > 0 || m_step++ > 0 ) return false; - return true; -} - - -bool CBotStack::BreakReturn(CBotStack* pfils, const char* name) -{ - if ( m_error>=0 ) return false; // sortie normale - if ( m_error==-3 ) return false; // sortie normale (return en cours) - - if (!m_labelBreak.IsEmpty() && (name[0] == 0 || m_labelBreak != name)) - return false; // c'est pas pour moi - - m_error = 0; - m_labelBreak.Empty(); - return Return(pfils); -} - -bool CBotStack::IfContinue(int state, const char* name) -{ - if ( m_error != -2 ) return false; - - if (!m_labelBreak.IsEmpty() && (name == NULL || m_labelBreak != name)) - return false; // c'est pas pour moi - - m_state = state; // o reprendre ? - m_error = 0; - m_labelBreak.Empty(); - if ( m_next != EOX ) m_next->Delete(); // purge la pile au dessus - return true; -} - -void CBotStack::SetBreak(int val, const char* name) -{ - m_error = -val; // ragit comme une Exception - m_labelBreak = name; - if (val == 3) // pour un return - { - m_retvar = m_var; - m_var = NULL; - } -} - -// remet sur la pile la valeur calcule par le dernier CBotReturn - -bool CBotStack::GivRetVar(bool bRet) -{ - if (m_error == -3) - { - if ( m_var ) delete m_var; - m_var = m_retvar; - m_retvar = NULL; - m_error = 0; - return true; - } - return bRet; // interrompu par autre chose que return -} - -int CBotStack::GivError(int& start, int& end) -{ - start = m_start; - end = m_end; - return m_error; -} - - -int CBotStack::GivType(int mode) -{ - if (m_var == NULL) return -1; - return m_var->GivType(mode); -} - -CBotTypResult CBotStack::GivTypResult(int mode) -{ - if (m_var == NULL) return -1; - return m_var->GivTypResult(mode); -} - -void CBotStack::SetType(CBotTypResult& type) -{ - if (m_var == NULL) return; - m_var->SetType( type ); -} - - -CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif) -{ - CBotStack* p = this; - CBotString name = pToken->GivString(); - - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (pp->GivName() == name) - { - if ( bUpdate ) - pp->Maj(m_pUser, false); - - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; -} - -CBotVar* CBotStack::FindVar(const char* name) -{ - CBotStack* p = this; - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (pp->GivName() == name) - { - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; -} - -CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif) -{ - CBotStack* p = this; - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (pp->GivUniqNum() == ident) - { - if ( bUpdate ) - pp->Maj(m_pUser, false); - - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; -} - - -CBotVar* CBotStack::FindVar(CBotToken& Token, bool bUpdate, bool bModif) -{ - CBotToken* pt = &Token; - return FindVar(pt, bUpdate, bModif); -} - - -CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) -{ - CBotVar* pVar = FindVar( Token, bUpdate ); - - if ( pVar == NULL) return NULL; - - CBotVar* pCopy = CBotVar::Create(pVar); - pCopy->Copy(pVar); - return pCopy; -} - - -bool CBotStack::SetState(int n, int limite) -{ - m_state = n; - - m_timer--; // dcompte les oprations - return ( m_timer > limite ); // interrompu si timer pass -} - -bool CBotStack::IncState(int limite) -{ - m_state++; - - m_timer--; // dcompte les oprations - return ( m_timer > limite ); // interrompu si timer pass -} - - -void CBotStack::SetError(int n, CBotToken* token) -{ - if ( n!= 0 && m_error != 0) return; // ne change pas une erreur dj existante - m_error = n; - if (token != NULL) - { - m_start = token->GivStart(); - m_end = token->GivEnd(); - } -} - -void CBotStack::ResetError(int n, int start, int end) -{ - m_error = n; - m_start = start; - m_end = end; -} - -void CBotStack::SetPosError(CBotToken* token) -{ - m_start = token->GivStart(); - m_end = token->GivEnd(); -} - -void CBotStack::SetTimer(int n) -{ - m_initimer = n; -} - -bool CBotStack::Execute() -{ - CBotCall* instr = NULL; // instruction la plus leve - CBotStack* pile; - - CBotStack* p = this; - - while (p != NULL) - { - if ( p->m_next2 != NULL ) break; - if ( p->m_call != NULL ) - { - instr = p->m_call; - pile = p->m_prev ; - } - p = p->m_next; - } - - if ( instr == NULL ) return true; // excution normale demande - - if (!instr->Run(pile)) return false; // excution partir de l - -#if STACKMEM - pile->m_next->Delete(); -#else - delete pile->m_next; -#endif - - pile->m_next = EOX; // spcial pour reprise - return true; -} - -// met sur le stack le pointeur une variable -void CBotStack::SetVar( CBotVar* var ) -{ - if (m_var) delete m_var; // remplacement d'une variable - m_var = var; -} - -// met sur le stack une copie d'une variable -void CBotStack::SetCopyVar( CBotVar* var ) -{ - if (m_var) delete m_var; // remplacement d'une variable - - m_var = CBotVar::Create("", var->GivTypResult(2)); - m_var->Copy( var ); -} - -CBotVar* CBotStack::GivVar() -{ - return m_var; -} - -CBotVar* CBotStack::GivPtVar() -{ - CBotVar* p = m_var; - m_var = NULL; // ne sera pas dtruit donc - return p; -} - -CBotVar* CBotStack::GivCopyVar() -{ - if (m_var == NULL) return NULL; - CBotVar* v = CBotVar::Create("", m_var->GivType()); - v->Copy( m_var ); - return v; -} - -long CBotStack::GivVal() -{ - if (m_var == NULL) return 0; - return m_var->GivValInt(); -} - - - - -void CBotStack::AddVar(CBotVar* pVar) -{ - CBotStack* p = this; - - // revient sur l'lement pre - while (p != NULL && p->m_bBlock == 0) p = p->m_prev; - - if ( p == NULL ) return; - -/// p->m_bDontDelete = bDontDelete; - - CBotVar** pp = &p->m_listVar; - while ( *pp != NULL ) pp = &(*pp)->m_next; - - *pp = pVar; // ajoute la suite - -#ifdef _DEBUG - if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); -#endif -} - -/*void CBotStack::RestoreVar(CBotVar* pVar) -{ - if ( !m_bDontDelete ) __asm int 3; - delete m_listVar; - m_listVar = pVar; // remplace directement -}*/ - -void CBotStack::SetBotCall(CBotProgram* p) -{ - m_prog = p; - m_bFunc = true; -} - -CBotProgram* CBotStack::GivBotCall(bool bFirst) -{ - if ( ! bFirst ) return m_prog; - CBotStack* p = this; - while ( p->m_prev != NULL ) p = p->m_prev; - return p->m_prog; -} - -void* CBotStack::GivPUser() -{ - return m_pUser; -} - - -bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype) -{ - CBotTypResult res; - - // cherche d'abord selon l'identificateur - - res = CBotCall::DoCall(nIdent, NULL, ppVar, this, rettype ); - if (res.GivType() >= 0) return res.GivType(); - - res = m_prog->GivFunctions()->DoCall(nIdent, NULL, ppVar, this, token ); - if (res.GivType() >= 0) return res.GivType(); - - // si pas trouv (recompil ?) cherche selon le nom - - nIdent = 0; - res = CBotCall::DoCall(nIdent, token, ppVar, this, rettype ); - if (res.GivType() >= 0) return res.GivType(); - - res = m_prog->GivFunctions()->DoCall(nIdent, token->GivString(), ppVar, this, token ); - if (res.GivType() >= 0) return res.GivType(); - - SetError(TX_NOCALL, token); - return true; -} - -void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) -{ - if ( m_next == NULL ) return; - - if ( !CBotCall::RestoreCall(nIdent, token, ppVar, this) ) - m_prog->GivFunctions()->RestoreCall(nIdent, token->GivString(), ppVar, this ); -} - - -bool SaveVar(FILE* pf, CBotVar* pVar) -{ - while ( true ) - { - if ( pVar == NULL ) - { - return WriteWord(pf, 0); // met un terminateur - } - - if ( !pVar->Save0State(pf)) return false; // entte commune - if ( !pVar->Save1State(pf) ) return false; // sauve selon la classe fille - - pVar = pVar->GivNext(); - } -} - -void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) -{ - CBotProgram* prog = m_prog; // programme courrant - - CBotInstr* funct = NULL; // fonction trouve - CBotInstr* instr = NULL; // instruction la plus leve - - CBotStack* p = this; - - while (p->m_next != NULL) - { - if ( p->m_instr != NULL ) instr = p->m_instr; - if ( p->m_bFunc == 1 ) funct = p->m_instr; - if ( p->m_next->m_prog != prog ) break ; - - if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; - else p = p->m_next; - } - - if ( p->m_instr != NULL ) instr = p->m_instr; - if ( p->m_bFunc == 1 ) funct = p->m_instr; - - if ( funct == NULL ) return; - - CBotToken* t = funct->GivToken(); - FunctionName = t->GivString(); - -// if ( p->m_instr != NULL ) instr = p->m_instr; - - t = instr->GivToken(); - start = t->GivStart(); - end = t->GivEnd(); -} - -CBotVar* CBotStack::GivStackVars(const char* &FunctionName, int level) -{ - CBotProgram* prog = m_prog; // programme courrant - FunctionName = NULL; - - // remonte la pile dans le module courant - CBotStack* p = this; - - while (p->m_next != NULL) - { - if ( p->m_next->m_prog != prog ) break ; - - if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; - else p = p->m_next; - } - - - // descend sur les lments de block - while ( p != NULL && !p->m_bBlock ) p = p->m_prev; - - while ( p != NULL && level++ < 0 ) - { - p = p->m_prev; - while ( p != NULL && !p->m_bBlock ) p = p->m_prev; - } - - if ( p == NULL ) return NULL; - - // recherche le nom de la fonction courante - CBotStack* pp = p; - while ( pp != NULL ) - { - if ( pp->m_bFunc == 1 ) break; - pp = pp->m_prev; - } - - if ( pp == NULL || pp->m_instr == NULL ) return NULL; - - CBotToken* t = pp->m_instr->GivToken(); - FunctionName = t->GivString(); - - return p->m_listVar; -} - -bool CBotStack::SaveState(FILE* pf) -{ - if ( this == NULL ) // fin de l'arbre ? - { - return WriteWord(pf, 0); // met un terminateur - } - - if ( m_next2 != NULL ) - { - if (!WriteWord(pf, 2)) return false; // une marque de poursuite - if (!m_next2->SaveState(pf)) return false; - } - else - { - if (!WriteWord(pf, 1)) return false; // une marque de poursuite - } - if (!WriteWord(pf, m_bBlock)) return false; // est-ce un bloc local - if (!WriteWord(pf, m_state)) return false; // dans quel tat - if (!WriteWord(pf, 0)) return false; // par compatibilit m_bDontDelete - if (!WriteWord(pf, m_step)) return false; // dans quel tat - - - if (!SaveVar(pf, m_var)) return false; // le rsultat courant - if (!SaveVar(pf, m_listVar)) return false; // les variables locales - - return m_next->SaveState(pf); // enregistre la suite -} - - -bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) -{ - unsigned short w; - - pStack = NULL; - if (!ReadWord(pf, w)) return false; - if ( w == 0 ) return true; - -#if STACKMEM - if ( this == NULL ) pStack = FirstStack(); - else pStack = AddStack(); -#else - pStack = new CBotStack(this); -#endif - - if ( w == 2 ) - { - if (!pStack->RestoreState(pf, pStack->m_next2)) return false; - } - - if (!ReadWord(pf, w)) return false; // est-ce un bloc local - pStack->m_bBlock = w; - - if (!ReadWord(pf, w)) return false; // dans quel tat j're ? - pStack->SetState((short)w); // dans le bon tat - - if (!ReadWord(pf, w)) return false; // dont delete ? - // plus utilis - - if (!ReadWord(pf, w)) return false; // pas pas - pStack->m_step = w; - - if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // la variable temp - if (!CBotVar::RestoreState(pf, pStack->m_listVar)) return false;// les variables locales - - return pStack->RestoreState(pf, pStack->m_next); -} - - -bool CBotVar::Save0State(FILE* pf) -{ - if (!WriteWord(pf, 100+m_mPrivate))return false; // variable prive ? - if (!WriteWord(pf, m_bStatic))return false; // variable static ? - if (!WriteWord(pf, m_type.GivType()))return false; // enregiste le type (toujours non nul) - if (!WriteWord(pf, m_binit))return false; // variable dfinie ? - return WriteString(pf, m_token->GivString()); // et le nom de la variable -} - -bool CBotVarInt::Save0State(FILE* pf) -{ - if ( !m_defnum.IsEmpty() ) - { - if(!WriteWord(pf, 200 )) return false; // marqueur spcial - if(!WriteString(pf, m_defnum)) return false; // nom de la valeur - } - - return CBotVar::Save0State(pf); -} - -bool CBotVarInt::Save1State(FILE* pf) -{ - return WriteWord(pf, m_val); // la valeur de la variable -} - -bool CBotVarBoolean::Save1State(FILE* pf) -{ - return WriteWord(pf, m_val); // la valeur de la variable -} - -bool CBotVarFloat::Save1State(FILE* pf) -{ - return WriteFloat(pf, m_val); // la valeur de la variable -} - -bool CBotVarString::Save1State(FILE* pf) -{ - return WriteString(pf, m_val); // la valeur de la variable -} - - - -bool CBotVarClass::Save1State(FILE* pf) -{ - if ( !WriteType(pf, m_type) ) return false; - if ( !WriteLong(pf, m_ItemIdent) ) return false; - - return SaveVar(pf, m_pVar); // contenu de l'objet -} - -bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) -{ - unsigned short w, wi, prv, st; - float ww; - CBotString name, s; - - delete pVar; - - pVar = NULL; - CBotVar* pNew = NULL; - CBotVar* pPrev = NULL; - - while ( true ) // recupre toute une liste - { - if (!ReadWord(pf, w)) return false; // priv ou type ? - if ( w == 0 ) return true; - - CBotString defnum; - if ( w == 200 ) - { - if (!ReadString(pf, defnum)) return false; // nombre avec un identifiant - if (!ReadWord(pf, w)) return false; // type - } - - prv = 100; st = 0; - if ( w >= 100 ) - { - prv = w; - if (!ReadWord(pf, st)) return false; // statique - if (!ReadWord(pf, w)) return false; // type - } - - if ( w == CBotTypClass ) w = CBotTypIntrinsic; // forcment intrinsque - - if (!ReadWord(pf, wi)) return false; // init ? - - if (!ReadString(pf, name)) return false; // nom de la variable - - CBotToken token(name, CBotString()); - - switch (w) - { - case CBotTypInt: - case CBotTypBoolean: - pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadWord(pf, w)) return false; - pNew->SetValInt((short)w, defnum); - break; - case CBotTypFloat: - pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadFloat(pf, ww)) return false; - pNew->SetValFloat(ww); - break; - case CBotTypString: - pNew = CBotVar::Create(&token, w); // cre une variable - if (!ReadString(pf, s)) return false; - pNew->SetValString(s); - break; - - // restitue un objet intrinsic ou un lment d'un array - case CBotTypIntrinsic: - case CBotTypArrayBody: - { - CBotTypResult r; - long id; - if (!ReadType(pf, r)) return false; // type complet - if (!ReadLong(pf, id) ) return false; - -// if (!ReadString(pf, s)) return false; - { - CBotVar* p = NULL; - if ( id ) p = CBotVarClass::Find(id) ; - - pNew = new CBotVarClass(&token, r); // cre directement une instance - // attention cptuse = 0 - if ( !RestoreState(pf, ((CBotVarClass*)pNew)->m_pVar)) return false; - pNew->SetIdent(id); - - if ( p != NULL ) - { - delete pNew; - pNew = p; // reprend l'lment connu - } - } - } - break; - - case CBotTypPointer: - case CBotTypNullPointer: - if (!ReadString(pf, s)) return false; - { - pNew = CBotVar::Create(&token, CBotTypResult(w, s));// cre une variable - CBotVarClass* p = NULL; - long id; - ReadLong(pf, id); -// if ( id ) p = CBotVarClass::Find(id); // retrouve l'instance ( fait par RestoreInstance ) - - // restitue une copie de l'instance d'origine - CBotVar* pInstance = NULL; - if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; - ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus - -// if ( p != NULL ) ((CBotVarPointer*)pNew)->SetPointer( p ); // plutt celui-ci ! - - } - break; - - case CBotTypArrayPointer: - { - CBotTypResult r; - if (!ReadType(pf, r)) return false; - - pNew = CBotVar::Create(&token, r); // cre une variable - - // restitue une copie de l'instance d'origine - CBotVar* pInstance = NULL; - if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; - ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // et pointe dessus - } - break; - default: - ASM_TRAP(); - } - - if ( pPrev != NULL ) pPrev->m_next = pNew; - if ( pVar == NULL ) pVar = pNew; - - pNew->m_binit = wi; // pNew->SetInit(wi); - pNew->SetStatic(st); - pNew->SetPrivate(prv-100); - pPrev = pNew; - } - return true; -} - - - - -//////////////////////////////////////////////////////////////////////////// -// gestion de la pile la compilation -//////////////////////////////////////////////////////////////////////////// - -CBotProgram* CBotCStack::m_prog = NULL; // init la variable statique -int CBotCStack::m_error = 0; -int CBotCStack::m_end = 0; -CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); -//CBotToken* CBotCStack::m_retClass= NULL; - - -CBotCStack::CBotCStack(CBotCStack* ppapa) -{ - m_next = NULL; - m_prev = ppapa; - - if (ppapa == NULL) - { - m_error = 0; - m_start = 0; - m_end = 0; - m_bBlock = true; - } - else - { - m_start = ppapa->m_start; - m_bBlock = false; - } - - m_listVar = NULL; - m_var = NULL; -} - -// destructeur -CBotCStack::~CBotCStack() -{ - if (m_next != NULL) delete m_next; - if (m_prev != NULL) m_prev->m_next = NULL; // enlve de la chane - - delete m_var; - delete m_listVar; -} - -// utilis uniquement la compilation -CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) -{ - if (m_next != NULL) return m_next; // reprise dans une pile existante - - CBotCStack* p = new CBotCStack(this); - m_next = p; // chane l'lment - p->m_bBlock = bBlock; - - if (pToken != NULL) p->SetStartError(pToken->GivStart()); - - return p; -} - - -CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) -{ - if ( pfils == this ) return inst; - - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable - - if (m_error) - { - m_start = pfils->m_start; // rcupre la position de l'erreur - m_end = pfils->m_end; - } - - delete pfils; - return inst; -} - -CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) -{ - if (m_var != NULL) delete m_var; // valeur remplace ? - m_var = pfils->m_var; // rsultat transmis - pfils->m_var = NULL; // ne pas dtruire la variable - - if (m_error) - { - m_start = pfils->m_start; // rcupre la position de l'erreur - m_end = pfils->m_end; - } - - delete pfils; - return inst; -} - -int CBotCStack::GivError(int& start, int& end) -{ - start = m_start; - end = m_end; - return m_error; -} - -int CBotCStack::GivError() -{ - return m_error; -} - -// type d'instruction sur la pile -CBotTypResult CBotCStack::GivTypResult(int mode) -{ - if (m_var == NULL) - return CBotTypResult(99); - return m_var->GivTypResult(mode); -} - -// type d'instruction sur la pile -int CBotCStack::GivType(int mode) -{ - if (m_var == NULL) - return 99; - return m_var->GivType(mode); -} - -// pointeur sur la pile est de quelle classe ? -CBotClass* CBotCStack::GivClass() -{ - if ( m_var == NULL ) - return NULL; - if ( m_var->GivType(1) != CBotTypPointer ) return NULL; - - return m_var->GivClass(); -} - -// type d'instruction sur la pile -void CBotCStack::SetType(CBotTypResult& type) -{ - if (m_var == NULL) return; - m_var->SetType( type ); -} - -// cherche une variable sur la pile -// le token peut tre une suite de TokenTypVar (objet d'une classe) -// ou un pointeur dans le source - -CBotVar* CBotCStack::FindVar(CBotToken* &pToken) -{ - CBotCStack* p = this; - CBotString name = pToken->GivString(); - - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (name == pp->GivName()) - { - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return NULL; -} - -CBotVar* CBotCStack::FindVar(CBotToken& Token) -{ - CBotToken* pt = &Token; - return FindVar(pt); -} - -CBotVar* CBotCStack::CopyVar(CBotToken& Token) -{ - CBotVar* pVar = FindVar( Token ); - - if ( pVar == NULL) return NULL; - - CBotVar* pCopy = CBotVar::Create( "", pVar->GivType() ); - pCopy->Copy(pVar); - return pCopy; -} - -bool CBotCStack::IsOk() -{ - return (m_error == 0); -} - - -void CBotCStack::SetStartError( int pos ) -{ - if ( m_error != 0) return; // ne change pas une erreur dj existante - m_start = pos; -} - -void CBotCStack::SetError(int n, int pos) -{ - if ( n!= 0 && m_error != 0) return; // ne change pas une erreur dj existante - m_error = n; - m_end = pos; -} - -void CBotCStack::SetError(int n, CBotToken* p) -{ - if (m_error) return; // ne change pas une erreur dj existante - m_error = n; - m_start = p->GivStart(); - m_end = p->GivEnd(); -} - -void CBotCStack::ResetError(int n, int start, int end) -{ - m_error = n; - m_start = start; - m_end = end; -} - -bool CBotCStack::NextToken(CBotToken* &p) -{ - CBotToken* pp = p; - - p = p->GivNext(); - if (p!=NULL) return true; - - SetError(TX_ENDOF, pp->GivEnd()); - return false; -} - -void CBotCStack::SetBotCall(CBotProgram* p) -{ - m_prog = p; -} - -CBotProgram* CBotCStack::GivBotCall() -{ - return m_prog; -} - -void CBotCStack::SetRetType(CBotTypResult& type) -{ - m_retTyp = type; -} - -CBotTypResult CBotCStack::GivRetType() -{ - return m_retTyp; -} - -void CBotCStack::SetVar( CBotVar* var ) -{ - if (m_var) delete m_var; // remplacement d'une variable - m_var = var; -} - -// met sur le stack une copie d'une variable -void CBotCStack::SetCopyVar( CBotVar* var ) -{ - if (m_var) delete m_var; // remplacement d'une variable - - if ( var == NULL ) return; - m_var = CBotVar::Create("", var->GivTypResult(2)); - m_var->Copy( var ); -} - -CBotVar* CBotCStack::GivVar() -{ - return m_var; -} - -void CBotCStack::AddVar(CBotVar* pVar) -{ - CBotCStack* p = this; - - // revient sur l'lement pre - while (p != NULL && p->m_bBlock == 0) p = p->m_prev; - - if ( p == NULL ) return; - - CBotVar** pp = &p->m_listVar; - while ( *pp != NULL ) pp = &(*pp)->m_next; - - *pp = pVar; // ajoute la suite - -#ifdef _DEBUG - if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); -#endif -} - -// test si une variable est dj dfinie localement - -bool CBotCStack::CheckVarLocal(CBotToken* &pToken) -{ - CBotCStack* p = this; - CBotString name = pToken->GivString(); - - while (p != NULL) - { - CBotVar* pp = p->m_listVar; - while ( pp != NULL) - { - if (name == pp->GivName()) - return true; - pp = pp->m_next; - } - if ( p->m_bBlock ) return false; - p = p->m_prev; - } - return false; -} - -CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) -{ - nIdent = 0; - CBotTypResult val(-1); - - val = CBotCall::CompileCall(p, ppVars, this, nIdent); - if (val.GivType() < 0) - { - val = m_prog->GivFunctions()->CompileCall(p->GivString(), ppVars, nIdent); - if ( val.GivType() < 0 ) - { - // pVar = NULL; // l'erreur n'est pas sur un paramtre en particulier - SetError( -val.GivType(), p ); - val.SetType(-val.GivType()); - return val; - } - } - return val; -} - -// test si un nom de procdure est dj dfini quelque part - -bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) -{ - CBotString name = pToken->GivString(); - - if ( CBotCall::CheckCall(name) ) return true; - - CBotFunction* pp = m_prog->GivFunctions(); - while ( pp != NULL ) - { - if ( pToken->GivString() == pp->GivName() ) - { - // les paramtres sont-ils exactement les mmes ? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->Next(); - } - - pp = CBotFunction::m_listPublic; - while ( pp != NULL ) - { - if ( pToken->GivString() == pp->GivName() ) - { - // les paramtres sont-ils exactement les mmes ? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->m_nextpublic; - } - - return false; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +//Management of the stack + + +#include "CBot.h" +#include +#include + + +#define ITIMER 100 + +//////////////////////////////////////////////////////////////////////////// +// management of a execution of a stack +//////////////////////////////////////////////////////////////////////////// + +int CBotStack::m_initimer = ITIMER; +int CBotStack::m_timer = 0; +CBotVar* CBotStack::m_retvar = NULL; +int CBotStack::m_error = 0; +int CBotStack::m_start = 0; +int CBotStack::m_end = 0; +CBotString CBotStack::m_labelBreak=""; +void* CBotStack::m_pUser = NULL; + +#if STACKMEM + +CBotStack* CBotStack::FirstStack() +{ + CBotStack* p; + + long size = sizeof(CBotStack); + size *= (MAXSTACK+10); + + // request a slice of memory for the stack + p = (CBotStack*)malloc(size); + + // completely empty + memset(p, 0, size); + + p-> m_bBlock = true; + m_timer = m_initimer; // sets the timer at the beginning + + CBotStack* pp = p; + pp += MAXSTACK; + int i; + for ( i = 0 ; i< 10 ; i++ ) + { + pp->m_bOver = true; + pp ++; + } +#ifdef _DEBUG + int n = 1; + pp = p; + for ( i = 0 ; i< MAXSTACK+10 ; i++ ) + { + pp->m_index = n++; + pp ++; + } +#endif + + m_error = 0; // avoids deadlocks because m_error is static + return p; +} + +CBotStack::CBotStack(CBotStack* ppapa) +{ + // constructor must exist or the destructor is never called! + ASM_TRAP(); +} + +CBotStack::~CBotStack() +{ + ASM_TRAP(); // use Delete () instead +} + +void CBotStack::Delete() +{ + if ( this == NULL || this == EOX ) return; + + m_next->Delete(); + m_next2->Delete(); + + if (m_prev != NULL) + { + if ( m_prev->m_next == this ) + m_prev->m_next = NULL; // removes chain + + if ( m_prev->m_next2 == this ) + m_prev->m_next2 = NULL; // removes chain + } + + delete m_var; + delete m_listVar; + + CBotStack* p = m_prev; + bool bOver = m_bOver; +#ifdef _DEBUG + int n = m_index; +#endif + + // clears the freed block + memset(this, 0, sizeof(CBotStack)); + m_bOver = bOver; +#ifdef _DEBUG + m_index = n; +#endif + + if ( p == NULL ) + free( this ); +} + + +// routine improved +CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) +{ + if (m_next != NULL) + { + return m_next; // included in an existing stack + } + +#ifdef _DEBUG + int n = 0; +#endif + CBotStack* p = this; + do + { + p ++; +#ifdef _DEBUG + n ++; +#endif + } + while ( p->m_prev != NULL ); + + m_next = p; // chain an element + p->m_bBlock = bBlock; + p->m_instr = instr; + p->m_prog = m_prog; + p->m_step = 0; + p->m_prev = this; + p->m_state = 0; + p->m_call = NULL; + p->m_bFunc = false; + return p; +} + +CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) +{ + if (m_next != NULL) + { + if ( m_next == EOX ) + { + m_next = NULL; + return EOX; + } + return m_next; // included in an existing stack + } + CBotStack* p = AddStack(NULL, bBlock); + p->m_call = instr; + p->m_bFunc = 2; // special + return p; +} + +CBotStack* CBotStack::AddStack2(bool bBlock) +{ + if (m_next2 != NULL) + { + m_next2->m_prog = m_prog; // special avoids RestoreStack2 + return m_next2; // included in an existing stack + } + + CBotStack* p = this; + do + { + p ++; + } + while ( p->m_prev != NULL ); + + m_next2 = p; // chain an element + p->m_prev = this; + p->m_bBlock = bBlock; + p->m_prog = m_prog; + p->m_step = 0; + return p; +} + +bool CBotStack::GivBlock() +{ + return m_bBlock; +} + +bool CBotStack::Return(CBotStack* pfils) +{ + if ( pfils == this ) return true; // special + + if (m_var != NULL) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = NULL; // not to destroy the variable + + m_next->Delete();m_next = NULL; // releases the stack above + m_next2->Delete();m_next2 = NULL; // also the second stack (catch) + + return (m_error == 0); // interrupted if error +} + +bool CBotStack::ReturnKeep(CBotStack* pfils) +{ + if ( pfils == this ) return true; // special + + if (m_var != NULL) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = NULL; // not to destroy the variable + + return (m_error == 0); // interrupted if error +} + +bool CBotStack::StackOver() +{ + if (!m_bOver) return false; + m_error = TX_STACKOVER; + return true; +} + +#else + +CBotStack::CBotStack(CBotStack* ppapa) +{ + m_next = NULL; + m_next2 = NULL; + m_prev = ppapa; + + m_bBlock = (ppapa == NULL) ? true : false; + + m_state = 0; + m_step = 1; + + if (ppapa == NULL) m_timer = m_initimer; // sets the timer at the beginning + + m_listVar = NULL; + m_bDontDelete = false; + + m_var = NULL; + m_prog = NULL; + m_instr = NULL; + m_call = NULL; + m_bFunc = false; +} + +// destructor +CBotStack::~CBotStack() +{ + if ( m_next != EOX) delete m_next; + delete m_next2; + if (m_prev != NULL && m_prev->m_next == this ) + m_prev->m_next = NULL; // removes chain + + delete m_var; + if ( !m_bDontDelete ) delete m_listVar; +} + +// \TODO routine has/to optimize +CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) +{ + if (m_next != NULL) + { + return m_next; // included in an existing stack + } + CBotStack* p = new CBotStack(this); + m_next = p; // chain an element + p->m_bBlock = bBlock; + p->m_instr = instr; + p->m_prog = m_prog; + p->m_step = 0; + return p; +} + +CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) +{ + if (m_next != NULL) + { + if ( m_next == EOX ) + { + m_next = NULL; + return EOX; + } + return m_next; // included in an existing stack + } + CBotStack* p = new CBotStack(this); + m_next = p; // chain an element + p->m_bBlock = bBlock; + p->m_call = instr; + p->m_prog = m_prog; + p->m_step = 0; + p->m_bFunc = 2; // special + return p; +} + +CBotStack* CBotStack::AddStack2(bool bBlock) +{ + if (m_next2 != NULL) + { + m_next2->m_prog = m_prog; // special avoids RestoreStack2 + return m_next2; // included in an existing stack + } + + CBotStack* p = new CBotStack(this); + m_next2 = p; // chain an element + p->m_bBlock = bBlock; + p->m_prog = m_prog; + p->m_step = 0; + + return p; +} + +bool CBotStack::Return(CBotStack* pfils) +{ + if ( pfils == this ) return true; // special + + if (m_var != NULL) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = NULL; // do not destroy the variable + + if ( m_next != EOX ) delete m_next; // releases the stack above + delete m_next2;m_next2 = NULL; // also the second stack (catch) + + return (m_error == 0); // interrupted if error +} + +bool CBotStack::StackOver() +{ + return false; // no overflow check in this version +} + +#endif + +void CBotStack::Reset(void* pUser) +{ + m_timer = m_initimer; // resets the timer + m_error = 0; +// m_start = 0; +// m_end = 0; + m_labelBreak.Empty(); + m_pUser = pUser; +} + + + + +CBotStack* CBotStack::RestoreStack(CBotInstr* instr) +{ + if (m_next != NULL) + { + m_next->m_instr = instr; // reset (if recovery after ) + m_next->m_prog = m_prog; + return m_next; // included in an existing stack + } + return NULL; +} + +CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) +{ + CBotStack* p = RestoreStack(); + p->m_call = instr; + return p; +} + + + +// routine for execution step by step +bool CBotStack::IfStep() +{ + if ( m_initimer > 0 || m_step++ > 0 ) return false; + return true; +} + + +bool CBotStack::BreakReturn(CBotStack* pfils, const char* name) +{ + if ( m_error>=0 ) return false; // normal output + if ( m_error==-3 ) return false; // normal output (return current) + + if (!m_labelBreak.IsEmpty() && (name[0] == 0 || m_labelBreak != name)) + return false; // it's not for me + + m_error = 0; + m_labelBreak.Empty(); + return Return(pfils); +} + +bool CBotStack::IfContinue(int state, const char* name) +{ + if ( m_error != -2 ) return false; + + if (!m_labelBreak.IsEmpty() && (name == NULL || m_labelBreak != name)) + return false; // it's not for me + + m_state = state; // where again? + m_error = 0; + m_labelBreak.Empty(); + if ( m_next != EOX ) m_next->Delete(); // purge above stack + return true; +} + +void CBotStack::SetBreak(int val, const char* name) +{ + m_error = -val; // reacts as an Exception + m_labelBreak = name; + if (val == 3) // for a return + { + m_retvar = m_var; + m_var = NULL; + } +} + +// gives on the stack value calculated by the last CBotReturn + +bool CBotStack::GivRetVar(bool bRet) +{ + if (m_error == -3) + { + if ( m_var ) delete m_var; + m_var = m_retvar; + m_retvar = NULL; + m_error = 0; + return true; + } + return bRet; // interrupted by something other than return +} + +int CBotStack::GivError(int& start, int& end) +{ + start = m_start; + end = m_end; + return m_error; +} + + +int CBotStack::GivType(int mode) +{ + if (m_var == NULL) return -1; + return m_var->GivType(mode); +} + +CBotTypResult CBotStack::GivTypResult(int mode) +{ + if (m_var == NULL) return -1; + return m_var->GivTypResult(mode); +} + +void CBotStack::SetType(CBotTypResult& type) +{ + if (m_var == NULL) return; + m_var->SetType( type ); +} + + +CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif) +{ + CBotStack* p = this; + CBotString name = pToken->GivString(); + + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (pp->GivName() == name) + { + if ( bUpdate ) + pp->Maj(m_pUser, false); + + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; +} + +CBotVar* CBotStack::FindVar(const char* name) +{ + CBotStack* p = this; + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (pp->GivName() == name) + { + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; +} + +CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif) +{ + CBotStack* p = this; + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (pp->GivUniqNum() == ident) + { + if ( bUpdate ) + pp->Maj(m_pUser, false); + + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; +} + + +CBotVar* CBotStack::FindVar(CBotToken& Token, bool bUpdate, bool bModif) +{ + CBotToken* pt = &Token; + return FindVar(pt, bUpdate, bModif); +} + + +CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) +{ + CBotVar* pVar = FindVar( Token, bUpdate ); + + if ( pVar == NULL) return NULL; + + CBotVar* pCopy = CBotVar::Create(pVar); + pCopy->Copy(pVar); + return pCopy; +} + + +bool CBotStack::SetState(int n, int limite) +{ + m_state = n; + + m_timer--; // decrement the operations \TODO decrement the operations + return ( m_timer > limite ); // interrupted if timer pass +} + +bool CBotStack::IncState(int limite) +{ + m_state++; + + m_timer--; // decrement the operations \TODO decompte les operations + return ( m_timer > limite ); // interrupted if timer pass +} + + +void CBotStack::SetError(int n, CBotToken* token) +{ + if ( n!= 0 && m_error != 0) return; // does not change existing error + m_error = n; + if (token != NULL) + { + m_start = token->GivStart(); + m_end = token->GivEnd(); + } +} + +void CBotStack::ResetError(int n, int start, int end) +{ + m_error = n; + m_start = start; + m_end = end; +} + +void CBotStack::SetPosError(CBotToken* token) +{ + m_start = token->GivStart(); + m_end = token->GivEnd(); +} + +void CBotStack::SetTimer(int n) +{ + m_initimer = n; +} + +bool CBotStack::Execute() +{ + CBotCall* instr = NULL; // the most highest instruction + CBotStack* pile; + + CBotStack* p = this; + + while (p != NULL) + { + if ( p->m_next2 != NULL ) break; + if ( p->m_call != NULL ) + { + instr = p->m_call; + pile = p->m_prev ; + } + p = p->m_next; + } + + if ( instr == NULL ) return true; // normal execution request + + if (!instr->Run(pile)) return false; // \TODO exécution à partir de là + +#if STACKMEM + pile->m_next->Delete(); +#else + delete pile->m_next; +#endif + + pile->m_next = EOX; // special for recovery + return true; +} + +// puts on the stack pointer to a variable +void CBotStack::SetVar( CBotVar* var ) +{ + if (m_var) delete m_var; // replacement of a variable + m_var = var; +} + +// puts on the stack a copy of a variable +void CBotStack::SetCopyVar( CBotVar* var ) +{ + if (m_var) delete m_var; // replacement of a variable + + m_var = CBotVar::Create("", var->GivTypResult(2)); + m_var->Copy( var ); +} + +CBotVar* CBotStack::GivVar() +{ + return m_var; +} + +CBotVar* CBotStack::GivPtVar() +{ + CBotVar* p = m_var; + m_var = NULL; // therefore will not be destroyed + return p; +} + +CBotVar* CBotStack::GivCopyVar() +{ + if (m_var == NULL) return NULL; + CBotVar* v = CBotVar::Create("", m_var->GivType()); + v->Copy( m_var ); + return v; +} + +long CBotStack::GivVal() +{ + if (m_var == NULL) return 0; + return m_var->GivValInt(); +} + + + + +void CBotStack::AddVar(CBotVar* pVar) +{ + CBotStack* p = this; + + // returns to the father element + while (p != NULL && p->m_bBlock == 0) p = p->m_prev; + + if ( p == NULL ) return; + +/// p->m_bDontDelete = bDontDelete; + + CBotVar** pp = &p->m_listVar; + while ( *pp != NULL ) pp = &(*pp)->m_next; + + *pp = pVar; // added after + +#ifdef _DEBUG + if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); +#endif +} + +/*void CBotStack::RestoreVar(CBotVar* pVar) +{ + if ( !m_bDontDelete ) __asm int 3; + delete m_listVar; + m_listVar = pVar; // direct replacement +}*/ + +void CBotStack::SetBotCall(CBotProgram* p) +{ + m_prog = p; + m_bFunc = true; +} + +CBotProgram* CBotStack::GivBotCall(bool bFirst) +{ + if ( ! bFirst ) return m_prog; + CBotStack* p = this; + while ( p->m_prev != NULL ) p = p->m_prev; + return p->m_prog; +} + +void* CBotStack::GivPUser() +{ + return m_pUser; +} + + +bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype) +{ + CBotTypResult res; + + // first looks by the identifier + + res = CBotCall::DoCall(nIdent, NULL, ppVar, this, rettype ); + if (res.GivType() >= 0) return res.GivType(); + + res = m_prog->GivFunctions()->DoCall(nIdent, NULL, ppVar, this, token ); + if (res.GivType() >= 0) return res.GivType(); + + // if not found (recompile?) seeks by name + + nIdent = 0; + res = CBotCall::DoCall(nIdent, token, ppVar, this, rettype ); + if (res.GivType() >= 0) return res.GivType(); + + res = m_prog->GivFunctions()->DoCall(nIdent, token->GivString(), ppVar, this, token ); + if (res.GivType() >= 0) return res.GivType(); + + SetError(TX_NOCALL, token); + return true; +} + +void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) +{ + if ( m_next == NULL ) return; + + if ( !CBotCall::RestoreCall(nIdent, token, ppVar, this) ) + m_prog->GivFunctions()->RestoreCall(nIdent, token->GivString(), ppVar, this ); +} + + +bool SaveVar(FILE* pf, CBotVar* pVar) +{ + while ( true ) + { + if ( pVar == NULL ) + { + return WriteWord(pf, 0); // is a terminator + } + + if ( !pVar->Save0State(pf)) return false; // common header + if ( !pVar->Save1State(pf) ) return false; // saves as the child class + + pVar = pVar->GivNext(); + } +} + +void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) +{ + CBotProgram* prog = m_prog; // Current program + + CBotInstr* funct = NULL; // function found + CBotInstr* instr = NULL; // the highest intruction + + CBotStack* p = this; + + while (p->m_next != NULL) + { + if ( p->m_instr != NULL ) instr = p->m_instr; + if ( p->m_bFunc == 1 ) funct = p->m_instr; + if ( p->m_next->m_prog != prog ) break ; + + if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; + else p = p->m_next; + } + + if ( p->m_instr != NULL ) instr = p->m_instr; + if ( p->m_bFunc == 1 ) funct = p->m_instr; + + if ( funct == NULL ) return; + + CBotToken* t = funct->GivToken(); + FunctionName = t->GivString(); + +// if ( p->m_instr != NULL ) instr = p->m_instr; + + t = instr->GivToken(); + start = t->GivStart(); + end = t->GivEnd(); +} + +CBotVar* CBotStack::GivStackVars(const char* &FunctionName, int level) +{ + CBotProgram* prog = m_prog; // current program + FunctionName = NULL; + + // back the stack in the current module + CBotStack* p = this; + + while (p->m_next != NULL) + { + if ( p->m_next->m_prog != prog ) break ; + + if (p->m_next2 && p->m_next2->m_state != 0) p = p->m_next2 ; + else p = p->m_next; + } + + + // descends upon the elements of block + while ( p != NULL && !p->m_bBlock ) p = p->m_prev; + + while ( p != NULL && level++ < 0 ) + { + p = p->m_prev; + while ( p != NULL && !p->m_bBlock ) p = p->m_prev; + } + + if ( p == NULL ) return NULL; + + // search the name of the current function + CBotStack* pp = p; + while ( pp != NULL ) + { + if ( pp->m_bFunc == 1 ) break; + pp = pp->m_prev; + } + + if ( pp == NULL || pp->m_instr == NULL ) return NULL; + + CBotToken* t = pp->m_instr->GivToken(); + FunctionName = t->GivString(); + + return p->m_listVar; +} + +bool CBotStack::SaveState(FILE* pf) +{ + if ( this == NULL ) // end of the tree? + { + return WriteWord(pf, 0); // is a terminator + } + + if ( m_next2 != NULL ) + { + if (!WriteWord(pf, 2)) return false; // a mark of pursuit + if (!m_next2->SaveState(pf)) return false; + } + else + { + if (!WriteWord(pf, 1)) return false; // a mark of pursuit + } + if (!WriteWord(pf, m_bBlock)) return false; // is a local block + if (!WriteWord(pf, m_state)) return false; // in what state? + if (!WriteWord(pf, 0)) return false; // by compatibility m_bDontDelete + if (!WriteWord(pf, m_step)) return false; // in what state? + + + if (!SaveVar(pf, m_var)) return false; // current result + if (!SaveVar(pf, m_listVar)) return false; // local variables + + return m_next->SaveState(pf); // saves the following +} + + +bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) +{ + unsigned short w; + + pStack = NULL; + if (!ReadWord(pf, w)) return false; + if ( w == 0 ) return true; + +#if STACKMEM + if ( this == NULL ) pStack = FirstStack(); + else pStack = AddStack(); +#else + pStack = new CBotStack(this); +#endif + + if ( w == 2 ) + { + if (!pStack->RestoreState(pf, pStack->m_next2)) return false; + } + + if (!ReadWord(pf, w)) return false; // is a local block + pStack->m_bBlock = w; + + if (!ReadWord(pf, w)) return false; // in what state ? + pStack->SetState((short)w); // in a good state + + if (!ReadWord(pf, w)) return false; // dont delete? + // uses more + + if (!ReadWord(pf, w)) return false; // step by step + pStack->m_step = w; + + if (!CBotVar::RestoreState(pf, pStack->m_var)) return false; // temp variable + if (!CBotVar::RestoreState(pf, pStack->m_listVar)) return false;// local variables + + return pStack->RestoreState(pf, pStack->m_next); +} + + +bool CBotVar::Save0State(FILE* pf) +{ + if (!WriteWord(pf, 100+m_mPrivate))return false; // private variable? + if (!WriteWord(pf, m_bStatic))return false; // static variable? + if (!WriteWord(pf, m_type.GivType()))return false; // saves the type (always non-zero) + if (!WriteWord(pf, m_binit))return false; // variable defined? + return WriteString(pf, m_token->GivString()); // and variable name +} + +bool CBotVarInt::Save0State(FILE* pf) +{ + if ( !m_defnum.IsEmpty() ) + { + if(!WriteWord(pf, 200 )) return false; // special marker + if(!WriteString(pf, m_defnum)) return false; // name of the value + } + + return CBotVar::Save0State(pf); +} + +bool CBotVarInt::Save1State(FILE* pf) +{ + return WriteWord(pf, m_val); // the value of the variable +} + +bool CBotVarBoolean::Save1State(FILE* pf) +{ + return WriteWord(pf, m_val); // the value of the variable +} + +bool CBotVarFloat::Save1State(FILE* pf) +{ + return WriteFloat(pf, m_val); // the value of the variable +} + +bool CBotVarString::Save1State(FILE* pf) +{ + return WriteString(pf, m_val); // the value of the variable +} + + + +bool CBotVarClass::Save1State(FILE* pf) +{ + if ( !WriteType(pf, m_type) ) return false; + if ( !WriteLong(pf, m_ItemIdent) ) return false; + + return SaveVar(pf, m_pVar); // content of the object +} + +bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) +{ + unsigned short w, wi, prv, st; + float ww; + CBotString name, s; + + delete pVar; + + pVar = NULL; + CBotVar* pNew = NULL; + CBotVar* pPrev = NULL; + + while ( true ) // retrieves a list + { + if (!ReadWord(pf, w)) return false; // private or type? + if ( w == 0 ) return true; + + CBotString defnum; + if ( w == 200 ) + { + if (!ReadString(pf, defnum)) return false; // number with identifier + if (!ReadWord(pf, w)) return false; // type + } + + prv = 100; st = 0; + if ( w >= 100 ) + { + prv = w; + if (!ReadWord(pf, st)) return false; // static + if (!ReadWord(pf, w)) return false; // type + } + + if ( w == CBotTypClass ) w = CBotTypIntrinsic; // necessarily intrinsic + + if (!ReadWord(pf, wi)) return false; // init ? + + if (!ReadString(pf, name)) return false; // variable name + + CBotToken token(name, CBotString()); + + switch (w) + { + case CBotTypInt: + case CBotTypBoolean: + pNew = CBotVar::Create(&token, w); // creates a variable + if (!ReadWord(pf, w)) return false; + pNew->SetValInt((short)w, defnum); + break; + case CBotTypFloat: + pNew = CBotVar::Create(&token, w); // creates a variable + if (!ReadFloat(pf, ww)) return false; + pNew->SetValFloat(ww); + break; + case CBotTypString: + pNew = CBotVar::Create(&token, w); // creates a variable + if (!ReadString(pf, s)) return false; + pNew->SetValString(s); + break; + + // returns an intrinsic object or element of an array + case CBotTypIntrinsic: + case CBotTypArrayBody: + { + CBotTypResult r; + long id; + if (!ReadType(pf, r)) return false; // complete type + if (!ReadLong(pf, id) ) return false; + +// if (!ReadString(pf, s)) return false; + { + CBotVar* p = NULL; + if ( id ) p = CBotVarClass::Find(id) ; + + pNew = new CBotVarClass(&token, r); // directly creates an instance + // attention cptuse = 0 + if ( !RestoreState(pf, ((CBotVarClass*)pNew)->m_pVar)) return false; + pNew->SetIdent(id); + + if ( p != NULL ) + { + delete pNew; + pNew = p; // resume known element + } + } + } + break; + + case CBotTypPointer: + case CBotTypNullPointer: + if (!ReadString(pf, s)) return false; + { + pNew = CBotVar::Create(&token, CBotTypResult(w, s));// creates a variable + CBotVarClass* p = NULL; + long id; + ReadLong(pf, id); +// if ( id ) p = CBotVarClass::Find(id); // found the instance (made by RestoreInstance) + + // returns a copy of the original instance + CBotVar* pInstance = NULL; + if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; + ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // and point over + +// if ( p != NULL ) ((CBotVarPointer*)pNew)->SetPointer( p ); // rather this one + + } + break; + + case CBotTypArrayPointer: + { + CBotTypResult r; + if (!ReadType(pf, r)) return false; + + pNew = CBotVar::Create(&token, r); // creates a variable + + // returns a copy of the original instance + CBotVar* pInstance = NULL; + if ( !CBotVar::RestoreState( pf, pInstance ) ) return false; + ((CBotVarPointer*)pNew)->SetPointer( pInstance ); // and point over + } + break; + default: + ASM_TRAP(); + } + + if ( pPrev != NULL ) pPrev->m_next = pNew; + if ( pVar == NULL ) pVar = pNew; + + pNew->m_binit = wi; // pNew->SetInit(wi); + pNew->SetStatic(st); + pNew->SetPrivate(prv-100); + pPrev = pNew; + } + return true; +} + + + + +//////////////////////////////////////////////////////////////////////////// +// management of the compile stack +//////////////////////////////////////////////////////////////////////////// + +CBotProgram* CBotCStack::m_prog = NULL; // init the static variable +int CBotCStack::m_error = 0; +int CBotCStack::m_end = 0; +CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); +//CBotToken* CBotCStack::m_retClass= NULL; + + +CBotCStack::CBotCStack(CBotCStack* ppapa) +{ + m_next = NULL; + m_prev = ppapa; + + if (ppapa == NULL) + { + m_error = 0; + m_start = 0; + m_end = 0; + m_bBlock = true; + } + else + { + m_start = ppapa->m_start; + m_bBlock = false; + } + + m_listVar = NULL; + m_var = NULL; +} + +// destructor +CBotCStack::~CBotCStack() +{ + if (m_next != NULL) delete m_next; + if (m_prev != NULL) m_prev->m_next = NULL; // removes chain + + delete m_var; + delete m_listVar; +} + +// used only at compile +CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) +{ + if (m_next != NULL) return m_next; // include on an existing stack + + CBotCStack* p = new CBotCStack(this); + m_next = p; // channel element + p->m_bBlock = bBlock; + + if (pToken != NULL) p->SetStartError(pToken->GivStart()); + + return p; +} + + +CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) +{ + if ( pfils == this ) return inst; + + if (m_var != NULL) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = NULL; // not to destroy the variable + + if (m_error) + { + m_start = pfils->m_start; // retrieves the position of the error + m_end = pfils->m_end; + } + + delete pfils; + return inst; +} + +CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) +{ + if (m_var != NULL) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = NULL; // not to destroy the variable + + if (m_error) + { + m_start = pfils->m_start; // retrieves the position of the error + m_end = pfils->m_end; + } + + delete pfils; + return inst; +} + +int CBotCStack::GivError(int& start, int& end) +{ + start = m_start; + end = m_end; + return m_error; +} + +int CBotCStack::GivError() +{ + return m_error; +} + +// type of instruction on the stack +CBotTypResult CBotCStack::GivTypResult(int mode) +{ + if (m_var == NULL) + return CBotTypResult(99); + return m_var->GivTypResult(mode); +} + +// type of instruction on the stack +int CBotCStack::GivType(int mode) +{ + if (m_var == NULL) + return 99; + return m_var->GivType(mode); +} + +// pointer on the stack is in what class? +CBotClass* CBotCStack::GivClass() +{ + if ( m_var == NULL ) + return NULL; + if ( m_var->GivType(1) != CBotTypPointer ) return NULL; + + return m_var->GivClass(); +} + +// type of instruction on the stack +void CBotCStack::SetType(CBotTypResult& type) +{ + if (m_var == NULL) return; + m_var->SetType( type ); +} + +// seeks a variable on the stack +// the token may be a result of TokenTypVar (object of a class) +// or a pointer in the source + +CBotVar* CBotCStack::FindVar(CBotToken* &pToken) +{ + CBotCStack* p = this; + CBotString name = pToken->GivString(); + + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (name == pp->GivName()) + { + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return NULL; +} + +CBotVar* CBotCStack::FindVar(CBotToken& Token) +{ + CBotToken* pt = &Token; + return FindVar(pt); +} + +CBotVar* CBotCStack::CopyVar(CBotToken& Token) +{ + CBotVar* pVar = FindVar( Token ); + + if ( pVar == NULL) return NULL; + + CBotVar* pCopy = CBotVar::Create( "", pVar->GivType() ); + pCopy->Copy(pVar); + return pCopy; +} + +bool CBotCStack::IsOk() +{ + return (m_error == 0); +} + + +void CBotCStack::SetStartError( int pos ) +{ + if ( m_error != 0) return; // does not change existing error + m_start = pos; +} + +void CBotCStack::SetError(int n, int pos) +{ + if ( n!= 0 && m_error != 0) return; // does not change existing error + m_error = n; + m_end = pos; +} + +void CBotCStack::SetError(int n, CBotToken* p) +{ + if (m_error) return; // does not change existing error + m_error = n; + m_start = p->GivStart(); + m_end = p->GivEnd(); +} + +void CBotCStack::ResetError(int n, int start, int end) +{ + m_error = n; + m_start = start; + m_end = end; +} + +bool CBotCStack::NextToken(CBotToken* &p) +{ + CBotToken* pp = p; + + p = p->GivNext(); + if (p!=NULL) return true; + + SetError(TX_ENDOF, pp->GivEnd()); + return false; +} + +void CBotCStack::SetBotCall(CBotProgram* p) +{ + m_prog = p; +} + +CBotProgram* CBotCStack::GivBotCall() +{ + return m_prog; +} + +void CBotCStack::SetRetType(CBotTypResult& type) +{ + m_retTyp = type; +} + +CBotTypResult CBotCStack::GivRetType() +{ + return m_retTyp; +} + +void CBotCStack::SetVar( CBotVar* var ) +{ + if (m_var) delete m_var; // replacement of a variable + m_var = var; +} + +// puts on the stack a copy of a variable +void CBotCStack::SetCopyVar( CBotVar* var ) +{ + if (m_var) delete m_var; // replacement of a variable + + if ( var == NULL ) return; + m_var = CBotVar::Create("", var->GivTypResult(2)); + m_var->Copy( var ); +} + +CBotVar* CBotCStack::GivVar() +{ + return m_var; +} + +void CBotCStack::AddVar(CBotVar* pVar) +{ + CBotCStack* p = this; + + // returns to the father element + while (p != NULL && p->m_bBlock == 0) p = p->m_prev; + + if ( p == NULL ) return; + + CBotVar** pp = &p->m_listVar; + while ( *pp != NULL ) pp = &(*pp)->m_next; + + *pp = pVar; // added after + +#ifdef _DEBUG + if ( pVar->GivUniqNum() == 0 ) ASM_TRAP(); +#endif +} + +// test whether a variable is already defined locally + +bool CBotCStack::CheckVarLocal(CBotToken* &pToken) +{ + CBotCStack* p = this; + CBotString name = pToken->GivString(); + + while (p != NULL) + { + CBotVar* pp = p->m_listVar; + while ( pp != NULL) + { + if (name == pp->GivName()) + return true; + pp = pp->m_next; + } + if ( p->m_bBlock ) return false; + p = p->m_prev; + } + return false; +} + +CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) +{ + nIdent = 0; + CBotTypResult val(-1); + + val = CBotCall::CompileCall(p, ppVars, this, nIdent); + if (val.GivType() < 0) + { + val = m_prog->GivFunctions()->CompileCall(p->GivString(), ppVars, nIdent); + if ( val.GivType() < 0 ) + { + // pVar = NULL; // the error is not on a particular parameter + SetError( -val.GivType(), p ); + val.SetType(-val.GivType()); + return val; + } + } + return val; +} + +// test if a procedure name is already defined somewhere + +bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) +{ + CBotString name = pToken->GivString(); + + if ( CBotCall::CheckCall(name) ) return true; + + CBotFunction* pp = m_prog->GivFunctions(); + while ( pp != NULL ) + { + if ( pToken->GivString() == pp->GivName() ) + { + // are parameters exactly the same? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->Next(); + } + + pp = CBotFunction::m_listPublic; + while ( pp != NULL ) + { + if ( pToken->GivString() == pp->GivName() ) + { + // are parameters exactly the same? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->m_nextpublic; + } + + return false; +} + diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 9d5d257..6acd96e 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -12,7 +12,8 @@ // * GNU General Public License for more details. // * // * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/.///////////////////////////////////////////////////// +// * along with this program. If not, see http://www.gnu.org/licenses/. +///////////////////////////////////////////////////// //strings management diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index 35a696a..8e9d1e3 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -32,6 +32,7 @@ // x // ) +#pragma once extern bool IsOfType(CBotToken* &p, int type1, int type2 = -1); extern bool IsOfTypeList(CBotToken* &p, int type1, ...); diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotTwoOpExpr.cpp index e2523b5..49cfcc8 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotTwoOpExpr.cpp @@ -1,566 +1,568 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/./////////////////////////////////////////////////// -// expression du genre Oprande1 + Oprande2 -// Oprande1 > Oprande2 - -#include "CBot.h" - -// divers constructeurs - -CBotTwoOpExpr::CBotTwoOpExpr() -{ - m_leftop = - m_rightop = NULL; // NULL pour pouvoir faire delete sans autre - name = "CBotTwoOpExpr"; // debug -} - -CBotTwoOpExpr::~CBotTwoOpExpr() -{ - delete m_leftop; - delete m_rightop; -} - -CBotLogicExpr::CBotLogicExpr() -{ - m_condition = - m_op1 = - m_op2 = NULL; // NULL pour pouvoir faire delete sans autre - name = "CBotLogicExpr"; // debug -} - -CBotLogicExpr::~CBotLogicExpr() -{ - delete m_condition; - delete m_op1; - delete m_op2; -} - - -// type d'oprandes accepts par les oprations -#define ENTIER ((1<TokenStack(); // un bout de pile svp - - // cherche des instructions qui peuvent convenir gauche de l'opration - CBotInstr* left = (*pOp == 0) ? - CBotParExpr::Compile( p, pStk ) : // expression (...) gauche - CBotTwoOpExpr::Compile( p, pStk, pOp ); // expression A * B gauche - - if (left == NULL) return pStack->Return(NULL, pStk); // si erreur, la transmet - - // est-ce qu'on a l'oprande prvu ensuite ? - int TypeOp = p->GivType(); - if ( IsInList( TypeOp, pOperations, typemasque ) ) - { - CBotTypResult type1, type2; - type1 = pStk->GivTypResult(); // de quel type le premier oprande ? - - if ( TypeOp == ID_LOGIC ) // cas spcial pour condition ? op1 : op2 ; - { - if ( !type1.Eq(CBotTypBoolean) ) - { - pStk->SetError( TX_BADTYPE, p); - return pStack->Return(NULL, pStk); - } - CBotLogicExpr* inst = new CBotLogicExpr(); - inst->m_condition = left; - - p = p->GivNext(); // saute le token de l'opration - inst->m_op1 = CBotExpression::Compile(p, pStk); - CBotToken* pp = p; - if ( inst->m_op1 == NULL || !IsOfType( p, ID_DOTS ) ) - { - pStk->SetError( TX_MISDOTS, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - type1 = pStk->GivTypResult(); - - inst->m_op2 = CBotExpression::Compile(p, pStk); - if ( inst->m_op2 == NULL ) - { - pStk->SetError( TX_ENDOF, p->GivStart() ); - delete inst; - return pStack->Return(NULL, pStk); - } - type2 = pStk->GivTypResult(); - if (!TypeCompatible(type1, type2)) - { - pStk->SetError( TX_BAD2TYPE, pp ); - delete inst; - return pStack->Return(NULL, pStk); - } - - pStk->SetType(type1); // le plus grand des 2 types - - return pStack->Return(inst, pStk); - } - - CBotTwoOpExpr* inst = new CBotTwoOpExpr(); // lment pour opration - inst->SetToken(p); // mmorise l'opration - - - p = p->GivNext(); // saute le token de l'opration - - // cherche des instructions qui peuvent convenir droite - - if ( NULL != (inst->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp )) ) - // expression (...) droite - { - // il y a un second oprande acceptable - - type2 = pStk->GivTypResult(); // de quel type le rsultat ? - - // quel est le type du rsultat ? - int TypeRes = MAX( type1.GivType(3), type2.GivType(3) ); - if ( TypeOp == ID_ADD && type1.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - type2 = type1; // tout type convertible en chane - } - else if ( TypeOp == ID_ADD && type2.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - type1 = type2; // tout type convertible en chane - } - else if (!TypeOk( TypeRes, typemasque )) type1.SetType(99);// erreur de type - - switch ( TypeOp ) - { - case ID_LOG_OR: - case ID_LOG_AND: - case ID_TXT_OR: - case ID_TXT_AND: - case ID_EQ: - case ID_NE: - case ID_HI: - case ID_LO: - case ID_HS: - case ID_LS: - TypeRes = CBotTypBoolean; - } - if ( TypeCompatible (type1, type2, TypeOp ) ) // les rsultats sont-ils compatibles - { - // si ok, enregistre l'oprande dans l'objet - inst->m_leftop = left; - - // spcial pour valuer les oprations de mme niveau de gauche droite - while ( IsInList( p->GivType(), pOperations, typemasque ) ) // mme(s) opration(s) suit ? - { - TypeOp = p->GivType(); - CBotTwoOpExpr* i = new CBotTwoOpExpr(); // lment pour opration - i->SetToken(p); // mmorise l'opration - i->m_leftop = inst; // oprande de gauche - type1 = TypeRes; - - p = p->GivNext(); // avance la suite - i->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp ); - type2 = pStk->GivTypResult(); - - if ( !TypeCompatible (type1, type2, TypeOp) ) // les rsultats sont-ils compatibles - { - pStk->SetError(TX_BAD2TYPE, &i->m_token); - delete i; - return pStack->Return(NULL, pStk); - } - - if ( TypeRes != CBotTypString ) - TypeRes = MAX(type1.GivType(), type2.GivType()); - inst = i; - } - - CBotTypResult t(type1); - t.SetType(TypeRes); - // met une variable sur la pile pour avoir le type de rsultat - pStk->SetVar(CBotVar::Create((CBotToken*)NULL, t)); - - // et rend l'object qui l'a demand - return pStack->Return(inst, pStk); - } - pStk->SetError(TX_BAD2TYPE, &inst->m_token); - } - - // en cas d'erreur, libre les lments - delete left; - delete inst; - // et transmet l'erreur qui se trouve sur la pile - return pStack->Return(NULL, pStk); - } - - // si on n'a pas affaire une opration + ou - - // rend qui l'a demand, l'oprande (de gauche) trouv - // la place de l'objet "addition" - return pStack->Return(left, pStk); -} - - -bool IsNan(CBotVar* left, CBotVar* right, int* err = NULL) -{ - if ( left ->GivInit() > IS_DEF || right->GivInit() > IS_DEF ) - { - if ( err != NULL ) *err = TX_OPNAN ; - return true; - } - return false; -} - - -// fait l'opration sur 2 oprandes - -bool CBotTwoOpExpr::Execute(CBotStack* &pStack) -{ - CBotStack* pStk1 = pStack->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pStk1 == EOX ) return true; - - // selon la reprise, on peut tre dans l'un des 2 tats - - if ( pStk1->GivState() == 0 ) // 1er tat, value l'oprande de gauche - { - if (!m_leftop->Execute(pStk1) ) return false; // interrompu ici ? - - // pour les OU et ET logique, n'value pas la seconde expression si pas ncessaire - if ( (GivTokenType() == ID_LOG_AND || GivTokenType() == ID_TXT_AND ) && pStk1->GivVal() == false ) - { - CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); - res->SetValInt(false); - pStk1->SetVar(res); - return pStack->Return(pStk1); // transmet le rsultat - } - if ( (GivTokenType() == ID_LOG_OR||GivTokenType() == ID_TXT_OR) && pStk1->GivVal() == true ) - { - CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); - res->SetValInt(true); - pStk1->SetVar(res); - return pStack->Return(pStk1); // transmet le rsultat - } - - // passe l'tape suivante - pStk1->SetState(1); // prt pour la suite - } - - - // demande un peu plus de stack pour ne pas toucher le rsultat de gauche - // qui se trouve sur la pile, justement. - - CBotStack* pStk2 = pStk1->AddStack(); // ajoute un lment la pile - // ou le retrouve en cas de reprise - - // 2e tat, value l'oprande de droite - if ( pStk2->GivState() == 0 ) - { - if ( !m_rightop->Execute(pStk2) ) return false; // interrompu ici ? - pStk2->IncState(); - } - - CBotTypResult type1 = pStk1->GivTypResult(); // de quels types les rsultats ? - CBotTypResult type2 = pStk2->GivTypResult(); - - CBotStack* pStk3 = pStk2->AddStack(this); // ajoute un lment la pile - if ( pStk3->IfStep() ) return false; // montre l'opration si step by step - - // cre une variable temporaire pour y mettre le rsultat - // quel est le type du rsultat ? - int TypeRes = MAX(type1.GivType(), type2.GivType()); - - if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - } - - switch ( GivTokenType() ) - { - case ID_LOG_OR: - case ID_LOG_AND: - case ID_TXT_OR: - case ID_TXT_AND: - case ID_EQ: - case ID_NE: - case ID_HI: - case ID_LO: - case ID_HS: - case ID_LS: - TypeRes = CBotTypBoolean; - break; - case ID_DIV: - TypeRes = MAX(TypeRes, CBotTypFloat); - } - - // cre une variable pour le rsultat - CBotVar* result = CBotVar::Create( (CBotToken*)NULL, TypeRes); - - // cre une variable pour effectuer le calcul dans le type adapt - TypeRes = MAX(type1.GivType(), type2.GivType()); - - if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - } - - CBotVar* temp; - - if ( TypeRes == CBotTypPointer ) TypeRes = CBotTypNullPointer; - if ( TypeRes == CBotTypClass ) temp = CBotVar::Create( (CBotToken*)NULL, CBotTypResult(CBotTypIntrinsic, type1.GivClass() ) ); - else temp = CBotVar::Create( (CBotToken*)NULL, TypeRes ); - - int err = 0; - // fait l'opration selon la demande - CBotVar* left = pStk1->GivVar(); - CBotVar* right = pStk2->GivVar(); - - switch (GivTokenType()) - { - case ID_ADD: - if ( !IsNan(left, right, &err) ) result->Add(left , right); // additionne - break; - case ID_SUB: - if ( !IsNan(left, right, &err) ) result->Sub(left , right); // soustrait - break; - case ID_MUL: - if ( !IsNan(left, right, &err) ) result->Mul(left , right); // multiplie - break; - case ID_POWER: - if ( !IsNan(left, right, &err) ) result->Power(left , right); // puissance - break; - case ID_DIV: - if ( !IsNan(left, right, &err) ) err = result->Div(left , right);// divise - break; - case ID_MODULO: - if ( !IsNan(left, right, &err) ) err = result->Modulo(left , right);// reste de division - break; - case ID_LO: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Lo(left , right)); // infrieur - break; - case ID_HI: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Hi(left , right)); // suprieur - break; - case ID_LS: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Ls(left , right)); // infrieur ou gal - break; - case ID_HS: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Hs(left , right)); // suprieur ou gal - break; - case ID_EQ: - if ( IsNan(left, right) ) - result->SetValInt(left->GivInit() == right->GivInit()) ; - else - result->SetValInt(temp->Eq(left , right)); // gal - break; - case ID_NE: - if ( IsNan(left, right) ) - result->SetValInt(left ->GivInit() != right->GivInit()) ; - else - result->SetValInt(temp->Ne(left , right)); // diffrent - break; - case ID_TXT_AND: - case ID_LOG_AND: - case ID_AND: - if ( !IsNan(left, right, &err) ) result->And(left , right); // ET - break; - case ID_TXT_OR: - case ID_LOG_OR: - case ID_OR: - if ( !IsNan(left, right, &err) ) result->Or(left , right); // OU - break; - case ID_XOR: - if ( !IsNan(left, right, &err) ) result->XOr(left , right); // OU exclusif - break; - case ID_ASR: - if ( !IsNan(left, right, &err) ) result->ASR(left , right); - break; - case ID_SR: - if ( !IsNan(left, right, &err) ) result->SR(left , right); - break; - case ID_SL: - if ( !IsNan(left, right, &err) ) result->SL(left , right); - break; - default: - ASM_TRAP(); - } - delete temp; - - pStk2->SetVar(result); // met le rsultat sur la pile - if ( err ) pStk2->SetError(err, &m_token); // et l'erreur ventuelle (division par zro) - -// pStk1->Return(pStk2); // libre la pile - return pStack->Return(pStk2); // transmet le rsultat -} - -void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, bool bMain) -{ - if ( !bMain ) return; - CBotStack* pStk1 = pStack->RestoreStack(this); // ajoute un lment la pile - if ( pStk1 == NULL ) return; - - // selon la reprise, on peut tre dans l'un des 2 tats - - if ( pStk1->GivState() == 0 ) // 1er tat, value l'oprande de gauche - { - m_leftop->RestoreState(pStk1, bMain); // interrompu ici ! - return; - } - - CBotStack* pStk2 = pStk1->RestoreStack(); // ajoute un lment la pile - if ( pStk2 == NULL ) return; - - // 2e tat, value l'oprande de droite - if ( pStk2->GivState() == 0 ) - { - m_rightop->RestoreState(pStk2, bMain); // interrompu ici ! - return; - } -} - - -bool CBotLogicExpr::Execute(CBotStack* &pStack) -{ - CBotStack* pStk1 = pStack->AddStack(this); // ajoute un lment la pile - // ou le retrouve en cas de reprise -// if ( pStk1 == EOX ) return true; - - if ( pStk1->GivState() == 0 ) - { - if ( !m_condition->Execute(pStk1) ) return false; - if (!pStk1->SetState(1)) return false; - } - - if ( pStk1->GivVal() == true ) - { - if ( !m_op1->Execute(pStk1) ) return false; - } - else - { - if ( !m_op2->Execute(pStk1) ) return false; - } - - return pStack->Return(pStk1); // transmet le rsultat -} - -void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pStk1 = pStack->RestoreStack(this); // ajoute un lment la pile - if ( pStk1 == NULL ) return; - - if ( pStk1->GivState() == 0 ) - { - m_condition->RestoreState(pStk1, bMain); - return; - } - - if ( pStk1->GivVal() == true ) - { - m_op1->RestoreState(pStk1, bMain); - } - else - { - m_op2->RestoreState(pStk1, bMain); - } -} - -#if 0 -void t() -{ - int x,y; - 1>0 ? x = 0 : y = 0; -} -#endif - -#if 01 -void t(bool t) -{ - int x; - x = 1 + t ? 1 : 3 + 4 * 2 ; - t ? 0 : "test"; -} -#endif +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +/////////////////////////////////////////////////// +// expression of type Opérande1 + Opérande2 +// Opérande1 > Opérande2 + +#include "CBot.h" + +// various constructors + +CBotTwoOpExpr::CBotTwoOpExpr() +{ + m_leftop = + m_rightop = NULL; // NULL to be able to delete without other + name = "CBotTwoOpExpr"; // debug +} + +CBotTwoOpExpr::~CBotTwoOpExpr() +{ + delete m_leftop; + delete m_rightop; +} + +CBotLogicExpr::CBotLogicExpr() +{ + m_condition = + m_op1 = + m_op2 = NULL; // NULL to be able to delete without other + name = "CBotLogicExpr"; // debug +} + +CBotLogicExpr::~CBotLogicExpr() +{ + delete m_condition; + delete m_op1; + delete m_op2; +} + + +// type of operands accepted by operations +#define ENTIER ((1<TokenStack(); // one end of stack please + + // search the intructions that may be suitable to the left of the operation + CBotInstr* left = (*pOp == 0) ? + CBotParExpr::Compile( p, pStk ) : // expression (...) left + CBotTwoOpExpr::Compile( p, pStk, pOp ); // expression A * B left + + if (left == NULL) return pStack->Return(NULL, pStk); // if error, transmit + + // did we expected the operand? + int TypeOp = p->GivType(); + if ( IsInList( TypeOp, pOperations, typemasque ) ) + { + CBotTypResult type1, type2; + type1 = pStk->GivTypResult(); // what kind of the first operand? + + if ( TypeOp == ID_LOGIC ) // special case provided for: ? op1: op2; + { + if ( !type1.Eq(CBotTypBoolean) ) + { + pStk->SetError( TX_BADTYPE, p); + return pStack->Return(NULL, pStk); + } + CBotLogicExpr* inst = new CBotLogicExpr(); + inst->m_condition = left; + + p = p->GivNext(); // skip the token of the operation + inst->m_op1 = CBotExpression::Compile(p, pStk); + CBotToken* pp = p; + if ( inst->m_op1 == NULL || !IsOfType( p, ID_DOTS ) ) + { + pStk->SetError( TX_MISDOTS, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + type1 = pStk->GivTypResult(); + + inst->m_op2 = CBotExpression::Compile(p, pStk); + if ( inst->m_op2 == NULL ) + { + pStk->SetError( TX_ENDOF, p->GivStart() ); + delete inst; + return pStack->Return(NULL, pStk); + } + type2 = pStk->GivTypResult(); + if (!TypeCompatible(type1, type2)) + { + pStk->SetError( TX_BAD2TYPE, pp ); + delete inst; + return pStack->Return(NULL, pStk); + } + + pStk->SetType(type1); // the greatest of 2 types + + return pStack->Return(inst, pStk); + } + + CBotTwoOpExpr* inst = new CBotTwoOpExpr(); // element for operation + inst->SetToken(p); // stores the operation + + + p = p->GivNext(); // skip the token of the operation + + // looking statements that may be suitable for right + + if ( NULL != (inst->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp )) ) + // expression (...) right + { + // there is an second operand acceptable + + type2 = pStk->GivTypResult(); // what kind of results? + + // what kind of result? + int TypeRes = MAX( type1.GivType(3), type2.GivType(3) ); + if ( TypeOp == ID_ADD && type1.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + type2 = type1; // any type convertible chain + } + else if ( TypeOp == ID_ADD && type2.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + type1 = type2; // any type convertible chain + } + else if (!TypeOk( TypeRes, typemasque )) type1.SetType(99);// error of type + + switch ( TypeOp ) + { + case ID_LOG_OR: + case ID_LOG_AND: + case ID_TXT_OR: + case ID_TXT_AND: + case ID_EQ: + case ID_NE: + case ID_HI: + case ID_LO: + case ID_HS: + case ID_LS: + TypeRes = CBotTypBoolean; + } + if ( TypeCompatible (type1, type2, TypeOp ) ) // the results are compatible + { + // ok so, saves the operand in the object + inst->m_leftop = left; + + // special for evaluation of the operations of the same level from left to right + while ( IsInList( p->GivType(), pOperations, typemasque ) ) // same operation(s) follows? + { + TypeOp = p->GivType(); + CBotTwoOpExpr* i = new CBotTwoOpExpr(); // element for operation + i->SetToken(p); // stores the operation + i->m_leftop = inst; // left operand + type1 = TypeRes; + + p = p->GivNext(); // advance after + i->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp ); + type2 = pStk->GivTypResult(); + + if ( !TypeCompatible (type1, type2, TypeOp) ) // the results are compatible + { + pStk->SetError(TX_BAD2TYPE, &i->m_token); + delete i; + return pStack->Return(NULL, pStk); + } + + if ( TypeRes != CBotTypString ) + TypeRes = MAX(type1.GivType(), type2.GivType()); + inst = i; + } + + CBotTypResult t(type1); + t.SetType(TypeRes); + // is a variable on the stack for the type of result + pStk->SetVar(CBotVar::Create((CBotToken*)NULL, t)); + + // and returns the requested object + return pStack->Return(inst, pStk); + } + pStk->SetError(TX_BAD2TYPE, &inst->m_token); + } + + // in case of error, releases the elements + delete left; + delete inst; + // and transmits the error to the stack + return pStack->Return(NULL, pStk); + } + + // if we are not dealing with an operation + or - + // goes to that requested, the operand (left) found + // instead of the object "addition" + return pStack->Return(left, pStk); +} + + +bool IsNan(CBotVar* left, CBotVar* right, int* err = NULL) +{ + if ( left ->GivInit() > IS_DEF || right->GivInit() > IS_DEF ) + { + if ( err != NULL ) *err = TX_OPNAN ; + return true; + } + return false; +} + + +// performes the operation on two operands + +bool CBotTwoOpExpr::Execute(CBotStack* &pStack) +{ + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or return in case of recovery +// if ( pStk1 == EOX ) return true; + + // according to recovery, it may be in one of two states + + if ( pStk1->GivState() == 0 ) // first state, evaluates the left operand + { + if (!m_leftop->Execute(pStk1) ) return false; // interrupted here? + + // for OR and AND logic does not evaluate the second expression if not necessary + if ( (GivTokenType() == ID_LOG_AND || GivTokenType() == ID_TXT_AND ) && pStk1->GivVal() == false ) + { + CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); + res->SetValInt(false); + pStk1->SetVar(res); + return pStack->Return(pStk1); // transmits the result + } + if ( (GivTokenType() == ID_LOG_OR||GivTokenType() == ID_TXT_OR) && pStk1->GivVal() == true ) + { + CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); + res->SetValInt(true); + pStk1->SetVar(res); + return pStack->Return(pStk1); // transmits the result + } + + // passes to the next step + pStk1->SetState(1); // ready for further + } + + + // requires a little more stack to avoid touching the result + // of which is left on the stack, precisely + + CBotStack* pStk2 = pStk1->AddStack(); // adds an item to the stack + // or return in case of recovery + + // 2e état, évalue l'opérande de droite + if ( pStk2->GivState() == 0 ) + { + if ( !m_rightop->Execute(pStk2) ) return false; // interrupted here? + pStk2->IncState(); + } + + CBotTypResult type1 = pStk1->GivTypResult(); // what kind of results? + CBotTypResult type2 = pStk2->GivTypResult(); + + CBotStack* pStk3 = pStk2->AddStack(this); // adds an item to the stack + if ( pStk3->IfStep() ) return false; // shows the operation if step by step + + // creates a temporary variable to put the result + // what kind of result? + int TypeRes = MAX(type1.GivType(), type2.GivType()); + + if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + } + + switch ( GivTokenType() ) + { + case ID_LOG_OR: + case ID_LOG_AND: + case ID_TXT_OR: + case ID_TXT_AND: + case ID_EQ: + case ID_NE: + case ID_HI: + case ID_LO: + case ID_HS: + case ID_LS: + TypeRes = CBotTypBoolean; + break; + case ID_DIV: + TypeRes = MAX(TypeRes, CBotTypFloat); + } + + // creates a variable for the result + CBotVar* result = CBotVar::Create( (CBotToken*)NULL, TypeRes); + + // creates a variable to perform the calculation in the appropriate type + TypeRes = MAX(type1.GivType(), type2.GivType()); + + if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + } + + CBotVar* temp; + + if ( TypeRes == CBotTypPointer ) TypeRes = CBotTypNullPointer; + if ( TypeRes == CBotTypClass ) temp = CBotVar::Create( (CBotToken*)NULL, CBotTypResult(CBotTypIntrinsic, type1.GivClass() ) ); + else temp = CBotVar::Create( (CBotToken*)NULL, TypeRes ); + + int err = 0; + // is a operation according to request + CBotVar* left = pStk1->GivVar(); + CBotVar* right = pStk2->GivVar(); + + switch (GivTokenType()) + { + case ID_ADD: + if ( !IsNan(left, right, &err) ) result->Add(left , right); // addition + break; + case ID_SUB: + if ( !IsNan(left, right, &err) ) result->Sub(left , right); // substraction + break; + case ID_MUL: + if ( !IsNan(left, right, &err) ) result->Mul(left , right); // multiplies + break; + case ID_POWER: + if ( !IsNan(left, right, &err) ) result->Power(left , right); // power + break; + case ID_DIV: + if ( !IsNan(left, right, &err) ) err = result->Div(left , right);// division + break; + case ID_MODULO: + if ( !IsNan(left, right, &err) ) err = result->Modulo(left , right);// remainder of division + break; + case ID_LO: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Lo(left , right)); // lower + break; + case ID_HI: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Hi(left , right)); // top + break; + case ID_LS: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Ls(left , right)); // less than or equal + break; + case ID_HS: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Hs(left , right)); // greater than or equal + break; + case ID_EQ: + if ( IsNan(left, right) ) + result->SetValInt(left->GivInit() == right->GivInit()) ; + else + result->SetValInt(temp->Eq(left , right)); // equal + break; + case ID_NE: + if ( IsNan(left, right) ) + result->SetValInt(left ->GivInit() != right->GivInit()) ; + else + result->SetValInt(temp->Ne(left , right)); // different + break; + case ID_TXT_AND: + case ID_LOG_AND: + case ID_AND: + if ( !IsNan(left, right, &err) ) result->And(left , right); // AND + break; + case ID_TXT_OR: + case ID_LOG_OR: + case ID_OR: + if ( !IsNan(left, right, &err) ) result->Or(left , right); // OR + break; + case ID_XOR: + if ( !IsNan(left, right, &err) ) result->XOr(left , right); // exclusive OR + break; + case ID_ASR: + if ( !IsNan(left, right, &err) ) result->ASR(left , right); + break; + case ID_SR: + if ( !IsNan(left, right, &err) ) result->SR(left , right); + break; + case ID_SL: + if ( !IsNan(left, right, &err) ) result->SL(left , right); + break; + default: + ASM_TRAP(); + } + delete temp; + + pStk2->SetVar(result); // puts the result on the stack + if ( err ) pStk2->SetError(err, &m_token); // and the possible error (division by zero) + +// pStk1->Return(pStk2); // releases the stack + return pStack->Return(pStk2); // transmits the result +} + +void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, bool bMain) +{ + if ( !bMain ) return; + CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack + if ( pStk1 == NULL ) return; + + // according to recovery, it may be in one of two states + + if ( pStk1->GivState() == 0 ) // first state, evaluates the left operand + { + m_leftop->RestoreState(pStk1, bMain); // interrupted here! + return; + } + + CBotStack* pStk2 = pStk1->RestoreStack(); // adds an item to the stack + if ( pStk2 == NULL ) return; + + // second state, evaluates the right operand + if ( pStk2->GivState() == 0 ) + { + m_rightop->RestoreState(pStk2, bMain); // interrupted here! + return; + } +} + + +bool CBotLogicExpr::Execute(CBotStack* &pStack) +{ + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or return in case of recovery +// if ( pStk1 == EOX ) return true; + + if ( pStk1->GivState() == 0 ) + { + if ( !m_condition->Execute(pStk1) ) return false; + if (!pStk1->SetState(1)) return false; + } + + if ( pStk1->GivVal() == true ) + { + if ( !m_op1->Execute(pStk1) ) return false; + } + else + { + if ( !m_op2->Execute(pStk1) ) return false; + } + + return pStack->Return(pStk1); // transmits the result +} + +void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack + if ( pStk1 == NULL ) return; + + if ( pStk1->GivState() == 0 ) + { + m_condition->RestoreState(pStk1, bMain); + return; + } + + if ( pStk1->GivVal() == true ) + { + m_op1->RestoreState(pStk1, bMain); + } + else + { + m_op2->RestoreState(pStk1, bMain); + } +} + +#if 0 +void t() +{ + int x,y; + 1>0 ? x = 0 : y = 0; +} +#endif + +#if 01 +void t(bool t) +{ + int x; + x = 1 + t ? 1 : 3 + 4 * 2 ; + t ? 0 : "test"; +} +#endif diff --git a/src/CBot/ClassFILE.cpp b/src/CBot/ClassFILE.cpp index b6c944c..418ddb3 100644 --- a/src/CBot/ClassFILE.cpp +++ b/src/CBot/ClassFILE.cpp @@ -65,10 +65,10 @@ bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exceptio CBotString mode; // accepts no parameters - if ( pVar == NULL ) return TRUE; + if ( pVar == NULL ) return true; // must be a string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return FALSE; } + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } CBotString filename = pVar->GivValString(); PrepareFilename(filename); //DR @@ -79,10 +79,10 @@ bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exceptio { // recovers the mode mode = pVar->GivValString(); - if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return FALSE; } + if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } // no third parameter, only two or one possible - if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return FALSE; } + if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } } // save the file name @@ -93,7 +93,7 @@ bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exceptio { // open the called file FILE* pFile = fopen( filename, mode ); - if ( pFile == NULL ) { Exception = CBotErrFileOpen; return FALSE; } + if ( pFile == NULL ) { Exception = CBotErrFileOpen; return false; } m_CompteurFileOpen ++; @@ -102,7 +102,7 @@ bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exceptio pVar->SetValInt((long)pFile); } - return TRUE; + return true; } // compilation @@ -126,7 +126,7 @@ CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); } - // le rsultat est de type void (constructeur) + // le r�sultat est de type void (constructeur) return CBotTypResult( 0 ); } @@ -140,7 +140,7 @@ bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception pVar = pThis->GivItem("handle"); // not open? no problem - if ( pVar->GivInit() != IS_DEF) return TRUE; + if ( pVar->GivInit() != IS_DEF) return true; FILE* pFile= (FILE*)pVar->GivValInt(); fclose(pFile); @@ -148,7 +148,7 @@ bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception pVar->SetInit(IS_NAN); - return TRUE; + return true; } @@ -159,10 +159,10 @@ bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // there must be a parameter - if ( pVar == NULL ) { Exception = CBotErrLowParam; return FALSE; } + if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } // must be a string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return FALSE; } + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } // there may be a second parameter if ( pVar->GivNext() != NULL ) @@ -180,16 +180,16 @@ bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) } CBotString mode = pVar->GivValString(); - if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return FALSE; } + if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } // No third parameter - if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return FALSE; } + if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } // retrieves the element "handle" pVar = pThis->GivItem("handle"); // which must not be initialized - if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return FALSE; } + if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return false; } // contains filename pVar = pThis->GivItem("filename"); @@ -201,8 +201,8 @@ bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) FILE* pFile = fopen( filename, mode ); if ( pFile == NULL ) //DR { - pResult->SetValInt(FALSE); //DR - return TRUE; //DR + pResult->SetValInt(false); //DR + return true; //DR } m_CompteurFileOpen ++; @@ -211,8 +211,8 @@ bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) pVar = pThis->GivItem("handle"); pVar->SetValInt((long)pFile); - pResult->SetValInt(TRUE); //DR - return TRUE; + pResult->SetValInt(true); //DR + return true; } // compilation @@ -253,7 +253,7 @@ bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) // retrieves the element "handle" pVar = pThis->GivItem("handle"); - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } FILE* pFile= (FILE*)pVar->GivValInt(); fclose(pFile); @@ -261,7 +261,7 @@ bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) pVar->SetInit(IS_NAN); - return TRUE; + return true; } // compilation @@ -280,26 +280,26 @@ CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // there must be a parameter - if ( pVar == NULL ) { Exception = CBotErrLowParam; return FALSE; } + if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } // must be a string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return FALSE; } + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } CBotString param = pVar->GivValString(); //retrieves the element "handle" pVar = pThis->GivItem("handle"); - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } FILE* pFile= (FILE*)pVar->GivValInt(); int res = fputs(param+CBotString("\n"), pFile); // on error throws an exception - if ( res < 0 ) { Exception = CBotErrWrite; return FALSE; } + if ( res < 0 ) { Exception = CBotErrWrite; return false; } - return TRUE; + return true; } // compilation @@ -324,12 +324,12 @@ CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // there shouldn't be any parameter - if ( pVar != NULL ) { Exception = CBotErrOverParam; return FALSE; } + if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } //retrieves the element "handle" pVar = pThis->GivItem("handle"); - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } FILE* pFile= (FILE*)pVar->GivValInt(); @@ -342,11 +342,11 @@ bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) for ( i = 0 ; i < 2000 ; i++ ) if (chaine[i] == '\n') chaine[i] = 0; // on error throws an exception - if ( ferror(pFile) ) { Exception = CBotErrRead; return FALSE; } + if ( ferror(pFile) ) { Exception = CBotErrRead; return false; } pResult->SetValString( chaine ); - return TRUE; + return true; } // compilation @@ -365,18 +365,18 @@ CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) { // there shouldn't be any parameter - if ( pVar != NULL ) { Exception = CBotErrOverParam; return FALSE; } + if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } // retrieves the element "handle" pVar = pThis->GivItem("handle"); - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return FALSE; } + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } FILE* pFile= (FILE*)pVar->GivValInt(); pResult->SetValInt( feof( pFile ) ); - return TRUE; + return true; } // compilation diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 27332db..213b956 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -1,434 +1,436 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// dfinition des fonctions sur les chanes - - -// donne la longueur d'une chane -// excution - -bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // pas de second paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // met la longueur sur la pile - pResult->SetValInt( s.GivLength() ); - return true; -} - -// int xxx ( string ) -// compilation - -CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADPARAM ); - - // pas de second paramtre - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - - // le rsultat final est un nombre entier - return CBotTypResult( CBotTypInt ); -} - - -// donne la partie gauche d'une chane -// excution - -bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - - // rcupre ce nombre - int n = pVar->GivValInt(); - - // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - - // prend la partie intressante - s = s.Left( n ); - - // la met sur la pile - pResult->SetValString( s ); - return true; -} - -// string xxx ( string, int ) -// compilation - -CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) - return CBotTypResult( TX_BADNUM ); - - // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - - // le rsultat final est une string - return CBotTypResult( CBotTypString ); -} - -// donne la partie droite d'une chane -// excution - -bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - - // rcupre ce nombre - int n = pVar->GivValInt(); - - // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - - // prend la partie intressante - s = s.Right( n ); - - // la met sur la pile - pResult->SetValString( s ); - return true; -} - -// donne la partie centrale d'une chane -// excution - -bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - - // rcupre ce nombre - int n = pVar->GivValInt(); - - // 3e paramtre optionnel - if ( pVar->GivNext() != NULL ) - { - pVar = pVar->GivNext(); - - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - - // rcupre ce nombre - int l = pVar->GivValInt(); - - // mais pas de 4e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - - // prend la partie intressante - s = s.Mid( n, l ); - } - else - { - // prend la partie intressante - s = s.Mid( n ); - } - - // la met sur la pile - pResult->SetValString( s ); - return true; -} - -// donne la partie centrale d'une chane -// compilation - -CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) - return CBotTypResult( TX_BADNUM ); - - // 3e paramtre optionnel - if ( pVar->GivNext() != NULL ) - { - - pVar = pVar->GivNext(); - // qui doit tre un nombre - if ( pVar->GivType() > CBotTypDouble ) - return CBotTypResult( TX_BADNUM ); - - // pas de 4e paramtre - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - } - - // le rsultat final est une string - return CBotTypResult( CBotTypString ); -} - - -// donne le nombre contenu dans une chane -// excution - -bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // mais pas de 2e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - - float val = GivNumFloat(s); - - // la met la valeur sur la pile - pResult->SetValFloat( val ); - return true; -} - -// float xxx ( string ) -// compilation - -CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // pas de 2e paramtre - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - - // le rsultat final est un nombre - return CBotTypResult( CBotTypFloat ); -} - - -// trouve une chaine dans une autre -// excution - -bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // rcupre ce nombre - CBotString s2 = pVar->GivValString(); - - // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - - // met le rsultat sur la pile - int res = s.Find(s2); - pResult->SetValInt( res ); - if ( res < 0 ) pResult->SetInit( IS_NAN ); - return true; -} - -// int xxx ( string, string ) -// compilation - -CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // il faut un second paramtre - pVar = pVar->GivNext(); - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // pas de 3e paramtre - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - - // le rsultat final est un nombre - return CBotTypResult( CBotTypInt ); -} - -// donne une chaine en majuscule -// excution - -bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // mais pas de 2e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - - - s.MakeUpper(); - - // la met la valeur sur la pile - pResult->SetValString( s ); - return true; -} - -// donne une chaine en minuscules -// excution - -bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // recupre le contenu de la string - CBotString s = pVar->GivValString(); - - // mais pas de 2e paramtre - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - - - s.MakeLower(); - - // la met la valeur sur la pile - pResult->SetValString( s ); - return true; -} - -// string xxx ( string ) -// compilation - -CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) -{ - // il faut un paramtre - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // qui doit tre une string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // pas de 2e paramtre - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - - // le rsultat final est une string - return CBotTypResult( CBotTypString ); -} - - -void InitStringFunctions() -{ - CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); - CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt ); - CBotProgram::AddFunction("strright", rStrRight, cStrStrInt ); - CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt ); - - CBotProgram::AddFunction("strval", rStrVal, cFloatStr ); - CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr ); - - CBotProgram::AddFunction("strupper", rStrUpper, cStrStr ); - CBotProgram::AddFunction("strlower", rStrLower, cStrStr ); -} +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// definition of string functions + + +// gives the length of a chain +// execution + +bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // no second parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // puts the length of the stack + pResult->SetValInt( s.GivLength() ); + return true; +} + +// int xxx ( string ) +// compilation + +CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADPARAM ); + + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is an integer + return CBotTypResult( CBotTypInt ); +} + + +// gives the left side of a chain +// execution + +bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // takes the interesting part + s = s.Left( n ); + + // puts on the stack + pResult->SetValString( s ); + return true; +} + +// string xxx ( string, int ) +// compilation + +CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a string + return CBotTypResult( CBotTypString ); +} + +// gives the right of a string +// execution + +bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // takes the interesting part + s = s.Right( n ); + + // puts on the stack + pResult->SetValString( s ); + return true; +} + +// gives the central part of a chain +// execution + +bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // third parameter optional + if ( pVar->GivNext() != NULL ) + { + pVar = pVar->GivNext(); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int l = pVar->GivValInt(); + + // but no fourth parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + // takes the interesting part + s = s.Mid( n, l ); + } + else + { + // takes the interesting part + s = s.Mid( n ); + } + + // puts on the stack + pResult->SetValString( s ); + return true; +} + +// gives the central part of a chain +// compilation + +CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // third parameter optional + if ( pVar->GivNext() != NULL ) + { + + pVar = pVar->GivNext(); + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // no fourth parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + } + + // the end result is a string + return CBotTypResult( CBotTypString ); +} + + +// gives the number stored in a string +// execution + +bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + float val = GivNumFloat(s); + + // puts the value on the stack + pResult->SetValFloat( val ); + return true; +} + +// float xxx ( string ) +// compilation + +CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a number + return CBotTypResult( CBotTypFloat ); +} + + +// find string in other +// exécution + +bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // retrieves this number + CBotString s2 = pVar->GivValString(); + + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + + // puts the result on the stack + int res = s.Find(s2); + pResult->SetValInt( res ); + if ( res < 0 ) pResult->SetInit( IS_NAN ); + return true; +} + +// int xxx ( string, string ) +// compilation + +CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a number + return CBotTypResult( CBotTypInt ); +} + +// gives a string to uppercase +// exécution + +bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + + s.MakeUpper(); + + // puts the value on the stack + pResult->SetValString( s ); + return true; +} + +// gives a string to lowercase +// exécution + +bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + + s.MakeLower(); + + // puts the value on the stack + pResult->SetValString( s ); + return true; +} + +// string xxx ( string ) +// compilation + +CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) +{ + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + + // the end result is a string + return CBotTypResult( CBotTypString ); +} + + +void InitStringFunctions() +{ + CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); + CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt ); + CBotProgram::AddFunction("strright", rStrRight, cStrStrInt ); + CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt ); + + CBotProgram::AddFunction("strval", rStrVal, cFloatStr ); + CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr ); + + CBotProgram::AddFunction("strupper", rStrUpper, cStrStr ); + CBotProgram::AddFunction("strlower", rStrLower, cStrStr ); +} -- cgit v1.2.3-1-g7c22 From a760e8a749af60cd3598ebe113b7692bc6592f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Konopacki?= Date: Wed, 8 Aug 2012 02:23:38 +0200 Subject: Cleanups --- src/CBot/CBot.aps | Bin 44112 -> 0 bytes src/CBot/TestCBot/B.txt | 18 - src/CBot/TestCBot/BUG2.txt | 107 ---- src/CBot/TestCBot/CBotConsoleDlg.cpp | 221 ------- src/CBot/TestCBot/CBotConsoleDlg.h | 85 --- src/CBot/TestCBot/ChildFrm.cpp | 74 --- src/CBot/TestCBot/ChildFrm.h | 66 -- src/CBot/TestCBot/Deleted.txt | 23 - src/CBot/TestCBot/MaClass.txt | 16 - src/CBot/TestCBot/MainFrm.cpp | 116 ---- src/CBot/TestCBot/MainFrm.h | 72 --- src/CBot/TestCBot/Mc2.txt | 4 - src/CBot/TestCBot/Mon fichier.txt | 2 - src/CBot/TestCBot/Nop.txt | 4 - src/CBot/TestCBot/POS.txt | 14 - src/CBot/TestCBot/PerformDlg.cpp | 177 ------ src/CBot/TestCBot/PerformDlg.h | 78 --- src/CBot/TestCBot/Routines.cpp | 153 ----- src/CBot/TestCBot/StdAfx.cpp | 20 - src/CBot/TestCBot/StdAfx.h | 40 -- src/CBot/TestCBot/T.txt | 4 - src/CBot/TestCBot/TESTALL.txt | 161 ----- src/CBot/TestCBot/TestCB1.txt | 18 - src/CBot/TestCBot/TestCBot.clw | 316 ---------- src/CBot/TestCBot/TestCBot.cpp | 267 -------- src/CBot/TestCBot/TestCBot.dsp | 201 ------ src/CBot/TestCBot/TestCBot.h | 78 --- src/CBot/TestCBot/TestCBot.rc | 564 ----------------- src/CBot/TestCBot/TestCBot1.txt | 27 - src/CBot/TestCBot/TestCBot3.txt | 24 - src/CBot/TestCBot/TestCBotDoc.cpp | 697 --------------------- src/CBot/TestCBot/TestCBotDoc.h | 78 --- src/CBot/TestCBot/TestCBotView.cpp | 142 ----- src/CBot/TestCBot/TestCBotView.h | 78 --- src/CBot/TestCBot/TestNull.txt | 15 - src/CBot/TestCBot/TestRestoreState.txt | 67 -- src/CBot/TestCBot/TestStatic.txt | 31 - src/CBot/TestCBot/TestStr.txt | 17 - src/CBot/TestCBot/Z.txt | 14 - src/CBot/TestCBot/array.txt | 24 - "src/CBot/TestCBot/a\2361.txt" | 96 --- "src/CBot/TestCBot/a\2471.txt" | 96 --- src/CBot/TestCBot/bug.txt | 12 - src/CBot/TestCBot/bugmw.txt | 9 - src/CBot/TestCBot/ccc.txt | 8 - src/CBot/TestCBot/enum.txt | 9 - src/CBot/TestCBot/fibo.txt | 25 - src/CBot/TestCBot/file.txt | 70 --- src/CBot/TestCBot/h.txt | 5 - src/CBot/TestCBot/include.txt | 27 - src/CBot/TestCBot/intrinsic.txt | 16 - src/CBot/TestCBot/methode1.txt | 57 -- src/CBot/TestCBot/methode2.txt | 50 -- src/CBot/TestCBot/mp1.txt | 25 - src/CBot/TestCBot/mp2.txt | 28 - src/CBot/TestCBot/mw.txt | 16 - src/CBot/TestCBot/null.txt | 5 - src/CBot/TestCBot/opnew.txt | 20 - src/CBot/TestCBot/plante.txt | 25 - src/CBot/TestCBot/pointer.txt | 41 -- src/CBot/TestCBot/postinc.txt | 7 - src/CBot/TestCBot/radar.txt | 39 -- src/CBot/TestCBot/res/TestCBot.ico | Bin 1078 -> 0 bytes src/CBot/TestCBot/res/TestCBot.rc2 | 13 - src/CBot/TestCBot/res/TestCBotDoc.ico | Bin 1078 -> 0 bytes src/CBot/TestCBot/res/Toolbar.bmp | Bin 1198 -> 0 bytes src/CBot/TestCBot/resource.h | 44 -- src/CBot/TestCBot/solution.txt | 13 - src/CBot/TestCBot/test.txt | 8 - src/CBot/TestCBot/test23.txt | 10 - src/CBot/TestCBot/testmw.txt | 14 - src/CBot/TestCBot/this.txt | 13 - src/CBot/TestCBot/tt.txt | 12 - src/CBot/TestCBot/tt2.txt | 5 - src/CBot/TestCBot/vide.txt | 0 src/CBot/TestCBot/xTestCBot.clw | 245 -------- src/CBot/TestCBot/zz.txt | 6 - src/CBot/_Copy.bat | 2 - src/CBot/colobot.ini | 49 -- src/CBot/old TstCBot/BotConsoleDlg.cpp | 164 ----- src/CBot/old TstCBot/BotConsoleDlg.h | 65 -- src/CBot/old TstCBot/BotErrorDlg.cpp | 56 -- src/CBot/old TstCBot/BotErrorDlg.h | 51 -- src/CBot/old TstCBot/CBotTest.txt | 36 -- src/CBot/old TstCBot/CMyThread.cpp | 107 ---- src/CBot/old TstCBot/CMyThread.h | 44 -- src/CBot/old TstCBot/MainFrm.cpp | 91 --- src/CBot/old TstCBot/MainFrm.h | 55 -- src/CBot/old TstCBot/ReadMe.txt | 93 --- src/CBot/old TstCBot/Resource.h | 68 -- src/CBot/old TstCBot/StdAfx.cpp | 6 - src/CBot/old TstCBot/StdAfx.h | 26 - src/CBot/old TstCBot/TstCBot.clw | 189 ------ src/CBot/old TstCBot/TstCBot.cpp | 412 ------------ src/CBot/old TstCBot/TstCBot.dsp | 180 ------ src/CBot/old TstCBot/TstCBot.h | 62 -- src/CBot/old TstCBot/TstCBot.rc | 471 -------------- src/CBot/old TstCBot/TstCBotDoc.cpp | 83 --- src/CBot/old TstCBot/TstCBotDoc.h | 55 -- src/CBot/old TstCBot/TstCBotView.cpp | 291 --------- src/CBot/old TstCBot/TstCBotView.h | 81 --- src/CBot/old TstCBot/res/TstCBot.ico | Bin 1078 -> 0 bytes src/CBot/old TstCBot/res/TstCBot.rc2 | 13 - src/CBot/old TstCBot/res/TstCBotDoc.ico | Bin 1078 -> 0 bytes src/CBot/old TstCBot/test complet 1.txt | 213 ------- src/CBot/old TstCBot/x.txt | 43 -- src/CBot/resource.h | 5 +- src/CBot/tests/TestCBot/CBotConsoleDlg.cpp | 221 +++++++ src/CBot/tests/TestCBot/CBotConsoleDlg.h | 85 +++ src/CBot/tests/TestCBot/ChildFrm.cpp | 74 +++ src/CBot/tests/TestCBot/ChildFrm.h | 66 ++ src/CBot/tests/TestCBot/MainFrm.cpp | 116 ++++ src/CBot/tests/TestCBot/MainFrm.h | 72 +++ src/CBot/tests/TestCBot/PerformDlg.cpp | 177 ++++++ src/CBot/tests/TestCBot/PerformDlg.h | 78 +++ src/CBot/tests/TestCBot/Routines.cpp | 153 +++++ src/CBot/tests/TestCBot/StdAfx.cpp | 20 + src/CBot/tests/TestCBot/StdAfx.h | 40 ++ src/CBot/tests/TestCBot/TestCBot.clw | 316 ++++++++++ src/CBot/tests/TestCBot/TestCBot.cpp | 267 ++++++++ src/CBot/tests/TestCBot/TestCBot.dsp | 201 ++++++ src/CBot/tests/TestCBot/TestCBot.h | 78 +++ src/CBot/tests/TestCBot/TestCBot.rc | 564 +++++++++++++++++ src/CBot/tests/TestCBot/TestCBotDoc.cpp | 697 +++++++++++++++++++++ src/CBot/tests/TestCBot/TestCBotDoc.h | 78 +++ src/CBot/tests/TestCBot/TestCBotView.cpp | 142 +++++ src/CBot/tests/TestCBot/TestCBotView.h | 78 +++ "src/CBot/tests/TestCBot/a\2471.txt~" | 96 +++ src/CBot/tests/TestCBot/res/TestCBot.ico | Bin 0 -> 1078 bytes src/CBot/tests/TestCBot/res/TestCBot.rc2 | 13 + src/CBot/tests/TestCBot/res/TestCBotDoc.ico | Bin 0 -> 1078 bytes src/CBot/tests/TestCBot/res/Toolbar.bmp | Bin 0 -> 1198 bytes src/CBot/tests/TestCBot/resource.h | 44 ++ src/CBot/tests/TestCBot/scenarios/B.txt | 18 + src/CBot/tests/TestCBot/scenarios/BUG2.txt | 107 ++++ src/CBot/tests/TestCBot/scenarios/Deleted.txt | 23 + src/CBot/tests/TestCBot/scenarios/MaClass.txt | 16 + src/CBot/tests/TestCBot/scenarios/Mc2.txt | 4 + src/CBot/tests/TestCBot/scenarios/Mon fichier.txt | 2 + src/CBot/tests/TestCBot/scenarios/Nop.txt | 4 + src/CBot/tests/TestCBot/scenarios/POS.txt | 14 + src/CBot/tests/TestCBot/scenarios/T.txt | 4 + src/CBot/tests/TestCBot/scenarios/TESTALL.txt | 161 +++++ src/CBot/tests/TestCBot/scenarios/TestCB1.txt | 18 + src/CBot/tests/TestCBot/scenarios/TestCBot1.txt | 27 + src/CBot/tests/TestCBot/scenarios/TestCBot3.txt | 24 + src/CBot/tests/TestCBot/scenarios/TestNull.txt | 15 + .../tests/TestCBot/scenarios/TestRestoreState.txt | 67 ++ src/CBot/tests/TestCBot/scenarios/TestStatic.txt | 31 + src/CBot/tests/TestCBot/scenarios/TestStr.txt | 17 + src/CBot/tests/TestCBot/scenarios/Z.txt | 14 + src/CBot/tests/TestCBot/scenarios/a1.txt | 96 +++ src/CBot/tests/TestCBot/scenarios/array.txt | 24 + "src/CBot/tests/TestCBot/scenarios/a\2361.txt" | 96 +++ "src/CBot/tests/TestCBot/scenarios/a\2471.txt" | 96 +++ src/CBot/tests/TestCBot/scenarios/bug.txt | 12 + src/CBot/tests/TestCBot/scenarios/bugmw.txt | 9 + src/CBot/tests/TestCBot/scenarios/ccc.txt | 8 + src/CBot/tests/TestCBot/scenarios/enum.txt | 9 + src/CBot/tests/TestCBot/scenarios/fibo.txt | 25 + src/CBot/tests/TestCBot/scenarios/file.txt | 70 +++ src/CBot/tests/TestCBot/scenarios/h.txt | 5 + src/CBot/tests/TestCBot/scenarios/include.txt | 27 + src/CBot/tests/TestCBot/scenarios/intrinsic.txt | 16 + src/CBot/tests/TestCBot/scenarios/methode1.txt | 57 ++ src/CBot/tests/TestCBot/scenarios/methode2.txt | 50 ++ src/CBot/tests/TestCBot/scenarios/mp1.txt | 25 + src/CBot/tests/TestCBot/scenarios/mp2.txt | 28 + src/CBot/tests/TestCBot/scenarios/mw.txt | 16 + src/CBot/tests/TestCBot/scenarios/null.txt | 5 + src/CBot/tests/TestCBot/scenarios/opnew.txt | 20 + src/CBot/tests/TestCBot/scenarios/plante.txt | 25 + src/CBot/tests/TestCBot/scenarios/pointer.txt | 41 ++ src/CBot/tests/TestCBot/scenarios/postinc.txt | 7 + src/CBot/tests/TestCBot/scenarios/radar.txt | 39 ++ src/CBot/tests/TestCBot/scenarios/solution.txt | 13 + src/CBot/tests/TestCBot/scenarios/test.txt | 8 + src/CBot/tests/TestCBot/scenarios/test23.txt | 10 + src/CBot/tests/TestCBot/scenarios/testmw.txt | 14 + src/CBot/tests/TestCBot/scenarios/this.txt | 13 + src/CBot/tests/TestCBot/scenarios/tt.txt | 12 + src/CBot/tests/TestCBot/scenarios/tt2.txt | 5 + src/CBot/tests/TestCBot/scenarios/vide.txt | 0 src/CBot/tests/TestCBot/scenarios/zz.txt | 6 + src/CBot/tests/TestCBot/xTestCBot.clw | 245 ++++++++ src/CBot/tests/old TstCBot/BotConsoleDlg.cpp | 164 +++++ src/CBot/tests/old TstCBot/BotConsoleDlg.h | 65 ++ src/CBot/tests/old TstCBot/BotErrorDlg.cpp | 56 ++ src/CBot/tests/old TstCBot/BotErrorDlg.h | 51 ++ src/CBot/tests/old TstCBot/CBotTest.txt | 36 ++ src/CBot/tests/old TstCBot/CMyThread.cpp | 107 ++++ src/CBot/tests/old TstCBot/CMyThread.h | 44 ++ src/CBot/tests/old TstCBot/MainFrm.cpp | 91 +++ src/CBot/tests/old TstCBot/MainFrm.h | 55 ++ src/CBot/tests/old TstCBot/ReadMe.txt | 93 +++ src/CBot/tests/old TstCBot/Resource.h | 68 ++ src/CBot/tests/old TstCBot/StdAfx.cpp | 6 + src/CBot/tests/old TstCBot/StdAfx.h | 26 + src/CBot/tests/old TstCBot/TstCBot.clw | 189 ++++++ src/CBot/tests/old TstCBot/TstCBot.cpp | 412 ++++++++++++ src/CBot/tests/old TstCBot/TstCBot.dsp | 180 ++++++ src/CBot/tests/old TstCBot/TstCBot.h | 62 ++ src/CBot/tests/old TstCBot/TstCBot.rc | 471 ++++++++++++++ src/CBot/tests/old TstCBot/TstCBotDoc.cpp | 83 +++ src/CBot/tests/old TstCBot/TstCBotDoc.h | 55 ++ src/CBot/tests/old TstCBot/TstCBotView.cpp | 291 +++++++++ src/CBot/tests/old TstCBot/TstCBotView.h | 81 +++ src/CBot/tests/old TstCBot/res/TstCBot.ico | Bin 0 -> 1078 bytes src/CBot/tests/old TstCBot/res/TstCBot.rc2 | 13 + src/CBot/tests/old TstCBot/res/TstCBotDoc.ico | Bin 0 -> 1078 bytes src/CBot/tests/old TstCBot/test complet 1.txt | 213 +++++++ src/CBot/tests/old TstCBot/x.txt | 43 ++ 212 files changed, 8332 insertions(+), 8190 deletions(-) delete mode 100644 src/CBot/CBot.aps delete mode 100644 src/CBot/TestCBot/B.txt delete mode 100644 src/CBot/TestCBot/BUG2.txt delete mode 100644 src/CBot/TestCBot/CBotConsoleDlg.cpp delete mode 100644 src/CBot/TestCBot/CBotConsoleDlg.h delete mode 100644 src/CBot/TestCBot/ChildFrm.cpp delete mode 100644 src/CBot/TestCBot/ChildFrm.h delete mode 100644 src/CBot/TestCBot/Deleted.txt delete mode 100644 src/CBot/TestCBot/MaClass.txt delete mode 100644 src/CBot/TestCBot/MainFrm.cpp delete mode 100644 src/CBot/TestCBot/MainFrm.h delete mode 100644 src/CBot/TestCBot/Mc2.txt delete mode 100644 src/CBot/TestCBot/Mon fichier.txt delete mode 100644 src/CBot/TestCBot/Nop.txt delete mode 100644 src/CBot/TestCBot/POS.txt delete mode 100644 src/CBot/TestCBot/PerformDlg.cpp delete mode 100644 src/CBot/TestCBot/PerformDlg.h delete mode 100644 src/CBot/TestCBot/Routines.cpp delete mode 100644 src/CBot/TestCBot/StdAfx.cpp delete mode 100644 src/CBot/TestCBot/StdAfx.h delete mode 100644 src/CBot/TestCBot/T.txt delete mode 100644 src/CBot/TestCBot/TESTALL.txt delete mode 100644 src/CBot/TestCBot/TestCB1.txt delete mode 100644 src/CBot/TestCBot/TestCBot.clw delete mode 100644 src/CBot/TestCBot/TestCBot.cpp delete mode 100644 src/CBot/TestCBot/TestCBot.dsp delete mode 100644 src/CBot/TestCBot/TestCBot.h delete mode 100644 src/CBot/TestCBot/TestCBot.rc delete mode 100644 src/CBot/TestCBot/TestCBot1.txt delete mode 100644 src/CBot/TestCBot/TestCBot3.txt delete mode 100644 src/CBot/TestCBot/TestCBotDoc.cpp delete mode 100644 src/CBot/TestCBot/TestCBotDoc.h delete mode 100644 src/CBot/TestCBot/TestCBotView.cpp delete mode 100644 src/CBot/TestCBot/TestCBotView.h delete mode 100644 src/CBot/TestCBot/TestNull.txt delete mode 100644 src/CBot/TestCBot/TestRestoreState.txt delete mode 100644 src/CBot/TestCBot/TestStatic.txt delete mode 100644 src/CBot/TestCBot/TestStr.txt delete mode 100644 src/CBot/TestCBot/Z.txt delete mode 100644 src/CBot/TestCBot/array.txt delete mode 100644 "src/CBot/TestCBot/a\2361.txt" delete mode 100644 "src/CBot/TestCBot/a\2471.txt" delete mode 100644 src/CBot/TestCBot/bug.txt delete mode 100644 src/CBot/TestCBot/bugmw.txt delete mode 100644 src/CBot/TestCBot/ccc.txt delete mode 100644 src/CBot/TestCBot/enum.txt delete mode 100644 src/CBot/TestCBot/fibo.txt delete mode 100644 src/CBot/TestCBot/file.txt delete mode 100644 src/CBot/TestCBot/h.txt delete mode 100644 src/CBot/TestCBot/include.txt delete mode 100644 src/CBot/TestCBot/intrinsic.txt delete mode 100644 src/CBot/TestCBot/methode1.txt delete mode 100644 src/CBot/TestCBot/methode2.txt delete mode 100644 src/CBot/TestCBot/mp1.txt delete mode 100644 src/CBot/TestCBot/mp2.txt delete mode 100644 src/CBot/TestCBot/mw.txt delete mode 100644 src/CBot/TestCBot/null.txt delete mode 100644 src/CBot/TestCBot/opnew.txt delete mode 100644 src/CBot/TestCBot/plante.txt delete mode 100644 src/CBot/TestCBot/pointer.txt delete mode 100644 src/CBot/TestCBot/postinc.txt delete mode 100644 src/CBot/TestCBot/radar.txt delete mode 100644 src/CBot/TestCBot/res/TestCBot.ico delete mode 100644 src/CBot/TestCBot/res/TestCBot.rc2 delete mode 100644 src/CBot/TestCBot/res/TestCBotDoc.ico delete mode 100644 src/CBot/TestCBot/res/Toolbar.bmp delete mode 100644 src/CBot/TestCBot/resource.h delete mode 100644 src/CBot/TestCBot/solution.txt delete mode 100644 src/CBot/TestCBot/test.txt delete mode 100644 src/CBot/TestCBot/test23.txt delete mode 100644 src/CBot/TestCBot/testmw.txt delete mode 100644 src/CBot/TestCBot/this.txt delete mode 100644 src/CBot/TestCBot/tt.txt delete mode 100644 src/CBot/TestCBot/tt2.txt delete mode 100644 src/CBot/TestCBot/vide.txt delete mode 100644 src/CBot/TestCBot/xTestCBot.clw delete mode 100644 src/CBot/TestCBot/zz.txt delete mode 100644 src/CBot/_Copy.bat delete mode 100644 src/CBot/colobot.ini delete mode 100644 src/CBot/old TstCBot/BotConsoleDlg.cpp delete mode 100644 src/CBot/old TstCBot/BotConsoleDlg.h delete mode 100644 src/CBot/old TstCBot/BotErrorDlg.cpp delete mode 100644 src/CBot/old TstCBot/BotErrorDlg.h delete mode 100644 src/CBot/old TstCBot/CBotTest.txt delete mode 100644 src/CBot/old TstCBot/CMyThread.cpp delete mode 100644 src/CBot/old TstCBot/CMyThread.h delete mode 100644 src/CBot/old TstCBot/MainFrm.cpp delete mode 100644 src/CBot/old TstCBot/MainFrm.h delete mode 100644 src/CBot/old TstCBot/ReadMe.txt delete mode 100644 src/CBot/old TstCBot/Resource.h delete mode 100644 src/CBot/old TstCBot/StdAfx.cpp delete mode 100644 src/CBot/old TstCBot/StdAfx.h delete mode 100644 src/CBot/old TstCBot/TstCBot.clw delete mode 100644 src/CBot/old TstCBot/TstCBot.cpp delete mode 100644 src/CBot/old TstCBot/TstCBot.dsp delete mode 100644 src/CBot/old TstCBot/TstCBot.h delete mode 100644 src/CBot/old TstCBot/TstCBot.rc delete mode 100644 src/CBot/old TstCBot/TstCBotDoc.cpp delete mode 100644 src/CBot/old TstCBot/TstCBotDoc.h delete mode 100644 src/CBot/old TstCBot/TstCBotView.cpp delete mode 100644 src/CBot/old TstCBot/TstCBotView.h delete mode 100644 src/CBot/old TstCBot/res/TstCBot.ico delete mode 100644 src/CBot/old TstCBot/res/TstCBot.rc2 delete mode 100644 src/CBot/old TstCBot/res/TstCBotDoc.ico delete mode 100644 src/CBot/old TstCBot/test complet 1.txt delete mode 100644 src/CBot/old TstCBot/x.txt create mode 100644 src/CBot/tests/TestCBot/CBotConsoleDlg.cpp create mode 100644 src/CBot/tests/TestCBot/CBotConsoleDlg.h create mode 100644 src/CBot/tests/TestCBot/ChildFrm.cpp create mode 100644 src/CBot/tests/TestCBot/ChildFrm.h create mode 100644 src/CBot/tests/TestCBot/MainFrm.cpp create mode 100644 src/CBot/tests/TestCBot/MainFrm.h create mode 100644 src/CBot/tests/TestCBot/PerformDlg.cpp create mode 100644 src/CBot/tests/TestCBot/PerformDlg.h create mode 100644 src/CBot/tests/TestCBot/Routines.cpp create mode 100644 src/CBot/tests/TestCBot/StdAfx.cpp create mode 100644 src/CBot/tests/TestCBot/StdAfx.h create mode 100644 src/CBot/tests/TestCBot/TestCBot.clw create mode 100644 src/CBot/tests/TestCBot/TestCBot.cpp create mode 100644 src/CBot/tests/TestCBot/TestCBot.dsp create mode 100644 src/CBot/tests/TestCBot/TestCBot.h create mode 100644 src/CBot/tests/TestCBot/TestCBot.rc create mode 100644 src/CBot/tests/TestCBot/TestCBotDoc.cpp create mode 100644 src/CBot/tests/TestCBot/TestCBotDoc.h create mode 100644 src/CBot/tests/TestCBot/TestCBotView.cpp create mode 100644 src/CBot/tests/TestCBot/TestCBotView.h create mode 100644 "src/CBot/tests/TestCBot/a\2471.txt~" create mode 100644 src/CBot/tests/TestCBot/res/TestCBot.ico create mode 100644 src/CBot/tests/TestCBot/res/TestCBot.rc2 create mode 100644 src/CBot/tests/TestCBot/res/TestCBotDoc.ico create mode 100644 src/CBot/tests/TestCBot/res/Toolbar.bmp create mode 100644 src/CBot/tests/TestCBot/resource.h create mode 100644 src/CBot/tests/TestCBot/scenarios/B.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/BUG2.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/Deleted.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/MaClass.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/Mc2.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/Mon fichier.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/Nop.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/POS.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/T.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TESTALL.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestCB1.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestCBot1.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestCBot3.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestNull.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestRestoreState.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestStatic.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/TestStr.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/Z.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/a1.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/array.txt create mode 100644 "src/CBot/tests/TestCBot/scenarios/a\2361.txt" create mode 100644 "src/CBot/tests/TestCBot/scenarios/a\2471.txt" create mode 100644 src/CBot/tests/TestCBot/scenarios/bug.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/bugmw.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/ccc.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/enum.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/fibo.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/file.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/h.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/include.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/intrinsic.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/methode1.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/methode2.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/mp1.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/mp2.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/mw.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/null.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/opnew.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/plante.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/pointer.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/postinc.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/radar.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/solution.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/test.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/test23.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/testmw.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/this.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/tt.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/tt2.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/vide.txt create mode 100644 src/CBot/tests/TestCBot/scenarios/zz.txt create mode 100644 src/CBot/tests/TestCBot/xTestCBot.clw create mode 100644 src/CBot/tests/old TstCBot/BotConsoleDlg.cpp create mode 100644 src/CBot/tests/old TstCBot/BotConsoleDlg.h create mode 100644 src/CBot/tests/old TstCBot/BotErrorDlg.cpp create mode 100644 src/CBot/tests/old TstCBot/BotErrorDlg.h create mode 100644 src/CBot/tests/old TstCBot/CBotTest.txt create mode 100644 src/CBot/tests/old TstCBot/CMyThread.cpp create mode 100644 src/CBot/tests/old TstCBot/CMyThread.h create mode 100644 src/CBot/tests/old TstCBot/MainFrm.cpp create mode 100644 src/CBot/tests/old TstCBot/MainFrm.h create mode 100644 src/CBot/tests/old TstCBot/ReadMe.txt create mode 100644 src/CBot/tests/old TstCBot/Resource.h create mode 100644 src/CBot/tests/old TstCBot/StdAfx.cpp create mode 100644 src/CBot/tests/old TstCBot/StdAfx.h create mode 100644 src/CBot/tests/old TstCBot/TstCBot.clw create mode 100644 src/CBot/tests/old TstCBot/TstCBot.cpp create mode 100644 src/CBot/tests/old TstCBot/TstCBot.dsp create mode 100644 src/CBot/tests/old TstCBot/TstCBot.h create mode 100644 src/CBot/tests/old TstCBot/TstCBot.rc create mode 100644 src/CBot/tests/old TstCBot/TstCBotDoc.cpp create mode 100644 src/CBot/tests/old TstCBot/TstCBotDoc.h create mode 100644 src/CBot/tests/old TstCBot/TstCBotView.cpp create mode 100644 src/CBot/tests/old TstCBot/TstCBotView.h create mode 100644 src/CBot/tests/old TstCBot/res/TstCBot.ico create mode 100644 src/CBot/tests/old TstCBot/res/TstCBot.rc2 create mode 100644 src/CBot/tests/old TstCBot/res/TstCBotDoc.ico create mode 100644 src/CBot/tests/old TstCBot/test complet 1.txt create mode 100644 src/CBot/tests/old TstCBot/x.txt diff --git a/src/CBot/CBot.aps b/src/CBot/CBot.aps deleted file mode 100644 index cd294ec..0000000 Binary files a/src/CBot/CBot.aps and /dev/null differ diff --git a/src/CBot/TestCBot/B.txt b/src/CBot/TestCBot/B.txt deleted file mode 100644 index 53715f8..0000000 --- a/src/CBot/TestCBot/B.txt +++ /dev/null @@ -1,18 +0,0 @@ - - float [ ] TEST2 ( int [ ] param ) - { - float [ ] z; - for ( int i = 0 ; i < sizeof( param ) ; i++ ) try { z [i] = param [i] / 3; } - return z; - } - -extern public void T() -{ - int a [4]; - for ( int i = 0 ; i < 3 ; i++ ) a[i] = 4*i; - a [2] = 22; - - float [] b ; - b = TEST2 ( a ) ; - show ( a, b ); -} diff --git a/src/CBot/TestCBot/BUG2.txt b/src/CBot/TestCBot/BUG2.txt deleted file mode 100644 index 44de05a..0000000 --- a/src/CBot/TestCBot/BUG2.txt +++ /dev/null @@ -1,107 +0,0 @@ -object object :: TT ( int n ) -{ - object XX = radar(); - if ( n == 0 ) return null; - - while ( null == XX ) XX = radar(); - return XX; -} - -extern void object::Attack( ) -{ - show ( TT ( 0 ) ) ; - show ( TT ( 1 ) ) ; - return; - - int list[]; - int i; - object p; - float dist, prox; - point dest; - boolean advance = true; - - TEST(0); // ne stoppe pas si erreur -// while ( F () != 0 ) F(1); - - i = 0; - list[i++] = WingedGrabber; - list[i++] = TrackedGrabber; - list[i++] = WheeledGrabber; - list[i++] = LeggedGrabber; - list[i++] = WingedShooter; - list[i++] = TrackedShooter; - list[i++] = WheeledShooter; - list[i++] = LeggedShooter; - list[i++] = WingedOrgaShooter; - list[i++] = TrackedOrgaShooter; - list[i++] = WheeledOrgaShooter; - list[i++] = LeggedOrgaShooter; - list[i++] = WingedSniffer; - list[i++] = TrackedSniffer; - list[i++] = WheeledSniffer; - list[i++] = LeggedSniffer; - list[i++] = Thumper; - list[i++] = PhazerShooter; - list[i++] = Recycler; - list[i++] = Shielder; - list[i++] = Subber; - list[i++] = Me; - list[i++] = 3333; - list[i++] = 3334; - list[i++] = 3335; - list[i++] = 3336; - list[i++] = 3337; - list[i++] = 3338; - list[i++] = 3339; - list[i++] = 3331; - list[i++] = 3332; - list[i++] = 3330; - list[i++] = 1111; - list[i++] = 1112; - - F(F(0)); - - while ( true ) - { - p = radar(list, 0, 360, 0, 1000); - if ( p == null ) - { - F(2); - } - else - { - dist = F(p.position, position); - if ( dist <= 40 && !advance ) - { - fire(p.position); - advance = true; - } - else - { -//? if ( RetBaseDistance() > 20 ) - { - prox = dist-(5+F()*5); - if ( prox < 5 ) prox = 5; - dest.x = (position.x-p.position.x)*prox/dist + p.position.x; - dest.y = (position.y-p.position.y)*prox/dist + p.position.y; - dest.z = (position.z-p.position.z)*prox/dist + p.position.z; - goto(dest); - advance = false; - } - } - } - } -} - -// Calcule la distance jusqu' la base. - -float object::RetBaseDistance() -{ - object p; - float dist; - - p = radar(4444, 0, 360, 0, 1000); - if ( p == null ) return 1000; - dist = F(p.position, position); - return dist; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/CBotConsoleDlg.cpp b/src/CBot/TestCBot/CBotConsoleDlg.cpp deleted file mode 100644 index 55a271a..0000000 --- a/src/CBot/TestCBot/CBotConsoleDlg.cpp +++ /dev/null @@ -1,221 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// CBotConsoleDlg.cpp : implementation file -// - -#include "stdafx.h" -#include "TestCBot.h" -#include "CBotConsoleDlg.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CBotConsoleDlg dialog - - -CBotConsoleDlg::CBotConsoleDlg(CWnd* pParent /*=NULL*/) - : CDialog(CBotConsoleDlg::IDD, pParent) -{ - //{{AFX_DATA_INIT(CBotConsoleDlg) - // NOTE: the ClassWizard will add member initialization here - //}}AFX_DATA_INIT - m_pProg = NULL; - m_threadinfo.m_bRun = FALSE; - m_code = 0; -} - - -void CBotConsoleDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialog::DoDataExchange(pDX); - //{{AFX_DATA_MAP(CBotConsoleDlg) - DDX_Control(pDX, IDOK, m_cOK); - DDX_Control(pDX, IDC_EDIT2, m_Edit2); - DDX_Control(pDX, IDC_EDIT1, m_Edit1); - //}}AFX_DATA_MAP -} - - -BEGIN_MESSAGE_MAP(CBotConsoleDlg, CDialog) - //{{AFX_MSG_MAP(CBotConsoleDlg) - ON_MESSAGE(WM_ENDPROG, EndProg) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CBotConsoleDlg message handlers - -UINT ThreadProc(ThreadInfo *info) -{ - CTime t0 = CTime::GetCurrentTime(); - int Cpt = 0; - - info->m_pProg->Start("LaCommande"); - while ( !info->m_bStop && !info->m_pProg->Run() ) - { -#if 0 - const char* FunctionName; - const char* FN; - int start, end; - - info->m_pProg->GetRunPos(FunctionName, start, end); - - if ( FunctionName != NULL ) - { - info->m_pEditx->SetSel(start, end); - - char buffer[200]; - sprintf( buffer, "step %s, %d, %d",FunctionName, start, end); - AfxMessageBox( buffer ); - - int level = 0; - do - { - CBotVar* t = info->m_pProg->GivStackVars(FN, level--); - if ( FN != FunctionName ) break; - if ( t != NULL ) - { - CString s ; - while ( t != NULL ) - { - if (s.IsEmpty()) s+= "Stack -> "; - else s+= " , "; - s += t->GivValString(); - t = t->GivNext(); - } - AfxMessageBox(s); - } - } while (TRUE); - } -#endif - Cpt++; - if ( Cpt%50 == 0 ) info->m_pEdit1->ReplaceSel("."); - } - - if ( info->m_bStop ) - { - info->m_pEdit1->ReplaceSel("\r\nInterrompu\r\n"); - } - else if (info->m_pProg->GivError() == 0) - { - CTime t = CTime::GetCurrentTime(); - CTimeSpan ts = t - t0; - - char buffer[200]; - sprintf( buffer, "\r\nExcution termine en %d secondes.\r\nInterrompue %d fois.\r\n", - ts.GetTotalSeconds(), Cpt); - - info->m_pEdit1->ReplaceSel(buffer); - } - - info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ; - return 0 ; -} - -LONG CBotConsoleDlg::EndProg(UINT wparam, LONG lparam) -{ - m_threadinfo.m_bRun = FALSE; - - if (m_pProg->GetError(m_code, m_start, m_end)) - { - CBotString TextError; - TextError = CBotProgram::GivErrorText(m_code); - AfxMessageBox(TextError); - CDialog::OnCancel(); - return 1; - } - delete m_pProg; - m_pProg = NULL; - - m_Edit2.EnableWindow(TRUE); - m_cOK.EnableWindow(TRUE); - - m_Edit2.SetWindowText(""); - m_Edit2.SetFocus(); - return 0 ; -} - -void CBotConsoleDlg::OnOK() -{ - CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); - pApp->m_pConsole = &m_Edit1; - m_code = 0; - - CString Commande; - m_Edit2.GetWindowText(Commande); - - CString s = "void LaCommande() { " + Commande + " ;}"; - m_pProg = new CBotProgram(); - CBotStringArray liste; - m_pProg->Compile(s, liste); - - int err, start, end; - if ( m_pProg->GetError(err, start, end) ) - { - CBotString TextError; - TextError = CBotProgram::GivErrorText(err); - AfxMessageBox(TextError); - m_Edit2.SetSel(start-20, end-20); - return; - } - - m_Edit1.ReplaceSel("\r\n" + Commande + " ->\r\n"); - - m_Edit2.SetWindowText(""); - m_Edit1.SetFocus(); - m_Edit2.EnableWindow(FALSE); - m_cOK.EnableWindow(FALSE); - - // lance un processus paralle pour l'excution - m_threadinfo.m_pWndMessage = this ; - - m_threadinfo.m_pEdit1 = &m_Edit1; - m_threadinfo.m_pEditx = m_pEditx; - m_threadinfo.m_pProg = m_pProg; - m_threadinfo.m_bStop = FALSE; - m_threadinfo.m_bRun = TRUE; - - AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ; -} - -void CBotConsoleDlg::OnCancel() -{ - if (!m_threadinfo.m_bRun) CDialog::OnCancel(); - m_threadinfo.m_bStop = TRUE ; -} - - -BOOL CBotConsoleDlg::OnInitDialog() -{ - CDialog::OnInitDialog(); - - m_Edit1.ReplaceSel("Les fonctions suivantes sont disponibles:\r\n"); - for ( int i = 0; i < m_pListe->GivSize(); i++ ) - { - CBotString x = (*m_pListe)[i] + CBotString("\r\n"); - m_Edit1.ReplaceSel(x); - } - m_Edit1.ReplaceSel("Entrez une commande ci-dessous.\r\n\r\n"); - - - return TRUE; // return TRUE unless you set the focus to a control - // EXCEPTION: OCX Property Pages should return FALSE -} diff --git a/src/CBot/TestCBot/CBotConsoleDlg.h b/src/CBot/TestCBot/CBotConsoleDlg.h deleted file mode 100644 index f289a4d..0000000 --- a/src/CBot/TestCBot/CBotConsoleDlg.h +++ /dev/null @@ -1,85 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -#if !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) -#define AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 -// CBotConsoleDlg.h : header file -// - -struct ThreadInfo -{ - CEdit* m_pEdit1 ; - CEdit* m_pEditx ; - CBotProgram* m_pProg; - CWnd* m_pWndMessage; - BOOL m_bStop; - BOOL m_bRun; -}; - - -///////////////////////////////////////////////////////////////////////////// -// CBotConsoleDlg dialog - -class CBotConsoleDlg : public CDialog -{ -// Construction -public: - CBotConsoleDlg(CWnd* pParent = NULL); // standard constructor - -// Dialog Data - //{{AFX_DATA(CBotConsoleDlg) - enum { IDD = IDD_CONSOLE }; - CButton m_cOK; - CEdit m_Edit2; - CEdit m_Edit1; - //}}AFX_DATA - - CBotProgram* m_pProg; - ThreadInfo m_threadinfo; - - CBotStringArray* - m_pListe; - int m_code, m_start, m_end; - CEdit* m_pEditx; - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CBotConsoleDlg) - protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - //}}AFX_VIRTUAL - -// Implementation -protected: - - // Generated message map functions - //{{AFX_MSG(CBotConsoleDlg) - virtual void OnOK(); - virtual void OnCancel(); - virtual BOOL OnInitDialog(); - afx_msg LONG EndProg(UINT wparam, LONG lparam) ; - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/ChildFrm.cpp b/src/CBot/TestCBot/ChildFrm.cpp deleted file mode 100644 index 4c40f90..0000000 --- a/src/CBot/TestCBot/ChildFrm.cpp +++ /dev/null @@ -1,74 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// ChildFrm.cpp : implementation of the CChildFrame class -// - -#include "stdafx.h" -#include "TestCBot.h" - -#include "ChildFrm.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CChildFrame - -IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd) - -BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd) - //{{AFX_MSG_MAP(CChildFrame) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CChildFrame construction/destruction - -CChildFrame::CChildFrame() -{ -} - -CChildFrame::~CChildFrame() -{ -} - -BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs) -{ - return CMDIChildWnd::PreCreateWindow(cs); -} - -///////////////////////////////////////////////////////////////////////////// -// CChildFrame diagnostics - -#ifdef _DEBUG -void CChildFrame::AssertValid() const -{ - CMDIChildWnd::AssertValid(); -} - -void CChildFrame::Dump(CDumpContext& dc) const -{ - CMDIChildWnd::Dump(dc); -} - -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CChildFrame message handlers diff --git a/src/CBot/TestCBot/ChildFrm.h b/src/CBot/TestCBot/ChildFrm.h deleted file mode 100644 index 2ad57b6..0000000 --- a/src/CBot/TestCBot/ChildFrm.h +++ /dev/null @@ -1,66 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// ChildFrm.h : interface of the CChildFrame class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_CHILDFRM_H__4D1BB909_8E74_11D4_A439_00D059085115__INCLUDED_) -#define AFX_CHILDFRM_H__4D1BB909_8E74_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -class CChildFrame : public CMDIChildWnd -{ - DECLARE_DYNCREATE(CChildFrame) -public: - CChildFrame(); - -// Attributes -public: - -// Operations -public: - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CChildFrame) - virtual BOOL PreCreateWindow(CREATESTRUCT& cs); - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CChildFrame(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -// Generated message map functions -protected: - //{{AFX_MSG(CChildFrame) - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_CHILDFRM_H__4D1BB909_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/Deleted.txt b/src/CBot/TestCBot/Deleted.txt deleted file mode 100644 index 469a624..0000000 --- a/src/CBot/TestCBot/Deleted.txt +++ /dev/null @@ -1,23 +0,0 @@ -public extern void object :: ESSAI() -{ - while(true) - { - if ( true ) - { - goto(12); - break; - } - } - object x = null ; - - while ( x == null ) x = radar(); - - show ( x.position ) ; - - TEST(5, x); - - if ( x == null ) show ( "DELETED" ); - - show ( x.position ) ; - -} \ No newline at end of file diff --git a/src/CBot/TestCBot/MaClass.txt b/src/CBot/TestCBot/MaClass.txt deleted file mode 100644 index ac472b4..0000000 --- a/src/CBot/TestCBot/MaClass.txt +++ /dev/null @@ -1,16 +0,0 @@ - -class MaClass -{ - int a = 1 ; - MaClass pointeur ; - MaClass next = null ; - CPoint autre = new CPoint( 1 , 1 ) ; -} - -extern public void Test ( ) -{ - MaClass x () ; - x.next = new MaClass ( ) ; - println ( x ) ; -} - diff --git a/src/CBot/TestCBot/MainFrm.cpp b/src/CBot/TestCBot/MainFrm.cpp deleted file mode 100644 index 6669350..0000000 --- a/src/CBot/TestCBot/MainFrm.cpp +++ /dev/null @@ -1,116 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// MainFrm.cpp : implementation of the CMainFrame class -// - -#include "stdafx.h" -#include "TestCBot.h" - -#include "MainFrm.h" -#include "TestCBotDoc.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame - -IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd) - -BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd) - //{{AFX_MSG_MAP(CMainFrame) - ON_WM_CREATE() - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -static UINT indicators[] = -{ - ID_SEPARATOR, // status line indicator - ID_INDICATOR_CAPS, - ID_INDICATOR_NUM, - ID_INDICATOR_SCRL, -}; - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame construction/destruction - -CMainFrame::CMainFrame() -{ -} - -CMainFrame::~CMainFrame() -{ -} - -int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) -{ - if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1) - return -1; - - if (!m_wndToolBar.Create(this) || - !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) - { - TRACE0("Failed to create toolbar\n"); - return -1; // fail to create - } - - if (!m_wndStatusBar.Create(this) || - !m_wndStatusBar.SetIndicators(indicators, - sizeof(indicators)/sizeof(UINT))) - { - TRACE0("Failed to create status bar\n"); - return -1; // fail to create - } - - m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | - CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); - - m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); - EnableDocking(CBRS_ALIGN_ANY); - DockControlBar(&m_wndToolBar); - - return 0; -} - -BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) -{ - return CMDIFrameWnd::PreCreateWindow(cs); -} - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame diagnostics - -#ifdef _DEBUG -void CMainFrame::AssertValid() const -{ - CMDIFrameWnd::AssertValid(); -} - -void CMainFrame::Dump(CDumpContext& dc) const -{ - CMDIFrameWnd::Dump(dc); -} - -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame message handlers - - diff --git a/src/CBot/TestCBot/MainFrm.h b/src/CBot/TestCBot/MainFrm.h deleted file mode 100644 index a1d34f4..0000000 --- a/src/CBot/TestCBot/MainFrm.h +++ /dev/null @@ -1,72 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// MainFrm.h : interface of the CMainFrame class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_MAINFRM_H__4D1BB907_8E74_11D4_A439_00D059085115__INCLUDED_) -#define AFX_MAINFRM_H__4D1BB907_8E74_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -class CMainFrame : public CMDIFrameWnd -{ - DECLARE_DYNAMIC(CMainFrame) -public: - CMainFrame(); - -// Attributes -public: - -// Operations -public: - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CMainFrame) - public: - virtual BOOL PreCreateWindow(CREATESTRUCT& cs); - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CMainFrame(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -protected: // control bar embedded members - CStatusBar m_wndStatusBar; - CToolBar m_wndToolBar; - -// Generated message map functions -protected: - //{{AFX_MSG(CMainFrame) - afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_MAINFRM_H__4D1BB907_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/Mc2.txt b/src/CBot/TestCBot/Mc2.txt deleted file mode 100644 index 172c259..0000000 --- a/src/CBot/TestCBot/Mc2.txt +++ /dev/null @@ -1,4 +0,0 @@ -class MaClass -{ - int t = 12; -} diff --git a/src/CBot/TestCBot/Mon fichier.txt b/src/CBot/TestCBot/Mon fichier.txt deleted file mode 100644 index 6b35bf8..0000000 --- a/src/CBot/TestCBot/Mon fichier.txt +++ /dev/null @@ -1,2 +0,0 @@ -Voici encore du texte -et une seconde ligne diff --git a/src/CBot/TestCBot/Nop.txt b/src/CBot/TestCBot/Nop.txt deleted file mode 100644 index 6a66f6f..0000000 --- a/src/CBot/TestCBot/Nop.txt +++ /dev/null @@ -1,4 +0,0 @@ -public extern void Nop() -{ - while ( true ) {} -} \ No newline at end of file diff --git a/src/CBot/TestCBot/POS.txt b/src/CBot/TestCBot/POS.txt deleted file mode 100644 index 688e4fb..0000000 --- a/src/CBot/TestCBot/POS.txt +++ /dev/null @@ -1,14 +0,0 @@ -void object :: T ( ) -{ - show ( position ) ; -} - -public extern void object :: POS() -{ - for ( int i = 0; i < 10 ; i++ ) - { - if ( i == 2 ) TEST ( 12 ) ; -// show ( position ); - T ( ) ; - } -} \ No newline at end of file diff --git a/src/CBot/TestCBot/PerformDlg.cpp b/src/CBot/TestCBot/PerformDlg.cpp deleted file mode 100644 index 8abbb4b..0000000 --- a/src/CBot/TestCBot/PerformDlg.cpp +++ /dev/null @@ -1,177 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// PerformDlg.cpp : implementation file -// - -#include "stdafx.h" -#include "testcbot.h" -#include "PerformDlg.h" - -//#include -#include -//#include - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CPerformDlg dialog - - -CPerformDlg::CPerformDlg(CWnd* pParent /*=NULL*/) - : CDialog(CPerformDlg::IDD, pParent) -{ - //{{AFX_DATA_INIT(CPerformDlg) - // NOTE: the ClassWizard will add member initialization here - //}}AFX_DATA_INIT -} - - -void CPerformDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialog::DoDataExchange(pDX); - //{{AFX_DATA_MAP(CPerformDlg) - DDX_Control(pDX, IDC_EDIT3, m_Edit3); - DDX_Control(pDX, IDC_EDIT1, m_Edit1); - //}}AFX_DATA_MAP -} - - -BEGIN_MESSAGE_MAP(CPerformDlg, CDialog) - //{{AFX_MSG_MAP(CPerformDlg) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CPerformDlg message handlers - -/* Pauses for a specified number of milliseconds. */ - -/*void sleep( double waitseconds ) -{ - clock_t wait = (clock_t)(waitseconds * CLOCKS_PER_SEC); - clock_t goal; - goal = wait + clock(); - while( goal > clock() ) - ; -}*/ - -void sleep( clock_t wait ) -{ - clock_t goal; - goal = wait + clock(); - while( goal > clock() ) - TRACE("%d \n", clock() ); -} - -void sleep2( clock_t wait ) -{ - struct _timeb timebuffer; - char *timeline; - - _ftime( &timebuffer ); - timeline = ctime( & ( timebuffer.time ) ); - long x = timebuffer.millitm; - while( x == timebuffer.millitm ) _ftime( &timebuffer ); -} - -#define NBLP 20 - -UINT ThreadProc2(ThreadInfo2 *info) -{ - int lp = NBLP; - int i; - clock_t start = clock(); - - while ( !info->m_bStop ) - { - for ( i = 0; i< info->m_nbscripts; i++ ) - { - info->m_pProg[i]->Run(); - } - -#ifdef _DEBUG - sleep2( 1 ); -#else - CString s ( "xx" ); - for ( long z = 0x5000; z>0; z-- ) s = s.Left(1); -#endif - if ( --lp == 0 ) - { - clock_t finish = clock(); - double n = (double)NBLP / (double)(finish-start) * CLOCKS_PER_SEC; - char b[30]; - sprintf( b, "%f", n); - info->m_pEdit->SetWindowText(b); - - n = n * 1100 / 200; // performances - sprintf( b, "%f", n); - info->m_pEdit3->SetWindowText(b); - start = finish; - lp = NBLP; - } - } - - return 0 ; -} - -BOOL CPerformDlg::OnInitDialog() -{ - CDialog::OnInitDialog(); - - - CBotStringArray liste; - // cre les scripts pour les tests - for ( int i = 0; i < 100; i++ ) - { - m_pProg[i] = new CBotProgram(); - m_pProg[i]->Compile(m_Script, liste); - m_pProg[i]->Start(liste[0]); - } - - // lance un processus paralle pour l'excution -// m_threadinfo2.m_pWndMessage = this ; - - m_threadinfo2.m_pEdit = &m_Edit1; - m_threadinfo2.m_pEdit3 = &m_Edit3; - m_threadinfo2.m_pProg = m_pProg; - m_threadinfo2.m_bStop = FALSE; - m_threadinfo2.m_nbscripts = 30; - - - AfxBeginThread((AFX_THREADPROC)ThreadProc2, &m_threadinfo2) ; - // TODO: Add extra initialization here - - return TRUE; // return TRUE unless you set the focus to a control - // EXCEPTION: OCX Property Pages should return FALSE -} - -void CPerformDlg::OnCancel() -{ - m_threadinfo2.m_bStop = TRUE; - sleep ( 2000 ); - - CDialog::OnCancel(); - - for ( int i = 0; i < 100; i++ ) - { - delete m_pProg[i]; - } -} diff --git a/src/CBot/TestCBot/PerformDlg.h b/src/CBot/TestCBot/PerformDlg.h deleted file mode 100644 index 29d567f..0000000 --- a/src/CBot/TestCBot/PerformDlg.h +++ /dev/null @@ -1,78 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -#if !defined(AFX_PERFORMDLG_H__EAF2D560_97D8_11D4_A439_00D059085115__INCLUDED_) -#define AFX_PERFORMDLG_H__EAF2D560_97D8_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 -// PerformDlg.h : header file -// - -struct ThreadInfo2 -{ - CEdit* m_pEdit ; - CEdit* m_pEdit3 ; - - CBotProgram** m_pProg; - BOOL m_bStop; - int m_nbscripts; -}; - - -///////////////////////////////////////////////////////////////////////////// -// CPerformDlg dialog - -class CPerformDlg : public CDialog -{ -// Construction -public: - CPerformDlg(CWnd* pParent = NULL); // standard constructor - -// Dialog Data - //{{AFX_DATA(CPerformDlg) - enum { IDD = IDD_DIALOG1 }; - CEdit m_Edit3; - CEdit m_Edit1; - //}}AFX_DATA - - CBotProgram* m_pProg[100]; - ThreadInfo2 m_threadinfo2; - CString m_Script; - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CPerformDlg) - protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - //}}AFX_VIRTUAL - -// Implementation -protected: - - // Generated message map functions - //{{AFX_MSG(CPerformDlg) - virtual BOOL OnInitDialog(); - virtual void OnCancel(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_PERFORMDLG_H__EAF2D560_97D8_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/Routines.cpp b/src/CBot/TestCBot/Routines.cpp deleted file mode 100644 index b37f027..0000000 --- a/src/CBot/TestCBot/Routines.cpp +++ /dev/null @@ -1,153 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -//////////////////////////////////////////////////////////////////// -// routine show() -// utilisable depuis le programme crit en CBot - -// excution -BOOL rShow( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) -{ - CString s; - - while ( pVar != NULL ) - { - CString ss; - ss.LoadString( TX_TYPENAMES + pVar->GivType() ); - s += ss + " "; - - ss = pVar->GivName(); - if (ss.IsEmpty()) ss = ""; - s += ss + " = "; - - s += pVar->GivValString(); - s += "\n"; - pVar = pVar->GivNext(); - } - - AfxMessageBox(s, MB_OK|MB_ICONINFORMATION); - - return TRUE; // pas d'interruption -} - -CBotTypResult cShow( CBotVar* &pVar, void* pUser) -{ - if ( pVar == NULL ) return CBotTypResult(5028); - return CBotTypResult(0); // tous paramtres accepts, void en retour -} - - -//////////////////////////////////////////////////////////////////// -// routine print() -// utilisable depuis le programme crit en CBot - -// excution -BOOL rPrintLn( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) -{ - CString s; - - CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); - CEdit* pEdit = pApp->m_pConsole; - - if (pEdit == NULL) return TRUE; - pEdit->GetWindowText(s); - - while ( pVar != NULL ) - { - if ( !s.IsEmpty() ) s += " "; - s += pVar->GivValString(); - pVar = pVar->GivNext(); - } - s += "\r\n"; - - pEdit->SetWindowText(s); - pEdit->SetSel(s.GetLength(), s.GetLength()); - pEdit->SetFocus(); - return TRUE; // pas d'interruption -} - -BOOL rPrint( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) -{ - CString s; - - CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); - CEdit* pEdit = pApp->m_pConsole; - - if (pEdit == NULL) return TRUE; - pEdit->GetWindowText(s); - - while ( pVar != NULL ) - { - if ( !s.IsEmpty() ) s += " "; - s += pVar->GivValString(); - pVar = pVar->GivNext(); - } - - pEdit->SetWindowText(s); - pEdit->SetSel(s.GetLength(), s.GetLength()); - pEdit->SetFocus(); - return TRUE; // pas d'interruption -} - -CBotTypResult cPrint( CBotVar* &pVar, void* pUser) -{ - return CBotTypResult(0); // tous paramtres accepts, un entier en retour -} - - -////////////////////////////////////////////////////////////////// -// class CPoint pour essayer - -// excution -BOOL rCPoint( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - CString s; - - if ( pVar == NULL )return TRUE; // constructeur sans paramtres est ok - - CBotVar* pX = pThis->GivItem("x"); - pX->SetValFloat( pVar->GivValFloat() ); - pVar = pVar->GivNext(); - - CBotVar* pY = pThis->GivItem("y"); - pY->SetValFloat( pVar->GivValFloat() ); - pVar = pVar->GivNext(); - - return TRUE; // pas d'interruption -} - -CBotTypResult cCPoint( CBotVar* pThis, CBotVar* &pVar) -{ - // ok si aucun paramtres ! - if ( pVar == NULL ) return CBotTypResult(0); - - // paramtre de type numrique svp - if ( pVar->GivType() > CBotTypDouble ) return CBotTypResult(5011); - pVar = pVar->GivNext(); - - // il doit y avoir un second paramtre - if ( pVar == NULL ) return 5028; - // galement de type numrique - if ( pVar->GivType() > CBotTypDouble )return CBotTypResult(5011); - pVar = pVar->GivNext(); - - // et pas plus de 2 paramtres svp - if ( pVar != NULL ) return CBotTypResult(5026); - - return CBotTypResult(0); // cette fonction retourne void -} - - diff --git a/src/CBot/TestCBot/StdAfx.cpp b/src/CBot/TestCBot/StdAfx.cpp deleted file mode 100644 index 7dd0f00..0000000 --- a/src/CBot/TestCBot/StdAfx.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// stdafx.cpp : source file that includes just the standard includes -// TestCBot.pch will be the pre-compiled header -// stdafx.obj will contain the pre-compiled type information - -#include "stdafx.h" - diff --git a/src/CBot/TestCBot/StdAfx.h b/src/CBot/TestCBot/StdAfx.h deleted file mode 100644 index c3659fb..0000000 --- a/src/CBot/TestCBot/StdAfx.h +++ /dev/null @@ -1,40 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// stdafx.h : include file for standard system include files, -// or project specific include files that are used frequently, but -// are changed infrequently -// - -#if !defined(AFX_STDAFX_H__4D1BB905_8E74_11D4_A439_00D059085115__INCLUDED_) -#define AFX_STDAFX_H__4D1BB905_8E74_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers - -#include // MFC core and standard components -#include // MFC extensions -#include // MFC OLE automation classes -#ifndef _AFX_NO_AFXCMN_SUPPORT -#include // MFC support for Windows Common Controls -#endif // _AFX_NO_AFXCMN_SUPPORT - - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_STDAFX_H__4D1BB905_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/T.txt b/src/CBot/TestCBot/T.txt deleted file mode 100644 index 50a792b..0000000 --- a/src/CBot/TestCBot/T.txt +++ /dev/null @@ -1,4 +0,0 @@ -public extern int T ( float n ) -{ - return n * 1.1; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/TESTALL.txt b/src/CBot/TestCBot/TESTALL.txt deleted file mode 100644 index 82247a0..0000000 --- a/src/CBot/TestCBot/TESTALL.txt +++ /dev/null @@ -1,161 +0,0 @@ -int T ( int z ) -{ - return 45 + z ; -} - -class toto -{ - int val = 3 ; - int x = 3 * 3 ; - void toto( int n ) - { val = n + 3 ; } - int retval ( int param ) - { int r = val + param + x ; - val = param ; - return r ; } -} - -public extern void object :: Chose( ) -{ - int z [ 6 ]; - for ( int i = 0 ; i < 6 ; ) z [ i++ ] = 3 - i ; - show ( z ) ; - return; - - // test des tableaux - int [ ] a [ 3 ] ; -// a = null; - if ( a == null ) show ( "NULL" ); - - a [ 2 / 2 ] [ 2 ]= 5 ; - int [ ] b ; b = a [1] ; - b [ 0 ] = -4; - a [ 4 / 2 ] [ 1 ]= 1 ; - show ( a , b ) ; - return ; - { - toto chose = new toto (5 ) ; - toto truc = chose ; - show ( chose, chose.retval( 100 ) , - truc, truc.retval (40 ) ) ; - - return; - } - { - point A = new - point ( 4 * 4 , 2 ) ; - show ( A ) ; - return; - } - { - show ( T ( 1 ) , T ( 3.7 ) ) ; - return; - } - - { - point A ( 3, 4 ) , - B = A ; - - int n = -4; - show ( n ); - - show ( A, B ) ; - - boolean a = false; - boolean b = a or true; - if ( not a and b ) ; - return; - } - { - // test try - float x = nan ; int z = 0 ; - try { -// throw ( 3 * 4 + 33 ) ; - int zz ; goto ( 12 ) ; z = 1 ; z = 0 / 0 ; z = 2 ; - } - catch ( 45 + 0 * 6000 ) - { - show( "Exception 6000", z ) ; - } - catch ( x == 0 ) { show( "x nul" ) ; } - finally { show ( "fini" ) ; } - show ( "continue" ); - return; - } - { - // test des if - int a = 3; - if ( a == 3 ) show ( "33"); - else show ( "44"); - if ( a != 3 ) show ( "333"); - else show ( "444"); - return; - } - { - int a = 0; - // test break -un: - while ( true ) - { -deux: - while ( true ) - { - a++; - if ( a == 2 ) continue; - if ( a == 3 ) break deux; - show ( a ) ; - if ( a == 5 ) break un; - } - show ( "DEUX" ); - } - return; - } - { - // test switch - int a = 0; - - switch ( a ) - { - case 1 : show( "un" ) ; break; - case 2 : show( "deux" ) ; // break; - case 3 : show( "trois" ) ; break; - case 4 : show( "quatre" ) ; // break; - default : show( "par dfaut" ) ; - } - return; - } - { - // test boucle while - float z = 3.3; - while ( z > 0 ) - { show ( z-- ) ; } - return; - } - - { - // test boucle do - float y = 3.3; - do { int x = 0; show(y); y++; } while ( y < 7 ) ; - return; - } - // test boucle for - int j = -7; show ( j ); - for ( int ii = 3, j = 31; ii < 6 ; ++ii, j = j -3 ) - { - j = 10 * j; - show ( ii, j ); - } - return; -{ - // dclarations de variables - int a; int b = 3; int c = 4*b, d = 1, e; - float x; float y = 3.3; float z = y / 2, u = 1, v; - boolean t; boolean tt = true or false; boolean ttt = false, tttt = true, t5; - string s; string ss = "hello"; string s2 = ss + " plus", s3 = "s3", s4; - - show( b, c, d ); - show( y, z, u ); - show( tt, ttt, tttt ); - show( ss, s2, s3 ); -} -} \ No newline at end of file diff --git a/src/CBot/TestCBot/TestCB1.txt b/src/CBot/TestCBot/TestCB1.txt deleted file mode 100644 index 516db47..0000000 --- a/src/CBot/TestCBot/TestCB1.txt +++ /dev/null @@ -1,18 +0,0 @@ -extern public void toto() -{ - print( "hello" ) ; - print( fac(5) ); - print( t() ) ; -} - -public int fac(int n) -{ - if ( n<2 ) return 1; - return n * fac(n-1); -} - -point t() -{ - point a(1,2); - return a; -} diff --git a/src/CBot/TestCBot/TestCBot.clw b/src/CBot/TestCBot/TestCBot.clw deleted file mode 100644 index 13f20f4..0000000 --- a/src/CBot/TestCBot/TestCBot.clw +++ /dev/null @@ -1,316 +0,0 @@ -; CLW file contains information for the MFC ClassWizard - -[General Info] -Version=1 -LastClass=CPerformDlg -LastTemplate=CDialog -NewFileInclude1=#include "stdafx.h" -NewFileInclude2=#include "testcbot.h" -LastPage=0 - -ClassCount=8 -Class1=CBotConsoleDlg -Class2=CChildFrame -Class3=CMainFrame -Class4=CTestCBotApp -Class5=CAboutDlg -Class6=CTestCBotDoc -Class7=CTestCBotView - -ResourceCount=12 -Resource1=IDD_CONSOLE -Resource2=IDR_TESTCBTYPE (French (France)) -Resource3=IDD_ABOUTBOX (French (France)) -Resource4=IDR_MAINFRAME (French (France)) -Resource5=IDR_MAINFRAME -Resource6=IDR_TESTCBTYPE -Resource7=IDD_ABOUTBOX -Resource8=IDD_CONSOLE (French (Switzerland)) -Class8=CPerformDlg -Resource9=IDD_DIALOG1 -Resource10=IDD_DIALOG2 -Resource11=IDD_DIALOG1 (French (Switzerland)) -Resource12=IDD_DIALOG2 (French (France)) - -[CLS:CBotConsoleDlg] -Type=0 -BaseClass=CDialog -HeaderFile=CBotConsoleDlg.h -ImplementationFile=CBotConsoleDlg.cpp -LastObject=IDC_EDIT1 - -[CLS:CChildFrame] -Type=0 -BaseClass=CMDIChildWnd -HeaderFile=ChildFrm.h -ImplementationFile=ChildFrm.cpp - -[CLS:CMainFrame] -Type=0 -BaseClass=CMDIFrameWnd -HeaderFile=MainFrm.h -ImplementationFile=MainFrm.cpp -Filter=T -VirtualFilter=fWC -LastObject=CMainFrame - -[CLS:CTestCBotApp] -Type=0 -BaseClass=CWinApp -HeaderFile=TestCBot.h -ImplementationFile=TestCBot.cpp -Filter=N -VirtualFilter=AC -LastObject=ID_TEST - -[CLS:CAboutDlg] -Type=0 -BaseClass=CDialog -HeaderFile=TestCBot.cpp -ImplementationFile=TestCBot.cpp -LastObject=CAboutDlg - -[CLS:CTestCBotDoc] -Type=0 -BaseClass=CDocument -HeaderFile=TestCBotDoc.h -ImplementationFile=TestCBotDoc.cpp -LastObject=CTestCBotDoc -Filter=N -VirtualFilter=DC - -[CLS:CTestCBotView] -Type=0 -BaseClass=CView -HeaderFile=TestCBotView.h -ImplementationFile=TestCBotView.cpp -LastObject=CTestCBotView -Filter=C -VirtualFilter=VWC - -[DLG:IDD_CONSOLE] -Type=1 -Class=CBotConsoleDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342308352 -Control2=IDC_EDIT2,edit,1350631552 -Control3=IDOK,button,1342242817 -Control4=IDC_EDIT1,edit,1352734724 - -[DLG:IDD_ABOUTBOX] -Type=1 -Class=CAboutDlg -ControlCount=7 -Control1=IDC_STATIC,static,1342177283 -Control2=IDC_STATIC,static,1342308480 -Control3=IDC_STATIC,static,1342308352 -Control4=IDOK,button,1342373889 -Control5=IDC_STATIC,static,1342308352 -Control6=IDC_STATIC,static,1342308352 -Control7=IDC_STATIC,static,1342308352 - -[TB:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command4=ID_EDIT_CUT -Command5=ID_EDIT_COPY -Command6=ID_EDIT_PASTE -Command7=ID_FILE_PRINT -Command8=ID_RUN -Command9=ID_APP_ABOUT -CommandCount=9 - -[MNU:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_MRU_FILE1 -Command4=ID_APP_EXIT -Command5=ID_VIEW_TOOLBAR -Command6=ID_VIEW_STATUS_BAR -Command7=ID_APP_ABOUT -CommandCount=7 - -[MNU:IDR_TESTCBTYPE (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_CLOSE -Command4=ID_FILE_SAVE -Command5=ID_FILE_SAVE_AS -Command6=ID_FILE_MRU_FILE1 -Command7=ID_APP_EXIT -Command8=ID_EDIT_UNDO -Command9=ID_EDIT_CUT -Command10=ID_EDIT_COPY -Command11=ID_EDIT_PASTE -Command12=ID_VIEW_TOOLBAR -Command13=ID_VIEW_STATUS_BAR -Command14=ID_WINDOW_NEW -Command15=ID_WINDOW_CASCADE -Command16=ID_WINDOW_TILE_HORZ -Command17=ID_WINDOW_ARRANGE -Command18=ID_APP_ABOUT -CommandCount=18 - -[ACL:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_EDIT_COPY -Command2=ID_FILE_NEW -Command3=ID_FILE_OPEN -Command4=ID_FILE_SAVE -Command5=ID_EDIT_PASTE -Command6=ID_EDIT_UNDO -Command7=ID_EDIT_CUT -Command8=ID_RUN -Command9=ID_NEXT_PANE -Command10=ID_PREV_PANE -Command11=ID_RUN -Command12=ID_TEST -Command13=ID_EDIT_COPY -Command14=ID_EDIT_PASTE -Command15=ID_EDIT_CUT -Command16=ID_EDIT_UNDO -CommandCount=16 - -[DLG:IDD_ABOUTBOX (French (France))] -Type=1 -Class=CAboutDlg -ControlCount=7 -Control1=IDC_STATIC,static,1342177283 -Control2=IDC_STATIC,static,1342308480 -Control3=IDC_STATIC,static,1342308352 -Control4=IDOK,button,1342373889 -Control5=IDC_STATIC,static,1342308352 -Control6=IDC_STATIC,static,1342308352 -Control7=IDC_STATIC,static,1342308352 - -[ACL:IDR_MAINFRAME] -Type=1 -Command1=ID_EDIT_COPY -Command2=ID_FILE_NEW -Command3=ID_FILE_OPEN -Command4=ID_FILE_SAVE -Command5=ID_EDIT_PASTE -Command6=ID_EDIT_UNDO -Command7=ID_EDIT_CUT -Command8=ID_RUN -Command9=ID_NEXT_PANE -Command10=ID_PREV_PANE -Command11=ID_RUN -Command12=ID_TEST -Command13=ID_EDIT_COPY -Command14=ID_EDIT_PASTE -Command15=ID_EDIT_CUT -Command16=ID_EDIT_UNDO -CommandCount=16 - -[TB:IDR_MAINFRAME] -Type=1 -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command4=ID_EDIT_CUT -Command5=ID_EDIT_COPY -Command6=ID_EDIT_PASTE -Command7=ID_FILE_PRINT -Command8=ID_RUN -Command9=ID_APP_ABOUT -CommandCount=9 - -[MNU:IDR_MAINFRAME] -Type=1 -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_MRU_FILE1 -Command4=ID_APP_EXIT -Command5=ID_VIEW_TOOLBAR -Command6=ID_VIEW_STATUS_BAR -Command7=ID_APP_ABOUT -CommandCount=7 - -[MNU:IDR_TESTCBTYPE] -Type=1 -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_CLOSE -Command4=ID_FILE_SAVE -Command5=ID_FILE_SAVE_AS -Command6=ID_FILE_MRU_FILE1 -Command7=ID_APP_EXIT -Command8=ID_EDIT_UNDO -Command9=ID_EDIT_CUT -Command10=ID_EDIT_COPY -Command11=ID_EDIT_PASTE -Command12=ID_VIEW_TOOLBAR -Command13=ID_VIEW_STATUS_BAR -Command14=ID_WINDOW_NEW -Command15=ID_WINDOW_CASCADE -Command16=ID_WINDOW_TILE_HORZ -Command17=ID_WINDOW_ARRANGE -Command18=ID_APP_ABOUT -CommandCount=18 - -[DLG:IDD_CONSOLE (French (Switzerland))] -Type=1 -Class=CBotConsoleDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342308352 -Control2=IDC_EDIT2,edit,1350631552 -Control3=IDOK,button,1342242817 -Control4=IDC_EDIT1,edit,1352734724 - -[DLG:IDD_DIALOG1] -Type=1 -Class=CPerformDlg -ControlCount=9 -Control1=IDC_STATIC,static,1342308352 -Control2=IDC_EDIT1,edit,1350633600 -Control3=IDC_STATIC,static,1342308352 -Control4=IDC_EDIT2,edit,1350631552 -Control5=IDC_SPIN1,msctls_updown32,1342177312 -Control6=IDC_COMBO1,combobox,1344339971 -Control7=IDC_STATIC,static,1342308352 -Control8=IDC_STATIC,static,1342308352 -Control9=IDC_EDIT3,edit,1350633600 - -[CLS:CPerformDlg] -Type=0 -HeaderFile=PerformDlg.h -ImplementationFile=PerformDlg.cpp -BaseClass=CDialog -Filter=D -VirtualFilter=dWC -LastObject=IDC_EDIT3 - -[DLG:IDD_DIALOG2] -Type=1 -ControlCount=2 -Control1=IDOK,button,1342242817 -Control2=IDCANCEL,button,1342242816 - -[DLG:IDD_DIALOG1 (French (Switzerland))] -Type=1 -ControlCount=9 -Control1=IDC_STATIC,static,1342308352 -Control2=IDC_EDIT1,edit,1350633600 -Control3=IDC_STATIC,static,1342308352 -Control4=IDC_EDIT2,edit,1350631552 -Control5=IDC_SPIN1,msctls_updown32,1342177312 -Control6=IDC_COMBO1,combobox,1344339971 -Control7=IDC_STATIC,static,1342308352 -Control8=IDC_STATIC,static,1342308352 -Control9=IDC_EDIT3,edit,1350633600 - -[DLG:IDD_DIALOG2 (French (France))] -Type=1 -ControlCount=2 -Control1=IDOK,button,1342242817 -Control2=IDCANCEL,button,1342242816 - diff --git a/src/CBot/TestCBot/TestCBot.cpp b/src/CBot/TestCBot/TestCBot.cpp deleted file mode 100644 index a76040a..0000000 --- a/src/CBot/TestCBot/TestCBot.cpp +++ /dev/null @@ -1,267 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBot.cpp : Defines the class behaviors for the application. -// - -#include "stdafx.h" -#include "TestCBot.h" - -#include "MainFrm.h" -#include "ChildFrm.h" -#include "TestCBotDoc.h" -#include "TestCBotView.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotApp - -BEGIN_MESSAGE_MAP(CTestCBotApp, CWinApp) - //{{AFX_MSG_MAP(CTestCBotApp) - ON_COMMAND(ID_APP_ABOUT, OnAppAbout) - //}}AFX_MSG_MAP - // Standard file based document commands - ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) - ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotApp construction - -CTestCBotApp::CTestCBotApp() -{ - m_pConsole = NULL; - m_LastActive = NULL; - m_pClassPoint= NULL; -} - - -///////////////////////////////////////////////////////////////////////////// -// The one and only CTestCBotApp object - -CTestCBotApp theApp; - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotApp initialization - -#include "Routines.cpp" - - -static char BASED_CODE szSection[] = "Recent File List"; -static char BASED_CODE szFilename[] = "File1"; - - -#include "../ClassFILE.cpp" - -// routine pour mettre jour l'instance de la classe Bot courante -void rMajObject( CBotVar* pThis, void* pUser ) -{ - if (!pThis->IsElemOfClass("object")) - return ; - CBotVar* pPos = pThis->GivItem("position"); - CBotVar* pX = pPos->GivItem("x"); - CBotVar* pY = pPos->GivItem("y"); - CBotVar* pZ = pPos->GivItem("z"); -// CBotVar* pPt = pThis->GivItem("transport"); - - CBotString p = pX->GivValString(); - -// pX->SetValFloat( pUser == (void*)1 ? (float)12.5 : (float)44.4 ); - pZ->SetValFloat( (float)0 ); - pY->SetValFloat( (float)-3.33 ); - pX->SetValFloat( pX->GivValFloat() + 10 ) ; - -// pX = pThis->GivItem( "xx" ); -// pX->SetValFloat( (float)22 ); - - // cre une instance sur une classe object -// CBotVar* pAutre = CBotVar::Create("autre", CBotTypClass, "object"); -// pAutre->SetUserPtr( (void*)3 ); -// pPt->SetPointer( pAutre ); -// pPt->SetPointer( NULL ); -// delete pAutre; -} - - -BOOL CTestCBotApp::InitInstance() -{ -////////////////////////////////////////////// -// dfini les mots clefs supplmentaires -// ------------------------------------------- - - CBotProgram::Init(); - -////////////////////////////////////////////// -// dfini les fonctions "show()" et "print()" -// ------------------------------------------- - - CBotProgram::AddFunction("show", rShow, cShow); - CBotProgram::AddFunction("print", rPrint, cPrint); - CBotProgram::AddFunction("println", rPrintLn, cPrint); - - -/////////////////////////////////// -// dfinie la classe globale CPoint -// -------------------------------- - - m_pClassPoint = new CBotClass("CPoint", NULL); - // ajoute le composant ".x" - m_pClassPoint->AddItem("x", CBotTypFloat); - // ajoute le composant ".y" - m_pClassPoint->AddItem("y", CBotTypFloat); - - // ajoute le constructeur pour cette classe - m_pClassPoint->AddFunction("CPoint", rCPoint, cCPoint); - - m_pClassPointIntr = new CBotClass("point", NULL, TRUE); - // ajoute le composant ".x" - m_pClassPointIntr->AddItem("x", CBotTypFloat); - // ajoute le composant ".y" - m_pClassPointIntr->AddItem("y", CBotTypFloat); - // ajoute le composant ".z" - m_pClassPointIntr->AddItem("z", CBotTypFloat); - - // ajoute le constructeur pour cette classe - m_pClassPointIntr->AddFunction("point", rCPoint, cCPoint); - - // dfini la classe "object" - CBotClass* pClassObject = new CBotClass( "object", NULL ) ; - pClassObject->AddItem( "xx", CBotTypFloat ); - pClassObject->AddItem( "position", CBotTypResult( CBotTypIntrinsic, "point" ) ); - pClassObject->AddItem( "transport", CBotTypResult( CBotTypPointer, "object" ) ); - pClassObject->AddUpdateFunc( rMajObject ); - - InitClassFILE(); - - AfxEnableControlContainer(); - - // Standard initialization - -#ifdef _AFXDLL - Enable3dControls(); // Call this when using MFC in a shared DLL -#else - Enable3dControlsStatic(); // Call this when linking to MFC statically -#endif - - // Change the registry key under which our settings are stored. - SetRegistryKey(_T("Local AppWizard-Generated Applications")); - - LoadStdProfileSettings(); // Load standard INI file options (including MRU) - - // Register document templates - - CMultiDocTemplate* pDocTemplate; - pDocTemplate = new CMultiDocTemplate( - IDR_TESTCBTYPE, - RUNTIME_CLASS(CTestCBotDoc), - RUNTIME_CLASS(CChildFrame), // custom MDI child frame - RUNTIME_CLASS(CTestCBotView)); - AddDocTemplate(pDocTemplate); - - // create main MDI Frame window - CMainFrame* pMainFrame = new CMainFrame; - if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) - return FALSE; - m_pMainWnd = pMainFrame; - - // Parse command line for standard shell commands, DDE, file open - CCommandLineInfo cmdInfo; - ParseCommandLine(cmdInfo); - - if (m_lpCmdLine[0] == 0) - { - CString Filename = GetProfileString(szSection, szFilename); - if (Filename.IsEmpty()) Filename = "TstCbot.txt"; - else OpenDocumentFile(Filename); - } - else - // Dispatch commands specified on the command line - if (!ProcessShellCommand(cmdInfo)) - return FALSE; - pMainFrame->ShowWindow(m_nCmdShow); - pMainFrame->UpdateWindow(); - - - return TRUE; -} - - -///////////////////////////////////////////////////////////////////////////// -// CAboutDlg dialog used for App About - -class CAboutDlg : public CDialog -{ -public: - CAboutDlg(); - -// Dialog Data - //{{AFX_DATA(CAboutDlg) - enum { IDD = IDD_ABOUTBOX }; - //}}AFX_DATA - - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CAboutDlg) - protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - //}}AFX_VIRTUAL - -// Implementation -protected: - //{{AFX_MSG(CAboutDlg) - // No message handlers - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) -{ - //{{AFX_DATA_INIT(CAboutDlg) - //}}AFX_DATA_INIT -} - -void CAboutDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialog::DoDataExchange(pDX); - //{{AFX_DATA_MAP(CAboutDlg) - //}}AFX_DATA_MAP -} - -BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) - //{{AFX_MSG_MAP(CAboutDlg) - // No message handlers - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -// App command to run the dialog -void CTestCBotApp::OnAppAbout() -{ - CAboutDlg aboutDlg; - aboutDlg.DoModal(); -} - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotApp commands - -int CTestCBotApp::ExitInstance() -{ - delete m_pFuncFile; - - CBotProgram::Free(); - return CWinApp::ExitInstance(); -} diff --git a/src/CBot/TestCBot/TestCBot.dsp b/src/CBot/TestCBot/TestCBot.dsp deleted file mode 100644 index 8ed9b11..0000000 --- a/src/CBot/TestCBot/TestCBot.dsp +++ /dev/null @@ -1,201 +0,0 @@ -# Microsoft Developer Studio Project File - Name="TestCBot" - Package Owner=<4> -# Microsoft Developer Studio Generated Build File, Format Version 5.00 -# ** DO NOT EDIT ** - -# TARGTYPE "Win32 (x86) Application" 0x0101 - -CFG=TestCBot - Win32 Debug -!MESSAGE This is not a valid makefile. To build this project using NMAKE, -!MESSAGE use the Export Makefile command and run -!MESSAGE -!MESSAGE NMAKE /f "TestCBot.mak". -!MESSAGE -!MESSAGE You can specify a configuration when running NMAKE -!MESSAGE by defining the macro CFG on the command line. For example: -!MESSAGE -!MESSAGE NMAKE /f "TestCBot.mak" CFG="TestCBot - Win32 Debug" -!MESSAGE -!MESSAGE Possible choices for configuration are: -!MESSAGE -!MESSAGE "TestCBot - Win32 Release" (based on "Win32 (x86) Application") -!MESSAGE "TestCBot - Win32 Debug" (based on "Win32 (x86) Application") -!MESSAGE - -# Begin Project -# PROP Scc_ProjName "" -# PROP Scc_LocalPath "" -CPP=cl.exe -MTL=midl.exe -RSC=rc.exe - -!IF "$(CFG)" == "TestCBot - Win32 Release" - -# PROP BASE Use_MFC 5 -# PROP BASE Use_Debug_Libraries 0 -# PROP BASE Output_Dir "Release" -# PROP BASE Intermediate_Dir "Release" -# PROP BASE Target_Dir "" -# PROP Use_MFC 5 -# PROP Use_Debug_Libraries 0 -# PROP Output_Dir "Release" -# PROP Intermediate_Dir "Release" -# PROP Target_Dir "" -# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c -# ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /FR /Yu"stdafx.h" /FD /c -# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 -# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 -# ADD BASE RSC /l 0x100c /d "NDEBUG" -# ADD RSC /l 0x100c /d "NDEBUG" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 /nologo /subsystem:windows /machine:I386 -# ADD LINK32 /nologo /subsystem:windows /machine:I386 - -!ELSEIF "$(CFG)" == "TestCBot - Win32 Debug" - -# PROP BASE Use_MFC 5 -# PROP BASE Use_Debug_Libraries 1 -# PROP BASE Output_Dir "Debug" -# PROP BASE Intermediate_Dir "Debug" -# PROP BASE Target_Dir "" -# PROP Use_MFC 5 -# PROP Use_Debug_Libraries 1 -# PROP Output_Dir "Debug" -# PROP Intermediate_Dir "Debug" -# PROP Ignore_Export_Lib 0 -# PROP Target_Dir "" -# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c -# ADD CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /FR /Yu"stdafx.h" /FD /c -# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 -# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 -# ADD BASE RSC /l 0x100c /d "_DEBUG" -# ADD RSC /l 0x100c /d "_DEBUG" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept -# ADD LINK32 /nologo /stack:0x7010 /subsystem:windows /debug /machine:I386 /pdbtype:sept - -!ENDIF - -# Begin Target - -# Name "TestCBot - Win32 Release" -# Name "TestCBot - Win32 Debug" -# Begin Group "Source Files" - -# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" -# Begin Source File - -SOURCE=.\CBotConsoleDlg.cpp -# End Source File -# Begin Source File - -SOURCE=.\ChildFrm.cpp -# End Source File -# Begin Source File - -SOURCE=.\MainFrm.cpp -# End Source File -# Begin Source File - -SOURCE=.\PerformDlg.cpp -# End Source File -# Begin Source File - -SOURCE=.\StdAfx.cpp -# ADD CPP /Yc"stdafx.h" -# End Source File -# Begin Source File - -SOURCE=.\TestCBot.cpp -# End Source File -# Begin Source File - -SOURCE=.\TestCBot.rc - -!IF "$(CFG)" == "TestCBot - Win32 Release" - -!ELSEIF "$(CFG)" == "TestCBot - Win32 Debug" - -!ENDIF - -# End Source File -# Begin Source File - -SOURCE=.\TestCBotDoc.cpp -# End Source File -# Begin Source File - -SOURCE=.\TestCBotView.cpp -# End Source File -# End Group -# Begin Group "Header Files" - -# PROP Default_Filter "h;hpp;hxx;hm;inl" -# Begin Source File - -SOURCE=.\CBotConsoleDlg.h -# End Source File -# Begin Source File - -SOURCE=.\ChildFrm.h -# End Source File -# Begin Source File - -SOURCE=.\MainFrm.h -# End Source File -# Begin Source File - -SOURCE=.\PerformDlg.h -# End Source File -# Begin Source File - -SOURCE=.\Resource.h -# End Source File -# Begin Source File - -SOURCE=.\StdAfx.h -# End Source File -# Begin Source File - -SOURCE=.\TestCBot.h -# End Source File -# Begin Source File - -SOURCE=.\TestCBotDoc.h -# End Source File -# Begin Source File - -SOURCE=.\TestCBotView.h -# End Source File -# End Group -# Begin Group "Resource Files" - -# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe" -# Begin Source File - -SOURCE=.\res\TestCBot.ico -# End Source File -# Begin Source File - -SOURCE=.\res\TestCBot.rc2 -# End Source File -# Begin Source File - -SOURCE=.\res\TestCBotDoc.ico -# End Source File -# Begin Source File - -SOURCE=.\res\Toolbar.bmp -# End Source File -# End Group -# Begin Source File - -SOURCE=..\Debug\CBot.lib -# End Source File -# End Target -# End Project diff --git a/src/CBot/TestCBot/TestCBot.h b/src/CBot/TestCBot/TestCBot.h deleted file mode 100644 index b3b15db..0000000 --- a/src/CBot/TestCBot/TestCBot.h +++ /dev/null @@ -1,78 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBot.h : main header file for the TESTCBOT application -// - -#if !defined(AFX_TESTCBOT_H__4D1BB903_8E74_11D4_A439_00D059085115__INCLUDED_) -#define AFX_TESTCBOT_H__4D1BB903_8E74_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -#ifndef __AFXWIN_H__ - #error include 'stdafx.h' before including this file for PCH -#endif - -#include "resource.h" // main symbols -//#include "../CbotDll.h" // librairie CBot -#include "../Cbot.h" // complet pour Browse - -class CTestCBotView; - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotApp: -// See TestCBot.cpp for the implementation of this class -// - -class CTestCBotApp : public CWinApp -{ -public: - CTestCBotApp(); - - CEdit* m_pConsole; - CTestCBotView* m_LastActive; - CBotClass* m_pClassPoint; - CBotClass* m_pClassPointIntr; - - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CTestCBotApp) - public: - virtual BOOL InitInstance(); - virtual int ExitInstance(); - //}}AFX_VIRTUAL - -// Implementation - - //{{AFX_MSG(CTestCBotApp) - afx_msg void OnAppAbout(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_TESTCBOT_H__4D1BB903_8E74_11D4_A439_00D059085115__INCLUDED_) - - -#define WM_STARTPROG WM_APP + 0 -#define WM_ENDPROG WM_APP + 1 -#define WM_ACTWINDOW WM_APP + 2 diff --git a/src/CBot/TestCBot/TestCBot.rc b/src/CBot/TestCBot/TestCBot.rc deleted file mode 100644 index 137458c..0000000 --- a/src/CBot/TestCBot/TestCBot.rc +++ /dev/null @@ -1,564 +0,0 @@ -//Microsoft Developer Studio generated resource script. -// -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "afxres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// French (France) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) -#ifdef _WIN32 -LANGUAGE LANG_FRENCH, SUBLANG_FRENCH -#pragma code_page(1252) -#endif //_WIN32 - -///////////////////////////////////////////////////////////////////////////// -// -// Icon -// - -// Icon with lowest ID value placed first to ensure application icon -// remains consistent on all systems. -IDR_MAINFRAME ICON DISCARDABLE "res\\TestCBot.ico" -IDR_TESTCBTYPE ICON DISCARDABLE "res\\TestCBotDoc.ico" - -///////////////////////////////////////////////////////////////////////////// -// -// Bitmap -// - -IDR_MAINFRAME BITMAP MOVEABLE PURE "res\\Toolbar.bmp" - -///////////////////////////////////////////////////////////////////////////// -// -// Toolbar -// - -IDR_MAINFRAME TOOLBAR DISCARDABLE 16, 15 -BEGIN - BUTTON ID_FILE_NEW - BUTTON ID_FILE_OPEN - BUTTON ID_FILE_SAVE - SEPARATOR - BUTTON ID_EDIT_CUT - BUTTON ID_EDIT_COPY - BUTTON ID_EDIT_PASTE - SEPARATOR - BUTTON ID_FILE_PRINT - BUTTON ID_RUN - SEPARATOR - BUTTON ID_APP_ABOUT -END - - -///////////////////////////////////////////////////////////////////////////// -// -// Menu -// - -IDR_MAINFRAME MENU PRELOAD DISCARDABLE -BEGIN - POPUP "&Fichier" - BEGIN - MENUITEM "&Nouveau\tCtrl+N", ID_FILE_NEW - MENUITEM "&Ouvrir...\tCtrl+O", ID_FILE_OPEN - MENUITEM SEPARATOR - MENUITEM "Fichier rcent", ID_FILE_MRU_FILE1, GRAYED - MENUITEM SEPARATOR - MENUITEM "&Quitter", ID_APP_EXIT - END - POPUP "&Affichage" - BEGIN - MENUITEM "&Barre d'outils", ID_VIEW_TOOLBAR - MENUITEM "Barre d'&tat", ID_VIEW_STATUS_BAR - END - POPUP "&?" - BEGIN - MENUITEM "&A propos de TestCBot...", ID_APP_ABOUT - END -END - -IDR_TESTCBTYPE MENU PRELOAD DISCARDABLE -BEGIN - POPUP "&Fichier" - BEGIN - MENUITEM "&Nouveau\tCtrl+N", ID_FILE_NEW - MENUITEM "&Ouvrir...\tCtrl+O", ID_FILE_OPEN - MENUITEM "&Fermer", ID_FILE_CLOSE - MENUITEM "&Enregistrer\tCtrl+S", ID_FILE_SAVE - MENUITEM "En®istrer sous...", ID_FILE_SAVE_AS - MENUITEM SEPARATOR - MENUITEM "Fichier rcent", ID_FILE_MRU_FILE1, GRAYED - MENUITEM SEPARATOR - MENUITEM "&Quitter", ID_APP_EXIT - END - POPUP "&Edition" - BEGIN - MENUITEM "&Annuler\tCtrl+Z", ID_EDIT_UNDO - MENUITEM SEPARATOR - MENUITEM "&Couper\tCtrl+X", ID_EDIT_CUT - MENUITEM "&Copier\tCtrl+C", ID_EDIT_COPY - MENUITEM "C&oller\tCtrl+V", ID_EDIT_PASTE - END - POPUP "&Affichage" - BEGIN - MENUITEM "&Barre d'outils", ID_VIEW_TOOLBAR - MENUITEM "Barre d'&tat", ID_VIEW_STATUS_BAR - END - POPUP "Fe&ntre" - BEGIN - MENUITEM "&Nouvelle fentre", ID_WINDOW_NEW - MENUITEM "&Cascade", ID_WINDOW_CASCADE - MENUITEM "&Mosaque", ID_WINDOW_TILE_HORZ - MENUITEM "&Rorganiser les icnes", ID_WINDOW_ARRANGE - END - POPUP "&?" - BEGIN - MENUITEM "&A propos de TestCBot...", ID_APP_ABOUT - END -END - - -///////////////////////////////////////////////////////////////////////////// -// -// Accelerator -// - -IDR_MAINFRAME ACCELERATORS PRELOAD MOVEABLE PURE -BEGIN - "C", ID_EDIT_COPY, VIRTKEY, CONTROL, NOINVERT - "N", ID_FILE_NEW, VIRTKEY, CONTROL, NOINVERT - "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT - "S", ID_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT - "V", ID_EDIT_PASTE, VIRTKEY, CONTROL, NOINVERT - VK_BACK, ID_EDIT_UNDO, VIRTKEY, ALT, NOINVERT - VK_DELETE, ID_EDIT_CUT, VIRTKEY, SHIFT, NOINVERT - VK_F5, ID_RUN, VIRTKEY, NOINVERT - VK_F6, ID_NEXT_PANE, VIRTKEY, NOINVERT - VK_F6, ID_PREV_PANE, VIRTKEY, SHIFT, NOINVERT - VK_F7, ID_RUN, VIRTKEY, NOINVERT - VK_F9, ID_TEST, VIRTKEY, NOINVERT - VK_INSERT, ID_EDIT_COPY, VIRTKEY, CONTROL, NOINVERT - VK_INSERT, ID_EDIT_PASTE, VIRTKEY, SHIFT, NOINVERT - "X", ID_EDIT_CUT, VIRTKEY, CONTROL, NOINVERT - "Z", ID_EDIT_UNDO, VIRTKEY, CONTROL, NOINVERT -END - - -///////////////////////////////////////////////////////////////////////////// -// -// Dialog -// - -IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 265, 206 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "A propos de TestCBot" -FONT 8, "MS Sans Serif" -BEGIN - ICON IDR_MAINFRAME,IDC_STATIC,11,17,21,20 - LTEXT "TestCBot version 1.0",IDC_STATIC,40,10,119,8, - SS_NOPREFIX - LTEXT "Copyright D. Dumoulin (C) 2000",IDC_STATIC,40,25,119,8 - DEFPUSHBUTTON "OK",IDOK,226,7,32,14,WS_GROUP - LTEXT "Programme de test pour la librairie CBot\n\nLes fonctions doivent tre dclares comme ""extern"" pour apparatre dans la liste lors de l'excution.\n\n", - IDC_STATIC,39,43,191,41 - LTEXT "Mais en fait, on peut accder toutes les fonctions marques ""public"" quelles soient dans la fentre active ou non.", - IDC_STATIC,39,89,187,36 - LTEXT "Les fonctions print( ... ) et println( ...) permettent d'afficher des rsultats dans la console.\n\nLa fonction show( ... ) affiche les paramtres dans un dialogue, et suspend donc l'excution.", - IDC_STATIC,39,130,187,54 -END - -IDD_DIALOG2 DIALOG DISCARDABLE 0, 0, 186, 95 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "Dialog" -FONT 8, "MS Sans Serif" -BEGIN - DEFPUSHBUTTON "OK",IDOK,129,7,50,14 - PUSHBUTTON "Cancel",IDCANCEL,129,24,50,14 -END - - -#ifndef _MAC -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,0,0,1 - PRODUCTVERSION 1,0,0,1 - FILEFLAGSMASK 0x3fL -#ifdef _DEBUG - FILEFLAGS 0x1L -#else - FILEFLAGS 0x0L -#endif - FILEOS 0x4L - FILETYPE 0x1L - FILESUBTYPE 0x0L -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040C04B0" - BEGIN - VALUE "CompanyName", "\0" - VALUE "FileDescription", "Application MFC TestCBot\0" - VALUE "FileVersion", "1, 0, 0, 1\0" - VALUE "InternalName", "TestCBot\0" - VALUE "LegalCopyright", "Copyright (C) 1900\0" - VALUE "LegalTrademarks", "\0" - VALUE "OriginalFilename", "TestCBot.EXE\0" - VALUE "ProductName", "Application TestCBot\0" - VALUE "ProductVersion", "1, 0, 0, 1\0" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Traduction", 0x40c, 1200 - END -END - -#endif // !_MAC - - -///////////////////////////////////////////////////////////////////////////// -// -// DESIGNINFO -// - -#ifdef APSTUDIO_INVOKED -GUIDELINES DESIGNINFO DISCARDABLE -BEGIN - IDD_ABOUTBOX, DIALOG - BEGIN - LEFTMARGIN, 7 - RIGHTMARGIN, 258 - TOPMARGIN, 7 - BOTTOMMARGIN, 199 - END - - IDD_DIALOG2, DIALOG - BEGIN - LEFTMARGIN, 7 - RIGHTMARGIN, 179 - TOPMARGIN, 7 - BOTTOMMARGIN, 88 - END -END -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// String Table -// - -STRINGTABLE PRELOAD DISCARDABLE -BEGIN - IDR_MAINFRAME "TestCBot" - IDR_TESTCBTYPE "\nTestCBot\nTestCBot\nCBot (*.txt)\n.txt\nTestCBot.Document\nTestCB Document" -END - -STRINGTABLE PRELOAD DISCARDABLE -BEGIN - AFX_IDS_APP_TITLE "TestCBot" - AFX_IDS_IDLEMESSAGE "Prt" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_INDICATOR_EXT "EXT" - ID_INDICATOR_CAPS "MAJ" - ID_INDICATOR_NUM "NUM" - ID_INDICATOR_SCRL "DEF" - ID_INDICATOR_OVR "ECR" - ID_INDICATOR_REC "ENR" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_FILE_NEW "Cre un nouveau document\nNouveau" - ID_FILE_OPEN "Ouvre un document existant\nOuvrir" - ID_FILE_CLOSE "Ferme le document actif\nFermer" - ID_FILE_SAVE "Enregistre le document actif\nEnregistrer" - ID_FILE_SAVE_AS "Enregistre le document actif sous un nouveau nom\nEnregistrer sous" - ID_FILE_PRINT "Imprime le document\nImprime" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_APP_ABOUT "Affiche des informations sur le programme\nA propos de" - ID_APP_EXIT "Ferme l'application ; propose d'enregistrer les documents\nQuitter" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_FILE_MRU_FILE1 "Ouvre ce document" - ID_FILE_MRU_FILE2 "Ouvre ce document" - ID_FILE_MRU_FILE3 "Ouvre ce document" - ID_FILE_MRU_FILE4 "Ouvre ce document" - ID_FILE_MRU_FILE5 "Ouvre ce document" - ID_FILE_MRU_FILE6 "Ouvre ce document" - ID_FILE_MRU_FILE7 "Ouvre ce document" - ID_FILE_MRU_FILE8 "Ouvre ce document" - ID_FILE_MRU_FILE9 "Ouvre ce document" - ID_FILE_MRU_FILE10 "Ouvre ce document" - ID_FILE_MRU_FILE11 "Ouvre ce document" - ID_FILE_MRU_FILE12 "Ouvre ce document" - ID_FILE_MRU_FILE13 "Ouvre ce document" - ID_FILE_MRU_FILE14 "Ouvre ce document" - ID_FILE_MRU_FILE15 "Ouvre ce document" - ID_FILE_MRU_FILE16 "Ouvre ce document" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_NEXT_PANE "Passe au volet de fentre suivant\nVolet suivant" - ID_PREV_PANE "Revient au volet prcdent\nVolet prcdent" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_WINDOW_NEW "Ouvre une nouvelle fentre pour le document actif\nNouvelle fentre" - ID_WINDOW_ARRANGE "Rorganise les icnes en bas de la fentre\nRorganise les icnes" - ID_WINDOW_CASCADE "Rorganise les fentres en cascade\nCascade" - ID_WINDOW_TILE_HORZ "Rorganise les fentres en une mosaque\nMosaque" - ID_WINDOW_TILE_VERT "Rorganise les fentres en une mosaque\nMosaque" - ID_WINDOW_SPLIT "Fractionne la fentre active en deux volets\nFractionner" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_EDIT_CLEAR "Efface la slection\nEffacer" - ID_EDIT_CLEAR_ALL "Efface tout\nEffacer tout" - ID_EDIT_COPY "Copie la slection et la place dans le Presse-papiers\nCopier" - ID_EDIT_CUT "Supprime la slection et la place dans le Presse-papiers\nCopier" - ID_EDIT_FIND "Recherche le texte spcifi\nRechercher" - ID_EDIT_PASTE "Insre le contenu du Presse-papiers\nColler" - ID_EDIT_REPEAT "Rpte la dernire action\nRpter" - ID_EDIT_REPLACE "Remplace le texte spcifique par un texte diffrent\nRemplacer" - ID_EDIT_SELECT_ALL "Slectionne le document entier\nSlectionner tout" - ID_EDIT_UNDO "Annule la dernire action\nAnnuler" - ID_EDIT_REDO "Rtablit l'action prcdemment annule\nRtablir" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_VIEW_TOOLBAR "Affiche ou masque la barre d'outils\nBarre d'outils" - ID_VIEW_STATUS_BAR "Affiche ou masque la barre d'tat\nBarre d'tat" -END - -STRINGTABLE DISCARDABLE -BEGIN - AFX_IDS_SCSIZE "Change la taille de la fentre" - AFX_IDS_SCMOVE "Change la position de la fentre" - AFX_IDS_SCMINIMIZE "Rduit la fentre en icne" - AFX_IDS_SCMAXIMIZE "Agrandit la fentre au format de l'cran" - AFX_IDS_SCNEXTWINDOW "Passe la fentre de document suivante" - AFX_IDS_SCPREVWINDOW "Passe la fentre de document prcdente" - AFX_IDS_SCCLOSE "Ferme la fentre active et propose l'enregistrement des documents" -END - -STRINGTABLE DISCARDABLE -BEGIN - AFX_IDS_SCRESTORE "Restaure la fentre sa taille d'origine" - AFX_IDS_SCTASKLIST "Active la liste des tches" - AFX_IDS_MDICHILD "Active cette fentre" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_RUN "Execute le programme CBot\nExecute (F5)" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_TYPENAMES "les diffrents types" - 1001 "Byte" - 1002 "Short" - 1003 "Char" - 1004 "Int" - 1005 "Long" - 1006 "Real" - 1007 "Double" -END - -STRINGTABLE DISCARDABLE -BEGIN - 1008 "Boolean" - 1009 "String" - 1010 "Array" - 1011 "Arraybody" - 1012 "Pointer" - 1013 "Nullpointer" - 1014 "nop" - 1015 "Class" - 1016 "Intrinsic" -END - -#endif // French (France) resources -///////////////////////////////////////////////////////////////////////////// - - -///////////////////////////////////////////////////////////////////////////// -// French (Switzerland) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRS) -#ifdef _WIN32 -LANGUAGE LANG_FRENCH, SUBLANG_FRENCH_SWISS -#pragma code_page(1252) -#endif //_WIN32 - -///////////////////////////////////////////////////////////////////////////// -// -// Dialog -// - -IDD_CONSOLE DIALOG DISCARDABLE 0, 0, 401, 210 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "CBot Console" -FONT 8, "MS Sans Serif" -BEGIN - LTEXT "Commande :",IDC_STATIC,7,177,40,8 - EDITTEXT IDC_EDIT2,7,189,329,14,ES_AUTOHSCROLL - DEFPUSHBUTTON "Excute",IDOK,344,189,50,14 - EDITTEXT IDC_EDIT1,7,7,387,167,ES_MULTILINE | ES_READONLY | - ES_WANTRETURN | WS_VSCROLL -END - -IDD_DIALOG1 DIALOG DISCARDABLE 0, 0, 177, 100 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "Test performances" -FONT 8, "MS Sans Serif" -BEGIN - LTEXT "Boucles par seconde",IDC_STATIC,7,9,68,8 - EDITTEXT IDC_EDIT1,111,7,51,14,ES_AUTOHSCROLL | ES_READONLY - LTEXT "Nombre de scripts",IDC_STATIC,7,55,58,8 - EDITTEXT IDC_EDIT2,111,52,40,14,ES_AUTOHSCROLL - CONTROL "Spin1",IDC_SPIN1,"msctls_updown32",UDS_ARROWKEYS,152,52, - 10,14 - COMBOBOX IDC_COMBO1,111,74,52,111,CBS_DROPDOWNLIST | WS_VSCROLL | - WS_TABSTOP - LTEXT "Timer",IDC_STATIC,7,77,18,8 - LTEXT "Performance %",IDC_STATIC,7,28,48,8 - EDITTEXT IDC_EDIT3,111,25,51,14,ES_AUTOHSCROLL | ES_READONLY -END - - -///////////////////////////////////////////////////////////////////////////// -// -// DESIGNINFO -// - -#ifdef APSTUDIO_INVOKED -GUIDELINES DESIGNINFO DISCARDABLE -BEGIN - IDD_CONSOLE, DIALOG - BEGIN - LEFTMARGIN, 7 - RIGHTMARGIN, 394 - TOPMARGIN, 7 - BOTTOMMARGIN, 203 - END - - IDD_DIALOG1, DIALOG - BEGIN - LEFTMARGIN, 7 - RIGHTMARGIN, 170 - TOPMARGIN, 7 - BOTTOMMARGIN, 93 - END -END -#endif // APSTUDIO_INVOKED - - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE DISCARDABLE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE DISCARDABLE -BEGIN - "#include ""afxres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE DISCARDABLE -BEGIN - "#define _AFX_NO_SPLITTER_RESOURCES\r\n" - "#define _AFX_NO_OLE_RESOURCES\r\n" - "#define _AFX_NO_TRACKER_RESOURCES\r\n" - "#define _AFX_NO_PROPERTY_RESOURCES\r\n" - "\r\n" - "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)\r\n" - "#ifdef _WIN32\r\n" - "LANGUAGE 12, 1\r\n" - "#pragma code_page(1252)\r\n" - "#endif\r\n" - "#include ""res\\TestCBot.rc2"" // non-Microsoft Visual C++ edited resources\r\n" - "#include ""l.fra\\afxres.rc"" // Standard components\r\n" - "#endif\0" -END - -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// Dialog Info -// - -IDD_DIALOG1 DLGINIT -BEGIN - IDC_COMBO1, 0x403, 2, 0 -0x0031, - IDC_COMBO1, 0x403, 3, 0 -0x3031, "\000" - IDC_COMBO1, 0x403, 4, 0 -0x3031, 0x0030, - IDC_COMBO1, 0x403, 5, 0 -0x3031, 0x3030, "\000" - 0 -END - -#endif // French (Switzerland) resources -///////////////////////////////////////////////////////////////////////////// - - - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// -#define _AFX_NO_SPLITTER_RESOURCES -#define _AFX_NO_OLE_RESOURCES -#define _AFX_NO_TRACKER_RESOURCES -#define _AFX_NO_PROPERTY_RESOURCES - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) -#ifdef _WIN32 -LANGUAGE 12, 1 -#pragma code_page(1252) -#endif -#include "res\TestCBot.rc2" // non-Microsoft Visual C++ edited resources -#include "l.fra\afxres.rc" // Standard components -#endif -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED - diff --git a/src/CBot/TestCBot/TestCBot1.txt b/src/CBot/TestCBot/TestCBot1.txt deleted file mode 100644 index d27b4f8..0000000 --- a/src/CBot/TestCBot/TestCBot1.txt +++ /dev/null @@ -1,27 +0,0 @@ - -class CPoint2 -{ - float x, y; - void CPoint2(float x, float y) - { - this.x = x; - this.y = y; - } -} - -public extern void T ( ) -{ - CPoint2 X( 12, 33 ), Y ( -4, 4/3 ); - print ( X, Y ) ; -} - -public extern void Hello ( ) - -{ - println ( "Hello" ); -} - -public extern void test ( int n ) -{ - for ( int i = n; i>0 ; i--) print (i); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/TestCBot3.txt b/src/CBot/TestCBot/TestCBot3.txt deleted file mode 100644 index b915f96..0000000 --- a/src/CBot/TestCBot/TestCBot3.txt +++ /dev/null @@ -1,24 +0,0 @@ -public extern void Test () -{ - for ( int x = 100000; x>0 ; x-- ) { } -} - -float MaRoutine( CPoint A, CPoint B ) -{ - A.x -= B.x ; // distance en x - A.y -= B.y ; // distance en y - A.x *= A.x; // carr de la distance - A.y += A.y; // carr de la distance - println ( A, B ) ; - return ( A.x + A.y ) ; -} - -public extern void TestAB ( ) -{ - CPoint A(3, 5) ; - CPoint B(4, -2); - println ( A, B ) ; - MaRoutine( A, B ) ; - println ( A, B ) ; -} - diff --git a/src/CBot/TestCBot/TestCBotDoc.cpp b/src/CBot/TestCBot/TestCBotDoc.cpp deleted file mode 100644 index 8880c57..0000000 --- a/src/CBot/TestCBot/TestCBotDoc.cpp +++ /dev/null @@ -1,697 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBotDoc.cpp : implementation of the CTestCBotDoc class -// - -#include "stdafx.h" -#include "TestCBot.h" - -#include "TestCBotDoc.h" -#include "TestCBotView.h" -#include "CBotConsoleDlg.h" -#include "PerformDlg.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotDoc - -IMPLEMENT_DYNCREATE(CTestCBotDoc, CDocument) - -BEGIN_MESSAGE_MAP(CTestCBotDoc, CDocument) - //{{AFX_MSG_MAP(CTestCBotDoc) - ON_COMMAND(ID_RUN, OnRun) - ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1) - ON_COMMAND(ID_TEST, OnTest) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotDoc construction/destruction - -static BOOL test = FALSE; - - -CTestCBotDoc::CTestCBotDoc() -{ - m_pEdit = NULL; - m_pProg = NULL; - m_bModified = FALSE; -} - -CTestCBotDoc::~CTestCBotDoc() -{ - delete m_pEdit; - delete m_pProg; -} - -BOOL CTestCBotDoc::OnNewDocument() -{ - if (!CDocument::OnNewDocument()) - return FALSE; - - return TRUE; -} - - - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotDoc serialization - -void CTestCBotDoc::Serialize(CArchive& ar) -{ - if (ar.IsStoring()) - { - m_pEdit->GetWindowText(m_DocText); - int w = m_DocText.GetLength(); - ar.Write((LPCTSTR)m_DocText, w); - } - else - { - int r; - char buf[10001]; - - r = ar.Read(buf, 10000); - buf[r] = 0; - m_DocText = buf; - - if ( m_pProg == NULL ) m_pProg = new CBotProgram(); - - if (!m_pProg->Compile(m_DocText, m_Liste, NULL)) - { - delete m_pProg; - m_pProg = NULL; - } - } -} - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotDoc diagnostics - -#ifdef _DEBUG -void CTestCBotDoc::AssertValid() const -{ - CDocument::AssertValid(); -} - -void CTestCBotDoc::Dump(CDumpContext& dc) const -{ - CDocument::Dump(dc); -} -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotDoc commands - -void CTestCBotDoc::OnRun() -{ - OnFileSave(); - - m_pEdit->GetWindowText(m_DocText); - - CString TextError; - int code, start, end; - - if ( m_pProg == NULL ) m_pProg = new CBotProgram(); - - CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); - - if (!m_pProg->Compile(m_DocText, m_Liste, NULL)) - { - m_pProg->GetError(code, start, end); - delete m_pProg; - m_pProg = NULL; - - m_pEdit->SetSel( start, end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - TextError = CBotProgram::GivErrorText( code ); - AfxMessageBox( TextError ); - - m_pEdit->SetFocus(); - return; - } - - if( m_Liste.GivSize() == 0 ) - { - AfxMessageBox("Aucune fonction marque \"extern\" !"); - return; - } - - for ( int i = 0; i < m_Liste.GivSize(); i++ ) - { - int start, stop; - m_pProg->GetPosition(m_Liste[i], start, stop, GetPosNom, GetPosParam); - m_Liste[i] = m_DocText.Mid( start, stop-start ); - } - - CBotConsoleDlg dlg; - dlg.m_pListe = &m_Liste; - dlg.m_pEditx = m_pEdit; - - dlg.DoModal(); // dialogue pour faire la console - - if ( dlg.m_code>0 ) - { - CString TextError; - - TextError = m_pProg->GivErrorText( dlg.m_code ); - - m_pEdit->SetSel( dlg.m_start, dlg.m_end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - AfxMessageBox(TextError); - } - - m_pEdit->SetFocus(); - - return; -} - - -void CTestCBotDoc::OnChangeEdit1() -{ - SetModifiedFlag(); - m_bModified = TRUE; -} - -BOOL CTestCBotDoc::Compile() -{ - m_pEdit->GetWindowText(m_DocText); - - CString TextError; - int code, start, end; - - if ( m_pProg == NULL ) m_pProg = new CBotProgram(); - - char buffer[100]; - strcpy(buffer, "le pointeur passer pour voir"); - - if (m_bModified && !m_pProg->Compile(m_DocText, m_Liste, (void*)buffer)) - { - m_pProg->GetError(code, start, end); - delete m_pProg; - m_pProg = NULL; - - m_pEdit->SetSel( start, end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - TextError = CBotProgram::GivErrorText( code ); - AfxMessageBox( TextError ); - - m_pEdit->SetFocus(); - m_bModified = FALSE; - return FALSE; - } - - if ( m_pProg->GetPosition( "TheTest", start, end) ) - { - m_pEdit->SetSel( start, end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - } - - m_bModified = FALSE; - return TRUE; -} - - - -static int compt = 0; -// routine retournant le "pointeur" un autre object -BOOL rRetObject( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - pResult->SetPointer( NULL ); - compt+=45671; - if (compt&0x11) return TRUE; - - CBotVar* pAutre = CBotVar::Create("autre", CBotTypResult( CBotTypClass, "object" )); - pAutre->SetUserPtr( (void*)2 ); - pResult->SetPointer( pAutre ); - - if (!pResult->IsElemOfClass("object")) - return TRUE; - - delete pAutre; - return TRUE; -} - -CBotTypResult cRetObject( CBotVar* &pVar, void* pUser ) -{ - return CBotTypResult( CBotTypPointer, "object"); -} - -BOOL roRadar( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - pResult->SetPointer( NULL ); - compt+=45671; - if (compt&0x11) return TRUE; - - CBotVar* pAutre = CBotVar::Create("autre", CBotTypResult( CBotTypClass, "object" )); - pAutre->SetUserPtr( (void*)2 ); - pResult->SetPointer( pAutre ); - - if (!pResult->IsElemOfClass("object")) - return TRUE; - - delete pAutre; - return TRUE; -} - -CBotTypResult coRadar( CBotVar* pThis, CBotVar* &pVar ) -{ - void* pUser = pThis->GivUserPtr(); - return CBotTypResult( CBotTypPointer, "object"); -} - -BOOL rMove( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - if ( test < 12 ) - { - test++; - return FALSE; - } - return TRUE; -} - -CBotTypResult cMove( CBotVar* &pVar, void* pUser ) -{ - return CBotTypResult( 0 ); -} - -BOOL rTurn( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - return TRUE; -} - -CBotTypResult cTurn( CBotVar* &pVar, void* pUser ) -{ - return CBotTypResult( 0 ); -} - -BOOL rRadar( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - pResult->SetPointer( NULL ); - - if ( pVar ) pVar->debug(); - - compt+=45671; - if (compt&0x11) - { - return FALSE; // TRUE; - } - - CBotVar* pAutre = CBotVar::Create("autre", CBotTypResult( CBotTypClass, "object" )); - pAutre->SetUserPtr( (void*)2 ); - pResult->SetPointer( pAutre ); - - if (!pResult->IsElemOfClass("object")) - return TRUE; - - delete pAutre; - return TRUE; -} - -CBotTypResult cRadar( CBotVar* &pVar, void* pUser ) -{ - return CBotTypResult( CBotTypPointer, "object"); -} - -// routine retournant le "pointeur" un autre object -BOOL rTEST( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - test = 1 ; - if ( pVar == NULL ) return TRUE; - - test = pVar->GivValInt(); - if ( test == 5 ) - { - pVar = pVar->GivNext(); - pVar->SetUserPtr( OBJECTDELETED ); - } - return TRUE; -} - -CBotTypResult cTEST( CBotVar* &pVar, void* pUser ) -{ - return CBotTypResult( 0 ); -} - -// routine retournant le "pointeur" un autre object -BOOL rF( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - if ( pResult == NULL ) return TRUE; - pResult->SetValInt(3); - return TRUE; -} - -CBotTypResult cF( CBotVar* &pVar, void* pUser ) -{ - return CBotTypResult( CBotTypFloat ); -} - -///////////////////////////////////////////////////////////////// - -// Compilation d'une procdure avec un "point". - -CBotTypResult cPoint(CBotVar* &var, void* user) -{ - if ( var == 0 ) return CBotTypResult( CBotErrLowParam ); - - if ( var->GivType() <= CBotTypDouble ) - { - var = var->GivNext(); - if ( var == 0 ) return CBotTypResult( CBotErrLowParam ); - if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); - var = var->GivNext(); - if ( var == 0 ) return CBotTypResult( CBotErrLowParam ); - if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); - var = var->GivNext(); - return CBotTypResult( 0 ); - } - - if ( var->GivType() == CBotTypClass ) - { - if ( !var->IsElemOfClass("point") ) return CBotTypResult( CBotErrBadParam ); - var = var->GivNext(); - return CBotTypResult( 0 ); - } - - return CBotTypResult( CBotErrBadParam ); -} - -// Donne un paramtre de type "point". -#define UNIT 1 - - -CBotTypResult cSpace(CBotVar* &var, void* user) -{ - CBotTypResult ret; - - if ( var == 0 ) return CBotTypResult( CBotTypIntrinsic, "point" ); - ret = cPoint(var, user); - if ( !ret.Eq(0) ) return ret; - - if ( var == 0 ) return CBotTypIntrinsic; - if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); - var = var->GivNext(); - - if ( var == 0 ) return CBotTypIntrinsic; - if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); - var = var->GivNext(); - - if ( var == 0 ) return CBotTypIntrinsic; - if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); - var = var->GivNext(); - - if ( var != 0 ) return CBotErrOverParam; - return CBotTypResult( CBotTypIntrinsic, "point" ); -} - -// Instruction "space(center, rMin, rMax, dist)". - -BOOL rSpace(CBotVar* var, CBotVar* result, int& exception, void* user) -{ - CBotVar* pSub; - float rMin, rMax, dist; - - rMin = 5.0f*UNIT; - rMax = 50.0f*UNIT; - dist = 4.0f*UNIT; - - if ( var == 0 ) - { -// center = pThis->RetPosition(0); - } - else - { - if ( var != 0 ) - { - rMin = var->GivValFloat()*UNIT; - var = var->GivNext(); - - if ( var != 0 ) - { - rMax = var->GivValFloat()*UNIT; - var = var->GivNext(); - - if ( var != 0 ) - { - dist = var->GivValFloat()*UNIT; - var = var->GivNext(); - } - } - } - } - - if ( result != 0 ) - { - pSub = result->GivItemList(); - if ( pSub != 0 ) - { - pSub->SetValFloat(1); - pSub = pSub->GivNext(); // "y" - pSub->SetValFloat(2); - pSub = pSub->GivNext(); // "z" -// pSub->SetValFloat(3); - } - } - return TRUE; -} -////////////////////////////////////////////////////////////// - - -void CTestCBotDoc::OnTest() -{ - CBotProgram::DefineNum("WingedGrabber", 1); - CBotProgram::DefineNum("TrackedGrabber", 2); - CBotProgram::DefineNum("WheeledGrabber", 3); - CBotProgram::DefineNum("LeggedGrabber", 4); - CBotProgram::DefineNum("WingedShooter", 5); - CBotProgram::DefineNum("TrackedShooter", 6); - CBotProgram::DefineNum("WheeledShooter", 7); - CBotProgram::DefineNum("LeggedShooter", 8); - CBotProgram::DefineNum("WingedOrgaShooter", 9); - CBotProgram::DefineNum("TrackedOrgaShooter", 10); - CBotProgram::DefineNum("WheeledOrgaShooter", 11); - CBotProgram::DefineNum("LeggedOrgaShooter", 12); - CBotProgram::DefineNum("WingedSniffer", 13); - CBotProgram::DefineNum("TrackedSniffer", 14); - CBotProgram::DefineNum("WheeledSniffer", 14); - CBotProgram::DefineNum("LeggedSniffer", 15); - CBotProgram::DefineNum("Thumper", 16); - CBotProgram::DefineNum("PhazerShooter", 17); - CBotProgram::DefineNum("Recycler", 18); - CBotProgram::DefineNum("Shielder", 19); - CBotProgram::DefineNum("Subber", 20); - CBotProgram::DefineNum("Me", 21); - - CBotProgram::DefineNum("TypeMarkPath", 111); - - OnFileSave(); - -// CPerformDlg dlg; -// dlg.m_Script = m_DocText; -// dlg.DoModal(); - - // dfini la routine RetObject - CBotProgram::AddFunction( "Radar", rRetObject, cRetObject ); - - // ajoute une routine pour cette classe - CBotProgram::AddFunction("Space", rSpace, cSpace); - - // dfini la routine Test - CBotProgram::AddFunction( "TEST", rTEST, cTEST ); - CBotProgram::AddFunction( "F", rF, cF ); - - CBotProgram::AddFunction( "goto", rMove, cMove ); - CBotProgram::AddFunction( "fire", rTurn, cTurn ); - CBotProgram::AddFunction( "radar", rRadar, cRadar ); - - // cre une instance de la classe "Bot" pour ce robot - CBotVar* pThisRobot = CBotVar::Create( "", CBotTypResult(CBotTypClass, "object") ); - pThisRobot->SetUserPtr( (void*)1 ); - pThisRobot->SetIdent( 1234 ); - - delete m_pProg; - // cre un objet programme associ cette instance - m_pProg = new CBotProgram(pThisRobot); - - // compile le programme - CString TextError; - int code, start, end; - - m_pEdit->GetWindowText(m_DocText); - if (!m_pProg->Compile(m_DocText, m_Liste, (void*) 44)) - { - m_pProg->GetError(code, start, end); - delete m_pProg; - m_pProg = NULL; - - delete pThisRobot; - - m_pEdit->SetSel( start, end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - TextError = CBotProgram::GivErrorText( code ); - AfxMessageBox( TextError ); - - m_pEdit->SetFocus(); - return; - } - - // excute pour voir - m_pProg->Start(m_Liste[0]); - - int mode = -1; - - if ( mode >= 0 ) { - - // sauve et restore chaque pas possible - while (!m_pProg->Run(NULL, 1)) - { - const char* FunctionName; - int start1, end1; - m_pProg->GetRunPos(FunctionName, start1, end1); - if ( end1 <= 0 ) - m_pProg->GetRunPos(FunctionName, start1, end1); - m_pEdit->SetSel(start1, end1); - -if ( mode == 0 ) continue; - - FILE* pf; - pf = fOpen( "TEST.CBO", "wb" ); - CBotClass::SaveStaticState(pf); - m_pProg->SaveState(pf); - fClose(pf); - -if ( mode == 2 ) if (!m_pProg->Compile(m_DocText, m_Liste, (void*) 44)) - { - m_pProg->GetError(code, start, end); - delete m_pProg; - m_pProg = NULL; - - delete pThisRobot; - - m_pEdit->SetSel( start, end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - TextError = CBotProgram::GivErrorText( code ); - AfxMessageBox( TextError ); - - m_pEdit->SetFocus(); - return; - } - - pf = fOpen( "TEST.CBO", "rb" ); - CBotClass::RestoreStaticState(pf); - m_pProg->RestoreState(pf); - fClose(pf); - - int start2, end2; - m_pProg->GetRunPos(FunctionName, start2, end2); - if ( end2 <= 0 ) - m_pProg->GetRunPos(FunctionName, start2, end2); - - if ( start1 != start2 || end1 != end2 ) - m_pProg->GetRunPos(FunctionName, start2, end2); - m_pEdit->SetSel(start2, end2); - } - - if (m_pProg->GetError(code, start, end)) - { - m_pEdit->SetSel(start, end); - TextError = CBotProgram::GivErrorText(code); - AfxMessageBox(TextError); - } - return;} - - while (!m_pProg->Run(NULL, 0)) - { - const char* FunctionName; - int start, end; - m_pProg->GetRunPos(FunctionName, start, end); - m_pEdit->SetSel(start, end); - - if ( FunctionName == NULL ) continue; - CString info (FunctionName); - CString sep (":\n"); - - int level = 0; - const char* Name; - while ( TRUE ) - { - CBotVar* pVar = m_pProg->GivStackVars(Name, level--); - if ( Name != FunctionName ) break; - if ( pVar == NULL ) continue; -// pVar->Maj(NULL, FALSE); - while ( pVar != NULL ) - { - info += sep; - info += pVar->GivName() + CBotString(" = ") + pVar->GivValString(); - sep = ", "; - pVar = pVar->GivNext(); - } - sep = "\n"; - } - if ( IDOK != AfxMessageBox(info, MB_OKCANCEL) ) break; - - if ( test == 1 ) - { - test = 0; - FILE* pf; - pf = fOpen( "TEST.CBO", "wb" ); - m_pProg->SaveState(pf); - fClose(pf); - } - - if ( test == 2 ) - { - test = 0; - FILE* pf; - pf = fOpen( "TEST.CBO", "rb" ); - m_pProg->RestoreState(pf); - fClose(pf); - } - - if ( test == 12 ) - { - test = 0; - FILE* pf; - pf = fOpen( "TEST.CBO", "wb" ); - m_pProg->SaveState(pf); - fClose(pf); - - pf = fOpen( "TEST.CBO", "rb" ); - m_pProg->RestoreState(pf); - fClose(pf); - - test = 13; - } - } - - if (m_pProg->GetError(code, start, end)) - { - m_pEdit->SetSel(start, end); - TextError = CBotProgram::GivErrorText(code); - AfxMessageBox(TextError); - } - - delete pThisRobot; -} - diff --git a/src/CBot/TestCBot/TestCBotDoc.h b/src/CBot/TestCBot/TestCBotDoc.h deleted file mode 100644 index 548607f..0000000 --- a/src/CBot/TestCBot/TestCBotDoc.h +++ /dev/null @@ -1,78 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBotDoc.h : interface of the CTestCBotDoc class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_TESTCBOTDOC_H__4D1BB90B_8E74_11D4_A439_00D059085115__INCLUDED_) -#define AFX_TESTCBOTDOC_H__4D1BB90B_8E74_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - - -class CTestCBotDoc : public CDocument -{ -protected: // create from serialization only - CTestCBotDoc(); - DECLARE_DYNCREATE(CTestCBotDoc) - -// Attributes -public: - CEdit* m_pEdit; // pour mmoriser le texte, et l'afficher - CBotProgram* m_pProg; // le programme compil - CString m_DocText; - CBotStringArray m_Liste; - BOOL m_bModified; - -// Operations -public: - BOOL Compile(); - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CTestCBotDoc) - public: - virtual BOOL OnNewDocument(); - virtual void Serialize(CArchive& ar); - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CTestCBotDoc(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -protected: - -// Generated message map functions -protected: - //{{AFX_MSG(CTestCBotDoc) - afx_msg void OnRun(); - afx_msg void OnChangeEdit1(); - afx_msg void OnTest(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_TESTCBOTDOC_H__4D1BB90B_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/TestCBotView.cpp b/src/CBot/TestCBot/TestCBotView.cpp deleted file mode 100644 index bca3c56..0000000 --- a/src/CBot/TestCBot/TestCBotView.cpp +++ /dev/null @@ -1,142 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// TestCBotView.cpp : implementation of the CTestCBotView class -// - -#include "stdafx.h" -#include "TestCBot.h" - -#include "TestCBotDoc.h" -#include "TestCBotView.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotView - -IMPLEMENT_DYNCREATE(CTestCBotView, CView) - -BEGIN_MESSAGE_MAP(CTestCBotView, CView) - //{{AFX_MSG_MAP(CTestCBotView) - ON_WM_SIZE() - ON_MESSAGE(WM_ACTWINDOW, ActWindow) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotView construction/destruction - -CTestCBotView::CTestCBotView() -{ -} - -CTestCBotView::~CTestCBotView() -{ -} - -BOOL CTestCBotView::PreCreateWindow(CREATESTRUCT& cs) -{ - return CView::PreCreateWindow(cs); -} - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotView drawing - -void CTestCBotView::OnDraw(CDC* pDC) -{ - CTestCBotDoc* pDoc = GetDocument(); - ASSERT_VALID(pDoc); -} - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotView diagnostics - -#ifdef _DEBUG -void CTestCBotView::AssertValid() const -{ - CView::AssertValid(); -} - -void CTestCBotView::Dump(CDumpContext& dc) const -{ - CView::Dump(dc); -} - -CTestCBotDoc* CTestCBotView::GetDocument() // non-debug version is inline -{ - ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestCBotDoc))); - return (CTestCBotDoc*)m_pDocument; -} -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CTestCBotView message handlers - -void CTestCBotView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) -{ - CTestCBotDoc* pDoc = GetDocument(); -// CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); - - if ( pDoc->m_pEdit == NULL) - { - pDoc->m_pEdit = new CEdit(); - CRect rect; - GetClientRect( rect ); - - pDoc->m_pEdit->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_NOHIDESEL|ES_AUTOVSCROLL, - rect, this, IDC_EDIT1 ); - pDoc->m_pEdit->SetTabStops(12); - pDoc->m_pEdit->SetWindowText(pDoc->m_DocText); - } - - if ( !bActivate && !pDoc->Compile() ) - { -// comment faire pour ractiver l'ancien document - } - - CView::OnActivateView(bActivate, pActivateView, pDeactiveView); - - if ( bActivate ) pDoc->m_pEdit->SetFocus(); -} - - -void CTestCBotView::OnSize(UINT nType, int cx, int cy) -{ - CView::OnSize(nType, cx, cy); - - CTestCBotDoc* pDoc = GetDocument(); - if ( pDoc->m_pEdit != NULL ) - { - CRect rect; - GetClientRect( rect ); - pDoc->m_pEdit->MoveWindow( rect ); - pDoc->m_pEdit->SetFocus(); - } -} - - - -LONG CTestCBotView::ActWindow(UINT wparam, LONG lparam) -{ -// GetParentFrame()->SetActiveView( this, TRUE ); -// CMDIChildWnd::OnMDIActivate(1, this, this) - return 0; -} diff --git a/src/CBot/TestCBot/TestCBotView.h b/src/CBot/TestCBot/TestCBotView.h deleted file mode 100644 index 065ee08..0000000 --- a/src/CBot/TestCBot/TestCBotView.h +++ /dev/null @@ -1,78 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBotView.h : interface of the CTestCBotView class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_TESTCBOTVIEW_H__4D1BB90D_8E74_11D4_A439_00D059085115__INCLUDED_) -#define AFX_TESTCBOTVIEW_H__4D1BB90D_8E74_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -class CTestCBotView : public CView -{ -protected: // create from serialization only - CTestCBotView(); - DECLARE_DYNCREATE(CTestCBotView) - -// Attributes -public: - CTestCBotDoc* GetDocument(); - -// Operations -public: - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CTestCBotView) - public: - virtual void OnDraw(CDC* pDC); // overridden to draw this view - virtual BOOL PreCreateWindow(CREATESTRUCT& cs); - protected: - virtual void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView); - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CTestCBotView(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -protected: - -// Generated message map functions -protected: - //{{AFX_MSG(CTestCBotView) - afx_msg void OnSize(UINT nType, int cx, int cy); - afx_msg LONG ActWindow(UINT wparam, LONG lparam) ; - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -#ifndef _DEBUG // debug version in TestCBotView.cpp -inline CTestCBotDoc* CTestCBotView::GetDocument() - { return (CTestCBotDoc*)m_pDocument; } -#endif - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_TESTCBOTVIEW_H__4D1BB90D_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/TestCBot/TestNull.txt b/src/CBot/TestCBot/TestNull.txt deleted file mode 100644 index f447245..0000000 --- a/src/CBot/TestCBot/TestNull.txt +++ /dev/null @@ -1,15 +0,0 @@ -extern public void TestNull () -{ - CPoint pointeur = null; - - try { - pointeur.x = 4; } - catch ( 6007 ) {} - - pointeur = new CPoint(1,2); - - print ( pointeur.x, pointeur.y, - pointeur ); - - pointeur.x = 5; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/TestRestoreState.txt b/src/CBot/TestCBot/TestRestoreState.txt deleted file mode 100644 index 1e49e37..0000000 --- a/src/CBot/TestCBot/TestRestoreState.txt +++ /dev/null @@ -1,67 +0,0 @@ -// routine de Daniel qui plante aprs RestoreState - -extern void object::Attack( ) -{ - int list[], i; - object p; - float dist, prox; - point nav1, nav2, dest; - boolean advance = true; - - i = 0; - list[i++] = WingedGrabber; - list[i++] = TrackedGrabber; - list[i++] = WheeledGrabber; - list[i++] = LeggedGrabber; - list[i++] = WingedShooter; - list[i++] = TrackedShooter; - list[i++] = WheeledShooter; - list[i++] = LeggedShooter; - list[i++] = WingedOrgaShooter; - list[i++] = TrackedOrgaShooter; - list[i++] = WheeledOrgaShooter; - list[i++] = LeggedOrgaShooter; - list[i++] = WingedSniffer; - list[i++] = TrackedSniffer; - list[i++] = WheeledSniffer; - list[i++] = LeggedSniffer; - list[i++] = Thumper; - list[i++] = PhazerShooter; - list[i++] = Recycler; - list[i++] = Shielder; - list[i++] = Subber; - list[i++] = Me; - - nav1.x = 1;//cmdline(0); - nav1.y = 1;//cmdline(1); - nav2.x = 2;//cmdline(2); - nav2.y = 2;//cmdline(3); - - while ( true ) - { - while ( true ) - { - // ennemi proximit ? - p = radar(list, 0, 360, 0, 40); - if ( p == null ) break; - // lui tire dessus - fire(p.position); - } - - // se promne vers le point A - goto(nav1); - - while ( true ) - { - // ennemi proximit ? - p = radar(list, 0, 360, 0, 40); - if ( p == null ) break; - // lui tire dessus - fire(p.position); - } - - // se promne vers le point B - goto(nav2); - } -} - diff --git a/src/CBot/TestCBot/TestStatic.txt b/src/CBot/TestCBot/TestStatic.txt deleted file mode 100644 index f501aa5..0000000 --- a/src/CBot/TestCBot/TestStatic.txt +++ /dev/null @@ -1,31 +0,0 @@ -class ESSAI -{ - int x = 0; - static int nb = 3; - static int [ ] array ; - - void Put( int val) - { -show(nb); - array[ nb ] = val; -// this.nb++; - this.nb = this.nb + 1; -show(nb, array); - } - int Get( ) - { - nb--; -show("out", nb, array); - return array[ nb ] ; - } -} - -extern public void T() -{ - ESSAI t1 ( ) ; - ESSAI t2 ( ) ; - t1.nb++; - t1.Put( 11 ); t1.Put( 12 ); t2.Put( 13 ); - - show ( t1.Get(), t2.Get(), t2.Get() ) ; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/TestStr.txt b/src/CBot/TestCBot/TestStr.txt deleted file mode 100644 index 683ec1b..0000000 --- a/src/CBot/TestCBot/TestStr.txt +++ /dev/null @@ -1,17 +0,0 @@ -extern public void TSTR() -{ - string s = "C'est un essai"; - - print ( s, strlen(s), strleft(s, 3), strright(s,3), strmid(s, 2), strmid(s,2,3), strfind(s, "un"), strfind(s, "sdgfld") ); - - show ( strupper(s), strlower(s) ); - - s = "123.45" ; - print ( strval(s) ); - - - string sub = strright("abcdef", 2); // sub vaut "ef###", # tant un caractre bizarre quelconque - show (sub); - int pos = strfind("abcdef", "xy"); // pos vaut -1. Pourquoi pas nan ? - show(pos); -} diff --git a/src/CBot/TestCBot/Z.txt b/src/CBot/TestCBot/Z.txt deleted file mode 100644 index 714119b..0000000 --- a/src/CBot/TestCBot/Z.txt +++ /dev/null @@ -1,14 +0,0 @@ -public extern void tp() -{ - int a [4], b[]; - a [ 0 ] = 8 ; - - b = T ( a ) ; - show ( a, b ); -} - -int[] T ( int[] Z ) -{ - for ( int i = 0; i < 4 ; i++ ) Z[ i ] = i * i ; - return Z; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/array.txt b/src/CBot/TestCBot/array.txt deleted file mode 100644 index 081b60e..0000000 --- a/src/CBot/TestCBot/array.txt +++ /dev/null @@ -1,24 +0,0 @@ - -public extern void TestTableau () -{ - int tableau [ 12 ] ; - - point array[ 12 ] [ 14 ] ; - - point zro ( 1, 2 ) ; - point a = zro ; - - for ( int i = 0 ; i < 10 ; i++ ) array[ i ] [ i ]= zro ; - - array[ 5 ] [3 ] . x =1.5 ; - - array[ 2 ] [ 2 ] . y = array[ 5 ] [ 5 ] . x ; - - array[ 4 ] = array [ 2 ] ; - - for ( int i = 0 ; i < 10 ; i++ ) for ( int j = 0 ; j < 4 ; j++ ) println ( i, j, array [ i ] [ j ] ) ; - - show( zro, a, array ); - -} - diff --git "a/src/CBot/TestCBot/a\2361.txt" "b/src/CBot/TestCBot/a\2361.txt" deleted file mode 100644 index 165bc95..0000000 --- "a/src/CBot/TestCBot/a\2361.txt" +++ /dev/null @@ -1,96 +0,0 @@ -object radarGuepe(point orig, float dist) -{ - int i; - object pr, r; - float mindist; - - i = 0; - mindist = 1000; - while (i<30) - { - pr = radar(i); - if (pr != null) - { - - if (F(orig, pr.position) < mindist and pr.category == AlienWasp and pr.altitude > 3) - { - mindist = distance(orig, pr.position); - r = pr; - } - } - i = i+1; - } - if (mindist < dist) return(r); else return(null); -} - - -class Guepe -{ - - point pos; - - - void cherche(point orig, float dist) - { - object p; - point o; - - p = radarGuepe(orig, dist); - while (p == null) - { - wait(0.1); - p = radarGuepe(orig, dist); - } - - pos.x = p.position.x; - pos.y = p.position.y; - pos.z = p.position.z; - - //o = p.position; - //wait(0.1); - - //vitessex = (p.position.x - o.x)/0.1; - //vitessey = (p.position.y - o.y)/0.1; - //vitessez = (p.position.z - o.z)/0.1; - - } - - - void tire(point orig, float orient) - { - //float t = 3; //temps d'anticipation - float angle; - point cible; - - cible.x = pos.x;// + t*vitessex; - cible.y = pos.y;// + t*vitessey; - cible.z = pos.z;// + t*vitessez; - - if (cible.x == 0) angle = 90; else - angle = atan(cible.y / cible.x); - if (cible.x < 0) angle = angle + 180; - angle = angle - orient; - if (angle > 180) angle = angle - 360; - if (angle < -180) angle = angle + 360; - turn(angle); - - angle = atan((cible.z-orig.z) / distance2d(orig, cible)); - aim(angle); - - fire(0.1); - - } -} - -extern void object::Fourmi6() -{ - //fps(1000); - Guepe guepe = new Guepe(); - - while (true) - { - guepe.cherche(position, 50); - - guepe.tire(position, orientation); - } -} diff --git "a/src/CBot/TestCBot/a\2471.txt" "b/src/CBot/TestCBot/a\2471.txt" deleted file mode 100644 index 0c57950..0000000 --- "a/src/CBot/TestCBot/a\2471.txt" +++ /dev/null @@ -1,96 +0,0 @@ -object radarGuepe(point orig, float dist) -{ - int i; - object pr, r; - float mindist; - - i = 0; - mindist = 1000; - while (i<30) - { - pr = radar(i); - if (pr != null) - { - - if (F(orig, pr.position) < mindist and pr.category == AlienWasp and pr.altitude > 3) - { - mindist = distance(orig, pr.position); - r = pr; - } - } - i = i+1; - } - if (mindist < dist) return(r); else return(null); -} - - -class Guepe -{ - - point pos; - - - void cherche(point orig, float dist) - { - object p; - point o; - - p = radarGuepe(orig, dist); - while (p == null) - { - wait(0.1); - p = radarGuepe(orig, dist); - } - - pos.x = p.position.x; - pos.y = p.position.y; - pos.z = p.position.z; - - //o = p.position; - //wait(0.1); - - //vitessex = (p.position.x - o.x)/0.1; - //vitessey = (p.position.y - o.y)/0.1; - //vitessez = (p.position.z - o.z)/0.1; - - } - - - void tire(point orig, float orient) - { - //float t = 3; //temps d'anticipation - float angle; - point cible; - - cible.x = pos.x;// + t*vitessex; - cible.y = pos.y;// + t*vitessey; - cible.z = pos.z;// + t*vitessez; - - if (cible.x == 0) angle = 90; else - angle = atan(cible.y / cible.x); - if (cible.x < 0) angle = angle + 180; - angle = angle - orient; - if (angle > 180) angle = angle - 360; - if (angle < -180) angle = angle + 360; - turn(angle); - - angle = atan((cible.z-orig.z) / distance2d(orig, cible)); - aim(angle); - - fire(0.1); - - } -} - -extern void object::Fourmi6() -{ - //fps(1000); - Guepe guepe = new Guepe(); - - while (true) - { - guepe.cherche(position, 50); - - guepe.tire(position, orientation); - } -} diff --git a/src/CBot/TestCBot/bug.txt b/src/CBot/TestCBot/bug.txt deleted file mode 100644 index 4ec6eb3..0000000 --- a/src/CBot/TestCBot/bug.txt +++ /dev/null @@ -1,12 +0,0 @@ -public extern void object::Bug() -{ - point a; - a = position; - TEST(); - float d=dist(a, position); -} - -float dist(point a, point b) -{ - return a.x-b.x; -} diff --git a/src/CBot/TestCBot/bugmw.txt b/src/CBot/TestCBot/bugmw.txt deleted file mode 100644 index 284ee43..0000000 --- a/src/CBot/TestCBot/bugmw.txt +++ /dev/null @@ -1,9 +0,0 @@ -extern public void main() -{ - show(fact(30)) ; -} - -public int fact(int n) -{ - return (fact(n-1)*n) ; -} diff --git a/src/CBot/TestCBot/ccc.txt b/src/CBot/TestCBot/ccc.txt deleted file mode 100644 index dbcd1d5..0000000 --- a/src/CBot/TestCBot/ccc.txt +++ /dev/null @@ -1,8 +0,0 @@ -public extern void ccc() -{ - int a; - a = 0 ; - - if ( a == 0 ); - -} \ No newline at end of file diff --git a/src/CBot/TestCBot/enum.txt b/src/CBot/TestCBot/enum.txt deleted file mode 100644 index a592a7f..0000000 --- a/src/CBot/TestCBot/enum.txt +++ /dev/null @@ -1,9 +0,0 @@ - -enum JourDeLaSemaine { - lundi = 1, - mardi, - mercredi, - jeudi, - vendredi, - samedi, - dimanche = 0 } \ No newline at end of file diff --git a/src/CBot/TestCBot/fibo.txt b/src/CBot/TestCBot/fibo.txt deleted file mode 100644 index 88f5357..0000000 --- a/src/CBot/TestCBot/fibo.txt +++ /dev/null @@ -1,25 +0,0 @@ - -extern public int Fibo( int n, boolean b ) -{ - if ( n < 2 ) return n; - int a = Fibo(n-1, b) + Fibo(n-2, false); - if ( b ) print (n + "=" + a); - return a; -} - -extern public void t() -{ - Fibo( 23, true); -} - -extern public void tt() -{ - t(); -} - -// cette routine n'est videmment pas du tout obtimise -// c'est mme un trs mauvais exemple de programmation rcursive - -// pour un test de dure, Fibo(23, true) prend -// en mode Debug 67 secondes -// en mode Release 8 secondes diff --git a/src/CBot/TestCBot/file.txt b/src/CBot/TestCBot/file.txt deleted file mode 100644 index 2a22dd9..0000000 --- a/src/CBot/TestCBot/file.txt +++ /dev/null @@ -1,70 +0,0 @@ -class CLASS22 -{ - static int nb = 2; - void T22 ( ) { nb = nb / 0 ; } -} - -public extern void object :: TEST() -{ - switch ( 1 ) - { - case 1: - { - file h(); - h.open("Mon Fichier.txt", "r"); -show ( h.filename, h.handle ); -h.filename = "xx"; -h.handle = 1 ; - h.readln(); - h.close(); - } - case 2: - { - file h("Mon Fichier.txt"); - h.open("r"); - h.readln(); - h.close(); - } - case 3: - { - file h("Mon Fichier.txt", "r"); - h.readln(); - h.close(); - } - case 4: - { - file h(); - h.filename = "Mon Fichier.txt"; - h.open("r"); - h.readln(); - h.close(); - } - case 5: - { - file h = fileopen( "Mon 2Fichier.txt", "r" ); - h.readln(); - h.close(); - } - } -{ - file h( ) ; - h.filename = "Test.h"; - h.open ( "r" ); - - - file pf ( "Mon Fichier.txt" ) ; - pf . open ( "w" ) ; - pf . writeln ( "Voici encore du texte" ) ; - pf . writeln ( "et une seconde ligne" ) ; - pf . close( ); - - pf . open ( "r" ) ; - - while ( not pf . eof( ) ) - { - string s = pf . readln ( ); - show ( s ); - } - pf.close( ); -} -} diff --git a/src/CBot/TestCBot/h.txt b/src/CBot/TestCBot/h.txt deleted file mode 100644 index c395319..0000000 --- a/src/CBot/TestCBot/h.txt +++ /dev/null @@ -1,5 +0,0 @@ -void tf() -{ - file h; - h.handle += 1 ; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/include.txt b/src/CBot/TestCBot/include.txt deleted file mode 100644 index e8f8cc9..0000000 --- a/src/CBot/TestCBot/include.txt +++ /dev/null @@ -1,27 +0,0 @@ -class Z -{ - static int x = 0; - private int y; - - void T( ) - { - // autoris ici - y = x ; - this.y = this.x ; - x = y ; - this.x = this.y ; - } -} - -extern public void test() -{ - Z a(); - 3 * a.x; // autoris -//vu 3 * a.y; // interdit -//vu a.y = 3; // interdit ici - a.x = 1; // autoris - - show ( a ); - a.T(); - show ( a ); -} diff --git a/src/CBot/TestCBot/intrinsic.txt b/src/CBot/TestCBot/intrinsic.txt deleted file mode 100644 index f215791..0000000 --- a/src/CBot/TestCBot/intrinsic.txt +++ /dev/null @@ -1,16 +0,0 @@ -public extern void TestIntrinsic() -{ - point a ( 1, 2 ); - print (a); - - a.x = 3; - a.y = 4; - - point b = a; - - println ( b.x, b.y, b ) ; - if ( b == a ) b.y = 0; - println (a,b); - if ( b != a ) b.y = a.y; - println(a,b); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/methode1.txt b/src/CBot/TestCBot/methode1.txt deleted file mode 100644 index 080bba2..0000000 --- a/src/CBot/TestCBot/methode1.txt +++ /dev/null @@ -1,57 +0,0 @@ -class t { - point p; -} - -void object :: toto() -{ - show ( Position ) ; -} - -extern public void object :: XX() -{ - int test []; - test [ 9999 ] = 3; - - toto () ; -/* - Radar(); - - object test ; - test = this. Radar(); - - do { - test = this.Radar(); - } while ( test == null ); - -/* - t test [ 4 ]; - for ( int i = 0 ; i < 4 ; i++ ) test [ i ] = new t(); - test [ 3 ] .p.x = 2; - show ( test ); -/* - int a = nan; - show ( a ) ; - - a = TypeMarkPath; - show ( a, a++, --a ) ; - - if ( a != nan ) a += 1 ; - - a = TypeMarkPath; - float q = a ; - show ( a, q ) ; - -return; - - a += ++a; - show ( a ) ; - - boolean i = false; - - if ( i == true ) {} - - object p; - if ( p == null) { p = p ; } -*/ -} - diff --git a/src/CBot/TestCBot/methode2.txt b/src/CBot/TestCBot/methode2.txt deleted file mode 100644 index 76ce7f4..0000000 --- a/src/CBot/TestCBot/methode2.txt +++ /dev/null @@ -1,50 +0,0 @@ - -extern void Toto() -{ - TEST(12); - - for ( int i = 0 ; i<1000; i++) - { - int j = 1; - if (i==55) TEST(12); - } - - TEST(2); - - -// Nouveau(); - int toto[4]; - point Z[3]; - - Z[1].x = 11; Z[1].y = 12; - - toto[2] = 12; - toto[1] = nan; - -// point test, autre(2,3) ; -// object titi = Radar(); - - TEST ( 1 ) ; - - toto[0] = 11; - - TEST ( 2 ) ; - - toto[6] = 0; -} - -extern void object::Nouveau() -{ - point a; - a = np(Position); -} - -point np(point b) -{ - point c; - c.x = b.y; - c.y = b.x; - return c ; -} - - diff --git a/src/CBot/TestCBot/mp1.txt b/src/CBot/TestCBot/mp1.txt deleted file mode 100644 index 599cfc4..0000000 --- a/src/CBot/TestCBot/mp1.txt +++ /dev/null @@ -1,25 +0,0 @@ -class Guepet -{ - - float a; - float b; - - void init() - { - a = 12.34; - b = 56.78; - } - - -} - -extern void object::Fourmi6() -{ - Guepet guepe =new Guepet(); - - guepe.init(); - - - show("test "+guepe.a+" "+guepe.b); - -} diff --git a/src/CBot/TestCBot/mp2.txt b/src/CBot/TestCBot/mp2.txt deleted file mode 100644 index 1c2972c..0000000 --- a/src/CBot/TestCBot/mp2.txt +++ /dev/null @@ -1,28 +0,0 @@ -class Guepet -{ - - float a; - float b; - - void init() - { - a = 12.34; - b = 56.78; - - object x = radar(123); - show("radar "+x.position.x); - show("C'est fait"); - } - - -} - -extern void object::Fourmi6() -{ - Guepet guepe=new Guepet(); - - guepe.init(); - - show("test "+guepe.a+" "+guepe.b); - -} diff --git a/src/CBot/TestCBot/mw.txt b/src/CBot/TestCBot/mw.txt deleted file mode 100644 index c237670..0000000 --- a/src/CBot/TestCBot/mw.txt +++ /dev/null @@ -1,16 +0,0 @@ -extern public void main() -{ -// goto( 3, 4 ); - - while( true ) - { - try { goto (12) ; } - catch( FF( ) ) - { show( "ko"); } - } -} - -boolean FF() -{ - return false; -} diff --git a/src/CBot/TestCBot/null.txt b/src/CBot/TestCBot/null.txt deleted file mode 100644 index ae76b74..0000000 --- a/src/CBot/TestCBot/null.txt +++ /dev/null @@ -1,5 +0,0 @@ -extern public void xxx () -{ - CPoint test = null ; - if ( test == null ) show ( "NULL" ); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/opnew.txt b/src/CBot/TestCBot/opnew.txt deleted file mode 100644 index 7d6838c..0000000 --- a/src/CBot/TestCBot/opnew.txt +++ /dev/null @@ -1,20 +0,0 @@ -extern public void xx () -{ - CPoint pointeur, test = null ; - pointeur = new CPoint ( 3, 4 ); - - if ( test == null ) show ( "NULL" ); - - CPoint pp = pointeur; - -show( pointeur , pp ); - - pp.x = 33.3; - if ( pointeur.x != pp.x ) 0/0; - - pp = new CPoint(); -// pointeur = pp; - -show( pointeur , pp ); - -} \ No newline at end of file diff --git a/src/CBot/TestCBot/plante.txt b/src/CBot/TestCBot/plante.txt deleted file mode 100644 index 363461b..0000000 --- a/src/CBot/TestCBot/plante.txt +++ /dev/null @@ -1,25 +0,0 @@ -class Guepet -{ - - point pos; - float t = 0.1; - - void init() - { - pos.x = 12.123; - pos.y = 34.345; - - F(t); - } - - -} - -extern void object::Fourmi6() -{ - Guepet guepe=new Guepet(); - - guepe.init(); - - show ( guepe ); -} diff --git a/src/CBot/TestCBot/pointer.txt b/src/CBot/TestCBot/pointer.txt deleted file mode 100644 index 2d4d907..0000000 --- a/src/CBot/TestCBot/pointer.txt +++ /dev/null @@ -1,41 +0,0 @@ -extern public void x () -{ - show ( 3 ** 4 ); - float z = 1e-3; - show ( z ); - - CPoint b ( 4,5 ); - show ( b ); - - CPoint a ( ) ; - a.x = 21; a.y = 12; - show ( a ) ; - - CPoint test = new CPoint ( 1,1 ); - test = new CPoint ( 2, 2 ); - show ( test ); -} - -// cre un objet et retourne son pointeur -CPoint newcpoint() -{ - CPoint p = new CPoint ( 3, 3 ); - return p; -} - -extern public void y () -{ - CPoint test = newcpoint(); - println ( test ); - dontmodif( test ); - println ( test ); -} - -// ne doit pas modifier l'objet en paramtre -void dontmodif ( CPoint pp ) -{ - pp.x = 5; - pp.y = 2; - println ( pp, pp.x, pp.y ); -} - diff --git a/src/CBot/TestCBot/postinc.txt b/src/CBot/TestCBot/postinc.txt deleted file mode 100644 index cdf6ab5..0000000 --- a/src/CBot/TestCBot/postinc.txt +++ /dev/null @@ -1,7 +0,0 @@ -extern public void X() -{ - point A [ ] ; - A[5] = new point (2,3); - int val = A[5].x++ + --A[5].y; - show ( A, val ); -} diff --git a/src/CBot/TestCBot/radar.txt b/src/CBot/TestCBot/radar.txt deleted file mode 100644 index 09d84a2..0000000 --- a/src/CBot/TestCBot/radar.txt +++ /dev/null @@ -1,39 +0,0 @@ -extern void object::Bug( ) -{ - try{ int a = 44 ; a = 12 / 0 ; } - catch(6000) { int b = 4 ; } - finally { int z = 1 ; } - -// tp ( A, B ); - -/* int a = 4, b = 2, c = nan; - float x, y = 3/2, z = nan; - boolean i, j = false, k = true; - - string s, ss = "xyz"; - - while ( false ) - { - object left, right; - - left = Radar(TypeMarkPath, -45, 120, 100); - right = Radar(TypeMarkPath, 45, 120, 100); - - if ( left == null && right == null ) - { - } - } - int t = fact ( 4 ) ;*/ -} - -void tp( point a , point b ) -{ - a.x += b.x; -} - - -int fact( int n ) -{ - if ( n < 2 ) return n; - return n * fact ( n - 1 ) ; -} \ No newline at end of file diff --git a/src/CBot/TestCBot/res/TestCBot.ico b/src/CBot/TestCBot/res/TestCBot.ico deleted file mode 100644 index 06a649d..0000000 Binary files a/src/CBot/TestCBot/res/TestCBot.ico and /dev/null differ diff --git a/src/CBot/TestCBot/res/TestCBot.rc2 b/src/CBot/TestCBot/res/TestCBot.rc2 deleted file mode 100644 index b55f0d9..0000000 --- a/src/CBot/TestCBot/res/TestCBot.rc2 +++ /dev/null @@ -1,13 +0,0 @@ -// -// TESTCBOT.RC2 - resources Microsoft Visual C++ does not edit directly -// - -#ifdef APSTUDIO_INVOKED - #error this file is not editable by Microsoft Visual C++ -#endif //APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// Add manually edited resources here... - -///////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/TestCBot/res/TestCBotDoc.ico b/src/CBot/TestCBot/res/TestCBotDoc.ico deleted file mode 100644 index 3545614..0000000 Binary files a/src/CBot/TestCBot/res/TestCBotDoc.ico and /dev/null differ diff --git a/src/CBot/TestCBot/res/Toolbar.bmp b/src/CBot/TestCBot/res/Toolbar.bmp deleted file mode 100644 index 04a71af..0000000 Binary files a/src/CBot/TestCBot/res/Toolbar.bmp and /dev/null differ diff --git a/src/CBot/TestCBot/resource.h b/src/CBot/TestCBot/resource.h deleted file mode 100644 index d661201..0000000 --- a/src/CBot/TestCBot/resource.h +++ /dev/null @@ -1,44 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/.//{{NO_DEPENDENCIES}} -// Microsoft Developer Studio generated include file. -// Used by TestCBot.rc -// -#define IDD_ABOUTBOX 100 -#define IDR_MAINFRAME 128 -#define IDR_TESTCBTYPE 129 -#define IDD_DIALOG1 130 -#define IDD_CONSOLE 131 -#define IDD_DIALOG2 133 -#define IDC_EDIT1 1000 -#define TX_TYPENAMES 1000 -#define IDC_SPIN1 1001 -#define IDC_EDIT2 1002 -#define IDC_COMBO1 1003 -#define IDC_EDIT3 1004 -#define ID_RUN 32771 -#define ID_TEST 32772 - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_3D_CONTROLS 1 -#define _APS_NEXT_RESOURCE_VALUE 135 -#define _APS_NEXT_COMMAND_VALUE 32773 -#define _APS_NEXT_CONTROL_VALUE 1004 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif diff --git a/src/CBot/TestCBot/solution.txt b/src/CBot/TestCBot/solution.txt deleted file mode 100644 index f78cf12..0000000 --- a/src/CBot/TestCBot/solution.txt +++ /dev/null @@ -1,13 +0,0 @@ -extern void object::Solution( ) -{ -show ( "Solution " + Position ); - Carr(15); - Carr(25); -} - -void object::Carr(float ct) -{ -show ( "Carr " + Position ); - Move(ct); - Turn(-90); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/test.txt b/src/CBot/TestCBot/test.txt deleted file mode 100644 index a912415..0000000 --- a/src/CBot/TestCBot/test.txt +++ /dev/null @@ -1,8 +0,0 @@ -extern public void x() -{ - float a= 1, b = 2; - a = b * ( 2 + 2 ); -// print (a); - a += 4; -// print (a); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/test23.txt b/src/CBot/TestCBot/test23.txt deleted file mode 100644 index d6e1ddd..0000000 --- a/src/CBot/TestCBot/test23.txt +++ /dev/null @@ -1,10 +0,0 @@ -extern public void object::TEST23() -{ - CLASS22 T; - T.T22( ) ; - - show( position ); - show( this.position ); - -// T22(); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/testmw.txt b/src/CBot/TestCBot/testmw.txt deleted file mode 100644 index 6570f6d..0000000 --- a/src/CBot/TestCBot/testmw.txt +++ /dev/null @@ -1,14 +0,0 @@ -extern public int testmw( int a) -{ - boolean b = true ; - - if (b) - return 1 ; - else - return a ; 0 * testmw(a-1) ; -} - -public int Fibo2 ( int n ) -{ - print ( " bof " ); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/this.txt b/src/CBot/TestCBot/this.txt deleted file mode 100644 index b8a9e04..0000000 --- a/src/CBot/TestCBot/this.txt +++ /dev/null @@ -1,13 +0,0 @@ -extern void object :: TEST22 ( ) -{ - show( position ); - show( this.position ); - - T(); -} - -public void object :: T22() -{ - show( position ); - show( this.position ); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/tt.txt b/src/CBot/TestCBot/tt.txt deleted file mode 100644 index cd13c9d..0000000 --- a/src/CBot/TestCBot/tt.txt +++ /dev/null @@ -1,12 +0,0 @@ -extern public void T() { T1(); } - -public void T1() -{ - show( "T1" ); - T2(); -} - -public void T2() -{ - show( "T2" ); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/tt2.txt b/src/CBot/TestCBot/tt2.txt deleted file mode 100644 index ad9dc1d..0000000 --- a/src/CBot/TestCBot/tt2.txt +++ /dev/null @@ -1,5 +0,0 @@ -extern public void TT() -{ - T1(); - T2(); -} \ No newline at end of file diff --git a/src/CBot/TestCBot/vide.txt b/src/CBot/TestCBot/vide.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/CBot/TestCBot/xTestCBot.clw b/src/CBot/TestCBot/xTestCBot.clw deleted file mode 100644 index 5b84c16..0000000 --- a/src/CBot/TestCBot/xTestCBot.clw +++ /dev/null @@ -1,245 +0,0 @@ -; CLW file contains information for the MFC ClassWizard - -[General Info] -Version=1 -LastClass=CBotConsoleDlg -LastTemplate=CDialog -NewFileInclude1=#include "stdafx.h" -NewFileInclude2=#include "TestCBot.h" -LastPage=0 - -ClassCount=7 -Class1=CTestCBotApp -Class2=CTestCBotDoc -Class3=CTestCBotView -Class4=CMainFrame - -ResourceCount=7 -Resource1=IDD_ABOUTBOX -Resource2=IDR_MAINFRAME -Resource3=IDR_TESTCBTYPE -Class5=CAboutDlg -Class6=CChildFrame -Resource4=IDD_ABOUTBOX (French (France)) -Resource5=IDR_TESTCBTYPE (French (France)) -Resource6=IDD_CONSOLE -Class7=CBotConsoleDlg -Resource7=IDR_MAINFRAME (French (France)) - -[CLS:CTestCBotApp] -Type=0 -HeaderFile=TestCBot.h -ImplementationFile=TestCBot.cpp -Filter=N - -[CLS:CTestCBotDoc] -Type=0 -HeaderFile=TestCBotDoc.h -ImplementationFile=TestCBotDoc.cpp -Filter=N -BaseClass=CDocument -VirtualFilter=DC -LastObject=IDC_EDIT2 - -[CLS:CTestCBotView] -Type=0 -HeaderFile=TestCBotView.h -ImplementationFile=TestCBotView.cpp -Filter=C -BaseClass=CView -VirtualFilter=VWC -LastObject=CTestCBotView - -[CLS:CMainFrame] -Type=0 -HeaderFile=MainFrm.h -ImplementationFile=MainFrm.cpp -Filter=T -BaseClass=CMDIFrameWnd -VirtualFilter=fWC -LastObject=CMainFrame - - -[CLS:CChildFrame] -Type=0 -HeaderFile=ChildFrm.h -ImplementationFile=ChildFrm.cpp -Filter=M - -[CLS:CAboutDlg] -Type=0 -HeaderFile=TestCBot.cpp -ImplementationFile=TestCBot.cpp -Filter=D - -[DLG:IDD_ABOUTBOX] -Type=1 -ControlCount=4 -Control1=IDC_STATIC,static,1342177283 -Control2=IDC_STATIC,static,1342308352 -Control3=IDC_STATIC,static,1342308352 -Control4=IDOK,button,1342373889 -Class=CAboutDlg - -[MNU:IDR_MAINFRAME] -Type=1 -Class=CMainFrame -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command4=ID_APP_EXIT -Command5=ID_VIEW_TOOLBAR -Command6=ID_VIEW_STATUS_BAR -Command7=ID_APP_ABOUT -CommandCount=7 -Command3=ID_FILE_MRU_FILE1 - -[TB:IDR_MAINFRAME] -Type=1 -Class=CMainFrame -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command4=ID_EDIT_CUT -Command5=ID_EDIT_COPY -Command6=ID_EDIT_PASTE -Command7=ID_FILE_PRINT -CommandCount=8 -Command8=ID_APP_ABOUT - -[MNU:IDR_TESTCBTYPE] -Type=1 -Class=CTestCBotView -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_CLOSE -Command4=ID_FILE_SAVE -Command5=ID_FILE_SAVE_AS -Command9=ID_EDIT_CUT -Command10=ID_EDIT_COPY -Command11=ID_EDIT_PASTE -Command12=ID_VIEW_TOOLBAR -Command13=ID_VIEW_STATUS_BAR -Command14=ID_WINDOW_NEW -CommandCount=18 -Command6=ID_FILE_MRU_FILE1 -Command7=ID_APP_EXIT -Command8=ID_EDIT_UNDO -Command15=ID_WINDOW_CASCADE -Command16=ID_WINDOW_TILE_HORZ -Command17=ID_WINDOW_ARRANGE -Command18=ID_APP_ABOUT - -[ACL:IDR_MAINFRAME] -Type=1 -Class=CMainFrame -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command5=ID_EDIT_CUT -Command6=ID_EDIT_COPY -Command7=ID_EDIT_PASTE -Command8=ID_EDIT_UNDO -Command9=ID_EDIT_CUT -Command10=ID_EDIT_COPY -Command11=ID_EDIT_PASTE -Command12=ID_NEXT_PANE -CommandCount=13 -Command4=ID_EDIT_UNDO -Command13=ID_PREV_PANE - - -[TB:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command4=ID_EDIT_CUT -Command5=ID_EDIT_COPY -Command6=ID_EDIT_PASTE -Command7=ID_FILE_PRINT -Command8=ID_RUN -Command9=ID_APP_ABOUT -CommandCount=9 - -[MNU:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_MRU_FILE1 -Command4=ID_APP_EXIT -Command5=ID_VIEW_TOOLBAR -Command6=ID_VIEW_STATUS_BAR -Command7=ID_APP_ABOUT -CommandCount=7 - -[MNU:IDR_TESTCBTYPE (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_CLOSE -Command4=ID_FILE_SAVE -Command5=ID_FILE_SAVE_AS -Command6=ID_FILE_MRU_FILE1 -Command7=ID_APP_EXIT -Command8=ID_EDIT_UNDO -Command9=ID_EDIT_CUT -Command10=ID_EDIT_COPY -Command11=ID_EDIT_PASTE -Command12=ID_VIEW_TOOLBAR -Command13=ID_VIEW_STATUS_BAR -Command14=ID_WINDOW_NEW -Command15=ID_WINDOW_CASCADE -Command16=ID_WINDOW_TILE_HORZ -Command17=ID_WINDOW_ARRANGE -Command18=ID_APP_ABOUT -CommandCount=18 - -[ACL:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_EDIT_COPY -Command2=ID_FILE_NEW -Command3=ID_FILE_OPEN -Command4=ID_FILE_SAVE -Command5=ID_EDIT_PASTE -Command6=ID_EDIT_UNDO -Command7=ID_EDIT_CUT -Command8=ID_RUN -Command9=ID_NEXT_PANE -Command10=ID_PREV_PANE -Command11=ID_RUN -Command12=ID_EDIT_COPY -Command13=ID_EDIT_PASTE -Command14=ID_EDIT_CUT -Command15=ID_EDIT_UNDO -CommandCount=15 - -[DLG:IDD_ABOUTBOX (French (France))] -Type=1 -Class=CAboutDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342177283 -Control2=IDC_STATIC,static,1342308480 -Control3=IDC_STATIC,static,1342308352 -Control4=IDOK,button,1342373889 - -[DLG:IDD_CONSOLE] -Type=1 -Class=CBotConsoleDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342308352 -Control2=IDC_EDIT2,edit,1350631552 -Control3=IDOK,button,1342242817 -Control4=IDC_EDIT1,edit,1352734724 - -[CLS:CBotConsoleDlg] -Type=0 -HeaderFile=CBotConsoleDlg.h -ImplementationFile=CBotConsoleDlg.cpp -BaseClass=CDialog -Filter=D -VirtualFilter=dWC - diff --git a/src/CBot/TestCBot/zz.txt b/src/CBot/TestCBot/zz.txt deleted file mode 100644 index da764ac..0000000 --- a/src/CBot/TestCBot/zz.txt +++ /dev/null @@ -1,6 +0,0 @@ -extern public void zz() -{ - MaClass TOTO (); - - show (TOTO); -} \ No newline at end of file diff --git a/src/CBot/_Copy.bat b/src/CBot/_Copy.bat deleted file mode 100644 index 510dc5a..0000000 --- a/src/CBot/_Copy.bat +++ /dev/null @@ -1,2 +0,0 @@ -copy debug\cbot.dll "F:\Program Files\Ceebot\cbot.dll" -cls \ No newline at end of file diff --git a/src/CBot/colobot.ini b/src/CBot/colobot.ini deleted file mode 100644 index 32163db..0000000 --- a/src/CBot/colobot.ini +++ /dev/null @@ -1,49 +0,0 @@ -[Directory] -scene=scene -savegame=savegame -public=program -user=user -[Setup] -TotoMode=1 -Tooltips=1 -InterfaceGlint=1 -NiceMouse=0 -Movies=1 -NiceReset=1 -HimselfDamage=1 -CameraScroll=1 -CameraInvertX=0 -InterfaceEffect=1 -GroundShadow=1 -GroundSpot=1 -ObjectDirty=1 -FogMode=1 -LensMode=1 -SkyMode=1 -PlanetMode=1 -LightMode=1 -UseJoystick=0 -ParticuleDensity=1.00 -ClippingDistance=1.00 -ObjectDetail=2.00 -GadgetQuantity=1.00 -TextureQuality=1 -AudioVolume=20 -MidiVolume=15 -Sound3D=0 -EditIndentMode=1 -EditIndentValue=4 -KeyMap=37+0 39+0 38+0 40+0 16+0 17+0 32+258 96+262 13+257 107+261 109+260 9+259 36+263 27+0 112+0 113+0 110+0 115+0 116+0 117+0 -[Engine] -AlphaMode=1 -StateColor=-1 -BlackSrcBlend=0 -BlackDestBlend=0 -WhiteSrcBlend=0 -WhiteDestBlend=0 -DiffuseSrcBlend=0 -DiffuseDestBlend=0 -AlphaSrcBlend=0 -AlphaDestBlend=0 -[Gamer] -LastName=Player diff --git a/src/CBot/old TstCBot/BotConsoleDlg.cpp b/src/CBot/old TstCBot/BotConsoleDlg.cpp deleted file mode 100644 index 077f080..0000000 --- a/src/CBot/old TstCBot/BotConsoleDlg.cpp +++ /dev/null @@ -1,164 +0,0 @@ -// BotConsoleDlg.cpp : implementation file -// - -#include "stdafx.h" -#include "TstCBot.h" -#include "BotConsoleDlg.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CBotConsoleDlg dialog - - -CBotConsoleDlg::CBotConsoleDlg(CWnd* pParent /*=NULL*/) - : CDialog(CBotConsoleDlg::IDD, pParent) -{ - //{{AFX_DATA_INIT(CBotConsoleDlg) - // NOTE: the ClassWizard will add member initialization here - //}}AFX_DATA_INIT - m_pProg = NULL; - m_threadinfo.m_bRun = FALSE; -} - - -void CBotConsoleDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialog::DoDataExchange(pDX); - //{{AFX_DATA_MAP(CBotConsoleDlg) - DDX_Control(pDX, IDOK, m_cOK); - DDX_Control(pDX, IDC_EDIT2, m_Edit2); - DDX_Control(pDX, IDC_EDIT1, m_Edit1); - //}}AFX_DATA_MAP -} - - -BEGIN_MESSAGE_MAP(CBotConsoleDlg, CDialog) - //{{AFX_MSG_MAP(CBotConsoleDlg) - ON_MESSAGE(WM_ENDPROG, EndProg) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CBotConsoleDlg message handlers - -UINT ThreadProc(ThreadInfo *info) -{ - CTime t0 = CTime::GetCurrentTime(); - int Cpt = 0; - - info->m_pProg->Start("LaCommande"); - while ( !info->m_bStop && !info->m_pProg->Run() ) - { - Cpt++; - if ( Cpt%20 == 0 ) info->m_pEdit1->ReplaceSel("."); - } - - if ( info->m_bStop ) - { - info->m_pEdit1->ReplaceSel("\r\nInterrompu\r\n"); - } - else if (info->m_pProg->GivError() == 0) - { - CTime t = CTime::GetCurrentTime(); - CTimeSpan ts = t - t0; - - char buffer[200]; - sprintf( buffer, "\r\nExcution termine en %d secondes.\r\nInterrompue %d fois.\r\n", - ts.GetTotalSeconds(), Cpt); - - info->m_pEdit1->ReplaceSel(buffer); - } - - info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ; - return 0 ; -} - -LONG CBotConsoleDlg::EndProg(UINT wparam, LONG lparam) -{ - m_threadinfo.m_bRun = FALSE; - - if (m_pProg->GetError(m_code, m_start, m_end)) - { - AfxMessageBox(m_code); - CDialog::OnCancel(); - return 1; - } - delete m_pProg; - m_pProg = NULL; - - m_Edit2.EnableWindow(TRUE); - m_cOK.EnableWindow(TRUE); - - m_Edit2.SetWindowText(""); - m_Edit2.SetFocus(); - return 0 ; -} - -void CBotConsoleDlg::OnOK() -{ - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - pApp->m_pConsole = &m_Edit1; - - CString Commande; - m_Edit2.GetWindowText(Commande); - - CString s = "void LaCommande() { " + Commande + " ;}"; - m_pProg = new CBotProgram(); - CBotStringArray liste; - m_pProg->Compile(s, liste); - int err, start, end; - if ( m_pProg->GetError(err, start, end) ) - { - AfxMessageBox(err); - m_Edit2.SetSel(start-20, end-20); - return; - } - - m_Edit1.ReplaceSel(Commande + " ->\r\n"); - - m_Edit2.SetWindowText(""); - m_Edit1.SetFocus(); - m_Edit2.EnableWindow(FALSE); - m_cOK.EnableWindow(FALSE); - - // lance un processus paralle pour l'excution - m_threadinfo.m_pWndMessage = this ; - - m_threadinfo.m_pEdit1 = &m_Edit1; - m_threadinfo.m_pProg = m_pProg; - m_threadinfo.m_bStop = FALSE; - m_threadinfo.m_bRun = TRUE; - - AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ; -} - -void CBotConsoleDlg::OnCancel() -{ - if (!m_threadinfo.m_bRun) CDialog::OnCancel(); - m_threadinfo.m_bStop = TRUE ; -} - - -BOOL CBotConsoleDlg::OnInitDialog() -{ - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - - CDialog::OnInitDialog(); - - m_Edit1.ReplaceSel("Les fonctions suivantes sont disponibles:\r\n"); - for ( int i = 0; i < pApp->m_Liste.RetSize(); i++ ) - { - CBotString x = CString(pApp->m_Liste[i]) + "\r\n"; - m_Edit1.ReplaceSel(x); - } - m_Edit1.ReplaceSel("Entrez une commande ci-dessous.\r\n\r\n"); - - - return TRUE; // return TRUE unless you set the focus to a control - // EXCEPTION: OCX Property Pages should return FALSE -} diff --git a/src/CBot/old TstCBot/BotConsoleDlg.h b/src/CBot/old TstCBot/BotConsoleDlg.h deleted file mode 100644 index 9b54ff2..0000000 --- a/src/CBot/old TstCBot/BotConsoleDlg.h +++ /dev/null @@ -1,65 +0,0 @@ -#if !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) -#define AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 -// BotConsoleDlg.h : header file -// - -struct ThreadInfo -{ - CEdit* m_pEdit1 ; - CBotProgram* m_pProg; - CWnd* m_pWndMessage; - BOOL m_bStop; - BOOL m_bRun; -}; - - -///////////////////////////////////////////////////////////////////////////// -// CBotConsoleDlg dialog - -class CBotConsoleDlg : public CDialog -{ -// Construction -public: - CBotConsoleDlg(CWnd* pParent = NULL); // standard constructor - -// Dialog Data - //{{AFX_DATA(CBotConsoleDlg) - enum { IDD = IDD_CONSOLE }; - CButton m_cOK; - CEdit m_Edit2; - CEdit m_Edit1; - //}}AFX_DATA - - CBotProgram* m_pProg; - ThreadInfo m_threadinfo; - - int m_code, m_start, m_end; - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CBotConsoleDlg) - protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - //}}AFX_VIRTUAL - -// Implementation -protected: - - // Generated message map functions - //{{AFX_MSG(CBotConsoleDlg) - virtual void OnOK(); - virtual void OnCancel(); - virtual BOOL OnInitDialog(); - afx_msg LONG EndProg(UINT wparam, LONG lparam) ; - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/old TstCBot/BotErrorDlg.cpp b/src/CBot/old TstCBot/BotErrorDlg.cpp deleted file mode 100644 index 87d56f0..0000000 --- a/src/CBot/old TstCBot/BotErrorDlg.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// BotErrorDlg.cpp : implementation file -// - -#include "stdafx.h" -#include "TstCBot.h" -#include "BotErrorDlg.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CBotErrorDlg dialog - - -CBotErrorDlg::CBotErrorDlg(CWnd* pParent /*=NULL*/) - : CDialog(CBotErrorDlg::IDD, pParent) -{ - //{{AFX_DATA_INIT(CBotErrorDlg) - m_TextProgram = _T(""); - //}}AFX_DATA_INIT -} - - -void CBotErrorDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialog::DoDataExchange(pDX); - //{{AFX_DATA_MAP(CBotErrorDlg) - DDX_Control(pDX, IDC_EDIT1, m_eProgram); - DDX_Control(pDX, IDC_STATIC1, m_sMessage); - DDX_Text(pDX, IDC_EDIT1, m_TextProgram); - //}}AFX_DATA_MAP -} - - -BEGIN_MESSAGE_MAP(CBotErrorDlg, CDialog) - //{{AFX_MSG_MAP(CBotErrorDlg) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CBotErrorDlg message handlers - -BOOL CBotErrorDlg::OnInitDialog() -{ - CDialog::OnInitDialog(); - - m_sMessage.SetWindowText(m_TextError); - m_eProgram.SetFocus(); - m_eProgram.SetSel(m_start, m_end); - - return FALSE; // return TRUE unless you set the focus to a control - // EXCEPTION: OCX Property Pages should return FALSE -} diff --git a/src/CBot/old TstCBot/BotErrorDlg.h b/src/CBot/old TstCBot/BotErrorDlg.h deleted file mode 100644 index 522afad..0000000 --- a/src/CBot/old TstCBot/BotErrorDlg.h +++ /dev/null @@ -1,51 +0,0 @@ -#if !defined(AFX_BOTERRORDLG_H__80E73D20_7454_11D4_A439_00D059085115__INCLUDED_) -#define AFX_BOTERRORDLG_H__80E73D20_7454_11D4_A439_00D059085115__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 -// BotErrorDlg.h : header file -// - -///////////////////////////////////////////////////////////////////////////// -// CBotErrorDlg dialog - -class CBotErrorDlg : public CDialog -{ -// Construction -public: - CBotErrorDlg(CWnd* pParent = NULL); // standard constructor - -// Dialog Data - //{{AFX_DATA(CBotErrorDlg) - enum { IDD = IDD_DIALOG1 }; - CEdit m_eProgram; - CStatic m_sMessage; - CString m_TextProgram; - //}}AFX_DATA - - - CString m_TextError; - int m_start, m_end; - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CBotErrorDlg) - protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - //}}AFX_VIRTUAL - -// Implementation -protected: - - // Generated message map functions - //{{AFX_MSG(CBotErrorDlg) - virtual BOOL OnInitDialog(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_BOTERRORDLG_H__80E73D20_7454_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/old TstCBot/CBotTest.txt b/src/CBot/old TstCBot/CBotTest.txt deleted file mode 100644 index ce20e26..0000000 --- a/src/CBot/old TstCBot/CBotTest.txt +++ /dev/null @@ -1,36 +0,0 @@ - -extern void TheTest() -{ - for (int x = 130; x>0; x--) print (x); -} - -extern void Test() -{ - int var = 10000 ; - while (var > 0) var = var -1; -} -// excut en 30 secondes - -extern void Autre() -{ - int var = 10000 ; - while (var > 0) if ( var > 0 ) var = var -1; -} -// excut en 45 secondes - -int Y ( int n ) -{ - if ( n < 2 ) return n; - int a = Y(n-1) + Y(n-2); - return a; -} - -extern int X ( int n ) -{ - if ( n < 2 ) { print(n); return n; } - int a = X(n-1) + Y(n-2); - print (a); - return a; -} - - diff --git a/src/CBot/old TstCBot/CMyThread.cpp b/src/CBot/old TstCBot/CMyThread.cpp deleted file mode 100644 index ca92c77..0000000 --- a/src/CBot/old TstCBot/CMyThread.cpp +++ /dev/null @@ -1,107 +0,0 @@ -// CMyThread.cpp : pour crer un processus pour la console -// - -#include "stdafx.h" -#include "TstCBot.h" -#include "CMyThread.h" -#include "BotConsoleDlg.h" - - -//IMPLEMENT_DYNAMIC (CMyThread, CWinThread) -IMPLEMENT_DYNCREATE (CMyThread, CWinThread) - -///////////////////////////////////////////////////////////////////////////// -// CMyThread - -BEGIN_MESSAGE_MAP(CMyThread, CWinThread) - //{{AFX_MSG_MAP(CMyThread) - //}}AFX_MSG_MAP - // Standard file based document commands -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CMyThread construction - -CMyThread::CMyThread() -{ - // TODO: add construction code here, - // Place all significant initialization in InitInstance -} - - -///////////////////////////////////////////////////////////////////////////// -// CMyThread initialization -/* -BOOL CMyThread::InitInstance() -{ - AfxEnableControlContainer(); - - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - - // ouvre une fentre pour afficher les sorties - CRect rect; - AfxGetMainWnd()->GetClientRect( rect ); - rect += CPoint(30,30); - - CWnd* pWnd = new CWnd(); - pWnd->CreateEx( 0, - AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)), - "CBot console", WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE, - rect, - AfxGetMainWnd()->GetParent(), NULL, NULL); - m_pMainWnd = pWnd; - - pApp->m_pEdit2 = new CEdit(); - - m_pMainWnd->GetClientRect( rect ); - rect.bottom -= 40; - pApp->m_pEdit2->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN| - ES_AUTOVSCROLL|ES_READONLY, - rect, m_pMainWnd, IDC_EDIT2 ); - - pApp->m_pEdit2->ReplaceSel("Les fonctions suivantes sont disponibles:\n\r"); - for ( int i = 0; i < pApp->m_Liste.RetSize(); i++ ) - { - pApp->m_pEdit2->ReplaceSel(pApp->m_Liste[i] + "\r\n"); - } - pApp->m_pEdit2->ReplaceSel("Entrez une commande ci-dessous.\r\r"); - - -// pApp->m_pEdit2->SetFocus(); - - pApp->m_pEdit3 = new CEdit(); - m_pMainWnd->GetClientRect( rect ); - rect.top = rect.bottom-40; - pApp->m_pEdit3->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP, - rect, m_pMainWnd, IDC_EDIT1 ); - pApp->m_pEdit3->SetFocus(); - - return TRUE; -}*/ - -BOOL CMyThread::InitInstance() -{ - CBotConsoleDlg dlg; - m_pMainWnd = &dlg; // cela ferme l'application avec la DBOX ! - - int nResponse = dlg.DoModal(); - - return TRUE; -} - - -int CMyThread::ExitInstance() -{ - return 0; -} - - -///////////////////////////////////////////////////////////////////////////// -// CMyThread message handlers - - -void CMyThread::OnReturn() -{ - // TODO: Add your command handler code here - __asm int 3; -} diff --git a/src/CBot/old TstCBot/CMyThread.h b/src/CBot/old TstCBot/CMyThread.h deleted file mode 100644 index 1134077..0000000 --- a/src/CBot/old TstCBot/CMyThread.h +++ /dev/null @@ -1,44 +0,0 @@ -// CMyThread.h : pour crer un processus pour la console -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_MAINFRM_H__20B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) -#define AFX_MAINFRM_H__20B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ - - -#include "stdafx.h" -#include "TstCBot.h" - -class CMyThread : public CWinThread -{ -// DECLARE_DYNAMIC(CMyThread) - DECLARE_DYNCREATE(CMyThread) - -public: - - -// Constructor - CMyThread(); - virtual BOOL InitInstance(); - virtual int ExitInstance(); // return app exit code - -// Implementation - - //{{AFX_MSG(CTstCBotApp) - afx_msg void OnAppAbout(); - // NOTE - the ClassWizard will add and remove member functions here. - // DO NOT EDIT what you see in these blocks of generated code ! - //}}AFX_MSG - -// Generated message map functions -protected: - //{{AFX_MSG(CMainFrame) - afx_msg void OnReturn(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_MAINFRM_H__20B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/old TstCBot/MainFrm.cpp b/src/CBot/old TstCBot/MainFrm.cpp deleted file mode 100644 index 6c0962c..0000000 --- a/src/CBot/old TstCBot/MainFrm.cpp +++ /dev/null @@ -1,91 +0,0 @@ -// MainFrm.cpp : implementation of the CMainFrame class -// - -#include "stdafx.h" -#include "TstCBot.h" - -#include "MainFrm.h" -#include "BotErrorDlg.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame - -IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) - -BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) - //{{AFX_MSG_MAP(CMainFrame) - ON_WM_CREATE() - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -static UINT indicators[] = -{ - ID_SEPARATOR, // status line indicator - ID_INDICATOR_CAPS, - ID_INDICATOR_NUM, - ID_INDICATOR_SCRL, -}; - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame construction/destruction - -CMainFrame::CMainFrame() -{ - // TODO: add member initialization code here - -} - -CMainFrame::~CMainFrame() -{ -} - -int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) -{ - if (CFrameWnd::OnCreate(lpCreateStruct) == -1) - return -1; - - if (!m_wndStatusBar.Create(this) || - !m_wndStatusBar.SetIndicators(indicators, - sizeof(indicators)/sizeof(UINT))) - { - TRACE0("Failed to create status bar\n"); - return -1; // fail to create - } - - return 0; -} - -BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) -{ - // TODO: Modify the Window class or styles here by modifying - // the CREATESTRUCT cs - - return CFrameWnd::PreCreateWindow(cs); -} - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame diagnostics - -#ifdef _DEBUG -void CMainFrame::AssertValid() const -{ - CFrameWnd::AssertValid(); -} - -void CMainFrame::Dump(CDumpContext& dc) const -{ - CFrameWnd::Dump(dc); -} - -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CMainFrame message handlers - - diff --git a/src/CBot/old TstCBot/MainFrm.h b/src/CBot/old TstCBot/MainFrm.h deleted file mode 100644 index 56b9c41..0000000 --- a/src/CBot/old TstCBot/MainFrm.h +++ /dev/null @@ -1,55 +0,0 @@ -// MainFrm.h : interface of the CMainFrame class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_MAINFRM_H__70B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) -#define AFX_MAINFRM_H__70B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -class CMainFrame : public CFrameWnd -{ -protected: // create from serialization only - CMainFrame(); - DECLARE_DYNCREATE(CMainFrame) - -// Attributes -public: - -// Operations -public: - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CMainFrame) - virtual BOOL PreCreateWindow(CREATESTRUCT& cs); - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CMainFrame(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -protected: // control bar embedded members - CStatusBar m_wndStatusBar; - -// Generated message map functions -protected: - //{{AFX_MSG(CMainFrame) - afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); - afx_msg void OnCp1(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_MAINFRM_H__70B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/old TstCBot/ReadMe.txt b/src/CBot/old TstCBot/ReadMe.txt deleted file mode 100644 index 67dc05b..0000000 --- a/src/CBot/old TstCBot/ReadMe.txt +++ /dev/null @@ -1,93 +0,0 @@ -======================================================================== - MICROSOFT FOUNDATION CLASS LIBRARY : TstCBot -======================================================================== - - -AppWizard has created this TstCBot application for you. This application -not only demonstrates the basics of using the Microsoft Foundation classes -but is also a starting point for writing your application. - -This file contains a summary of what you will find in each of the files that -make up your TstCBot application. - -TstCBot.h - This is the main header file for the application. It includes other - project specific headers (including Resource.h) and declares the - CTstCBotApp application class. - -TstCBot.cpp - This is the main application source file that contains the application - class CTstCBotApp. - -TstCBot.rc - This is a listing of all of the Microsoft Windows resources that the - program uses. It includes the icons, bitmaps, and cursors that are stored - in the RES subdirectory. This file can be directly edited in Microsoft - Developer Studio. - -res\TstCBot.ico - This is an icon file, which is used as the application's icon. This - icon is included by the main resource file TstCBot.rc. - -res\TstCBot.rc2 - This file contains resources that are not edited by Microsoft - Developer Studio. You should place all resources not - editable by the resource editor in this file. - -TstCBot.clw - This file contains information used by ClassWizard to edit existing - classes or add new classes. ClassWizard also uses this file to store - information needed to create and edit message maps and dialog data - maps and to create prototype member functions. - -///////////////////////////////////////////////////////////////////////////// - -For the main frame window: - -MainFrm.h, MainFrm.cpp - These files contain the frame class CMainFrame, which is derived from - CFrameWnd and controls all SDI frame features. - - -///////////////////////////////////////////////////////////////////////////// - -AppWizard creates one document type and one view: - -TstCBotDoc.h, TstCBotDoc.cpp - the document - These files contain your CTstCBotDoc class. Edit these files to - add your special document data and to implement file saving and loading - (via CTstCBotDoc::Serialize). - -TstCBotView.h, TstCBotView.cpp - the view of the document - These files contain your CTstCBotView class. - CTstCBotView objects are used to view CTstCBotDoc objects. - - - -///////////////////////////////////////////////////////////////////////////// -Other standard files: - -StdAfx.h, StdAfx.cpp - These files are used to build a precompiled header (PCH) file - named TstCBot.pch and a precompiled types file named StdAfx.obj. - -Resource.h - This is the standard header file, which defines new resource IDs. - Microsoft Developer Studio reads and updates this file. - -///////////////////////////////////////////////////////////////////////////// -Other notes: - -AppWizard uses "TODO:" to indicate parts of the source code you -should add to or customize. - -If your application uses MFC in a shared DLL, and your application is -in a language other than the operating system's current language, you -will need to copy the corresponding localized resources MFC40XXX.DLL -from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, -and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. -For example, MFC40DEU.DLL contains resources translated to German.) If you -don't do this, some of the UI elements of your application will remain in the -language of the operating system. - -///////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/old TstCBot/Resource.h b/src/CBot/old TstCBot/Resource.h deleted file mode 100644 index 6863fd8..0000000 --- a/src/CBot/old TstCBot/Resource.h +++ /dev/null @@ -1,68 +0,0 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Developer Studio generated include file. -// Used by TstCBot.rc -// -#define IDD_ABOUTBOX 100 -#define IDR_MAINFRAME 128 -#define IDR_TSTCBOTYPE 129 -#define IDD_DIALOG1 130 -#define IDD_CONSOLE 131 -#define IDC_EDIT1 1000 -#define TX_TYPENAMES 1000 -#define IDC_STATIC1 1001 -#define IDC_EDIT2 1002 -#define TX_OPENPAR 5000 -#define TX_CLOSEPAR 5001 -#define TX_NOTBOOL 5002 -#define TX_UNDEFVAR 5003 -#define TX_BADLEFT 5004 -#define TX_ENDOF 5005 -#define TX_OUTCASE 5006 -#define TX_NOTERM 5007 -#define TX_CLOSEBLK 5008 -#define TX_ELSEWITHOUTIF 5009 -#define TX_OPENBLK 5010 -#define TX_BADTYPE 5011 -#define TX_REDEFVAR 5012 -#define TX_BAD2TYPE 5013 -#define TX_UNDEFCALL 5014 -#define TX_MISDOTS 5015 -#define TX_WHILE 5016 -#define TX_BREAK 5017 -#define TX_LABEL 5018 -#define TX_NOLABEL 5019 -#define TX_NOCASE 5020 -#define TX_BADNUM 5021 -#define TX_VOID 5022 -#define TX_NOTYP 5023 -#define TX_NOVAR 5024 -#define TX_NOFONC 5025 -#define TX_OVERPARAM 5026 -#define TX_REDEF 5027 -#define TX_LOWPARAM 5028 -#define TX_BADPARAM 5029 -#define TX_NUMPARAM 5030 -#define TX_NOITEM 5031 -#define TX_DOT 5032 -#define TX_NOCONST 5033 -#define TX_REDEFCLASS 5034 -#define TX_DIVZERO 6000 -#define TX_NOTINIT 6001 -#define TX_BADTHROW 6002 -#define TX_NORETVAL 6003 -#define TX_NORUN 6004 -#define TX_NOCALL 6005 -#define ID_CP1 32771 -#define ID_EXE 32772 - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_3D_CONTROLS 1 -#define _APS_NEXT_RESOURCE_VALUE 132 -#define _APS_NEXT_COMMAND_VALUE 32775 -#define _APS_NEXT_CONTROL_VALUE 1002 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif diff --git a/src/CBot/old TstCBot/StdAfx.cpp b/src/CBot/old TstCBot/StdAfx.cpp deleted file mode 100644 index ae0ec93..0000000 --- a/src/CBot/old TstCBot/StdAfx.cpp +++ /dev/null @@ -1,6 +0,0 @@ -// stdafx.cpp : source file that includes just the standard includes -// TstCBot.pch will be the pre-compiled header -// stdafx.obj will contain the pre-compiled type information - -#include "stdafx.h" - diff --git a/src/CBot/old TstCBot/StdAfx.h b/src/CBot/old TstCBot/StdAfx.h deleted file mode 100644 index 7d46ace..0000000 --- a/src/CBot/old TstCBot/StdAfx.h +++ /dev/null @@ -1,26 +0,0 @@ -// stdafx.h : include file for standard system include files, -// or project specific include files that are used frequently, but -// are changed infrequently -// - -#if !defined(AFX_STDAFX_H__70B3756A_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) -#define AFX_STDAFX_H__70B3756A_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers - -#include // MFC core and standard components -#include // MFC extensions -#include // MFC OLE automation classes -#ifndef _AFX_NO_AFXCMN_SUPPORT -#include // MFC support for Windows Common Controls -#endif // _AFX_NO_AFXCMN_SUPPORT - - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_STDAFX_H__70B3756A_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/old TstCBot/TstCBot.clw b/src/CBot/old TstCBot/TstCBot.clw deleted file mode 100644 index 4c54168..0000000 --- a/src/CBot/old TstCBot/TstCBot.clw +++ /dev/null @@ -1,189 +0,0 @@ -; CLW file contains information for the MFC ClassWizard - -[General Info] -Version=1 -LastClass=CTstCBotView -LastTemplate=CDialog -NewFileInclude1=#include "stdafx.h" -NewFileInclude2=#include "TstCBot.h" -LastPage=0 - -ClassCount=7 -Class1=CTstCBotApp -Class2=CTstCBotDoc -Class3=CTstCBotView -Class4=CMainFrame - -ResourceCount=6 -Resource1=IDD_ABOUTBOX -Resource2=IDR_MAINFRAME -Class5=CAboutDlg -Resource3=IDD_ABOUTBOX (French (France)) -Resource4=IDD_CONSOLE -Class6=CBotErrorDlg -Resource5=IDD_DIALOG1 (French (Switzerland)) -Class7=CBotConsoleDlg -Resource6=IDR_MAINFRAME (French (France)) - -[CLS:CTstCBotApp] -Type=0 -HeaderFile=TstCBot.h -ImplementationFile=TstCBot.cpp -Filter=N - -[CLS:CTstCBotDoc] -Type=0 -HeaderFile=TstCBotDoc.h -ImplementationFile=TstCBotDoc.cpp -Filter=N -BaseClass=CDocument -VirtualFilter=DC -LastObject=CTstCBotDoc - -[CLS:CTstCBotView] -Type=0 -HeaderFile=TstCBotView.h -ImplementationFile=TstCBotView.cpp -Filter=C -BaseClass=CView -VirtualFilter=VWC -LastObject=CTstCBotView - -[CLS:CMainFrame] -Type=0 -HeaderFile=MainFrm.h -ImplementationFile=MainFrm.cpp -Filter=T -BaseClass=CFrameWnd -VirtualFilter=fWC -LastObject=CMainFrame - - - -[CLS:CAboutDlg] -Type=0 -HeaderFile=TstCBot.cpp -ImplementationFile=TstCBot.cpp -Filter=D - -[DLG:IDD_ABOUTBOX] -Type=1 -Class=CAboutDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342177283 -Control2=IDC_STATIC,static,1342308480 -Control3=IDC_STATIC,static,1342308352 -Control4=IDOK,button,1342373889 - -[MNU:IDR_MAINFRAME] -Type=1 -Class=CMainFrame -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command4=ID_FILE_SAVE_AS -Command5=ID_FILE_MRU_FILE1 -Command6=ID_APP_EXIT -Command7=ID_EDIT_UNDO -Command8=ID_EDIT_CUT -Command9=ID_EDIT_COPY -Command10=ID_EDIT_PASTE -Command11=ID_VIEW_STATUS_BAR -Command12=ID_CP1 -Command13=ID_EXE -Command14=ID_APP_ABOUT -CommandCount=14 - -[ACL:IDR_MAINFRAME] -Type=1 -Class=CMainFrame -Command1=ID_CP1 -Command2=ID_FILE_NEW -Command3=ID_FILE_OPEN -Command4=ID_FILE_SAVE -Command5=ID_EXE -Command6=ID_EDIT_UNDO -Command7=ID_EDIT_CUT -Command8=ID_EXE -Command9=ID_CP1 -Command10=ID_EXE -CommandCount=10 - -[MNU:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_FILE_NEW -Command2=ID_FILE_OPEN -Command3=ID_FILE_SAVE -Command4=ID_FILE_SAVE_AS -Command5=ID_FILE_MRU_FILE1 -Command6=ID_APP_EXIT -Command7=ID_EDIT_UNDO -Command8=ID_EDIT_CUT -Command9=ID_EDIT_COPY -Command10=ID_EDIT_PASTE -Command11=ID_VIEW_STATUS_BAR -Command12=ID_CP1 -Command13=ID_EXE -Command14=ID_APP_ABOUT -CommandCount=14 - -[ACL:IDR_MAINFRAME (French (France))] -Type=1 -Class=? -Command1=ID_CP1 -Command2=ID_FILE_NEW -Command3=ID_FILE_OPEN -Command4=ID_FILE_SAVE -Command5=ID_EXE -Command6=ID_EDIT_UNDO -Command7=ID_EDIT_CUT -Command8=ID_EXE -Command9=ID_CP1 -Command10=ID_EXE -CommandCount=10 - -[DLG:IDD_ABOUTBOX (French (France))] -Type=1 -Class=CAboutDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342177283 -Control2=IDC_STATIC,static,1342308480 -Control3=IDC_STATIC,static,1342308352 -Control4=IDOK,button,1342373889 - -[CLS:CBotErrorDlg] -Type=0 -HeaderFile=BotErrorDlg.h -ImplementationFile=BotErrorDlg.cpp -BaseClass=CDialog -Filter=D -VirtualFilter=dWC -LastObject=CBotErrorDlg - -[DLG:IDD_DIALOG1 (French (Switzerland))] -Type=1 -ControlCount=4 -Control1=IDOK,button,1342242817 -Control2=IDC_EDIT1,edit,1352728708 -Control3=IDC_STATIC,static,1342308352 -Control4=IDC_STATIC1,static,1342308352 - -[DLG:IDD_CONSOLE] -Type=1 -Class=CBotConsoleDlg -ControlCount=4 -Control1=IDC_STATIC,static,1342308352 -Control2=IDC_EDIT2,edit,1350631552 -Control3=IDOK,button,1342242817 -Control4=IDC_EDIT1,edit,1352734724 - -[CLS:CBotConsoleDlg] -Type=0 -HeaderFile=BotConsoleDlg.h -ImplementationFile=BotConsoleDlg.cpp -BaseClass=CDialog -Filter=D -VirtualFilter=dWC -LastObject=IDOK - diff --git a/src/CBot/old TstCBot/TstCBot.cpp b/src/CBot/old TstCBot/TstCBot.cpp deleted file mode 100644 index 8ac4557..0000000 --- a/src/CBot/old TstCBot/TstCBot.cpp +++ /dev/null @@ -1,412 +0,0 @@ -// TstCBot.cpp : Defines the class behaviors for the application. -// - -#include "stdafx.h" -#include "TstCBot.h" - -#include "MainFrm.h" -#include "TstCBotDoc.h" -#include "TstCBotView.h" -#include "CMyThread.h" - - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - - -//////////////////////////////////////////////////////////////////// -// routine show() -// utilisable depuis le programme crit en CBot - -// excution -BOOL rShow( CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - CString s; - - if ( pVar == NULL ) - { - Exception = 22; return FALSE; - } - - while ( pVar != NULL ) - { - CString ss; - ss.LoadString( TX_TYPENAMES + pVar->RetType() ); - s += ss + " "; - - ss = pVar->RetName(); - if (ss.IsEmpty()) ss = ""; - s += ss + " = "; - - s += pVar->RetValString(); - s += "\n"; - pVar = pVar->RetNext(); - } - - AfxMessageBox(s, MB_OK|MB_ICONINFORMATION); - -// if ( pResult && pResult->RetType() == CBotTypInt) pResult->SetValInt(123); - - return TRUE; // pas d'interruption -} - -int cShow( CBotVar* &pVar, CBotString& RetClass) -{ - if ( pVar == NULL ) return 22; - return CBotTypInt; // tous paramtres accepts, un entier en retour -} - -int cErr( CBotVar* &pVar, CBotString& RetClass) -{ - pVar = pVar->RetNext(); // avance le pointeur sur l'erreur - return 6666; -} - -//////////////////////////////////////////////////////////////////// -// routine print() -// utilisable depuis le programme crit en CBot - -// excution -BOOL rPrintLn( CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - CString s; - - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - CEdit* pEdit = pApp->m_pConsole; - - if (pEdit == NULL) return TRUE; - pEdit->GetWindowText(s); - - while ( pVar != NULL ) - { - if ( !s.IsEmpty() ) s += "\r\n"; - s += pVar->RetValString(); - pVar = pVar->RetNext(); - } - - pEdit->SetWindowText(s); - pEdit->SetSel(s.GetLength(), s.GetLength()); - pEdit->SetFocus(); - return TRUE; // pas d'interruption -} - -BOOL rPrint( CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - CString s; - - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - CEdit* pEdit = pApp->m_pConsole; - - if (pEdit == NULL) return TRUE; - pEdit->GetWindowText(s); - - while ( pVar != NULL ) - { - if ( !s.IsEmpty() ) s += " "; - s += pVar->RetValString(); - pVar = pVar->RetNext(); - } - - pEdit->SetWindowText(s); - pEdit->SetSel(s.GetLength(), s.GetLength()); - pEdit->SetFocus(); - return TRUE; // pas d'interruption -} - -int cPrint( CBotVar* &pVar, CBotString& RetClass) -{ - return 0; // tous paramtres accepts, un entier en retour -} - - -////////////////////////////////////////////////////////////////// -// class CPoint pour essayer - -// excution -BOOL rCPoint( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - CString s; - - if ( pVar == NULL )return TRUE; // constructeur sans paramtres est ok - - if ( pVar->RetType() > CBotTypDouble ) - { - Exception = 6023; return FALSE; - } - - CBotVar* pX = pThis->RetItem("x"); - if ( pX == NULL ) - { - Exception = 6024; return FALSE; - } - - pX->SetValFloat( pVar->RetValFloat() ); - pVar = pVar->RetNext(); - - if ( pVar == NULL ) - { - Exception = 6022; return FALSE; - } - - if ( pVar->RetType() > CBotTypDouble ) - { - Exception = 6023; return FALSE; - } - - CBotVar* pY = pThis->RetItem("y"); - if ( pY == NULL ) - { - Exception = 6024; return FALSE; - } - - pY->SetValFloat( pVar->RetValFloat() ); - pVar = pVar->RetNext(); - - if ( pVar != NULL ) - { - Exception = 6025; return FALSE; - } - - return TRUE; // pas d'interruption -} - -int cCPoint( CBotVar* pThis, CBotVar* &pVar, CBotString& RetClass) -{ - // l'objet doit tre de la classe CPoint - if ( !pThis->IsElemOfClass("CPoint") ) return 6021; - - // ok si aucun paramtres ! - if ( pVar == NULL ) return 0; - - // paramtre de type numrique svp - if ( pVar->RetType() > CBotTypDouble ) return 6023; - pVar = pVar->RetNext(); - - // il doit y avoir un second paramtre - if ( pVar == NULL ) return 6022; - // galement de type numrique - if ( pVar->RetType() > CBotTypDouble )return 6023; - pVar = pVar->RetNext(); - - // et pas plus de 2 paramtres svp - if ( pVar != NULL ) return 6025; - - return 0; // cette fonction retourne void -} - -// mthode dterminant l'oppos -BOOL rOppose( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) -{ - CString s; - - if ( pVar != NULL ) // pas de paramtre - { - Exception = 6025; return FALSE; - } - - CBotVar* pvar = pThis->RetItemList(); // demande la chane des items - - // tous les paramtres sont des nombres - while (pvar != NULL) - { - pvar->SetValFloat( -pvar->RetValFloat() ); - pvar = pvar->RetNext(); - } - - pResult->Copy(pThis); - return TRUE; // pas d'interruption -} - -int cOppose( CBotVar* pThis, CBotVar* &pVar, CBotString& RetClass) -{ - // l'objet doit tre de la classe CPoint - if ( !pThis->IsElemOfClass("CPoint") ) return 6021; - - RetClass = "CPoint"; // l'objet rendu est de cette class - - // ok si aucun paramtres ! - if ( pVar == NULL ) return CBotTypClass; // le paramtre retourn est une instance de la classe - - return TX_OVERPARAM; // a va pas -} - - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotApp - -BEGIN_MESSAGE_MAP(CTstCBotApp, CWinApp) - //{{AFX_MSG_MAP(CTstCBotApp) - ON_COMMAND(ID_APP_ABOUT, OnAppAbout) - // NOTE - the ClassWizard will add and remove mapping macros here. - // DO NOT EDIT what you see in these blocks of generated code! - //}}AFX_MSG_MAP - // Standard file based document commands - ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) - ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotApp construction - -CTstCBotApp::CTstCBotApp() -{ - // TODO: add construction code here, - // Place all significant initialization in InitInstance -} - -///////////////////////////////////////////////////////////////////////////// -// The one and only CTstCBotApp object - -CTstCBotApp theApp; - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotApp initialization - -BOOL CTstCBotApp::InitInstance() -{ - AfxEnableControlContainer(); - - // Standard initialization - // If you are not using these features and wish to reduce the size - // of your final executable, you should remove from the following - // the specific initialization routines you do not need. - -#ifdef _AFXDLL - Enable3dControls(); // Call this when using MFC in a shared DLL -#else - Enable3dControlsStatic(); // Call this when linking to MFC statically -#endif - - // Change the registry key under which our settings are stored. - // You should modify this string to be something appropriate - // such as the name of your company or organization. - SetRegistryKey(_T("Local AppWizard-Generated Applications")); - - LoadStdProfileSettings(); // Load standard INI file options (including MRU) - - // Register the application's document templates. Document templates - // serve as the connection between documents, frame windows and views. - - CSingleDocTemplate* pDocTemplate; - pDocTemplate = new CSingleDocTemplate( - IDR_MAINFRAME, - RUNTIME_CLASS(CTstCBotDoc), - RUNTIME_CLASS(CMainFrame), // main SDI frame window - RUNTIME_CLASS(CTstCBotView)); - AddDocTemplate(pDocTemplate); - - // Parse command line for standard shell commands, DDE, file open - CCommandLineInfo cmdInfo; - ParseCommandLine(cmdInfo); - - // Dispatch commands specified on the command line - if (!ProcessShellCommand(cmdInfo)) - return FALSE; - - // The one and only window has been initialized, so show and update it. - m_pMainWnd->ShowWindow(SW_SHOW); - m_pMainWnd->UpdateWindow(); - - - -/////////////////////////////////// -// dfini la fonction "show()" -// -------------------------------- - - CBotProgram::AddFunction("show", rShow, cShow); - CBotProgram::AddFunction("err", rShow, cErr); - CBotProgram::AddFunction("print", rPrint, cPrint); - CBotProgram::AddFunction("println", rPrintLn, cPrint); - - -/////////////////////////////////// -// dfinie la classe globale CPoint -// -------------------------------- - - CBotClass* m_pClassPoint; - - m_pClassPoint = new CBotClass("CPoint", NULL); - // ajoute le composant ".x" - m_pClassPoint->AddItem("x", CBotTypFloat); - // ajoute le composant ".y" - m_pClassPoint->AddItem("y", CBotTypFloat); - - // ajoute le constructeur pour cette classe - m_pClassPoint->AddFunction("CPoint", rCPoint, cCPoint); - // ajoute la mthode Oppos - m_pClassPoint->AddFunction("Oppos", rOppose, cOppose); - - -////////////////////////////////////////////////////////////////// -// compile un bout de programme pour voir s'il est bien accessible -// depuis un autre "module" - - CBotProgram* p = new CBotProgram; - CBotStringArray Liste; - p->Compile(" public void MonProgram( ) { show (\"mon programme\") ;}", Liste ); - - // l'objet n'est pas dtruit et plus rfrenc - // je sais c'est pas bien - - - return TRUE; -} - -///////////////////////////////////////////////////////////////////////////// -// CAboutDlg dialog used for App About - -class CAboutDlg : public CDialog -{ -public: - CAboutDlg(); - -// Dialog Data - //{{AFX_DATA(CAboutDlg) - enum { IDD = IDD_ABOUTBOX }; - //}}AFX_DATA - - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CAboutDlg) - protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - //}}AFX_VIRTUAL - -// Implementation -protected: - //{{AFX_MSG(CAboutDlg) - // No message handlers - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) -{ - //{{AFX_DATA_INIT(CAboutDlg) - //}}AFX_DATA_INIT -} - -void CAboutDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialog::DoDataExchange(pDX); - //{{AFX_DATA_MAP(CAboutDlg) - //}}AFX_DATA_MAP -} - -BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) - //{{AFX_MSG_MAP(CAboutDlg) - // No message handlers - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -// App command to run the dialog -void CTstCBotApp::OnAppAbout() -{ - CAboutDlg aboutDlg; - aboutDlg.DoModal(); -} - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotApp commands diff --git a/src/CBot/old TstCBot/TstCBot.dsp b/src/CBot/old TstCBot/TstCBot.dsp deleted file mode 100644 index 35e5c0b..0000000 --- a/src/CBot/old TstCBot/TstCBot.dsp +++ /dev/null @@ -1,180 +0,0 @@ -# Microsoft Developer Studio Project File - Name="TstCBot" - Package Owner=<4> -# Microsoft Developer Studio Generated Build File, Format Version 5.00 -# ** DO NOT EDIT ** - -# TARGTYPE "Win32 (x86) Application" 0x0101 - -CFG=TstCBot - Win32 Debug -!MESSAGE This is not a valid makefile. To build this project using NMAKE, -!MESSAGE use the Export Makefile command and run -!MESSAGE -!MESSAGE NMAKE /f "TstCBot.mak". -!MESSAGE -!MESSAGE You can specify a configuration when running NMAKE -!MESSAGE by defining the macro CFG on the command line. For example: -!MESSAGE -!MESSAGE NMAKE /f "TstCBot.mak" CFG="TstCBot - Win32 Debug" -!MESSAGE -!MESSAGE Possible choices for configuration are: -!MESSAGE -!MESSAGE "TstCBot - Win32 Release" (based on "Win32 (x86) Application") -!MESSAGE "TstCBot - Win32 Debug" (based on "Win32 (x86) Application") -!MESSAGE - -# Begin Project -# PROP Scc_ProjName "" -# PROP Scc_LocalPath "" -CPP=cl.exe -MTL=midl.exe -RSC=rc.exe - -!IF "$(CFG)" == "TstCBot - Win32 Release" - -# PROP BASE Use_MFC 6 -# PROP BASE Use_Debug_Libraries 0 -# PROP BASE Output_Dir "Release" -# PROP BASE Intermediate_Dir "Release" -# PROP BASE Target_Dir "" -# PROP Use_MFC 6 -# PROP Use_Debug_Libraries 0 -# PROP Output_Dir "Release" -# PROP Intermediate_Dir "Release" -# PROP Target_Dir "" -# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /Yu"stdafx.h" /FD /c -# ADD CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /FR /Yu"stdafx.h" /FD /c -# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 -# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 -# ADD BASE RSC /l 0x40c /d "NDEBUG" /d "_AFXDLL" -# ADD RSC /l 0x40c /d "NDEBUG" /d "_AFXDLL" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 /nologo /subsystem:windows /machine:I386 -# ADD LINK32 /nologo /subsystem:windows /machine:I386 - -!ELSEIF "$(CFG)" == "TstCBot - Win32 Debug" - -# PROP BASE Use_MFC 6 -# PROP BASE Use_Debug_Libraries 1 -# PROP BASE Output_Dir "Debug" -# PROP BASE Intermediate_Dir "Debug" -# PROP BASE Target_Dir "" -# PROP Use_MFC 6 -# PROP Use_Debug_Libraries 1 -# PROP Output_Dir "Debug" -# PROP Intermediate_Dir "Debug" -# PROP Target_Dir "" -# ADD BASE CPP /nologo /MDd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /Yu"stdafx.h" /FD /c -# ADD CPP /nologo /MDd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /FR /Yu"stdafx.h" /FD /c -# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 -# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 -# ADD BASE RSC /l 0x40c /d "_DEBUG" /d "_AFXDLL" -# ADD RSC /l 0x40c /d "_DEBUG" /d "_AFXDLL" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept -# ADD LINK32 /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept - -!ENDIF - -# Begin Target - -# Name "TstCBot - Win32 Release" -# Name "TstCBot - Win32 Debug" -# Begin Group "Source Files" - -# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" -# Begin Source File - -SOURCE=.\BotConsoleDlg.cpp -# End Source File -# Begin Source File - -SOURCE=.\MainFrm.cpp -# End Source File -# Begin Source File - -SOURCE=.\StdAfx.cpp -# ADD CPP /Yc"stdafx.h" -# End Source File -# Begin Source File - -SOURCE=.\TstCBot.cpp -# End Source File -# Begin Source File - -SOURCE=.\TstCBot.rc - -!IF "$(CFG)" == "TstCBot - Win32 Release" - -!ELSEIF "$(CFG)" == "TstCBot - Win32 Debug" - -!ENDIF - -# End Source File -# Begin Source File - -SOURCE=.\TstCBotDoc.cpp -# End Source File -# Begin Source File - -SOURCE=.\TstCBotView.cpp -# End Source File -# End Group -# Begin Group "Header Files" - -# PROP Default_Filter "h;hpp;hxx;hm;inl" -# Begin Source File - -SOURCE=.\BotConsoleDlg.h -# End Source File -# Begin Source File - -SOURCE=.\MainFrm.h -# End Source File -# Begin Source File - -SOURCE=.\Resource.h -# End Source File -# Begin Source File - -SOURCE=.\StdAfx.h -# End Source File -# Begin Source File - -SOURCE=.\TstCBot.h -# End Source File -# Begin Source File - -SOURCE=.\TstCBotDoc.h -# End Source File -# Begin Source File - -SOURCE=.\TstCBotView.h -# End Source File -# End Group -# Begin Group "Resource Files" - -# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe" -# Begin Source File - -SOURCE=.\res\TstCBot.ico -# End Source File -# Begin Source File - -SOURCE=.\res\TstCBot.rc2 -# End Source File -# Begin Source File - -SOURCE=.\res\TstCBotDoc.ico -# End Source File -# End Group -# Begin Source File - -SOURCE=.\ReadMe.txt -# End Source File -# End Target -# End Project diff --git a/src/CBot/old TstCBot/TstCBot.h b/src/CBot/old TstCBot/TstCBot.h deleted file mode 100644 index 616db43..0000000 --- a/src/CBot/old TstCBot/TstCBot.h +++ /dev/null @@ -1,62 +0,0 @@ -// TstCBot.h : main header file for the TSTCBOT application -// - -#if !defined(AFX_TSTCBOT_H__70B37568_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) -#define AFX_TSTCBOT_H__70B37568_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - -#ifndef __AFXWIN_H__ - #error include 'stdafx.h' before including this file for PCH -#endif - -#include "resource.h" // main symbols -#include "..\CBotDll.h" - - -class CMyThread; - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotApp: -// See TstCBot.cpp for the implementation of this class -// - -class CTstCBotApp : public CWinApp -{ -public: - CTstCBotApp(); - - CMyThread* m_pThread; - CWnd* m_pView; - CEdit* m_pConsole; - CBotStringArray m_Liste; - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CTstCBotApp) - public: - virtual BOOL InitInstance(); - //}}AFX_VIRTUAL - -// Implementation - - //{{AFX_MSG(CTstCBotApp) - afx_msg void OnAppAbout(); - // NOTE - the ClassWizard will add and remove member functions here. - // DO NOT EDIT what you see in these blocks of generated code ! - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_TSTCBOT_H__70B37568_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) - -#define WM_STARTPROG WM_APP + 0 -#define WM_ENDPROG WM_APP + 1 diff --git a/src/CBot/old TstCBot/TstCBot.rc b/src/CBot/old TstCBot/TstCBot.rc deleted file mode 100644 index 9e91c76..0000000 --- a/src/CBot/old TstCBot/TstCBot.rc +++ /dev/null @@ -1,471 +0,0 @@ -//Microsoft Developer Studio generated resource script. -// -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "afxres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// French (France) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) -#ifdef _WIN32 -LANGUAGE LANG_FRENCH, SUBLANG_FRENCH -#pragma code_page(1252) -#endif //_WIN32 - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE DISCARDABLE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE DISCARDABLE -BEGIN - "#include ""afxres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE DISCARDABLE -BEGIN - "#define _AFX_NO_SPLITTER_RESOURCES\r\n" - "#define _AFX_NO_OLE_RESOURCES\r\n" - "#define _AFX_NO_TRACKER_RESOURCES\r\n" - "#define _AFX_NO_PROPERTY_RESOURCES\r\n" - "\r\n" - "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)\r\n" - "#ifdef _WIN32\r\n" - "LANGUAGE 12, 1\r\n" - "#pragma code_page(1252)\r\n" - "#endif\r\n" - "#include ""res\\TstCBot.rc2"" // non-Microsoft Visual C++ edited resources\r\n" - "#include ""l.fra\\afxres.rc"" // Standard components\r\n" - "#endif\0" -END - -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// Icon -// - -// Icon with lowest ID value placed first to ensure application icon -// remains consistent on all systems. -IDR_MAINFRAME ICON DISCARDABLE "res\\TstCBot.ico" -IDR_TSTCBOTYPE ICON DISCARDABLE "res\\TstCBotDoc.ico" - -///////////////////////////////////////////////////////////////////////////// -// -// Menu -// - -IDR_MAINFRAME MENU PRELOAD DISCARDABLE -BEGIN - POPUP "&Fichier" - BEGIN - MENUITEM "&Nouveau\tCtrl+N", ID_FILE_NEW - MENUITEM "&Ouvrir...\tCtrl+O", ID_FILE_OPEN - MENUITEM "&Enregistrer\tCtrl+S", ID_FILE_SAVE - MENUITEM "En®istrer sous...", ID_FILE_SAVE_AS - MENUITEM SEPARATOR - MENUITEM "Fichier rcent", ID_FILE_MRU_FILE1, GRAYED - MENUITEM SEPARATOR - MENUITEM "&Quitter", ID_APP_EXIT - END - POPUP "&Edition" - BEGIN - MENUITEM "&Annuler\tCtrl+Z", ID_EDIT_UNDO - MENUITEM SEPARATOR - MENUITEM "&Couper\tCtrl+X", ID_EDIT_CUT - MENUITEM "&Copier\tCtrl+C", ID_EDIT_COPY - MENUITEM "C&oller\tCtrl+V", ID_EDIT_PASTE - END - POPUP "&Affichage" - BEGIN - MENUITEM "Barre d'&tat", ID_VIEW_STATUS_BAR - END - POPUP "&Tests" - BEGIN - MENUITEM "&Compile\tAlt+C", ID_CP1 - MENUITEM "&Execute\tAlt+V", ID_EXE - END - POPUP "&?" - BEGIN - MENUITEM "&A propos de TstCBot...", ID_APP_ABOUT - END -END - - -///////////////////////////////////////////////////////////////////////////// -// -// Accelerator -// - -IDR_MAINFRAME ACCELERATORS PRELOAD MOVEABLE PURE -BEGIN - "C", ID_CP1, VIRTKEY, ALT, NOINVERT - "N", ID_FILE_NEW, VIRTKEY, CONTROL, NOINVERT - "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT - "S", ID_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT - "V", ID_EXE, VIRTKEY, ALT, NOINVERT - VK_BACK, ID_EDIT_UNDO, VIRTKEY, ALT, NOINVERT - VK_DELETE, ID_EDIT_CUT, VIRTKEY, SHIFT, NOINVERT - VK_F5, ID_EXE, VIRTKEY, NOINVERT - VK_F7, ID_CP1, VIRTKEY, NOINVERT - "X", ID_EXE, VIRTKEY, ALT, NOINVERT -END - - -///////////////////////////////////////////////////////////////////////////// -// -// Dialog -// - -IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "A propos de TstCBot" -FONT 8, "MS Sans Serif" -BEGIN - ICON IDR_MAINFRAME,IDC_STATIC,11,17,20,20 - LTEXT "TstCBot version 1.0",IDC_STATIC,40,10,119,8,SS_NOPREFIX - LTEXT "Copyright (C) 1900",IDC_STATIC,40,25,119,8 - DEFPUSHBUTTON "OK",IDOK,178,7,32,14,WS_GROUP -END - - -#ifndef _MAC -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,0,0,1 - PRODUCTVERSION 1,0,0,1 - FILEFLAGSMASK 0x3fL -#ifdef _DEBUG - FILEFLAGS 0x1L -#else - FILEFLAGS 0x0L -#endif - FILEOS 0x4L - FILETYPE 0x1L - FILESUBTYPE 0x0L -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040C04B0" - BEGIN - VALUE "CompanyName", "\0" - VALUE "FileDescription", "Application MFC TstCBot\0" - VALUE "FileVersion", "1, 0, 0, 1\0" - VALUE "InternalName", "TstCBot\0" - VALUE "LegalCopyright", "Copyright (C) 1900\0" - VALUE "LegalTrademarks", "\0" - VALUE "OriginalFilename", "TstCBot.EXE\0" - VALUE "ProductName", "Application TstCBot\0" - VALUE "ProductVersion", "1, 0, 0, 1\0" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Traduction", 0x40c, 1200 - END -END - -#endif // !_MAC - - -///////////////////////////////////////////////////////////////////////////// -// -// DESIGNINFO -// - -#ifdef APSTUDIO_INVOKED -GUIDELINES DESIGNINFO DISCARDABLE -BEGIN - IDD_ABOUTBOX, DIALOG - BEGIN - LEFTMARGIN, 7 - RIGHTMARGIN, 210 - TOPMARGIN, 7 - BOTTOMMARGIN, 48 - END -END -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// String Table -// - -STRINGTABLE PRELOAD DISCARDABLE -BEGIN - IDR_MAINFRAME "TstCBot\n\nTstCBo\n\n\nTstCBot.Document\nTstCBo Document" -END - -STRINGTABLE PRELOAD DISCARDABLE -BEGIN - AFX_IDS_APP_TITLE "TstCBot" - AFX_IDS_IDLEMESSAGE "Prt" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_INDICATOR_EXT "EXT" - ID_INDICATOR_CAPS "MAJ" - ID_INDICATOR_NUM "NUM" - ID_INDICATOR_SCRL "DEF" - ID_INDICATOR_OVR "ECR" - ID_INDICATOR_REC "ENR" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_FILE_NEW "Cre un nouveau document\nNouveau" - ID_FILE_OPEN "Ouvre un document existant\nOuvrir" - ID_FILE_CLOSE "Ferme le document actif\nFermer" - ID_FILE_SAVE "Enregistre le document actif\nEnregistrer" - ID_FILE_SAVE_AS "Enregistre le document actif sous un nouveau nom\nEnregistrer sous" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_APP_ABOUT "Affiche des informations sur le programme, le numro de version et le copyright\nA propos de" - ID_APP_EXIT "Ferme l'application ; propose d'enregistrer les documents\nQuitter" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_FILE_MRU_FILE1 "Ouvre ce document" - ID_FILE_MRU_FILE2 "Ouvre ce document" - ID_FILE_MRU_FILE3 "Ouvre ce document" - ID_FILE_MRU_FILE4 "Ouvre ce document" - ID_FILE_MRU_FILE5 "Ouvre ce document" - ID_FILE_MRU_FILE6 "Ouvre ce document" - ID_FILE_MRU_FILE7 "Ouvre ce document" - ID_FILE_MRU_FILE8 "Ouvre ce document" - ID_FILE_MRU_FILE9 "Ouvre ce document" - ID_FILE_MRU_FILE10 "Ouvre ce document" - ID_FILE_MRU_FILE11 "Ouvre ce document" - ID_FILE_MRU_FILE12 "Ouvre ce document" - ID_FILE_MRU_FILE13 "Ouvre ce document" - ID_FILE_MRU_FILE14 "Ouvre ce document" - ID_FILE_MRU_FILE15 "Ouvre ce document" - ID_FILE_MRU_FILE16 "Ouvre ce document" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_NEXT_PANE "Passe au volet de fentre suivant\nVolet suivant" - ID_PREV_PANE "Revient au volet prcdent\nVolet prcdent" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_WINDOW_SPLIT "Fractionne la fentre active en deux volets\nFractionner" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_EDIT_CLEAR "Efface la slection\nEffacer" - ID_EDIT_CLEAR_ALL "Efface tout\nEffacer tout" - ID_EDIT_COPY "Copie la slection et la place dans le Presse-papiers\nCopier" - ID_EDIT_CUT "Supprime la slection et la place dans le Presse-papiers\nCopier" - ID_EDIT_FIND "Recherche le texte spcifi\nRechercher" - ID_EDIT_PASTE "Insre le contenu du Presse-papiers\nColler" - ID_EDIT_REPEAT "Rpte la dernire action\nRpter" - ID_EDIT_REPLACE "Remplace le texte spcifique par un texte diffrent\nRemplacer" - ID_EDIT_SELECT_ALL "Slectionne le document entier\nSlectionner tout" - ID_EDIT_UNDO "Annule la dernire action\nAnnuler" - ID_EDIT_REDO "Rtablit l'action prcdemment annule\nRtablir" -END - -STRINGTABLE DISCARDABLE -BEGIN - ID_VIEW_STATUS_BAR "Affiche ou masque la barre d'tat\nBarre d'tat" -END - -STRINGTABLE DISCARDABLE -BEGIN - AFX_IDS_SCSIZE "Change la taille de la fentre" - AFX_IDS_SCMOVE "Change la position de la fentre" - AFX_IDS_SCMINIMIZE "Rduit la fentre en icne" - AFX_IDS_SCMAXIMIZE "Agrandit la fentre au format de l'cran" - AFX_IDS_SCNEXTWINDOW "Passe la fentre de document suivante" - AFX_IDS_SCPREVWINDOW "Passe la fentre de document prcdente" - AFX_IDS_SCCLOSE "Ferme la fentre active et propose l'enregistrement des documents" -END - -STRINGTABLE DISCARDABLE -BEGIN - AFX_IDS_SCRESTORE "Restaure la fentre sa taille d'origine" - AFX_IDS_SCTASKLIST "Active la liste des tches" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_TYPENAMES "les diffrents types" - 1001 "Byte" - 1002 "Short" - 1003 "Char" - 1004 "Int" - 1005 "Long" - 1006 "Real" - 1007 "Double" -END - -STRINGTABLE DISCARDABLE -BEGIN - 1008 "Boolean" - 1009 "Class" - 1010 "String" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_OPENPAR "Il manque une parenthse ouvrante." - TX_CLOSEPAR "Il manque une parenthse fermante." - TX_NOTBOOL "L'expression doit tre un boolean." - TX_UNDEFVAR "Variable non dclare." - TX_BADLEFT "Assignation impossible." - TX_ENDOF "Instruction non termine." - TX_OUTCASE "Instruction ""case"" hors d'un bloc ""switch""." - TX_NOTERM "Instructions aprs la fin." -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_CLOSEBLK "Il manque la fin du bloc." - TX_ELSEWITHOUTIF "Instruction ""else"" sans ""if"" correspondant." - TX_OPENBLK "Dbut d'un bloc attendu." - TX_BADTYPE "Mauvais type de rsultat pour l'assignation." - TX_REDEFVAR "Redfinition d'une variable." - TX_BAD2TYPE "Les deux oprandes ne sont pas de types compatibles." - TX_UNDEFCALL "Routine inconnue." - TX_MISDOTS "Sparateur "" : "" attendu." - TX_WHILE "Manque le mot ""while""." - TX_BREAK "Instruction ""break"" en dehors d'une boucle." - TX_LABEL "Un label ne peut se placer que devant un ""for"", un ""while"", un ""do"" ou un ""switch""." - TX_NOLABEL "Cette tiquette n'existe pas" - TX_NOCASE "Manque une instruction ""case""." - TX_BADNUM "Un nombre est attendu." - TX_VOID "Paramtre void." - TX_NOTYP "Dclaration de type attendu" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_DIVZERO "Division par zro." - TX_NOTINIT "Variable non initialise." - TX_BADTHROW "Valeur ngative refuse pour ""throw""." - TX_NORETVAL "La fonction n'a pas retourn de rsultat" - TX_NORUN "Pas de fonction en excution" - TX_NOCALL "Appel d'une fonction inexistante" -END - -STRINGTABLE DISCARDABLE -BEGIN - TX_NOVAR "Nom d'une variable attendu" - TX_NOFONC "Nom de la fonction attendu." - TX_OVERPARAM "Trop de paramtres" - TX_REDEF "Cette fonction existe dj." - TX_LOWPARAM "Pas assez de paramtres" - TX_BADPARAM "Aucune fonction de ce nom n'accepte ce(s) type(s) de paramtre(s)" - TX_NUMPARAM "Aucune fonction de ce nom n'accepte ce nombre de paramtres" - TX_NOITEM "Cet lment n'exite pas dans cette classe." - TX_DOT "L'objet n'est pas une instance d'une classe." - TX_NOCONST "Il n'y a pas de constructeur appropri." - TX_REDEFCLASS "Cette classe existe dj." -END - -#endif // French (France) resources -///////////////////////////////////////////////////////////////////////////// - - -///////////////////////////////////////////////////////////////////////////// -// French (Switzerland) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRS) -#ifdef _WIN32 -LANGUAGE LANG_FRENCH, SUBLANG_FRENCH_SWISS -#pragma code_page(1252) -#endif //_WIN32 - -///////////////////////////////////////////////////////////////////////////// -// -// Dialog -// - -IDD_CONSOLE DIALOG DISCARDABLE 0, 0, 401, 210 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "CBot Console" -FONT 8, "MS Sans Serif" -BEGIN - LTEXT "Commande :",IDC_STATIC,7,177,40,8 - EDITTEXT IDC_EDIT2,7,189,329,14,ES_AUTOHSCROLL - DEFPUSHBUTTON "Excute",IDOK,344,189,50,14 - EDITTEXT IDC_EDIT1,7,7,387,167,ES_MULTILINE | ES_READONLY | - ES_WANTRETURN | WS_VSCROLL -END - - -///////////////////////////////////////////////////////////////////////////// -// -// DESIGNINFO -// - -#ifdef APSTUDIO_INVOKED -GUIDELINES DESIGNINFO DISCARDABLE -BEGIN - IDD_CONSOLE, DIALOG - BEGIN - LEFTMARGIN, 7 - RIGHTMARGIN, 394 - TOPMARGIN, 7 - BOTTOMMARGIN, 203 - END -END -#endif // APSTUDIO_INVOKED - -#endif // French (Switzerland) resources -///////////////////////////////////////////////////////////////////////////// - - - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// -#define _AFX_NO_SPLITTER_RESOURCES -#define _AFX_NO_OLE_RESOURCES -#define _AFX_NO_TRACKER_RESOURCES -#define _AFX_NO_PROPERTY_RESOURCES - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) -#ifdef _WIN32 -LANGUAGE 12, 1 -#pragma code_page(1252) -#endif -#include "res\TstCBot.rc2" // non-Microsoft Visual C++ edited resources -#include "l.fra\afxres.rc" // Standard components -#endif -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED - diff --git a/src/CBot/old TstCBot/TstCBotDoc.cpp b/src/CBot/old TstCBot/TstCBotDoc.cpp deleted file mode 100644 index 7d7e2ef..0000000 --- a/src/CBot/old TstCBot/TstCBotDoc.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// TstCBotDoc.cpp : implementation of the CTstCBotDoc class -// - -#include "stdafx.h" -#include "TstCBot.h" - -#include "TstCBotDoc.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotDoc - -IMPLEMENT_DYNCREATE(CTstCBotDoc, CDocument) - -BEGIN_MESSAGE_MAP(CTstCBotDoc, CDocument) - //{{AFX_MSG_MAP(CTstCBotDoc) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotDoc construction/destruction - -CTstCBotDoc::CTstCBotDoc() -{ - // TODO: add one-time construction code here - -} - -CTstCBotDoc::~CTstCBotDoc() -{ -} - -BOOL CTstCBotDoc::OnNewDocument() -{ - if (!CDocument::OnNewDocument()) - return FALSE; - - // TODO: add reinitialization code here - // (SDI documents will reuse this document) - - return TRUE; -} - - - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotDoc serialization - -void CTstCBotDoc::Serialize(CArchive& ar) -{ - if (ar.IsStoring()) - { - // TODO: add storing code here - } - else - { - // TODO: add loading code here - } -} - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotDoc diagnostics - -#ifdef _DEBUG -void CTstCBotDoc::AssertValid() const -{ - CDocument::AssertValid(); -} - -void CTstCBotDoc::Dump(CDumpContext& dc) const -{ - CDocument::Dump(dc); -} -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotDoc commands - diff --git a/src/CBot/old TstCBot/TstCBotDoc.h b/src/CBot/old TstCBot/TstCBotDoc.h deleted file mode 100644 index ae1d0f7..0000000 --- a/src/CBot/old TstCBot/TstCBotDoc.h +++ /dev/null @@ -1,55 +0,0 @@ -// TstCBotDoc.h : interface of the CTstCBotDoc class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_TSTCBOTDOC_H__70B3756E_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) -#define AFX_TSTCBOTDOC_H__70B3756E_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - - -class CTstCBotDoc : public CDocument -{ -protected: // create from serialization only - CTstCBotDoc(); - DECLARE_DYNCREATE(CTstCBotDoc) - -// Attributes -public: - -// Operations -public: - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CTstCBotDoc) - public: - virtual BOOL OnNewDocument(); - virtual void Serialize(CArchive& ar); - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CTstCBotDoc(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -protected: - -// Generated message map functions -protected: - //{{AFX_MSG(CTstCBotDoc) - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_TSTCBOTDOC_H__70B3756E_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/old TstCBot/TstCBotView.cpp b/src/CBot/old TstCBot/TstCBotView.cpp deleted file mode 100644 index 3ee9094..0000000 --- a/src/CBot/old TstCBot/TstCBotView.cpp +++ /dev/null @@ -1,291 +0,0 @@ -// TstCBotView.cpp : implementation of the CTstCBotView class -// - -#include "stdafx.h" -#include "TstCBot.h" - -#include "TstCBotDoc.h" -#include "TstCBotView.h" -#include "BotConsoleDlg.h" - -#ifdef _DEBUG -#define new DEBUG_NEW -#undef THIS_FILE -static char THIS_FILE[] = __FILE__; -#endif - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotView - -IMPLEMENT_DYNCREATE(CTstCBotView, CView) - -BEGIN_MESSAGE_MAP(CTstCBotView, CView) - //{{AFX_MSG_MAP(CTstCBotView) - ON_WM_SIZE() - ON_COMMAND(ID_CP1, OnCp1) - ON_COMMAND(ID_EXE, OnExe) - ON_COMMAND(ID_FILE_SAVE, OnFileSave) - ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) - //}}AFX_MSG_MAP -END_MESSAGE_MAP() - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotView construction/destruction - -CTstCBotView::CTstCBotView() -{ - // TODO: add construction code here - m_pEdit = NULL; - m_pProg = NULL; -} - -CTstCBotView::~CTstCBotView() -{ -} - -BOOL CTstCBotView::PreCreateWindow(CREATESTRUCT& cs) -{ - // TODO: Modify the Window class or styles here by modifying - // the CREATESTRUCT cs - - return CView::PreCreateWindow(cs); -} - -void CTstCBotView::OnActivateView( BOOL bActivate, CView* pActivateView, CView* pDeactiveView ) -{ - if ( m_pEdit == NULL) - { - m_pEdit = new CEdit(); - CRect rect; - GetClientRect( rect ); - - m_pEdit->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_NOHIDESEL|ES_AUTOVSCROLL, - rect, this, IDC_EDIT1 ); - m_pEdit->SetTabStops(12); - LoadEdition("CBotTest.txt"); - m_pEdit->SetFocus(); - } -} - - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotView drawing - -void CTstCBotView::OnDraw(CDC* pDC) -{ - CTstCBotDoc* pDoc = GetDocument(); - ASSERT_VALID(pDoc); - - // TODO: add draw code for native data here -} - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotView diagnostics - -#ifdef _DEBUG -void CTstCBotView::AssertValid() const -{ - CView::AssertValid(); -} - -void CTstCBotView::Dump(CDumpContext& dc) const -{ - CView::Dump(dc); -} - -CTstCBotDoc* CTstCBotView::GetDocument() // non-debug version is inline -{ - ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTstCBotDoc))); - return (CTstCBotDoc*)m_pDocument; -} -#endif //_DEBUG - -///////////////////////////////////////////////////////////////////////////// -// CTstCBotView message handlers - -void CTstCBotView::OnSize(UINT nType, int cx, int cy) -{ - CView::OnSize(nType, cx, cy); - - if ( m_pEdit != NULL ) - { - CRect rect; - GetClientRect( rect ); - m_pEdit->MoveWindow( rect ); - m_pEdit->SetFocus(); - } -} - -void CTstCBotView::SaveEdition(const char* filename) -{ - CString program; - - m_pEdit->GetWindowText(program); - - FILE* pf = fopen(filename, "wb"); - if (pf==NULL) return; - - fputs (program, pf); - fclose(pf); -} - -void CTstCBotView::LoadEdition(const char* filename) -{ - CString program("{ int x = 10000; while (x > 0) x = x-1; }"); - - FILE* pf = fopen(filename, "r"); - if (pf!=NULL) - { - char buffer[10000]; - program.Empty(); - - while (NULL != fgets (buffer, 100000, pf)) - { - program += buffer; - program = program.Left(program.GetLength()-1) + "\r\n"; - } - - fclose(pf); - } - - m_pEdit->SetWindowText(program); -} - - - -// compile le programme -#include - -void CTstCBotView::OnCp1() -{ - CString program; - - SaveEdition("CBotTest.txt"); - - m_pEdit->GetWindowText(program); - - CString TextError; - int code, start, end; - - if ( m_pProg == NULL ) m_pProg = new CBotProgram(); - - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - - if (m_pProg->Compile(program, pApp->m_Liste)) - { - CString done = "Compilation sans erreur.\nLes fonctions suivantes sont externes:\n"; - - for ( int i = 0; i < pApp->m_Liste.RetSize(); i++) - { - done += CString(pApp->m_Liste[i]) + "\n"; - } - - AfxMessageBox( done ); - } - else - { - m_pProg->GetError(code, start, end); - delete m_pProg; - m_pProg = NULL; - - m_pEdit->SetSel( start, end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - TextError.LoadString( code ); - if (TextError.IsEmpty()) - { - char buf[100]; - sprintf(buf, "Erreur numro %d.", code); - TextError = buf; - } - AfxMessageBox( TextError ); - } - - m_pEdit->SetFocus(); -} - - -////////////////////////////////////////////////////// - - -void CTstCBotView::OnExe() -{ - CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); - - if( m_pProg == NULL) - { - AfxMessageBox("Pas de programme compil !"); - return; - } - - if( pApp->m_Liste.RetSize() == 0 ) - { - AfxMessageBox("Aucune fonction marque \"extern\" !"); - return; - } - - - - CBotConsoleDlg dlg; - dlg.DoModal(); // dialogue pour faire la console - - if ( dlg.m_code>0 ) - { - CString TextError; - - m_pEdit->SetSel( dlg.m_start, dlg.m_end ); - m_pEdit->SetFocus(); // met en vidence la partie avec problme - - TextError.LoadString( dlg.m_code ); - if (TextError.IsEmpty()) - { - char buf[100]; - sprintf(buf, "Erreur numro %d.", dlg.m_code); - TextError = buf; - } -// AfxMessageBox( TextError ); - } - - m_pEdit->SetFocus(); - - return; -} - - - -void CTstCBotView::OnFileSave() -{ - // TODO: Add your command handler code here - SaveEdition("CBotTest.txt"); -} - -void CTstCBotView::OnFileSaveAs() -{ - CFileDialog *pDlg; - CString s; - - pDlg = new CFileDialog(FALSE, "TXT", NULL, - OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY, - "cboxtest|*.txt", this); - if ( pDlg == NULL ) return; - - if ( pDlg->DoModal() == IDOK ) // choix du fichier ... - { - SaveEdition(pDlg->GetPathName()); - } - - delete pDlg; -} - -#if 0 -void test() -{ - int y,z; - - for (;;); - for (x = 0; y = 1; z = 3) int q = 6; - for (int x = 0; int y = 1; int z = 3) int q = 6; - // pour voir -} -#endif - diff --git a/src/CBot/old TstCBot/TstCBotView.h b/src/CBot/old TstCBot/TstCBotView.h deleted file mode 100644 index d5aede5..0000000 --- a/src/CBot/old TstCBot/TstCBotView.h +++ /dev/null @@ -1,81 +0,0 @@ -// TstCBotView.h : interface of the CTstCBotView class -// -///////////////////////////////////////////////////////////////////////////// - -#if !defined(AFX_TSTCBOTVIEW_H__70B37570_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) -#define AFX_TSTCBOTVIEW_H__70B37570_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ - -#if _MSC_VER >= 1000 -#pragma once -#endif // _MSC_VER >= 1000 - - -class CBotProgram; -class CBotClass; - - -class CTstCBotView : public CView -{ -protected: // create from serialization only - CTstCBotView(); - DECLARE_DYNCREATE(CTstCBotView) - - CEdit* m_pEdit; // texte en dition - CWnd* m_pWnd; - CBotProgram* m_pProg; // programme compil - -// Attributes -public: - CTstCBotDoc* GetDocument(); - -// Operations -public: - void LoadEdition(const char* name); - void SaveEdition(const char* name); - -// Overrides - // ClassWizard generated virtual function overrides - //{{AFX_VIRTUAL(CTstCBotView) - public: - virtual void OnDraw(CDC* pDC); // overridden to draw this view - virtual BOOL PreCreateWindow(CREATESTRUCT& cs); - virtual void OnActivateView( BOOL bActivate, CView* pActivateView, CView* pDeactiveView ); - protected: - //}}AFX_VIRTUAL - -// Implementation -public: - virtual ~CTstCBotView(); -#ifdef _DEBUG - virtual void AssertValid() const; - virtual void Dump(CDumpContext& dc) const; -#endif - -protected: - -// Generated message map functions -protected: - //{{AFX_MSG(CTstCBotView) - afx_msg void OnSize(UINT nType, int cx, int cy); - afx_msg void OnCp1(); - afx_msg void OnExe(); - afx_msg void OnFileSave(); - afx_msg void OnFileSaveAs(); - //}}AFX_MSG - DECLARE_MESSAGE_MAP() -}; - -#ifndef _DEBUG // debug version in TstCBotView.cpp -inline CTstCBotDoc* CTstCBotView::GetDocument() - { return (CTstCBotDoc*)m_pDocument; } -#endif - -///////////////////////////////////////////////////////////////////////////// - -//{{AFX_INSERT_LOCATION}} -// Microsoft Developer Studio will insert additional declarations immediately before the previous line. - -#endif // !defined(AFX_TSTCBOTVIEW_H__70B37570_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) - - - diff --git a/src/CBot/old TstCBot/res/TstCBot.ico b/src/CBot/old TstCBot/res/TstCBot.ico deleted file mode 100644 index 7eef0bc..0000000 Binary files a/src/CBot/old TstCBot/res/TstCBot.ico and /dev/null differ diff --git a/src/CBot/old TstCBot/res/TstCBot.rc2 b/src/CBot/old TstCBot/res/TstCBot.rc2 deleted file mode 100644 index 2186272..0000000 --- a/src/CBot/old TstCBot/res/TstCBot.rc2 +++ /dev/null @@ -1,13 +0,0 @@ -// -// TSTCBOT.RC2 - resources Microsoft Visual C++ does not edit directly -// - -#ifdef APSTUDIO_INVOKED - #error this file is not editable by Microsoft Visual C++ -#endif //APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// Add manually edited resources here... - -///////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/old TstCBot/res/TstCBotDoc.ico b/src/CBot/old TstCBot/res/TstCBotDoc.ico deleted file mode 100644 index 2a1f1ae..0000000 Binary files a/src/CBot/old TstCBot/res/TstCBotDoc.ico and /dev/null differ diff --git a/src/CBot/old TstCBot/test complet 1.txt b/src/CBot/old TstCBot/test complet 1.txt deleted file mode 100644 index 0fd4fa5..0000000 --- a/src/CBot/old TstCBot/test complet 1.txt +++ /dev/null @@ -1,213 +0,0 @@ -// test de l'interprteur CBot, (c) D. Dumoulin 2000 - -Int Somme ( Int x, Int y ) -{ - return x + y; -} - -Real Somme ( Real x, Real y ) -{ - return x + y; -} - -void A_Faire() -{ - CPoint position; // utilise une classe externe - position.x = 123.5; - position.y = -45.1; - - show ( position ); -} - -/* Les nouveauts sont les suivantes - __________________________________________________ - - On peut dfinir des fonctions, avec la syntaxe habituelle au C - void MaFonction( Int x, Real y ) { ... } - - Les caractristiques sont pour l'instant les suivantes: - - - ce programme TstCBot excute la dernire fonction dfinie - - - on peut dfinir deux fonctions du mme nom, - si la liste de paramtres est diffrente. - Par exemple - Int Somme( Int x, Int y ) - Real Somme( Real x, Real y ); - Note: si la seconde n'existait pas, Somme ( 1.3, 4.8 ) - serait fait sur les nombres entier 1 + 4 - La priorit est donne la routine qui ne pert pas - de bits dans la conversion des paramtres. - - - il n'y a pas d'erreur de compilation si une routine - ne retourne pas de valeur alors qu'elle devrait, - par contre il y a une erreur "correcte" l'excution - - - il est possible d'utiliser une fonction qui est dfinie - plus bas dans le programme. - __________________________________________________ - - Tous les blocs d'instructions existent maintenant, savoir - - label : - while (condition) { instructions; break label; continue label; } - - label : - do { instructions; break label; continue label; } while (condition) - - label: - for (initial; condition; incrment) { instructions; break; continue } - - switch ( valeur ) { case 1: instructions; case 2: break ; } - - try {instructions; throw exception; } catch (exception) {instructions;} - catch (testlogique) {instructions;} - finally {instructions;} - // le bloc finally est excuter dans tous les cas - // qu'il y ait eu exception ou non, et aussi en cas de break, continue ou return - __________________________________________________ - - Les "exceptions" sont juste des numros (31 bits) - 6000 = division par zro - 6001 = variable non initialise - 6002 = valeur ngative pour un throw - 6003 = la fonction n'a pas retourn de valeur - - les autres numros sont disposition - (COLOBOT aura surement des numros d'exception propre) - l'association d'un mot clef pour ces exceptions est venir. - __________________________________________________ - - L'interprteur a t un peu optimiser, une boucle de un millon de dcrment - ne prend plus que -*/ - -void Test () -{ // dbut du bloc d'instructions - - Int z = 1000000; - while ( z>0 ) z--; - - return; - { - // test la prsance pour les assignations - Int a = 9; - a += (a = 3); - if ( a != 12 ) 1/0; // le rsultat correct est 12 - - Int b = 9; - b = b + (b = 3); - if (b != 12) 1/0; // mme chose - - // la fonction show est une fonction externe - // dfinie par TstCBot - // elle peut prendre un nombre quelconque de paramtres - show ( a, b ); - } - - { - // petit test sur les chanes - String x = "ch." ; - String y ; - x += y = x + " de la brume."; - - // concatnation de chanes, accepte des autres types - String s = 1 + 2 + " test " + 3 + 4 ; - - show( x, y, s ); - - // les tests sur les chanes ne sont pas standard en Java - // mais c'est si pratique : - - if ( s != "3 test 34" ) 1/0; // le rsultat correct est "3 test 34" - // car 1+2 est valu en premier entre 2 nombres - // et ensuite on additionne des chanes "3" "4" - } - - { - // teste toutes les oprations avec les entiers (32 bits) - Int a = 4; - Int b = 4; - - Int c = a++ * --b; // post incrment, pr dcrment - if ( c != 12 ) 1/0; - - c = ++a * b--; // pr incrment, post dcrment - if ( c!=18 ) 1/0; - - a = a+b-a*b/a%3; // 6 + 2 - ( 6 * 2 / 6 % 3 ) -> 6 - if ( a != 6 ) 1/0; - - a += 2; a-=1; a*=3; a/=4; a%=3; // (6+2 -1) *3 /4 modulo 3 = 21 / 4 modulo 3 = 2 - if ( a!= 2) 0/0; - - if (-5 << 3 != -40) 0/0; // shift gauche - if ( -5 >> 1 != -3) 0/0; // shift arithmtique droite 11111011 -> 11111101 = -3 - if ( -5 >>> 1 != 0x3ffffffd) 0/0; // shift non sign droite - - a = -10; // fait la mme chose en assignation - a <<= 1; // -20 - a >>= 2; // -5 - a >>>= 1; // pert le signe - if ( a != 0x3ffffffd) 0/0; // - - Int x = 5/3; // division d'entiers - if ( x != 1 ) 0/0; - Int xx = 5.0/3.0; // division de rels, assign un entier - if ( xx != 1 ) 0/0; - - Int y = 0xF0035678; - if ( ~y != 0x0FFCA987 ) 0/0; // NOT bit bit - if ( (0x3456 ^ 0x54f0) != 0x60A6) // XOR bit bit - 0/0; - if ( (0x23 | 0x83) != 0xA3 ) 0/0; // OR bit bit - if ( (0x23 & 0x83) != 0x03 ) 0/0; // AND bit bit - - Int z = 0x0123; - z |= 0x8010; if ( z != 0x8133) 0/0; - z &= 0xF018; if ( z != 0x8010) 0/0; - z ^= 0xFF17; if ( z != 0x7F07) 0/0; - } - - { - // test pour les boolens - Boolean a, b= true, c = false; - a = b | c & b; - if ( a != b ) 0/0; - if ( !a ) 0/0; - if ( b ^ a ) 0/0; // XOR - if ( true || 0/0<1 ) {}; - if ( false && 0/0<1) {}; - // a ? "vrai" : "faux"; - } - - { - // petit test sur les nombres rels - Real x = 1. / 3, y = 0; - - if ( 3 * x != 1 ) x = x / y; // provoque une division par zro - else y = 1.123; - } - - - // test de dure - // attention, le programme de test ne stoppe qu' la fin d'excution - // bien que la boucle est interrompue plusieures fois - - // la boucle est plus rapide si elle est au dbut du programme ! - { - Int z = 10000; - while ( z > 0 ) z = z - 1; - } - -} - -void t() -{ - A_Faire(); - - show ( Somme ( 1, 2 ) ); - show ( Somme ( 1., 2 ) ); - show ( Somme ( 4.5, 2.7 ) ); -} - diff --git a/src/CBot/old TstCBot/x.txt b/src/CBot/old TstCBot/x.txt deleted file mode 100644 index 95856e0..0000000 --- a/src/CBot/old TstCBot/x.txt +++ /dev/null @@ -1,43 +0,0 @@ -// test de l'interprteur CBot, (c) D. Dumoulin 2000 - -// pour l'instant, seule les primitives suivantes sont implmentes - -// { ... ; ... ; ... } un bloc d'instructions -// int x, y = 12, z; // dclaration de nombre entier -// float a, b= 2/3, c=b+1; // dclaration de nombres rels -// boolean tst = true; // dclaration d'un boolen -// String x = "hello"; // dclaration d'une chanes - -// z = x = x * y / ( z + 1 - x ); // assignation en chane et les 4 oprations - -// while ( x >= 0 ) x = x - 1; // boucle while, et test > >= < <= == != -// if ( x < y ) x = x + 1; // test si -// else y = y + 1; // sinon - -/* et les oprations suivantes: - + plus unaire x = +y; - - moins unaire x = -y; - - || OU logique - && ET logique - ! NOT logique - | OU bit bit - & ET bit bit - ^ XOR bit bit - ~ NON bit bit - -// les commentaires sont accepts -/* y compris les commentaires - sur plusieures lignes */ - - -{ -String str ; - -str = "abc" ; - -show (str) ; - -show( str = str + "+++" , ) ; - -} diff --git a/src/CBot/resource.h b/src/CBot/resource.h index 6bf48e1..da1ad23 100644 --- a/src/CBot/resource.h +++ b/src/CBot/resource.h @@ -12,7 +12,9 @@ // * GNU General Public License for more details. // * // * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/.//{{NO_DEPENDENCIES}} +// * along with this program. If not, see http://www.gnu.org/licenses/. + +#pragma once #ifndef _RESOURCE_H_ #define _RESOURCE_H_ @@ -173,4 +175,3 @@ enum EID #define TX_ERRWRITE 6015 #endif //_RESOURCE_H_ - diff --git a/src/CBot/tests/TestCBot/CBotConsoleDlg.cpp b/src/CBot/tests/TestCBot/CBotConsoleDlg.cpp new file mode 100644 index 0000000..55a271a --- /dev/null +++ b/src/CBot/tests/TestCBot/CBotConsoleDlg.cpp @@ -0,0 +1,221 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// CBotConsoleDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "TestCBot.h" +#include "CBotConsoleDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CBotConsoleDlg dialog + + +CBotConsoleDlg::CBotConsoleDlg(CWnd* pParent /*=NULL*/) + : CDialog(CBotConsoleDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(CBotConsoleDlg) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + m_pProg = NULL; + m_threadinfo.m_bRun = FALSE; + m_code = 0; +} + + +void CBotConsoleDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(CBotConsoleDlg) + DDX_Control(pDX, IDOK, m_cOK); + DDX_Control(pDX, IDC_EDIT2, m_Edit2); + DDX_Control(pDX, IDC_EDIT1, m_Edit1); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(CBotConsoleDlg, CDialog) + //{{AFX_MSG_MAP(CBotConsoleDlg) + ON_MESSAGE(WM_ENDPROG, EndProg) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CBotConsoleDlg message handlers + +UINT ThreadProc(ThreadInfo *info) +{ + CTime t0 = CTime::GetCurrentTime(); + int Cpt = 0; + + info->m_pProg->Start("LaCommande"); + while ( !info->m_bStop && !info->m_pProg->Run() ) + { +#if 0 + const char* FunctionName; + const char* FN; + int start, end; + + info->m_pProg->GetRunPos(FunctionName, start, end); + + if ( FunctionName != NULL ) + { + info->m_pEditx->SetSel(start, end); + + char buffer[200]; + sprintf( buffer, "step %s, %d, %d",FunctionName, start, end); + AfxMessageBox( buffer ); + + int level = 0; + do + { + CBotVar* t = info->m_pProg->GivStackVars(FN, level--); + if ( FN != FunctionName ) break; + if ( t != NULL ) + { + CString s ; + while ( t != NULL ) + { + if (s.IsEmpty()) s+= "Stack -> "; + else s+= " , "; + s += t->GivValString(); + t = t->GivNext(); + } + AfxMessageBox(s); + } + } while (TRUE); + } +#endif + Cpt++; + if ( Cpt%50 == 0 ) info->m_pEdit1->ReplaceSel("."); + } + + if ( info->m_bStop ) + { + info->m_pEdit1->ReplaceSel("\r\nInterrompu\r\n"); + } + else if (info->m_pProg->GivError() == 0) + { + CTime t = CTime::GetCurrentTime(); + CTimeSpan ts = t - t0; + + char buffer[200]; + sprintf( buffer, "\r\nExcution termine en %d secondes.\r\nInterrompue %d fois.\r\n", + ts.GetTotalSeconds(), Cpt); + + info->m_pEdit1->ReplaceSel(buffer); + } + + info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ; + return 0 ; +} + +LONG CBotConsoleDlg::EndProg(UINT wparam, LONG lparam) +{ + m_threadinfo.m_bRun = FALSE; + + if (m_pProg->GetError(m_code, m_start, m_end)) + { + CBotString TextError; + TextError = CBotProgram::GivErrorText(m_code); + AfxMessageBox(TextError); + CDialog::OnCancel(); + return 1; + } + delete m_pProg; + m_pProg = NULL; + + m_Edit2.EnableWindow(TRUE); + m_cOK.EnableWindow(TRUE); + + m_Edit2.SetWindowText(""); + m_Edit2.SetFocus(); + return 0 ; +} + +void CBotConsoleDlg::OnOK() +{ + CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); + pApp->m_pConsole = &m_Edit1; + m_code = 0; + + CString Commande; + m_Edit2.GetWindowText(Commande); + + CString s = "void LaCommande() { " + Commande + " ;}"; + m_pProg = new CBotProgram(); + CBotStringArray liste; + m_pProg->Compile(s, liste); + + int err, start, end; + if ( m_pProg->GetError(err, start, end) ) + { + CBotString TextError; + TextError = CBotProgram::GivErrorText(err); + AfxMessageBox(TextError); + m_Edit2.SetSel(start-20, end-20); + return; + } + + m_Edit1.ReplaceSel("\r\n" + Commande + " ->\r\n"); + + m_Edit2.SetWindowText(""); + m_Edit1.SetFocus(); + m_Edit2.EnableWindow(FALSE); + m_cOK.EnableWindow(FALSE); + + // lance un processus paralle pour l'excution + m_threadinfo.m_pWndMessage = this ; + + m_threadinfo.m_pEdit1 = &m_Edit1; + m_threadinfo.m_pEditx = m_pEditx; + m_threadinfo.m_pProg = m_pProg; + m_threadinfo.m_bStop = FALSE; + m_threadinfo.m_bRun = TRUE; + + AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ; +} + +void CBotConsoleDlg::OnCancel() +{ + if (!m_threadinfo.m_bRun) CDialog::OnCancel(); + m_threadinfo.m_bStop = TRUE ; +} + + +BOOL CBotConsoleDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_Edit1.ReplaceSel("Les fonctions suivantes sont disponibles:\r\n"); + for ( int i = 0; i < m_pListe->GivSize(); i++ ) + { + CBotString x = (*m_pListe)[i] + CBotString("\r\n"); + m_Edit1.ReplaceSel(x); + } + m_Edit1.ReplaceSel("Entrez une commande ci-dessous.\r\n\r\n"); + + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/CBot/tests/TestCBot/CBotConsoleDlg.h b/src/CBot/tests/TestCBot/CBotConsoleDlg.h new file mode 100644 index 0000000..f289a4d --- /dev/null +++ b/src/CBot/tests/TestCBot/CBotConsoleDlg.h @@ -0,0 +1,85 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +#if !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) +#define AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 +// CBotConsoleDlg.h : header file +// + +struct ThreadInfo +{ + CEdit* m_pEdit1 ; + CEdit* m_pEditx ; + CBotProgram* m_pProg; + CWnd* m_pWndMessage; + BOOL m_bStop; + BOOL m_bRun; +}; + + +///////////////////////////////////////////////////////////////////////////// +// CBotConsoleDlg dialog + +class CBotConsoleDlg : public CDialog +{ +// Construction +public: + CBotConsoleDlg(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(CBotConsoleDlg) + enum { IDD = IDD_CONSOLE }; + CButton m_cOK; + CEdit m_Edit2; + CEdit m_Edit1; + //}}AFX_DATA + + CBotProgram* m_pProg; + ThreadInfo m_threadinfo; + + CBotStringArray* + m_pListe; + int m_code, m_start, m_end; + CEdit* m_pEditx; + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CBotConsoleDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(CBotConsoleDlg) + virtual void OnOK(); + virtual void OnCancel(); + virtual BOOL OnInitDialog(); + afx_msg LONG EndProg(UINT wparam, LONG lparam) ; + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/TestCBot/ChildFrm.cpp b/src/CBot/tests/TestCBot/ChildFrm.cpp new file mode 100644 index 0000000..4c40f90 --- /dev/null +++ b/src/CBot/tests/TestCBot/ChildFrm.cpp @@ -0,0 +1,74 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// ChildFrm.cpp : implementation of the CChildFrame class +// + +#include "stdafx.h" +#include "TestCBot.h" + +#include "ChildFrm.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CChildFrame + +IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd) + +BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd) + //{{AFX_MSG_MAP(CChildFrame) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CChildFrame construction/destruction + +CChildFrame::CChildFrame() +{ +} + +CChildFrame::~CChildFrame() +{ +} + +BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs) +{ + return CMDIChildWnd::PreCreateWindow(cs); +} + +///////////////////////////////////////////////////////////////////////////// +// CChildFrame diagnostics + +#ifdef _DEBUG +void CChildFrame::AssertValid() const +{ + CMDIChildWnd::AssertValid(); +} + +void CChildFrame::Dump(CDumpContext& dc) const +{ + CMDIChildWnd::Dump(dc); +} + +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CChildFrame message handlers diff --git a/src/CBot/tests/TestCBot/ChildFrm.h b/src/CBot/tests/TestCBot/ChildFrm.h new file mode 100644 index 0000000..2ad57b6 --- /dev/null +++ b/src/CBot/tests/TestCBot/ChildFrm.h @@ -0,0 +1,66 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// ChildFrm.h : interface of the CChildFrame class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_CHILDFRM_H__4D1BB909_8E74_11D4_A439_00D059085115__INCLUDED_) +#define AFX_CHILDFRM_H__4D1BB909_8E74_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +class CChildFrame : public CMDIChildWnd +{ + DECLARE_DYNCREATE(CChildFrame) +public: + CChildFrame(); + +// Attributes +public: + +// Operations +public: + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CChildFrame) + virtual BOOL PreCreateWindow(CREATESTRUCT& cs); + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CChildFrame(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +// Generated message map functions +protected: + //{{AFX_MSG(CChildFrame) + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_CHILDFRM_H__4D1BB909_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/TestCBot/MainFrm.cpp b/src/CBot/tests/TestCBot/MainFrm.cpp new file mode 100644 index 0000000..6669350 --- /dev/null +++ b/src/CBot/tests/TestCBot/MainFrm.cpp @@ -0,0 +1,116 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// MainFrm.cpp : implementation of the CMainFrame class +// + +#include "stdafx.h" +#include "TestCBot.h" + +#include "MainFrm.h" +#include "TestCBotDoc.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame + +IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd) + +BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd) + //{{AFX_MSG_MAP(CMainFrame) + ON_WM_CREATE() + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +static UINT indicators[] = +{ + ID_SEPARATOR, // status line indicator + ID_INDICATOR_CAPS, + ID_INDICATOR_NUM, + ID_INDICATOR_SCRL, +}; + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame construction/destruction + +CMainFrame::CMainFrame() +{ +} + +CMainFrame::~CMainFrame() +{ +} + +int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) +{ + if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1) + return -1; + + if (!m_wndToolBar.Create(this) || + !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) + { + TRACE0("Failed to create toolbar\n"); + return -1; // fail to create + } + + if (!m_wndStatusBar.Create(this) || + !m_wndStatusBar.SetIndicators(indicators, + sizeof(indicators)/sizeof(UINT))) + { + TRACE0("Failed to create status bar\n"); + return -1; // fail to create + } + + m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | + CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); + + m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); + EnableDocking(CBRS_ALIGN_ANY); + DockControlBar(&m_wndToolBar); + + return 0; +} + +BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) +{ + return CMDIFrameWnd::PreCreateWindow(cs); +} + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame diagnostics + +#ifdef _DEBUG +void CMainFrame::AssertValid() const +{ + CMDIFrameWnd::AssertValid(); +} + +void CMainFrame::Dump(CDumpContext& dc) const +{ + CMDIFrameWnd::Dump(dc); +} + +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame message handlers + + diff --git a/src/CBot/tests/TestCBot/MainFrm.h b/src/CBot/tests/TestCBot/MainFrm.h new file mode 100644 index 0000000..a1d34f4 --- /dev/null +++ b/src/CBot/tests/TestCBot/MainFrm.h @@ -0,0 +1,72 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// MainFrm.h : interface of the CMainFrame class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_MAINFRM_H__4D1BB907_8E74_11D4_A439_00D059085115__INCLUDED_) +#define AFX_MAINFRM_H__4D1BB907_8E74_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +class CMainFrame : public CMDIFrameWnd +{ + DECLARE_DYNAMIC(CMainFrame) +public: + CMainFrame(); + +// Attributes +public: + +// Operations +public: + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CMainFrame) + public: + virtual BOOL PreCreateWindow(CREATESTRUCT& cs); + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CMainFrame(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +protected: // control bar embedded members + CStatusBar m_wndStatusBar; + CToolBar m_wndToolBar; + +// Generated message map functions +protected: + //{{AFX_MSG(CMainFrame) + afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAINFRM_H__4D1BB907_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/TestCBot/PerformDlg.cpp b/src/CBot/tests/TestCBot/PerformDlg.cpp new file mode 100644 index 0000000..8abbb4b --- /dev/null +++ b/src/CBot/tests/TestCBot/PerformDlg.cpp @@ -0,0 +1,177 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// PerformDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "testcbot.h" +#include "PerformDlg.h" + +//#include +#include +//#include + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CPerformDlg dialog + + +CPerformDlg::CPerformDlg(CWnd* pParent /*=NULL*/) + : CDialog(CPerformDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(CPerformDlg) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void CPerformDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(CPerformDlg) + DDX_Control(pDX, IDC_EDIT3, m_Edit3); + DDX_Control(pDX, IDC_EDIT1, m_Edit1); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(CPerformDlg, CDialog) + //{{AFX_MSG_MAP(CPerformDlg) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CPerformDlg message handlers + +/* Pauses for a specified number of milliseconds. */ + +/*void sleep( double waitseconds ) +{ + clock_t wait = (clock_t)(waitseconds * CLOCKS_PER_SEC); + clock_t goal; + goal = wait + clock(); + while( goal > clock() ) + ; +}*/ + +void sleep( clock_t wait ) +{ + clock_t goal; + goal = wait + clock(); + while( goal > clock() ) + TRACE("%d \n", clock() ); +} + +void sleep2( clock_t wait ) +{ + struct _timeb timebuffer; + char *timeline; + + _ftime( &timebuffer ); + timeline = ctime( & ( timebuffer.time ) ); + long x = timebuffer.millitm; + while( x == timebuffer.millitm ) _ftime( &timebuffer ); +} + +#define NBLP 20 + +UINT ThreadProc2(ThreadInfo2 *info) +{ + int lp = NBLP; + int i; + clock_t start = clock(); + + while ( !info->m_bStop ) + { + for ( i = 0; i< info->m_nbscripts; i++ ) + { + info->m_pProg[i]->Run(); + } + +#ifdef _DEBUG + sleep2( 1 ); +#else + CString s ( "xx" ); + for ( long z = 0x5000; z>0; z-- ) s = s.Left(1); +#endif + if ( --lp == 0 ) + { + clock_t finish = clock(); + double n = (double)NBLP / (double)(finish-start) * CLOCKS_PER_SEC; + char b[30]; + sprintf( b, "%f", n); + info->m_pEdit->SetWindowText(b); + + n = n * 1100 / 200; // performances + sprintf( b, "%f", n); + info->m_pEdit3->SetWindowText(b); + start = finish; + lp = NBLP; + } + } + + return 0 ; +} + +BOOL CPerformDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + + CBotStringArray liste; + // cre les scripts pour les tests + for ( int i = 0; i < 100; i++ ) + { + m_pProg[i] = new CBotProgram(); + m_pProg[i]->Compile(m_Script, liste); + m_pProg[i]->Start(liste[0]); + } + + // lance un processus paralle pour l'excution +// m_threadinfo2.m_pWndMessage = this ; + + m_threadinfo2.m_pEdit = &m_Edit1; + m_threadinfo2.m_pEdit3 = &m_Edit3; + m_threadinfo2.m_pProg = m_pProg; + m_threadinfo2.m_bStop = FALSE; + m_threadinfo2.m_nbscripts = 30; + + + AfxBeginThread((AFX_THREADPROC)ThreadProc2, &m_threadinfo2) ; + // TODO: Add extra initialization here + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void CPerformDlg::OnCancel() +{ + m_threadinfo2.m_bStop = TRUE; + sleep ( 2000 ); + + CDialog::OnCancel(); + + for ( int i = 0; i < 100; i++ ) + { + delete m_pProg[i]; + } +} diff --git a/src/CBot/tests/TestCBot/PerformDlg.h b/src/CBot/tests/TestCBot/PerformDlg.h new file mode 100644 index 0000000..29d567f --- /dev/null +++ b/src/CBot/tests/TestCBot/PerformDlg.h @@ -0,0 +1,78 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +#if !defined(AFX_PERFORMDLG_H__EAF2D560_97D8_11D4_A439_00D059085115__INCLUDED_) +#define AFX_PERFORMDLG_H__EAF2D560_97D8_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 +// PerformDlg.h : header file +// + +struct ThreadInfo2 +{ + CEdit* m_pEdit ; + CEdit* m_pEdit3 ; + + CBotProgram** m_pProg; + BOOL m_bStop; + int m_nbscripts; +}; + + +///////////////////////////////////////////////////////////////////////////// +// CPerformDlg dialog + +class CPerformDlg : public CDialog +{ +// Construction +public: + CPerformDlg(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(CPerformDlg) + enum { IDD = IDD_DIALOG1 }; + CEdit m_Edit3; + CEdit m_Edit1; + //}}AFX_DATA + + CBotProgram* m_pProg[100]; + ThreadInfo2 m_threadinfo2; + CString m_Script; + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CPerformDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(CPerformDlg) + virtual BOOL OnInitDialog(); + virtual void OnCancel(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_PERFORMDLG_H__EAF2D560_97D8_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/TestCBot/Routines.cpp b/src/CBot/tests/TestCBot/Routines.cpp new file mode 100644 index 0000000..b37f027 --- /dev/null +++ b/src/CBot/tests/TestCBot/Routines.cpp @@ -0,0 +1,153 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +//////////////////////////////////////////////////////////////////// +// routine show() +// utilisable depuis le programme crit en CBot + +// excution +BOOL rShow( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) +{ + CString s; + + while ( pVar != NULL ) + { + CString ss; + ss.LoadString( TX_TYPENAMES + pVar->GivType() ); + s += ss + " "; + + ss = pVar->GivName(); + if (ss.IsEmpty()) ss = ""; + s += ss + " = "; + + s += pVar->GivValString(); + s += "\n"; + pVar = pVar->GivNext(); + } + + AfxMessageBox(s, MB_OK|MB_ICONINFORMATION); + + return TRUE; // pas d'interruption +} + +CBotTypResult cShow( CBotVar* &pVar, void* pUser) +{ + if ( pVar == NULL ) return CBotTypResult(5028); + return CBotTypResult(0); // tous paramtres accepts, void en retour +} + + +//////////////////////////////////////////////////////////////////// +// routine print() +// utilisable depuis le programme crit en CBot + +// excution +BOOL rPrintLn( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) +{ + CString s; + + CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); + CEdit* pEdit = pApp->m_pConsole; + + if (pEdit == NULL) return TRUE; + pEdit->GetWindowText(s); + + while ( pVar != NULL ) + { + if ( !s.IsEmpty() ) s += " "; + s += pVar->GivValString(); + pVar = pVar->GivNext(); + } + s += "\r\n"; + + pEdit->SetWindowText(s); + pEdit->SetSel(s.GetLength(), s.GetLength()); + pEdit->SetFocus(); + return TRUE; // pas d'interruption +} + +BOOL rPrint( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) +{ + CString s; + + CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); + CEdit* pEdit = pApp->m_pConsole; + + if (pEdit == NULL) return TRUE; + pEdit->GetWindowText(s); + + while ( pVar != NULL ) + { + if ( !s.IsEmpty() ) s += " "; + s += pVar->GivValString(); + pVar = pVar->GivNext(); + } + + pEdit->SetWindowText(s); + pEdit->SetSel(s.GetLength(), s.GetLength()); + pEdit->SetFocus(); + return TRUE; // pas d'interruption +} + +CBotTypResult cPrint( CBotVar* &pVar, void* pUser) +{ + return CBotTypResult(0); // tous paramtres accepts, un entier en retour +} + + +////////////////////////////////////////////////////////////////// +// class CPoint pour essayer + +// excution +BOOL rCPoint( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + CString s; + + if ( pVar == NULL )return TRUE; // constructeur sans paramtres est ok + + CBotVar* pX = pThis->GivItem("x"); + pX->SetValFloat( pVar->GivValFloat() ); + pVar = pVar->GivNext(); + + CBotVar* pY = pThis->GivItem("y"); + pY->SetValFloat( pVar->GivValFloat() ); + pVar = pVar->GivNext(); + + return TRUE; // pas d'interruption +} + +CBotTypResult cCPoint( CBotVar* pThis, CBotVar* &pVar) +{ + // ok si aucun paramtres ! + if ( pVar == NULL ) return CBotTypResult(0); + + // paramtre de type numrique svp + if ( pVar->GivType() > CBotTypDouble ) return CBotTypResult(5011); + pVar = pVar->GivNext(); + + // il doit y avoir un second paramtre + if ( pVar == NULL ) return 5028; + // galement de type numrique + if ( pVar->GivType() > CBotTypDouble )return CBotTypResult(5011); + pVar = pVar->GivNext(); + + // et pas plus de 2 paramtres svp + if ( pVar != NULL ) return CBotTypResult(5026); + + return CBotTypResult(0); // cette fonction retourne void +} + + diff --git a/src/CBot/tests/TestCBot/StdAfx.cpp b/src/CBot/tests/TestCBot/StdAfx.cpp new file mode 100644 index 0000000..7dd0f00 --- /dev/null +++ b/src/CBot/tests/TestCBot/StdAfx.cpp @@ -0,0 +1,20 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// stdafx.cpp : source file that includes just the standard includes +// TestCBot.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + diff --git a/src/CBot/tests/TestCBot/StdAfx.h b/src/CBot/tests/TestCBot/StdAfx.h new file mode 100644 index 0000000..c3659fb --- /dev/null +++ b/src/CBot/tests/TestCBot/StdAfx.h @@ -0,0 +1,40 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#if !defined(AFX_STDAFX_H__4D1BB905_8E74_11D4_A439_00D059085115__INCLUDED_) +#define AFX_STDAFX_H__4D1BB905_8E74_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers + +#include // MFC core and standard components +#include // MFC extensions +#include // MFC OLE automation classes +#ifndef _AFX_NO_AFXCMN_SUPPORT +#include // MFC support for Windows Common Controls +#endif // _AFX_NO_AFXCMN_SUPPORT + + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_STDAFX_H__4D1BB905_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/TestCBot/TestCBot.clw b/src/CBot/tests/TestCBot/TestCBot.clw new file mode 100644 index 0000000..13f20f4 --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBot.clw @@ -0,0 +1,316 @@ +; CLW file contains information for the MFC ClassWizard + +[General Info] +Version=1 +LastClass=CPerformDlg +LastTemplate=CDialog +NewFileInclude1=#include "stdafx.h" +NewFileInclude2=#include "testcbot.h" +LastPage=0 + +ClassCount=8 +Class1=CBotConsoleDlg +Class2=CChildFrame +Class3=CMainFrame +Class4=CTestCBotApp +Class5=CAboutDlg +Class6=CTestCBotDoc +Class7=CTestCBotView + +ResourceCount=12 +Resource1=IDD_CONSOLE +Resource2=IDR_TESTCBTYPE (French (France)) +Resource3=IDD_ABOUTBOX (French (France)) +Resource4=IDR_MAINFRAME (French (France)) +Resource5=IDR_MAINFRAME +Resource6=IDR_TESTCBTYPE +Resource7=IDD_ABOUTBOX +Resource8=IDD_CONSOLE (French (Switzerland)) +Class8=CPerformDlg +Resource9=IDD_DIALOG1 +Resource10=IDD_DIALOG2 +Resource11=IDD_DIALOG1 (French (Switzerland)) +Resource12=IDD_DIALOG2 (French (France)) + +[CLS:CBotConsoleDlg] +Type=0 +BaseClass=CDialog +HeaderFile=CBotConsoleDlg.h +ImplementationFile=CBotConsoleDlg.cpp +LastObject=IDC_EDIT1 + +[CLS:CChildFrame] +Type=0 +BaseClass=CMDIChildWnd +HeaderFile=ChildFrm.h +ImplementationFile=ChildFrm.cpp + +[CLS:CMainFrame] +Type=0 +BaseClass=CMDIFrameWnd +HeaderFile=MainFrm.h +ImplementationFile=MainFrm.cpp +Filter=T +VirtualFilter=fWC +LastObject=CMainFrame + +[CLS:CTestCBotApp] +Type=0 +BaseClass=CWinApp +HeaderFile=TestCBot.h +ImplementationFile=TestCBot.cpp +Filter=N +VirtualFilter=AC +LastObject=ID_TEST + +[CLS:CAboutDlg] +Type=0 +BaseClass=CDialog +HeaderFile=TestCBot.cpp +ImplementationFile=TestCBot.cpp +LastObject=CAboutDlg + +[CLS:CTestCBotDoc] +Type=0 +BaseClass=CDocument +HeaderFile=TestCBotDoc.h +ImplementationFile=TestCBotDoc.cpp +LastObject=CTestCBotDoc +Filter=N +VirtualFilter=DC + +[CLS:CTestCBotView] +Type=0 +BaseClass=CView +HeaderFile=TestCBotView.h +ImplementationFile=TestCBotView.cpp +LastObject=CTestCBotView +Filter=C +VirtualFilter=VWC + +[DLG:IDD_CONSOLE] +Type=1 +Class=CBotConsoleDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_EDIT2,edit,1350631552 +Control3=IDOK,button,1342242817 +Control4=IDC_EDIT1,edit,1352734724 + +[DLG:IDD_ABOUTBOX] +Type=1 +Class=CAboutDlg +ControlCount=7 +Control1=IDC_STATIC,static,1342177283 +Control2=IDC_STATIC,static,1342308480 +Control3=IDC_STATIC,static,1342308352 +Control4=IDOK,button,1342373889 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_STATIC,static,1342308352 +Control7=IDC_STATIC,static,1342308352 + +[TB:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command4=ID_EDIT_CUT +Command5=ID_EDIT_COPY +Command6=ID_EDIT_PASTE +Command7=ID_FILE_PRINT +Command8=ID_RUN +Command9=ID_APP_ABOUT +CommandCount=9 + +[MNU:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_MRU_FILE1 +Command4=ID_APP_EXIT +Command5=ID_VIEW_TOOLBAR +Command6=ID_VIEW_STATUS_BAR +Command7=ID_APP_ABOUT +CommandCount=7 + +[MNU:IDR_TESTCBTYPE (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_CLOSE +Command4=ID_FILE_SAVE +Command5=ID_FILE_SAVE_AS +Command6=ID_FILE_MRU_FILE1 +Command7=ID_APP_EXIT +Command8=ID_EDIT_UNDO +Command9=ID_EDIT_CUT +Command10=ID_EDIT_COPY +Command11=ID_EDIT_PASTE +Command12=ID_VIEW_TOOLBAR +Command13=ID_VIEW_STATUS_BAR +Command14=ID_WINDOW_NEW +Command15=ID_WINDOW_CASCADE +Command16=ID_WINDOW_TILE_HORZ +Command17=ID_WINDOW_ARRANGE +Command18=ID_APP_ABOUT +CommandCount=18 + +[ACL:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_EDIT_COPY +Command2=ID_FILE_NEW +Command3=ID_FILE_OPEN +Command4=ID_FILE_SAVE +Command5=ID_EDIT_PASTE +Command6=ID_EDIT_UNDO +Command7=ID_EDIT_CUT +Command8=ID_RUN +Command9=ID_NEXT_PANE +Command10=ID_PREV_PANE +Command11=ID_RUN +Command12=ID_TEST +Command13=ID_EDIT_COPY +Command14=ID_EDIT_PASTE +Command15=ID_EDIT_CUT +Command16=ID_EDIT_UNDO +CommandCount=16 + +[DLG:IDD_ABOUTBOX (French (France))] +Type=1 +Class=CAboutDlg +ControlCount=7 +Control1=IDC_STATIC,static,1342177283 +Control2=IDC_STATIC,static,1342308480 +Control3=IDC_STATIC,static,1342308352 +Control4=IDOK,button,1342373889 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_STATIC,static,1342308352 +Control7=IDC_STATIC,static,1342308352 + +[ACL:IDR_MAINFRAME] +Type=1 +Command1=ID_EDIT_COPY +Command2=ID_FILE_NEW +Command3=ID_FILE_OPEN +Command4=ID_FILE_SAVE +Command5=ID_EDIT_PASTE +Command6=ID_EDIT_UNDO +Command7=ID_EDIT_CUT +Command8=ID_RUN +Command9=ID_NEXT_PANE +Command10=ID_PREV_PANE +Command11=ID_RUN +Command12=ID_TEST +Command13=ID_EDIT_COPY +Command14=ID_EDIT_PASTE +Command15=ID_EDIT_CUT +Command16=ID_EDIT_UNDO +CommandCount=16 + +[TB:IDR_MAINFRAME] +Type=1 +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command4=ID_EDIT_CUT +Command5=ID_EDIT_COPY +Command6=ID_EDIT_PASTE +Command7=ID_FILE_PRINT +Command8=ID_RUN +Command9=ID_APP_ABOUT +CommandCount=9 + +[MNU:IDR_MAINFRAME] +Type=1 +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_MRU_FILE1 +Command4=ID_APP_EXIT +Command5=ID_VIEW_TOOLBAR +Command6=ID_VIEW_STATUS_BAR +Command7=ID_APP_ABOUT +CommandCount=7 + +[MNU:IDR_TESTCBTYPE] +Type=1 +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_CLOSE +Command4=ID_FILE_SAVE +Command5=ID_FILE_SAVE_AS +Command6=ID_FILE_MRU_FILE1 +Command7=ID_APP_EXIT +Command8=ID_EDIT_UNDO +Command9=ID_EDIT_CUT +Command10=ID_EDIT_COPY +Command11=ID_EDIT_PASTE +Command12=ID_VIEW_TOOLBAR +Command13=ID_VIEW_STATUS_BAR +Command14=ID_WINDOW_NEW +Command15=ID_WINDOW_CASCADE +Command16=ID_WINDOW_TILE_HORZ +Command17=ID_WINDOW_ARRANGE +Command18=ID_APP_ABOUT +CommandCount=18 + +[DLG:IDD_CONSOLE (French (Switzerland))] +Type=1 +Class=CBotConsoleDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_EDIT2,edit,1350631552 +Control3=IDOK,button,1342242817 +Control4=IDC_EDIT1,edit,1352734724 + +[DLG:IDD_DIALOG1] +Type=1 +Class=CPerformDlg +ControlCount=9 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_EDIT1,edit,1350633600 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_EDIT2,edit,1350631552 +Control5=IDC_SPIN1,msctls_updown32,1342177312 +Control6=IDC_COMBO1,combobox,1344339971 +Control7=IDC_STATIC,static,1342308352 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_EDIT3,edit,1350633600 + +[CLS:CPerformDlg] +Type=0 +HeaderFile=PerformDlg.h +ImplementationFile=PerformDlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=IDC_EDIT3 + +[DLG:IDD_DIALOG2] +Type=1 +ControlCount=2 +Control1=IDOK,button,1342242817 +Control2=IDCANCEL,button,1342242816 + +[DLG:IDD_DIALOG1 (French (Switzerland))] +Type=1 +ControlCount=9 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_EDIT1,edit,1350633600 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_EDIT2,edit,1350631552 +Control5=IDC_SPIN1,msctls_updown32,1342177312 +Control6=IDC_COMBO1,combobox,1344339971 +Control7=IDC_STATIC,static,1342308352 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_EDIT3,edit,1350633600 + +[DLG:IDD_DIALOG2 (French (France))] +Type=1 +ControlCount=2 +Control1=IDOK,button,1342242817 +Control2=IDCANCEL,button,1342242816 + diff --git a/src/CBot/tests/TestCBot/TestCBot.cpp b/src/CBot/tests/TestCBot/TestCBot.cpp new file mode 100644 index 0000000..a76040a --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBot.cpp @@ -0,0 +1,267 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBot.cpp : Defines the class behaviors for the application. +// + +#include "stdafx.h" +#include "TestCBot.h" + +#include "MainFrm.h" +#include "ChildFrm.h" +#include "TestCBotDoc.h" +#include "TestCBotView.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotApp + +BEGIN_MESSAGE_MAP(CTestCBotApp, CWinApp) + //{{AFX_MSG_MAP(CTestCBotApp) + ON_COMMAND(ID_APP_ABOUT, OnAppAbout) + //}}AFX_MSG_MAP + // Standard file based document commands + ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) + ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotApp construction + +CTestCBotApp::CTestCBotApp() +{ + m_pConsole = NULL; + m_LastActive = NULL; + m_pClassPoint= NULL; +} + + +///////////////////////////////////////////////////////////////////////////// +// The one and only CTestCBotApp object + +CTestCBotApp theApp; + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotApp initialization + +#include "Routines.cpp" + + +static char BASED_CODE szSection[] = "Recent File List"; +static char BASED_CODE szFilename[] = "File1"; + + +#include "../ClassFILE.cpp" + +// routine pour mettre jour l'instance de la classe Bot courante +void rMajObject( CBotVar* pThis, void* pUser ) +{ + if (!pThis->IsElemOfClass("object")) + return ; + CBotVar* pPos = pThis->GivItem("position"); + CBotVar* pX = pPos->GivItem("x"); + CBotVar* pY = pPos->GivItem("y"); + CBotVar* pZ = pPos->GivItem("z"); +// CBotVar* pPt = pThis->GivItem("transport"); + + CBotString p = pX->GivValString(); + +// pX->SetValFloat( pUser == (void*)1 ? (float)12.5 : (float)44.4 ); + pZ->SetValFloat( (float)0 ); + pY->SetValFloat( (float)-3.33 ); + pX->SetValFloat( pX->GivValFloat() + 10 ) ; + +// pX = pThis->GivItem( "xx" ); +// pX->SetValFloat( (float)22 ); + + // cre une instance sur une classe object +// CBotVar* pAutre = CBotVar::Create("autre", CBotTypClass, "object"); +// pAutre->SetUserPtr( (void*)3 ); +// pPt->SetPointer( pAutre ); +// pPt->SetPointer( NULL ); +// delete pAutre; +} + + +BOOL CTestCBotApp::InitInstance() +{ +////////////////////////////////////////////// +// dfini les mots clefs supplmentaires +// ------------------------------------------- + + CBotProgram::Init(); + +////////////////////////////////////////////// +// dfini les fonctions "show()" et "print()" +// ------------------------------------------- + + CBotProgram::AddFunction("show", rShow, cShow); + CBotProgram::AddFunction("print", rPrint, cPrint); + CBotProgram::AddFunction("println", rPrintLn, cPrint); + + +/////////////////////////////////// +// dfinie la classe globale CPoint +// -------------------------------- + + m_pClassPoint = new CBotClass("CPoint", NULL); + // ajoute le composant ".x" + m_pClassPoint->AddItem("x", CBotTypFloat); + // ajoute le composant ".y" + m_pClassPoint->AddItem("y", CBotTypFloat); + + // ajoute le constructeur pour cette classe + m_pClassPoint->AddFunction("CPoint", rCPoint, cCPoint); + + m_pClassPointIntr = new CBotClass("point", NULL, TRUE); + // ajoute le composant ".x" + m_pClassPointIntr->AddItem("x", CBotTypFloat); + // ajoute le composant ".y" + m_pClassPointIntr->AddItem("y", CBotTypFloat); + // ajoute le composant ".z" + m_pClassPointIntr->AddItem("z", CBotTypFloat); + + // ajoute le constructeur pour cette classe + m_pClassPointIntr->AddFunction("point", rCPoint, cCPoint); + + // dfini la classe "object" + CBotClass* pClassObject = new CBotClass( "object", NULL ) ; + pClassObject->AddItem( "xx", CBotTypFloat ); + pClassObject->AddItem( "position", CBotTypResult( CBotTypIntrinsic, "point" ) ); + pClassObject->AddItem( "transport", CBotTypResult( CBotTypPointer, "object" ) ); + pClassObject->AddUpdateFunc( rMajObject ); + + InitClassFILE(); + + AfxEnableControlContainer(); + + // Standard initialization + +#ifdef _AFXDLL + Enable3dControls(); // Call this when using MFC in a shared DLL +#else + Enable3dControlsStatic(); // Call this when linking to MFC statically +#endif + + // Change the registry key under which our settings are stored. + SetRegistryKey(_T("Local AppWizard-Generated Applications")); + + LoadStdProfileSettings(); // Load standard INI file options (including MRU) + + // Register document templates + + CMultiDocTemplate* pDocTemplate; + pDocTemplate = new CMultiDocTemplate( + IDR_TESTCBTYPE, + RUNTIME_CLASS(CTestCBotDoc), + RUNTIME_CLASS(CChildFrame), // custom MDI child frame + RUNTIME_CLASS(CTestCBotView)); + AddDocTemplate(pDocTemplate); + + // create main MDI Frame window + CMainFrame* pMainFrame = new CMainFrame; + if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) + return FALSE; + m_pMainWnd = pMainFrame; + + // Parse command line for standard shell commands, DDE, file open + CCommandLineInfo cmdInfo; + ParseCommandLine(cmdInfo); + + if (m_lpCmdLine[0] == 0) + { + CString Filename = GetProfileString(szSection, szFilename); + if (Filename.IsEmpty()) Filename = "TstCbot.txt"; + else OpenDocumentFile(Filename); + } + else + // Dispatch commands specified on the command line + if (!ProcessShellCommand(cmdInfo)) + return FALSE; + pMainFrame->ShowWindow(m_nCmdShow); + pMainFrame->UpdateWindow(); + + + return TRUE; +} + + +///////////////////////////////////////////////////////////////////////////// +// CAboutDlg dialog used for App About + +class CAboutDlg : public CDialog +{ +public: + CAboutDlg(); + +// Dialog Data + //{{AFX_DATA(CAboutDlg) + enum { IDD = IDD_ABOUTBOX }; + //}}AFX_DATA + + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CAboutDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + //{{AFX_MSG(CAboutDlg) + // No message handlers + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) +{ + //{{AFX_DATA_INIT(CAboutDlg) + //}}AFX_DATA_INIT +} + +void CAboutDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(CAboutDlg) + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) + //{{AFX_MSG_MAP(CAboutDlg) + // No message handlers + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +// App command to run the dialog +void CTestCBotApp::OnAppAbout() +{ + CAboutDlg aboutDlg; + aboutDlg.DoModal(); +} + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotApp commands + +int CTestCBotApp::ExitInstance() +{ + delete m_pFuncFile; + + CBotProgram::Free(); + return CWinApp::ExitInstance(); +} diff --git a/src/CBot/tests/TestCBot/TestCBot.dsp b/src/CBot/tests/TestCBot/TestCBot.dsp new file mode 100644 index 0000000..8ed9b11 --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBot.dsp @@ -0,0 +1,201 @@ +# Microsoft Developer Studio Project File - Name="TestCBot" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 5.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Application" 0x0101 + +CFG=TestCBot - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "TestCBot.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "TestCBot.mak" CFG="TestCBot - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "TestCBot - Win32 Release" (based on "Win32 (x86) Application") +!MESSAGE "TestCBot - Win32 Debug" (based on "Win32 (x86) Application") +!MESSAGE + +# Begin Project +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "TestCBot - Win32 Release" + +# PROP BASE Use_MFC 5 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 5 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /FR /Yu"stdafx.h" /FD /c +# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 +# ADD BASE RSC /l 0x100c /d "NDEBUG" +# ADD RSC /l 0x100c /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 /nologo /subsystem:windows /machine:I386 +# ADD LINK32 /nologo /subsystem:windows /machine:I386 + +!ELSEIF "$(CFG)" == "TestCBot - Win32 Debug" + +# PROP BASE Use_MFC 5 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 5 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /FR /Yu"stdafx.h" /FD /c +# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 +# ADD BASE RSC /l 0x100c /d "_DEBUG" +# ADD RSC /l 0x100c /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept +# ADD LINK32 /nologo /stack:0x7010 /subsystem:windows /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "TestCBot - Win32 Release" +# Name "TestCBot - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\CBotConsoleDlg.cpp +# End Source File +# Begin Source File + +SOURCE=.\ChildFrm.cpp +# End Source File +# Begin Source File + +SOURCE=.\MainFrm.cpp +# End Source File +# Begin Source File + +SOURCE=.\PerformDlg.cpp +# End Source File +# Begin Source File + +SOURCE=.\StdAfx.cpp +# ADD CPP /Yc"stdafx.h" +# End Source File +# Begin Source File + +SOURCE=.\TestCBot.cpp +# End Source File +# Begin Source File + +SOURCE=.\TestCBot.rc + +!IF "$(CFG)" == "TestCBot - Win32 Release" + +!ELSEIF "$(CFG)" == "TestCBot - Win32 Debug" + +!ENDIF + +# End Source File +# Begin Source File + +SOURCE=.\TestCBotDoc.cpp +# End Source File +# Begin Source File + +SOURCE=.\TestCBotView.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\CBotConsoleDlg.h +# End Source File +# Begin Source File + +SOURCE=.\ChildFrm.h +# End Source File +# Begin Source File + +SOURCE=.\MainFrm.h +# End Source File +# Begin Source File + +SOURCE=.\PerformDlg.h +# End Source File +# Begin Source File + +SOURCE=.\Resource.h +# End Source File +# Begin Source File + +SOURCE=.\StdAfx.h +# End Source File +# Begin Source File + +SOURCE=.\TestCBot.h +# End Source File +# Begin Source File + +SOURCE=.\TestCBotDoc.h +# End Source File +# Begin Source File + +SOURCE=.\TestCBotView.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe" +# Begin Source File + +SOURCE=.\res\TestCBot.ico +# End Source File +# Begin Source File + +SOURCE=.\res\TestCBot.rc2 +# End Source File +# Begin Source File + +SOURCE=.\res\TestCBotDoc.ico +# End Source File +# Begin Source File + +SOURCE=.\res\Toolbar.bmp +# End Source File +# End Group +# Begin Source File + +SOURCE=..\Debug\CBot.lib +# End Source File +# End Target +# End Project diff --git a/src/CBot/tests/TestCBot/TestCBot.h b/src/CBot/tests/TestCBot/TestCBot.h new file mode 100644 index 0000000..c2595b6 --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBot.h @@ -0,0 +1,78 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBot.h : main header file for the TESTCBOT application +// + +#if !defined(AFX_TESTCBOT_H__4D1BB903_8E74_11D4_A439_00D059085115__INCLUDED_) +#define AFX_TESTCBOT_H__4D1BB903_8E74_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +#ifndef __AFXWIN_H__ + #error include 'stdafx.h' before including this file for PCH +#endif + +#include "resource.h" // main symbols +//#include "../CbotDll.h" // librairie CBot +#include "../Cbot.h" // complet pour Browse + +class CTestCBotView; + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotApp: +// See TestCBot.cpp for the implementation of this class +// + +class CTestCBotApp : public CWinApp +{ +public: + CTestCBotApp(); + + CEdit* m_pConsole; + CTestCBotView* m_LastActive; + CBotClass* m_pClassPoint; + CBotClass* m_pClassPointIntr; + + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CTestCBotApp) + public: + virtual BOOL InitInstance(); + virtual int ExitInstance(); + //}}AFX_VIRTUAL + +// Implementation + + //{{AFX_MSG(CTestCBotApp) + afx_msg void OnAppAbout(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TESTCBOT_H__4D1BB903_8E74_11D4_A439_00D059085115__INCLUDED_) + + +#define WM_STARTPROG WM_APP + 0 +#define WM_ENDPROG WM_APP + 1 +#define WM_ACTWINDOW WM_APP + 2 diff --git a/src/CBot/tests/TestCBot/TestCBot.rc b/src/CBot/tests/TestCBot/TestCBot.rc new file mode 100644 index 0000000..137458c --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBot.rc @@ -0,0 +1,564 @@ +//Microsoft Developer Studio generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// French (France) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) +#ifdef _WIN32 +LANGUAGE LANG_FRENCH, SUBLANG_FRENCH +#pragma code_page(1252) +#endif //_WIN32 + +///////////////////////////////////////////////////////////////////////////// +// +// Icon +// + +// Icon with lowest ID value placed first to ensure application icon +// remains consistent on all systems. +IDR_MAINFRAME ICON DISCARDABLE "res\\TestCBot.ico" +IDR_TESTCBTYPE ICON DISCARDABLE "res\\TestCBotDoc.ico" + +///////////////////////////////////////////////////////////////////////////// +// +// Bitmap +// + +IDR_MAINFRAME BITMAP MOVEABLE PURE "res\\Toolbar.bmp" + +///////////////////////////////////////////////////////////////////////////// +// +// Toolbar +// + +IDR_MAINFRAME TOOLBAR DISCARDABLE 16, 15 +BEGIN + BUTTON ID_FILE_NEW + BUTTON ID_FILE_OPEN + BUTTON ID_FILE_SAVE + SEPARATOR + BUTTON ID_EDIT_CUT + BUTTON ID_EDIT_COPY + BUTTON ID_EDIT_PASTE + SEPARATOR + BUTTON ID_FILE_PRINT + BUTTON ID_RUN + SEPARATOR + BUTTON ID_APP_ABOUT +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Menu +// + +IDR_MAINFRAME MENU PRELOAD DISCARDABLE +BEGIN + POPUP "&Fichier" + BEGIN + MENUITEM "&Nouveau\tCtrl+N", ID_FILE_NEW + MENUITEM "&Ouvrir...\tCtrl+O", ID_FILE_OPEN + MENUITEM SEPARATOR + MENUITEM "Fichier rcent", ID_FILE_MRU_FILE1, GRAYED + MENUITEM SEPARATOR + MENUITEM "&Quitter", ID_APP_EXIT + END + POPUP "&Affichage" + BEGIN + MENUITEM "&Barre d'outils", ID_VIEW_TOOLBAR + MENUITEM "Barre d'&tat", ID_VIEW_STATUS_BAR + END + POPUP "&?" + BEGIN + MENUITEM "&A propos de TestCBot...", ID_APP_ABOUT + END +END + +IDR_TESTCBTYPE MENU PRELOAD DISCARDABLE +BEGIN + POPUP "&Fichier" + BEGIN + MENUITEM "&Nouveau\tCtrl+N", ID_FILE_NEW + MENUITEM "&Ouvrir...\tCtrl+O", ID_FILE_OPEN + MENUITEM "&Fermer", ID_FILE_CLOSE + MENUITEM "&Enregistrer\tCtrl+S", ID_FILE_SAVE + MENUITEM "En®istrer sous...", ID_FILE_SAVE_AS + MENUITEM SEPARATOR + MENUITEM "Fichier rcent", ID_FILE_MRU_FILE1, GRAYED + MENUITEM SEPARATOR + MENUITEM "&Quitter", ID_APP_EXIT + END + POPUP "&Edition" + BEGIN + MENUITEM "&Annuler\tCtrl+Z", ID_EDIT_UNDO + MENUITEM SEPARATOR + MENUITEM "&Couper\tCtrl+X", ID_EDIT_CUT + MENUITEM "&Copier\tCtrl+C", ID_EDIT_COPY + MENUITEM "C&oller\tCtrl+V", ID_EDIT_PASTE + END + POPUP "&Affichage" + BEGIN + MENUITEM "&Barre d'outils", ID_VIEW_TOOLBAR + MENUITEM "Barre d'&tat", ID_VIEW_STATUS_BAR + END + POPUP "Fe&ntre" + BEGIN + MENUITEM "&Nouvelle fentre", ID_WINDOW_NEW + MENUITEM "&Cascade", ID_WINDOW_CASCADE + MENUITEM "&Mosaque", ID_WINDOW_TILE_HORZ + MENUITEM "&Rorganiser les icnes", ID_WINDOW_ARRANGE + END + POPUP "&?" + BEGIN + MENUITEM "&A propos de TestCBot...", ID_APP_ABOUT + END +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Accelerator +// + +IDR_MAINFRAME ACCELERATORS PRELOAD MOVEABLE PURE +BEGIN + "C", ID_EDIT_COPY, VIRTKEY, CONTROL, NOINVERT + "N", ID_FILE_NEW, VIRTKEY, CONTROL, NOINVERT + "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT + "S", ID_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT + "V", ID_EDIT_PASTE, VIRTKEY, CONTROL, NOINVERT + VK_BACK, ID_EDIT_UNDO, VIRTKEY, ALT, NOINVERT + VK_DELETE, ID_EDIT_CUT, VIRTKEY, SHIFT, NOINVERT + VK_F5, ID_RUN, VIRTKEY, NOINVERT + VK_F6, ID_NEXT_PANE, VIRTKEY, NOINVERT + VK_F6, ID_PREV_PANE, VIRTKEY, SHIFT, NOINVERT + VK_F7, ID_RUN, VIRTKEY, NOINVERT + VK_F9, ID_TEST, VIRTKEY, NOINVERT + VK_INSERT, ID_EDIT_COPY, VIRTKEY, CONTROL, NOINVERT + VK_INSERT, ID_EDIT_PASTE, VIRTKEY, SHIFT, NOINVERT + "X", ID_EDIT_CUT, VIRTKEY, CONTROL, NOINVERT + "Z", ID_EDIT_UNDO, VIRTKEY, CONTROL, NOINVERT +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 265, 206 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "A propos de TestCBot" +FONT 8, "MS Sans Serif" +BEGIN + ICON IDR_MAINFRAME,IDC_STATIC,11,17,21,20 + LTEXT "TestCBot version 1.0",IDC_STATIC,40,10,119,8, + SS_NOPREFIX + LTEXT "Copyright D. Dumoulin (C) 2000",IDC_STATIC,40,25,119,8 + DEFPUSHBUTTON "OK",IDOK,226,7,32,14,WS_GROUP + LTEXT "Programme de test pour la librairie CBot\n\nLes fonctions doivent tre dclares comme ""extern"" pour apparatre dans la liste lors de l'excution.\n\n", + IDC_STATIC,39,43,191,41 + LTEXT "Mais en fait, on peut accder toutes les fonctions marques ""public"" quelles soient dans la fentre active ou non.", + IDC_STATIC,39,89,187,36 + LTEXT "Les fonctions print( ... ) et println( ...) permettent d'afficher des rsultats dans la console.\n\nLa fonction show( ... ) affiche les paramtres dans un dialogue, et suspend donc l'excution.", + IDC_STATIC,39,130,187,54 +END + +IDD_DIALOG2 DIALOG DISCARDABLE 0, 0, 186, 95 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Dialog" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",IDOK,129,7,50,14 + PUSHBUTTON "Cancel",IDCANCEL,129,24,50,14 +END + + +#ifndef _MAC +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,0,1 + PRODUCTVERSION 1,0,0,1 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040C04B0" + BEGIN + VALUE "CompanyName", "\0" + VALUE "FileDescription", "Application MFC TestCBot\0" + VALUE "FileVersion", "1, 0, 0, 1\0" + VALUE "InternalName", "TestCBot\0" + VALUE "LegalCopyright", "Copyright (C) 1900\0" + VALUE "LegalTrademarks", "\0" + VALUE "OriginalFilename", "TestCBot.EXE\0" + VALUE "ProductName", "Application TestCBot\0" + VALUE "ProductVersion", "1, 0, 0, 1\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Traduction", 0x40c, 1200 + END +END + +#endif // !_MAC + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO DISCARDABLE +BEGIN + IDD_ABOUTBOX, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 258 + TOPMARGIN, 7 + BOTTOMMARGIN, 199 + END + + IDD_DIALOG2, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 88 + END +END +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// String Table +// + +STRINGTABLE PRELOAD DISCARDABLE +BEGIN + IDR_MAINFRAME "TestCBot" + IDR_TESTCBTYPE "\nTestCBot\nTestCBot\nCBot (*.txt)\n.txt\nTestCBot.Document\nTestCB Document" +END + +STRINGTABLE PRELOAD DISCARDABLE +BEGIN + AFX_IDS_APP_TITLE "TestCBot" + AFX_IDS_IDLEMESSAGE "Prt" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_INDICATOR_EXT "EXT" + ID_INDICATOR_CAPS "MAJ" + ID_INDICATOR_NUM "NUM" + ID_INDICATOR_SCRL "DEF" + ID_INDICATOR_OVR "ECR" + ID_INDICATOR_REC "ENR" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_FILE_NEW "Cre un nouveau document\nNouveau" + ID_FILE_OPEN "Ouvre un document existant\nOuvrir" + ID_FILE_CLOSE "Ferme le document actif\nFermer" + ID_FILE_SAVE "Enregistre le document actif\nEnregistrer" + ID_FILE_SAVE_AS "Enregistre le document actif sous un nouveau nom\nEnregistrer sous" + ID_FILE_PRINT "Imprime le document\nImprime" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_APP_ABOUT "Affiche des informations sur le programme\nA propos de" + ID_APP_EXIT "Ferme l'application ; propose d'enregistrer les documents\nQuitter" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_FILE_MRU_FILE1 "Ouvre ce document" + ID_FILE_MRU_FILE2 "Ouvre ce document" + ID_FILE_MRU_FILE3 "Ouvre ce document" + ID_FILE_MRU_FILE4 "Ouvre ce document" + ID_FILE_MRU_FILE5 "Ouvre ce document" + ID_FILE_MRU_FILE6 "Ouvre ce document" + ID_FILE_MRU_FILE7 "Ouvre ce document" + ID_FILE_MRU_FILE8 "Ouvre ce document" + ID_FILE_MRU_FILE9 "Ouvre ce document" + ID_FILE_MRU_FILE10 "Ouvre ce document" + ID_FILE_MRU_FILE11 "Ouvre ce document" + ID_FILE_MRU_FILE12 "Ouvre ce document" + ID_FILE_MRU_FILE13 "Ouvre ce document" + ID_FILE_MRU_FILE14 "Ouvre ce document" + ID_FILE_MRU_FILE15 "Ouvre ce document" + ID_FILE_MRU_FILE16 "Ouvre ce document" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_NEXT_PANE "Passe au volet de fentre suivant\nVolet suivant" + ID_PREV_PANE "Revient au volet prcdent\nVolet prcdent" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_WINDOW_NEW "Ouvre une nouvelle fentre pour le document actif\nNouvelle fentre" + ID_WINDOW_ARRANGE "Rorganise les icnes en bas de la fentre\nRorganise les icnes" + ID_WINDOW_CASCADE "Rorganise les fentres en cascade\nCascade" + ID_WINDOW_TILE_HORZ "Rorganise les fentres en une mosaque\nMosaque" + ID_WINDOW_TILE_VERT "Rorganise les fentres en une mosaque\nMosaque" + ID_WINDOW_SPLIT "Fractionne la fentre active en deux volets\nFractionner" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_EDIT_CLEAR "Efface la slection\nEffacer" + ID_EDIT_CLEAR_ALL "Efface tout\nEffacer tout" + ID_EDIT_COPY "Copie la slection et la place dans le Presse-papiers\nCopier" + ID_EDIT_CUT "Supprime la slection et la place dans le Presse-papiers\nCopier" + ID_EDIT_FIND "Recherche le texte spcifi\nRechercher" + ID_EDIT_PASTE "Insre le contenu du Presse-papiers\nColler" + ID_EDIT_REPEAT "Rpte la dernire action\nRpter" + ID_EDIT_REPLACE "Remplace le texte spcifique par un texte diffrent\nRemplacer" + ID_EDIT_SELECT_ALL "Slectionne le document entier\nSlectionner tout" + ID_EDIT_UNDO "Annule la dernire action\nAnnuler" + ID_EDIT_REDO "Rtablit l'action prcdemment annule\nRtablir" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_VIEW_TOOLBAR "Affiche ou masque la barre d'outils\nBarre d'outils" + ID_VIEW_STATUS_BAR "Affiche ou masque la barre d'tat\nBarre d'tat" +END + +STRINGTABLE DISCARDABLE +BEGIN + AFX_IDS_SCSIZE "Change la taille de la fentre" + AFX_IDS_SCMOVE "Change la position de la fentre" + AFX_IDS_SCMINIMIZE "Rduit la fentre en icne" + AFX_IDS_SCMAXIMIZE "Agrandit la fentre au format de l'cran" + AFX_IDS_SCNEXTWINDOW "Passe la fentre de document suivante" + AFX_IDS_SCPREVWINDOW "Passe la fentre de document prcdente" + AFX_IDS_SCCLOSE "Ferme la fentre active et propose l'enregistrement des documents" +END + +STRINGTABLE DISCARDABLE +BEGIN + AFX_IDS_SCRESTORE "Restaure la fentre sa taille d'origine" + AFX_IDS_SCTASKLIST "Active la liste des tches" + AFX_IDS_MDICHILD "Active cette fentre" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_RUN "Execute le programme CBot\nExecute (F5)" +END + +STRINGTABLE DISCARDABLE +BEGIN + TX_TYPENAMES "les diffrents types" + 1001 "Byte" + 1002 "Short" + 1003 "Char" + 1004 "Int" + 1005 "Long" + 1006 "Real" + 1007 "Double" +END + +STRINGTABLE DISCARDABLE +BEGIN + 1008 "Boolean" + 1009 "String" + 1010 "Array" + 1011 "Arraybody" + 1012 "Pointer" + 1013 "Nullpointer" + 1014 "nop" + 1015 "Class" + 1016 "Intrinsic" +END + +#endif // French (France) resources +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// French (Switzerland) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRS) +#ifdef _WIN32 +LANGUAGE LANG_FRENCH, SUBLANG_FRENCH_SWISS +#pragma code_page(1252) +#endif //_WIN32 + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_CONSOLE DIALOG DISCARDABLE 0, 0, 401, 210 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "CBot Console" +FONT 8, "MS Sans Serif" +BEGIN + LTEXT "Commande :",IDC_STATIC,7,177,40,8 + EDITTEXT IDC_EDIT2,7,189,329,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "Excute",IDOK,344,189,50,14 + EDITTEXT IDC_EDIT1,7,7,387,167,ES_MULTILINE | ES_READONLY | + ES_WANTRETURN | WS_VSCROLL +END + +IDD_DIALOG1 DIALOG DISCARDABLE 0, 0, 177, 100 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Test performances" +FONT 8, "MS Sans Serif" +BEGIN + LTEXT "Boucles par seconde",IDC_STATIC,7,9,68,8 + EDITTEXT IDC_EDIT1,111,7,51,14,ES_AUTOHSCROLL | ES_READONLY + LTEXT "Nombre de scripts",IDC_STATIC,7,55,58,8 + EDITTEXT IDC_EDIT2,111,52,40,14,ES_AUTOHSCROLL + CONTROL "Spin1",IDC_SPIN1,"msctls_updown32",UDS_ARROWKEYS,152,52, + 10,14 + COMBOBOX IDC_COMBO1,111,74,52,111,CBS_DROPDOWNLIST | WS_VSCROLL | + WS_TABSTOP + LTEXT "Timer",IDC_STATIC,7,77,18,8 + LTEXT "Performance %",IDC_STATIC,7,28,48,8 + EDITTEXT IDC_EDIT3,111,25,51,14,ES_AUTOHSCROLL | ES_READONLY +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO DISCARDABLE +BEGIN + IDD_CONSOLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 394 + TOPMARGIN, 7 + BOTTOMMARGIN, 203 + END + + IDD_DIALOG1, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 170 + TOPMARGIN, 7 + BOTTOMMARGIN, 93 + END +END +#endif // APSTUDIO_INVOKED + + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE DISCARDABLE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE DISCARDABLE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE DISCARDABLE +BEGIN + "#define _AFX_NO_SPLITTER_RESOURCES\r\n" + "#define _AFX_NO_OLE_RESOURCES\r\n" + "#define _AFX_NO_TRACKER_RESOURCES\r\n" + "#define _AFX_NO_PROPERTY_RESOURCES\r\n" + "\r\n" + "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)\r\n" + "#ifdef _WIN32\r\n" + "LANGUAGE 12, 1\r\n" + "#pragma code_page(1252)\r\n" + "#endif\r\n" + "#include ""res\\TestCBot.rc2"" // non-Microsoft Visual C++ edited resources\r\n" + "#include ""l.fra\\afxres.rc"" // Standard components\r\n" + "#endif\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog Info +// + +IDD_DIALOG1 DLGINIT +BEGIN + IDC_COMBO1, 0x403, 2, 0 +0x0031, + IDC_COMBO1, 0x403, 3, 0 +0x3031, "\000" + IDC_COMBO1, 0x403, 4, 0 +0x3031, 0x0030, + IDC_COMBO1, 0x403, 5, 0 +0x3031, 0x3030, "\000" + 0 +END + +#endif // French (Switzerland) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// +#define _AFX_NO_SPLITTER_RESOURCES +#define _AFX_NO_OLE_RESOURCES +#define _AFX_NO_TRACKER_RESOURCES +#define _AFX_NO_PROPERTY_RESOURCES + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) +#ifdef _WIN32 +LANGUAGE 12, 1 +#pragma code_page(1252) +#endif +#include "res\TestCBot.rc2" // non-Microsoft Visual C++ edited resources +#include "l.fra\afxres.rc" // Standard components +#endif +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/src/CBot/tests/TestCBot/TestCBotDoc.cpp b/src/CBot/tests/TestCBot/TestCBotDoc.cpp new file mode 100644 index 0000000..8880c57 --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBotDoc.cpp @@ -0,0 +1,697 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBotDoc.cpp : implementation of the CTestCBotDoc class +// + +#include "stdafx.h" +#include "TestCBot.h" + +#include "TestCBotDoc.h" +#include "TestCBotView.h" +#include "CBotConsoleDlg.h" +#include "PerformDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotDoc + +IMPLEMENT_DYNCREATE(CTestCBotDoc, CDocument) + +BEGIN_MESSAGE_MAP(CTestCBotDoc, CDocument) + //{{AFX_MSG_MAP(CTestCBotDoc) + ON_COMMAND(ID_RUN, OnRun) + ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1) + ON_COMMAND(ID_TEST, OnTest) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotDoc construction/destruction + +static BOOL test = FALSE; + + +CTestCBotDoc::CTestCBotDoc() +{ + m_pEdit = NULL; + m_pProg = NULL; + m_bModified = FALSE; +} + +CTestCBotDoc::~CTestCBotDoc() +{ + delete m_pEdit; + delete m_pProg; +} + +BOOL CTestCBotDoc::OnNewDocument() +{ + if (!CDocument::OnNewDocument()) + return FALSE; + + return TRUE; +} + + + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotDoc serialization + +void CTestCBotDoc::Serialize(CArchive& ar) +{ + if (ar.IsStoring()) + { + m_pEdit->GetWindowText(m_DocText); + int w = m_DocText.GetLength(); + ar.Write((LPCTSTR)m_DocText, w); + } + else + { + int r; + char buf[10001]; + + r = ar.Read(buf, 10000); + buf[r] = 0; + m_DocText = buf; + + if ( m_pProg == NULL ) m_pProg = new CBotProgram(); + + if (!m_pProg->Compile(m_DocText, m_Liste, NULL)) + { + delete m_pProg; + m_pProg = NULL; + } + } +} + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotDoc diagnostics + +#ifdef _DEBUG +void CTestCBotDoc::AssertValid() const +{ + CDocument::AssertValid(); +} + +void CTestCBotDoc::Dump(CDumpContext& dc) const +{ + CDocument::Dump(dc); +} +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotDoc commands + +void CTestCBotDoc::OnRun() +{ + OnFileSave(); + + m_pEdit->GetWindowText(m_DocText); + + CString TextError; + int code, start, end; + + if ( m_pProg == NULL ) m_pProg = new CBotProgram(); + + CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); + + if (!m_pProg->Compile(m_DocText, m_Liste, NULL)) + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + + m_pEdit->SetSel( start, end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + TextError = CBotProgram::GivErrorText( code ); + AfxMessageBox( TextError ); + + m_pEdit->SetFocus(); + return; + } + + if( m_Liste.GivSize() == 0 ) + { + AfxMessageBox("Aucune fonction marque \"extern\" !"); + return; + } + + for ( int i = 0; i < m_Liste.GivSize(); i++ ) + { + int start, stop; + m_pProg->GetPosition(m_Liste[i], start, stop, GetPosNom, GetPosParam); + m_Liste[i] = m_DocText.Mid( start, stop-start ); + } + + CBotConsoleDlg dlg; + dlg.m_pListe = &m_Liste; + dlg.m_pEditx = m_pEdit; + + dlg.DoModal(); // dialogue pour faire la console + + if ( dlg.m_code>0 ) + { + CString TextError; + + TextError = m_pProg->GivErrorText( dlg.m_code ); + + m_pEdit->SetSel( dlg.m_start, dlg.m_end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + AfxMessageBox(TextError); + } + + m_pEdit->SetFocus(); + + return; +} + + +void CTestCBotDoc::OnChangeEdit1() +{ + SetModifiedFlag(); + m_bModified = TRUE; +} + +BOOL CTestCBotDoc::Compile() +{ + m_pEdit->GetWindowText(m_DocText); + + CString TextError; + int code, start, end; + + if ( m_pProg == NULL ) m_pProg = new CBotProgram(); + + char buffer[100]; + strcpy(buffer, "le pointeur passer pour voir"); + + if (m_bModified && !m_pProg->Compile(m_DocText, m_Liste, (void*)buffer)) + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + + m_pEdit->SetSel( start, end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + TextError = CBotProgram::GivErrorText( code ); + AfxMessageBox( TextError ); + + m_pEdit->SetFocus(); + m_bModified = FALSE; + return FALSE; + } + + if ( m_pProg->GetPosition( "TheTest", start, end) ) + { + m_pEdit->SetSel( start, end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + } + + m_bModified = FALSE; + return TRUE; +} + + + +static int compt = 0; +// routine retournant le "pointeur" un autre object +BOOL rRetObject( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + pResult->SetPointer( NULL ); + compt+=45671; + if (compt&0x11) return TRUE; + + CBotVar* pAutre = CBotVar::Create("autre", CBotTypResult( CBotTypClass, "object" )); + pAutre->SetUserPtr( (void*)2 ); + pResult->SetPointer( pAutre ); + + if (!pResult->IsElemOfClass("object")) + return TRUE; + + delete pAutre; + return TRUE; +} + +CBotTypResult cRetObject( CBotVar* &pVar, void* pUser ) +{ + return CBotTypResult( CBotTypPointer, "object"); +} + +BOOL roRadar( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + pResult->SetPointer( NULL ); + compt+=45671; + if (compt&0x11) return TRUE; + + CBotVar* pAutre = CBotVar::Create("autre", CBotTypResult( CBotTypClass, "object" )); + pAutre->SetUserPtr( (void*)2 ); + pResult->SetPointer( pAutre ); + + if (!pResult->IsElemOfClass("object")) + return TRUE; + + delete pAutre; + return TRUE; +} + +CBotTypResult coRadar( CBotVar* pThis, CBotVar* &pVar ) +{ + void* pUser = pThis->GivUserPtr(); + return CBotTypResult( CBotTypPointer, "object"); +} + +BOOL rMove( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + if ( test < 12 ) + { + test++; + return FALSE; + } + return TRUE; +} + +CBotTypResult cMove( CBotVar* &pVar, void* pUser ) +{ + return CBotTypResult( 0 ); +} + +BOOL rTurn( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + return TRUE; +} + +CBotTypResult cTurn( CBotVar* &pVar, void* pUser ) +{ + return CBotTypResult( 0 ); +} + +BOOL rRadar( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + pResult->SetPointer( NULL ); + + if ( pVar ) pVar->debug(); + + compt+=45671; + if (compt&0x11) + { + return FALSE; // TRUE; + } + + CBotVar* pAutre = CBotVar::Create("autre", CBotTypResult( CBotTypClass, "object" )); + pAutre->SetUserPtr( (void*)2 ); + pResult->SetPointer( pAutre ); + + if (!pResult->IsElemOfClass("object")) + return TRUE; + + delete pAutre; + return TRUE; +} + +CBotTypResult cRadar( CBotVar* &pVar, void* pUser ) +{ + return CBotTypResult( CBotTypPointer, "object"); +} + +// routine retournant le "pointeur" un autre object +BOOL rTEST( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + test = 1 ; + if ( pVar == NULL ) return TRUE; + + test = pVar->GivValInt(); + if ( test == 5 ) + { + pVar = pVar->GivNext(); + pVar->SetUserPtr( OBJECTDELETED ); + } + return TRUE; +} + +CBotTypResult cTEST( CBotVar* &pVar, void* pUser ) +{ + return CBotTypResult( 0 ); +} + +// routine retournant le "pointeur" un autre object +BOOL rF( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + if ( pResult == NULL ) return TRUE; + pResult->SetValInt(3); + return TRUE; +} + +CBotTypResult cF( CBotVar* &pVar, void* pUser ) +{ + return CBotTypResult( CBotTypFloat ); +} + +///////////////////////////////////////////////////////////////// + +// Compilation d'une procdure avec un "point". + +CBotTypResult cPoint(CBotVar* &var, void* user) +{ + if ( var == 0 ) return CBotTypResult( CBotErrLowParam ); + + if ( var->GivType() <= CBotTypDouble ) + { + var = var->GivNext(); + if ( var == 0 ) return CBotTypResult( CBotErrLowParam ); + if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); + var = var->GivNext(); + if ( var == 0 ) return CBotTypResult( CBotErrLowParam ); + if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); + var = var->GivNext(); + return CBotTypResult( 0 ); + } + + if ( var->GivType() == CBotTypClass ) + { + if ( !var->IsElemOfClass("point") ) return CBotTypResult( CBotErrBadParam ); + var = var->GivNext(); + return CBotTypResult( 0 ); + } + + return CBotTypResult( CBotErrBadParam ); +} + +// Donne un paramtre de type "point". +#define UNIT 1 + + +CBotTypResult cSpace(CBotVar* &var, void* user) +{ + CBotTypResult ret; + + if ( var == 0 ) return CBotTypResult( CBotTypIntrinsic, "point" ); + ret = cPoint(var, user); + if ( !ret.Eq(0) ) return ret; + + if ( var == 0 ) return CBotTypIntrinsic; + if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); + var = var->GivNext(); + + if ( var == 0 ) return CBotTypIntrinsic; + if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); + var = var->GivNext(); + + if ( var == 0 ) return CBotTypIntrinsic; + if ( var->GivType() > CBotTypDouble ) return CBotTypResult( CBotErrBadNum ); + var = var->GivNext(); + + if ( var != 0 ) return CBotErrOverParam; + return CBotTypResult( CBotTypIntrinsic, "point" ); +} + +// Instruction "space(center, rMin, rMax, dist)". + +BOOL rSpace(CBotVar* var, CBotVar* result, int& exception, void* user) +{ + CBotVar* pSub; + float rMin, rMax, dist; + + rMin = 5.0f*UNIT; + rMax = 50.0f*UNIT; + dist = 4.0f*UNIT; + + if ( var == 0 ) + { +// center = pThis->RetPosition(0); + } + else + { + if ( var != 0 ) + { + rMin = var->GivValFloat()*UNIT; + var = var->GivNext(); + + if ( var != 0 ) + { + rMax = var->GivValFloat()*UNIT; + var = var->GivNext(); + + if ( var != 0 ) + { + dist = var->GivValFloat()*UNIT; + var = var->GivNext(); + } + } + } + } + + if ( result != 0 ) + { + pSub = result->GivItemList(); + if ( pSub != 0 ) + { + pSub->SetValFloat(1); + pSub = pSub->GivNext(); // "y" + pSub->SetValFloat(2); + pSub = pSub->GivNext(); // "z" +// pSub->SetValFloat(3); + } + } + return TRUE; +} +////////////////////////////////////////////////////////////// + + +void CTestCBotDoc::OnTest() +{ + CBotProgram::DefineNum("WingedGrabber", 1); + CBotProgram::DefineNum("TrackedGrabber", 2); + CBotProgram::DefineNum("WheeledGrabber", 3); + CBotProgram::DefineNum("LeggedGrabber", 4); + CBotProgram::DefineNum("WingedShooter", 5); + CBotProgram::DefineNum("TrackedShooter", 6); + CBotProgram::DefineNum("WheeledShooter", 7); + CBotProgram::DefineNum("LeggedShooter", 8); + CBotProgram::DefineNum("WingedOrgaShooter", 9); + CBotProgram::DefineNum("TrackedOrgaShooter", 10); + CBotProgram::DefineNum("WheeledOrgaShooter", 11); + CBotProgram::DefineNum("LeggedOrgaShooter", 12); + CBotProgram::DefineNum("WingedSniffer", 13); + CBotProgram::DefineNum("TrackedSniffer", 14); + CBotProgram::DefineNum("WheeledSniffer", 14); + CBotProgram::DefineNum("LeggedSniffer", 15); + CBotProgram::DefineNum("Thumper", 16); + CBotProgram::DefineNum("PhazerShooter", 17); + CBotProgram::DefineNum("Recycler", 18); + CBotProgram::DefineNum("Shielder", 19); + CBotProgram::DefineNum("Subber", 20); + CBotProgram::DefineNum("Me", 21); + + CBotProgram::DefineNum("TypeMarkPath", 111); + + OnFileSave(); + +// CPerformDlg dlg; +// dlg.m_Script = m_DocText; +// dlg.DoModal(); + + // dfini la routine RetObject + CBotProgram::AddFunction( "Radar", rRetObject, cRetObject ); + + // ajoute une routine pour cette classe + CBotProgram::AddFunction("Space", rSpace, cSpace); + + // dfini la routine Test + CBotProgram::AddFunction( "TEST", rTEST, cTEST ); + CBotProgram::AddFunction( "F", rF, cF ); + + CBotProgram::AddFunction( "goto", rMove, cMove ); + CBotProgram::AddFunction( "fire", rTurn, cTurn ); + CBotProgram::AddFunction( "radar", rRadar, cRadar ); + + // cre une instance de la classe "Bot" pour ce robot + CBotVar* pThisRobot = CBotVar::Create( "", CBotTypResult(CBotTypClass, "object") ); + pThisRobot->SetUserPtr( (void*)1 ); + pThisRobot->SetIdent( 1234 ); + + delete m_pProg; + // cre un objet programme associ cette instance + m_pProg = new CBotProgram(pThisRobot); + + // compile le programme + CString TextError; + int code, start, end; + + m_pEdit->GetWindowText(m_DocText); + if (!m_pProg->Compile(m_DocText, m_Liste, (void*) 44)) + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + + delete pThisRobot; + + m_pEdit->SetSel( start, end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + TextError = CBotProgram::GivErrorText( code ); + AfxMessageBox( TextError ); + + m_pEdit->SetFocus(); + return; + } + + // excute pour voir + m_pProg->Start(m_Liste[0]); + + int mode = -1; + + if ( mode >= 0 ) { + + // sauve et restore chaque pas possible + while (!m_pProg->Run(NULL, 1)) + { + const char* FunctionName; + int start1, end1; + m_pProg->GetRunPos(FunctionName, start1, end1); + if ( end1 <= 0 ) + m_pProg->GetRunPos(FunctionName, start1, end1); + m_pEdit->SetSel(start1, end1); + +if ( mode == 0 ) continue; + + FILE* pf; + pf = fOpen( "TEST.CBO", "wb" ); + CBotClass::SaveStaticState(pf); + m_pProg->SaveState(pf); + fClose(pf); + +if ( mode == 2 ) if (!m_pProg->Compile(m_DocText, m_Liste, (void*) 44)) + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + + delete pThisRobot; + + m_pEdit->SetSel( start, end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + TextError = CBotProgram::GivErrorText( code ); + AfxMessageBox( TextError ); + + m_pEdit->SetFocus(); + return; + } + + pf = fOpen( "TEST.CBO", "rb" ); + CBotClass::RestoreStaticState(pf); + m_pProg->RestoreState(pf); + fClose(pf); + + int start2, end2; + m_pProg->GetRunPos(FunctionName, start2, end2); + if ( end2 <= 0 ) + m_pProg->GetRunPos(FunctionName, start2, end2); + + if ( start1 != start2 || end1 != end2 ) + m_pProg->GetRunPos(FunctionName, start2, end2); + m_pEdit->SetSel(start2, end2); + } + + if (m_pProg->GetError(code, start, end)) + { + m_pEdit->SetSel(start, end); + TextError = CBotProgram::GivErrorText(code); + AfxMessageBox(TextError); + } + return;} + + while (!m_pProg->Run(NULL, 0)) + { + const char* FunctionName; + int start, end; + m_pProg->GetRunPos(FunctionName, start, end); + m_pEdit->SetSel(start, end); + + if ( FunctionName == NULL ) continue; + CString info (FunctionName); + CString sep (":\n"); + + int level = 0; + const char* Name; + while ( TRUE ) + { + CBotVar* pVar = m_pProg->GivStackVars(Name, level--); + if ( Name != FunctionName ) break; + if ( pVar == NULL ) continue; +// pVar->Maj(NULL, FALSE); + while ( pVar != NULL ) + { + info += sep; + info += pVar->GivName() + CBotString(" = ") + pVar->GivValString(); + sep = ", "; + pVar = pVar->GivNext(); + } + sep = "\n"; + } + if ( IDOK != AfxMessageBox(info, MB_OKCANCEL) ) break; + + if ( test == 1 ) + { + test = 0; + FILE* pf; + pf = fOpen( "TEST.CBO", "wb" ); + m_pProg->SaveState(pf); + fClose(pf); + } + + if ( test == 2 ) + { + test = 0; + FILE* pf; + pf = fOpen( "TEST.CBO", "rb" ); + m_pProg->RestoreState(pf); + fClose(pf); + } + + if ( test == 12 ) + { + test = 0; + FILE* pf; + pf = fOpen( "TEST.CBO", "wb" ); + m_pProg->SaveState(pf); + fClose(pf); + + pf = fOpen( "TEST.CBO", "rb" ); + m_pProg->RestoreState(pf); + fClose(pf); + + test = 13; + } + } + + if (m_pProg->GetError(code, start, end)) + { + m_pEdit->SetSel(start, end); + TextError = CBotProgram::GivErrorText(code); + AfxMessageBox(TextError); + } + + delete pThisRobot; +} + diff --git a/src/CBot/tests/TestCBot/TestCBotDoc.h b/src/CBot/tests/TestCBot/TestCBotDoc.h new file mode 100644 index 0000000..548607f --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBotDoc.h @@ -0,0 +1,78 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBotDoc.h : interface of the CTestCBotDoc class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_TESTCBOTDOC_H__4D1BB90B_8E74_11D4_A439_00D059085115__INCLUDED_) +#define AFX_TESTCBOTDOC_H__4D1BB90B_8E74_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + + +class CTestCBotDoc : public CDocument +{ +protected: // create from serialization only + CTestCBotDoc(); + DECLARE_DYNCREATE(CTestCBotDoc) + +// Attributes +public: + CEdit* m_pEdit; // pour mmoriser le texte, et l'afficher + CBotProgram* m_pProg; // le programme compil + CString m_DocText; + CBotStringArray m_Liste; + BOOL m_bModified; + +// Operations +public: + BOOL Compile(); + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CTestCBotDoc) + public: + virtual BOOL OnNewDocument(); + virtual void Serialize(CArchive& ar); + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CTestCBotDoc(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +protected: + +// Generated message map functions +protected: + //{{AFX_MSG(CTestCBotDoc) + afx_msg void OnRun(); + afx_msg void OnChangeEdit1(); + afx_msg void OnTest(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TESTCBOTDOC_H__4D1BB90B_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/TestCBot/TestCBotView.cpp b/src/CBot/tests/TestCBot/TestCBotView.cpp new file mode 100644 index 0000000..bca3c56 --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBotView.cpp @@ -0,0 +1,142 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// TestCBotView.cpp : implementation of the CTestCBotView class +// + +#include "stdafx.h" +#include "TestCBot.h" + +#include "TestCBotDoc.h" +#include "TestCBotView.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotView + +IMPLEMENT_DYNCREATE(CTestCBotView, CView) + +BEGIN_MESSAGE_MAP(CTestCBotView, CView) + //{{AFX_MSG_MAP(CTestCBotView) + ON_WM_SIZE() + ON_MESSAGE(WM_ACTWINDOW, ActWindow) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotView construction/destruction + +CTestCBotView::CTestCBotView() +{ +} + +CTestCBotView::~CTestCBotView() +{ +} + +BOOL CTestCBotView::PreCreateWindow(CREATESTRUCT& cs) +{ + return CView::PreCreateWindow(cs); +} + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotView drawing + +void CTestCBotView::OnDraw(CDC* pDC) +{ + CTestCBotDoc* pDoc = GetDocument(); + ASSERT_VALID(pDoc); +} + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotView diagnostics + +#ifdef _DEBUG +void CTestCBotView::AssertValid() const +{ + CView::AssertValid(); +} + +void CTestCBotView::Dump(CDumpContext& dc) const +{ + CView::Dump(dc); +} + +CTestCBotDoc* CTestCBotView::GetDocument() // non-debug version is inline +{ + ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestCBotDoc))); + return (CTestCBotDoc*)m_pDocument; +} +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CTestCBotView message handlers + +void CTestCBotView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) +{ + CTestCBotDoc* pDoc = GetDocument(); +// CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp(); + + if ( pDoc->m_pEdit == NULL) + { + pDoc->m_pEdit = new CEdit(); + CRect rect; + GetClientRect( rect ); + + pDoc->m_pEdit->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_NOHIDESEL|ES_AUTOVSCROLL, + rect, this, IDC_EDIT1 ); + pDoc->m_pEdit->SetTabStops(12); + pDoc->m_pEdit->SetWindowText(pDoc->m_DocText); + } + + if ( !bActivate && !pDoc->Compile() ) + { +// comment faire pour ractiver l'ancien document + } + + CView::OnActivateView(bActivate, pActivateView, pDeactiveView); + + if ( bActivate ) pDoc->m_pEdit->SetFocus(); +} + + +void CTestCBotView::OnSize(UINT nType, int cx, int cy) +{ + CView::OnSize(nType, cx, cy); + + CTestCBotDoc* pDoc = GetDocument(); + if ( pDoc->m_pEdit != NULL ) + { + CRect rect; + GetClientRect( rect ); + pDoc->m_pEdit->MoveWindow( rect ); + pDoc->m_pEdit->SetFocus(); + } +} + + + +LONG CTestCBotView::ActWindow(UINT wparam, LONG lparam) +{ +// GetParentFrame()->SetActiveView( this, TRUE ); +// CMDIChildWnd::OnMDIActivate(1, this, this) + return 0; +} diff --git a/src/CBot/tests/TestCBot/TestCBotView.h b/src/CBot/tests/TestCBot/TestCBotView.h new file mode 100644 index 0000000..065ee08 --- /dev/null +++ b/src/CBot/tests/TestCBot/TestCBotView.h @@ -0,0 +1,78 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.// TestCBotView.h : interface of the CTestCBotView class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_TESTCBOTVIEW_H__4D1BB90D_8E74_11D4_A439_00D059085115__INCLUDED_) +#define AFX_TESTCBOTVIEW_H__4D1BB90D_8E74_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +class CTestCBotView : public CView +{ +protected: // create from serialization only + CTestCBotView(); + DECLARE_DYNCREATE(CTestCBotView) + +// Attributes +public: + CTestCBotDoc* GetDocument(); + +// Operations +public: + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CTestCBotView) + public: + virtual void OnDraw(CDC* pDC); // overridden to draw this view + virtual BOOL PreCreateWindow(CREATESTRUCT& cs); + protected: + virtual void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView); + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CTestCBotView(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +protected: + +// Generated message map functions +protected: + //{{AFX_MSG(CTestCBotView) + afx_msg void OnSize(UINT nType, int cx, int cy); + afx_msg LONG ActWindow(UINT wparam, LONG lparam) ; + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +#ifndef _DEBUG // debug version in TestCBotView.cpp +inline CTestCBotDoc* CTestCBotView::GetDocument() + { return (CTestCBotDoc*)m_pDocument; } +#endif + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TESTCBOTVIEW_H__4D1BB90D_8E74_11D4_A439_00D059085115__INCLUDED_) diff --git "a/src/CBot/tests/TestCBot/a\2471.txt~" "b/src/CBot/tests/TestCBot/a\2471.txt~" new file mode 100644 index 0000000..0c57950 --- /dev/null +++ "b/src/CBot/tests/TestCBot/a\2471.txt~" @@ -0,0 +1,96 @@ +object radarGuepe(point orig, float dist) +{ + int i; + object pr, r; + float mindist; + + i = 0; + mindist = 1000; + while (i<30) + { + pr = radar(i); + if (pr != null) + { + + if (F(orig, pr.position) < mindist and pr.category == AlienWasp and pr.altitude > 3) + { + mindist = distance(orig, pr.position); + r = pr; + } + } + i = i+1; + } + if (mindist < dist) return(r); else return(null); +} + + +class Guepe +{ + + point pos; + + + void cherche(point orig, float dist) + { + object p; + point o; + + p = radarGuepe(orig, dist); + while (p == null) + { + wait(0.1); + p = radarGuepe(orig, dist); + } + + pos.x = p.position.x; + pos.y = p.position.y; + pos.z = p.position.z; + + //o = p.position; + //wait(0.1); + + //vitessex = (p.position.x - o.x)/0.1; + //vitessey = (p.position.y - o.y)/0.1; + //vitessez = (p.position.z - o.z)/0.1; + + } + + + void tire(point orig, float orient) + { + //float t = 3; //temps d'anticipation + float angle; + point cible; + + cible.x = pos.x;// + t*vitessex; + cible.y = pos.y;// + t*vitessey; + cible.z = pos.z;// + t*vitessez; + + if (cible.x == 0) angle = 90; else + angle = atan(cible.y / cible.x); + if (cible.x < 0) angle = angle + 180; + angle = angle - orient; + if (angle > 180) angle = angle - 360; + if (angle < -180) angle = angle + 360; + turn(angle); + + angle = atan((cible.z-orig.z) / distance2d(orig, cible)); + aim(angle); + + fire(0.1); + + } +} + +extern void object::Fourmi6() +{ + //fps(1000); + Guepe guepe = new Guepe(); + + while (true) + { + guepe.cherche(position, 50); + + guepe.tire(position, orientation); + } +} diff --git a/src/CBot/tests/TestCBot/res/TestCBot.ico b/src/CBot/tests/TestCBot/res/TestCBot.ico new file mode 100644 index 0000000..06a649d Binary files /dev/null and b/src/CBot/tests/TestCBot/res/TestCBot.ico differ diff --git a/src/CBot/tests/TestCBot/res/TestCBot.rc2 b/src/CBot/tests/TestCBot/res/TestCBot.rc2 new file mode 100644 index 0000000..b55f0d9 --- /dev/null +++ b/src/CBot/tests/TestCBot/res/TestCBot.rc2 @@ -0,0 +1,13 @@ +// +// TESTCBOT.RC2 - resources Microsoft Visual C++ does not edit directly +// + +#ifdef APSTUDIO_INVOKED + #error this file is not editable by Microsoft Visual C++ +#endif //APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// Add manually edited resources here... + +///////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/tests/TestCBot/res/TestCBotDoc.ico b/src/CBot/tests/TestCBot/res/TestCBotDoc.ico new file mode 100644 index 0000000..3545614 Binary files /dev/null and b/src/CBot/tests/TestCBot/res/TestCBotDoc.ico differ diff --git a/src/CBot/tests/TestCBot/res/Toolbar.bmp b/src/CBot/tests/TestCBot/res/Toolbar.bmp new file mode 100644 index 0000000..04a71af Binary files /dev/null and b/src/CBot/tests/TestCBot/res/Toolbar.bmp differ diff --git a/src/CBot/tests/TestCBot/resource.h b/src/CBot/tests/TestCBot/resource.h new file mode 100644 index 0000000..d661201 --- /dev/null +++ b/src/CBot/tests/TestCBot/resource.h @@ -0,0 +1,44 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/.//{{NO_DEPENDENCIES}} +// Microsoft Developer Studio generated include file. +// Used by TestCBot.rc +// +#define IDD_ABOUTBOX 100 +#define IDR_MAINFRAME 128 +#define IDR_TESTCBTYPE 129 +#define IDD_DIALOG1 130 +#define IDD_CONSOLE 131 +#define IDD_DIALOG2 133 +#define IDC_EDIT1 1000 +#define TX_TYPENAMES 1000 +#define IDC_SPIN1 1001 +#define IDC_EDIT2 1002 +#define IDC_COMBO1 1003 +#define IDC_EDIT3 1004 +#define ID_RUN 32771 +#define ID_TEST 32772 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_3D_CONTROLS 1 +#define _APS_NEXT_RESOURCE_VALUE 135 +#define _APS_NEXT_COMMAND_VALUE 32773 +#define _APS_NEXT_CONTROL_VALUE 1004 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/src/CBot/tests/TestCBot/scenarios/B.txt b/src/CBot/tests/TestCBot/scenarios/B.txt new file mode 100644 index 0000000..53715f8 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/B.txt @@ -0,0 +1,18 @@ + + float [ ] TEST2 ( int [ ] param ) + { + float [ ] z; + for ( int i = 0 ; i < sizeof( param ) ; i++ ) try { z [i] = param [i] / 3; } + return z; + } + +extern public void T() +{ + int a [4]; + for ( int i = 0 ; i < 3 ; i++ ) a[i] = 4*i; + a [2] = 22; + + float [] b ; + b = TEST2 ( a ) ; + show ( a, b ); +} diff --git a/src/CBot/tests/TestCBot/scenarios/BUG2.txt b/src/CBot/tests/TestCBot/scenarios/BUG2.txt new file mode 100644 index 0000000..44de05a --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/BUG2.txt @@ -0,0 +1,107 @@ +object object :: TT ( int n ) +{ + object XX = radar(); + if ( n == 0 ) return null; + + while ( null == XX ) XX = radar(); + return XX; +} + +extern void object::Attack( ) +{ + show ( TT ( 0 ) ) ; + show ( TT ( 1 ) ) ; + return; + + int list[]; + int i; + object p; + float dist, prox; + point dest; + boolean advance = true; + + TEST(0); // ne stoppe pas si erreur +// while ( F () != 0 ) F(1); + + i = 0; + list[i++] = WingedGrabber; + list[i++] = TrackedGrabber; + list[i++] = WheeledGrabber; + list[i++] = LeggedGrabber; + list[i++] = WingedShooter; + list[i++] = TrackedShooter; + list[i++] = WheeledShooter; + list[i++] = LeggedShooter; + list[i++] = WingedOrgaShooter; + list[i++] = TrackedOrgaShooter; + list[i++] = WheeledOrgaShooter; + list[i++] = LeggedOrgaShooter; + list[i++] = WingedSniffer; + list[i++] = TrackedSniffer; + list[i++] = WheeledSniffer; + list[i++] = LeggedSniffer; + list[i++] = Thumper; + list[i++] = PhazerShooter; + list[i++] = Recycler; + list[i++] = Shielder; + list[i++] = Subber; + list[i++] = Me; + list[i++] = 3333; + list[i++] = 3334; + list[i++] = 3335; + list[i++] = 3336; + list[i++] = 3337; + list[i++] = 3338; + list[i++] = 3339; + list[i++] = 3331; + list[i++] = 3332; + list[i++] = 3330; + list[i++] = 1111; + list[i++] = 1112; + + F(F(0)); + + while ( true ) + { + p = radar(list, 0, 360, 0, 1000); + if ( p == null ) + { + F(2); + } + else + { + dist = F(p.position, position); + if ( dist <= 40 && !advance ) + { + fire(p.position); + advance = true; + } + else + { +//? if ( RetBaseDistance() > 20 ) + { + prox = dist-(5+F()*5); + if ( prox < 5 ) prox = 5; + dest.x = (position.x-p.position.x)*prox/dist + p.position.x; + dest.y = (position.y-p.position.y)*prox/dist + p.position.y; + dest.z = (position.z-p.position.z)*prox/dist + p.position.z; + goto(dest); + advance = false; + } + } + } + } +} + +// Calcule la distance jusqu' la base. + +float object::RetBaseDistance() +{ + object p; + float dist; + + p = radar(4444, 0, 360, 0, 1000); + if ( p == null ) return 1000; + dist = F(p.position, position); + return dist; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/Deleted.txt b/src/CBot/tests/TestCBot/scenarios/Deleted.txt new file mode 100644 index 0000000..469a624 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/Deleted.txt @@ -0,0 +1,23 @@ +public extern void object :: ESSAI() +{ + while(true) + { + if ( true ) + { + goto(12); + break; + } + } + object x = null ; + + while ( x == null ) x = radar(); + + show ( x.position ) ; + + TEST(5, x); + + if ( x == null ) show ( "DELETED" ); + + show ( x.position ) ; + +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/MaClass.txt b/src/CBot/tests/TestCBot/scenarios/MaClass.txt new file mode 100644 index 0000000..ac472b4 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/MaClass.txt @@ -0,0 +1,16 @@ + +class MaClass +{ + int a = 1 ; + MaClass pointeur ; + MaClass next = null ; + CPoint autre = new CPoint( 1 , 1 ) ; +} + +extern public void Test ( ) +{ + MaClass x () ; + x.next = new MaClass ( ) ; + println ( x ) ; +} + diff --git a/src/CBot/tests/TestCBot/scenarios/Mc2.txt b/src/CBot/tests/TestCBot/scenarios/Mc2.txt new file mode 100644 index 0000000..172c259 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/Mc2.txt @@ -0,0 +1,4 @@ +class MaClass +{ + int t = 12; +} diff --git a/src/CBot/tests/TestCBot/scenarios/Mon fichier.txt b/src/CBot/tests/TestCBot/scenarios/Mon fichier.txt new file mode 100644 index 0000000..6b35bf8 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/Mon fichier.txt @@ -0,0 +1,2 @@ +Voici encore du texte +et une seconde ligne diff --git a/src/CBot/tests/TestCBot/scenarios/Nop.txt b/src/CBot/tests/TestCBot/scenarios/Nop.txt new file mode 100644 index 0000000..6a66f6f --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/Nop.txt @@ -0,0 +1,4 @@ +public extern void Nop() +{ + while ( true ) {} +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/POS.txt b/src/CBot/tests/TestCBot/scenarios/POS.txt new file mode 100644 index 0000000..688e4fb --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/POS.txt @@ -0,0 +1,14 @@ +void object :: T ( ) +{ + show ( position ) ; +} + +public extern void object :: POS() +{ + for ( int i = 0; i < 10 ; i++ ) + { + if ( i == 2 ) TEST ( 12 ) ; +// show ( position ); + T ( ) ; + } +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/T.txt b/src/CBot/tests/TestCBot/scenarios/T.txt new file mode 100644 index 0000000..50a792b --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/T.txt @@ -0,0 +1,4 @@ +public extern int T ( float n ) +{ + return n * 1.1; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/TESTALL.txt b/src/CBot/tests/TestCBot/scenarios/TESTALL.txt new file mode 100644 index 0000000..82247a0 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TESTALL.txt @@ -0,0 +1,161 @@ +int T ( int z ) +{ + return 45 + z ; +} + +class toto +{ + int val = 3 ; + int x = 3 * 3 ; + void toto( int n ) + { val = n + 3 ; } + int retval ( int param ) + { int r = val + param + x ; + val = param ; + return r ; } +} + +public extern void object :: Chose( ) +{ + int z [ 6 ]; + for ( int i = 0 ; i < 6 ; ) z [ i++ ] = 3 - i ; + show ( z ) ; + return; + + // test des tableaux + int [ ] a [ 3 ] ; +// a = null; + if ( a == null ) show ( "NULL" ); + + a [ 2 / 2 ] [ 2 ]= 5 ; + int [ ] b ; b = a [1] ; + b [ 0 ] = -4; + a [ 4 / 2 ] [ 1 ]= 1 ; + show ( a , b ) ; + return ; + { + toto chose = new toto (5 ) ; + toto truc = chose ; + show ( chose, chose.retval( 100 ) , + truc, truc.retval (40 ) ) ; + + return; + } + { + point A = new + point ( 4 * 4 , 2 ) ; + show ( A ) ; + return; + } + { + show ( T ( 1 ) , T ( 3.7 ) ) ; + return; + } + + { + point A ( 3, 4 ) , + B = A ; + + int n = -4; + show ( n ); + + show ( A, B ) ; + + boolean a = false; + boolean b = a or true; + if ( not a and b ) ; + return; + } + { + // test try + float x = nan ; int z = 0 ; + try { +// throw ( 3 * 4 + 33 ) ; + int zz ; goto ( 12 ) ; z = 1 ; z = 0 / 0 ; z = 2 ; + } + catch ( 45 + 0 * 6000 ) + { + show( "Exception 6000", z ) ; + } + catch ( x == 0 ) { show( "x nul" ) ; } + finally { show ( "fini" ) ; } + show ( "continue" ); + return; + } + { + // test des if + int a = 3; + if ( a == 3 ) show ( "33"); + else show ( "44"); + if ( a != 3 ) show ( "333"); + else show ( "444"); + return; + } + { + int a = 0; + // test break +un: + while ( true ) + { +deux: + while ( true ) + { + a++; + if ( a == 2 ) continue; + if ( a == 3 ) break deux; + show ( a ) ; + if ( a == 5 ) break un; + } + show ( "DEUX" ); + } + return; + } + { + // test switch + int a = 0; + + switch ( a ) + { + case 1 : show( "un" ) ; break; + case 2 : show( "deux" ) ; // break; + case 3 : show( "trois" ) ; break; + case 4 : show( "quatre" ) ; // break; + default : show( "par dfaut" ) ; + } + return; + } + { + // test boucle while + float z = 3.3; + while ( z > 0 ) + { show ( z-- ) ; } + return; + } + + { + // test boucle do + float y = 3.3; + do { int x = 0; show(y); y++; } while ( y < 7 ) ; + return; + } + // test boucle for + int j = -7; show ( j ); + for ( int ii = 3, j = 31; ii < 6 ; ++ii, j = j -3 ) + { + j = 10 * j; + show ( ii, j ); + } + return; +{ + // dclarations de variables + int a; int b = 3; int c = 4*b, d = 1, e; + float x; float y = 3.3; float z = y / 2, u = 1, v; + boolean t; boolean tt = true or false; boolean ttt = false, tttt = true, t5; + string s; string ss = "hello"; string s2 = ss + " plus", s3 = "s3", s4; + + show( b, c, d ); + show( y, z, u ); + show( tt, ttt, tttt ); + show( ss, s2, s3 ); +} +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/TestCB1.txt b/src/CBot/tests/TestCBot/scenarios/TestCB1.txt new file mode 100644 index 0000000..516db47 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestCB1.txt @@ -0,0 +1,18 @@ +extern public void toto() +{ + print( "hello" ) ; + print( fac(5) ); + print( t() ) ; +} + +public int fac(int n) +{ + if ( n<2 ) return 1; + return n * fac(n-1); +} + +point t() +{ + point a(1,2); + return a; +} diff --git a/src/CBot/tests/TestCBot/scenarios/TestCBot1.txt b/src/CBot/tests/TestCBot/scenarios/TestCBot1.txt new file mode 100644 index 0000000..d27b4f8 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestCBot1.txt @@ -0,0 +1,27 @@ + +class CPoint2 +{ + float x, y; + void CPoint2(float x, float y) + { + this.x = x; + this.y = y; + } +} + +public extern void T ( ) +{ + CPoint2 X( 12, 33 ), Y ( -4, 4/3 ); + print ( X, Y ) ; +} + +public extern void Hello ( ) + +{ + println ( "Hello" ); +} + +public extern void test ( int n ) +{ + for ( int i = n; i>0 ; i--) print (i); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/TestCBot3.txt b/src/CBot/tests/TestCBot/scenarios/TestCBot3.txt new file mode 100644 index 0000000..b915f96 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestCBot3.txt @@ -0,0 +1,24 @@ +public extern void Test () +{ + for ( int x = 100000; x>0 ; x-- ) { } +} + +float MaRoutine( CPoint A, CPoint B ) +{ + A.x -= B.x ; // distance en x + A.y -= B.y ; // distance en y + A.x *= A.x; // carr de la distance + A.y += A.y; // carr de la distance + println ( A, B ) ; + return ( A.x + A.y ) ; +} + +public extern void TestAB ( ) +{ + CPoint A(3, 5) ; + CPoint B(4, -2); + println ( A, B ) ; + MaRoutine( A, B ) ; + println ( A, B ) ; +} + diff --git a/src/CBot/tests/TestCBot/scenarios/TestNull.txt b/src/CBot/tests/TestCBot/scenarios/TestNull.txt new file mode 100644 index 0000000..f447245 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestNull.txt @@ -0,0 +1,15 @@ +extern public void TestNull () +{ + CPoint pointeur = null; + + try { + pointeur.x = 4; } + catch ( 6007 ) {} + + pointeur = new CPoint(1,2); + + print ( pointeur.x, pointeur.y, + pointeur ); + + pointeur.x = 5; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/TestRestoreState.txt b/src/CBot/tests/TestCBot/scenarios/TestRestoreState.txt new file mode 100644 index 0000000..1e49e37 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestRestoreState.txt @@ -0,0 +1,67 @@ +// routine de Daniel qui plante aprs RestoreState + +extern void object::Attack( ) +{ + int list[], i; + object p; + float dist, prox; + point nav1, nav2, dest; + boolean advance = true; + + i = 0; + list[i++] = WingedGrabber; + list[i++] = TrackedGrabber; + list[i++] = WheeledGrabber; + list[i++] = LeggedGrabber; + list[i++] = WingedShooter; + list[i++] = TrackedShooter; + list[i++] = WheeledShooter; + list[i++] = LeggedShooter; + list[i++] = WingedOrgaShooter; + list[i++] = TrackedOrgaShooter; + list[i++] = WheeledOrgaShooter; + list[i++] = LeggedOrgaShooter; + list[i++] = WingedSniffer; + list[i++] = TrackedSniffer; + list[i++] = WheeledSniffer; + list[i++] = LeggedSniffer; + list[i++] = Thumper; + list[i++] = PhazerShooter; + list[i++] = Recycler; + list[i++] = Shielder; + list[i++] = Subber; + list[i++] = Me; + + nav1.x = 1;//cmdline(0); + nav1.y = 1;//cmdline(1); + nav2.x = 2;//cmdline(2); + nav2.y = 2;//cmdline(3); + + while ( true ) + { + while ( true ) + { + // ennemi proximit ? + p = radar(list, 0, 360, 0, 40); + if ( p == null ) break; + // lui tire dessus + fire(p.position); + } + + // se promne vers le point A + goto(nav1); + + while ( true ) + { + // ennemi proximit ? + p = radar(list, 0, 360, 0, 40); + if ( p == null ) break; + // lui tire dessus + fire(p.position); + } + + // se promne vers le point B + goto(nav2); + } +} + diff --git a/src/CBot/tests/TestCBot/scenarios/TestStatic.txt b/src/CBot/tests/TestCBot/scenarios/TestStatic.txt new file mode 100644 index 0000000..f501aa5 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestStatic.txt @@ -0,0 +1,31 @@ +class ESSAI +{ + int x = 0; + static int nb = 3; + static int [ ] array ; + + void Put( int val) + { +show(nb); + array[ nb ] = val; +// this.nb++; + this.nb = this.nb + 1; +show(nb, array); + } + int Get( ) + { + nb--; +show("out", nb, array); + return array[ nb ] ; + } +} + +extern public void T() +{ + ESSAI t1 ( ) ; + ESSAI t2 ( ) ; + t1.nb++; + t1.Put( 11 ); t1.Put( 12 ); t2.Put( 13 ); + + show ( t1.Get(), t2.Get(), t2.Get() ) ; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/TestStr.txt b/src/CBot/tests/TestCBot/scenarios/TestStr.txt new file mode 100644 index 0000000..683ec1b --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/TestStr.txt @@ -0,0 +1,17 @@ +extern public void TSTR() +{ + string s = "C'est un essai"; + + print ( s, strlen(s), strleft(s, 3), strright(s,3), strmid(s, 2), strmid(s,2,3), strfind(s, "un"), strfind(s, "sdgfld") ); + + show ( strupper(s), strlower(s) ); + + s = "123.45" ; + print ( strval(s) ); + + + string sub = strright("abcdef", 2); // sub vaut "ef###", # tant un caractre bizarre quelconque + show (sub); + int pos = strfind("abcdef", "xy"); // pos vaut -1. Pourquoi pas nan ? + show(pos); +} diff --git a/src/CBot/tests/TestCBot/scenarios/Z.txt b/src/CBot/tests/TestCBot/scenarios/Z.txt new file mode 100644 index 0000000..714119b --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/Z.txt @@ -0,0 +1,14 @@ +public extern void tp() +{ + int a [4], b[]; + a [ 0 ] = 8 ; + + b = T ( a ) ; + show ( a, b ); +} + +int[] T ( int[] Z ) +{ + for ( int i = 0; i < 4 ; i++ ) Z[ i ] = i * i ; + return Z; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/a1.txt b/src/CBot/tests/TestCBot/scenarios/a1.txt new file mode 100644 index 0000000..165bc95 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/a1.txt @@ -0,0 +1,96 @@ +object radarGuepe(point orig, float dist) +{ + int i; + object pr, r; + float mindist; + + i = 0; + mindist = 1000; + while (i<30) + { + pr = radar(i); + if (pr != null) + { + + if (F(orig, pr.position) < mindist and pr.category == AlienWasp and pr.altitude > 3) + { + mindist = distance(orig, pr.position); + r = pr; + } + } + i = i+1; + } + if (mindist < dist) return(r); else return(null); +} + + +class Guepe +{ + + point pos; + + + void cherche(point orig, float dist) + { + object p; + point o; + + p = radarGuepe(orig, dist); + while (p == null) + { + wait(0.1); + p = radarGuepe(orig, dist); + } + + pos.x = p.position.x; + pos.y = p.position.y; + pos.z = p.position.z; + + //o = p.position; + //wait(0.1); + + //vitessex = (p.position.x - o.x)/0.1; + //vitessey = (p.position.y - o.y)/0.1; + //vitessez = (p.position.z - o.z)/0.1; + + } + + + void tire(point orig, float orient) + { + //float t = 3; //temps d'anticipation + float angle; + point cible; + + cible.x = pos.x;// + t*vitessex; + cible.y = pos.y;// + t*vitessey; + cible.z = pos.z;// + t*vitessez; + + if (cible.x == 0) angle = 90; else + angle = atan(cible.y / cible.x); + if (cible.x < 0) angle = angle + 180; + angle = angle - orient; + if (angle > 180) angle = angle - 360; + if (angle < -180) angle = angle + 360; + turn(angle); + + angle = atan((cible.z-orig.z) / distance2d(orig, cible)); + aim(angle); + + fire(0.1); + + } +} + +extern void object::Fourmi6() +{ + //fps(1000); + Guepe guepe = new Guepe(); + + while (true) + { + guepe.cherche(position, 50); + + guepe.tire(position, orientation); + } +} diff --git a/src/CBot/tests/TestCBot/scenarios/array.txt b/src/CBot/tests/TestCBot/scenarios/array.txt new file mode 100644 index 0000000..081b60e --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/array.txt @@ -0,0 +1,24 @@ + +public extern void TestTableau () +{ + int tableau [ 12 ] ; + + point array[ 12 ] [ 14 ] ; + + point zro ( 1, 2 ) ; + point a = zro ; + + for ( int i = 0 ; i < 10 ; i++ ) array[ i ] [ i ]= zro ; + + array[ 5 ] [3 ] . x =1.5 ; + + array[ 2 ] [ 2 ] . y = array[ 5 ] [ 5 ] . x ; + + array[ 4 ] = array [ 2 ] ; + + for ( int i = 0 ; i < 10 ; i++ ) for ( int j = 0 ; j < 4 ; j++ ) println ( i, j, array [ i ] [ j ] ) ; + + show( zro, a, array ); + +} + diff --git "a/src/CBot/tests/TestCBot/scenarios/a\2361.txt" "b/src/CBot/tests/TestCBot/scenarios/a\2361.txt" new file mode 100644 index 0000000..165bc95 --- /dev/null +++ "b/src/CBot/tests/TestCBot/scenarios/a\2361.txt" @@ -0,0 +1,96 @@ +object radarGuepe(point orig, float dist) +{ + int i; + object pr, r; + float mindist; + + i = 0; + mindist = 1000; + while (i<30) + { + pr = radar(i); + if (pr != null) + { + + if (F(orig, pr.position) < mindist and pr.category == AlienWasp and pr.altitude > 3) + { + mindist = distance(orig, pr.position); + r = pr; + } + } + i = i+1; + } + if (mindist < dist) return(r); else return(null); +} + + +class Guepe +{ + + point pos; + + + void cherche(point orig, float dist) + { + object p; + point o; + + p = radarGuepe(orig, dist); + while (p == null) + { + wait(0.1); + p = radarGuepe(orig, dist); + } + + pos.x = p.position.x; + pos.y = p.position.y; + pos.z = p.position.z; + + //o = p.position; + //wait(0.1); + + //vitessex = (p.position.x - o.x)/0.1; + //vitessey = (p.position.y - o.y)/0.1; + //vitessez = (p.position.z - o.z)/0.1; + + } + + + void tire(point orig, float orient) + { + //float t = 3; //temps d'anticipation + float angle; + point cible; + + cible.x = pos.x;// + t*vitessex; + cible.y = pos.y;// + t*vitessey; + cible.z = pos.z;// + t*vitessez; + + if (cible.x == 0) angle = 90; else + angle = atan(cible.y / cible.x); + if (cible.x < 0) angle = angle + 180; + angle = angle - orient; + if (angle > 180) angle = angle - 360; + if (angle < -180) angle = angle + 360; + turn(angle); + + angle = atan((cible.z-orig.z) / distance2d(orig, cible)); + aim(angle); + + fire(0.1); + + } +} + +extern void object::Fourmi6() +{ + //fps(1000); + Guepe guepe = new Guepe(); + + while (true) + { + guepe.cherche(position, 50); + + guepe.tire(position, orientation); + } +} diff --git "a/src/CBot/tests/TestCBot/scenarios/a\2471.txt" "b/src/CBot/tests/TestCBot/scenarios/a\2471.txt" new file mode 100644 index 0000000..0c57950 --- /dev/null +++ "b/src/CBot/tests/TestCBot/scenarios/a\2471.txt" @@ -0,0 +1,96 @@ +object radarGuepe(point orig, float dist) +{ + int i; + object pr, r; + float mindist; + + i = 0; + mindist = 1000; + while (i<30) + { + pr = radar(i); + if (pr != null) + { + + if (F(orig, pr.position) < mindist and pr.category == AlienWasp and pr.altitude > 3) + { + mindist = distance(orig, pr.position); + r = pr; + } + } + i = i+1; + } + if (mindist < dist) return(r); else return(null); +} + + +class Guepe +{ + + point pos; + + + void cherche(point orig, float dist) + { + object p; + point o; + + p = radarGuepe(orig, dist); + while (p == null) + { + wait(0.1); + p = radarGuepe(orig, dist); + } + + pos.x = p.position.x; + pos.y = p.position.y; + pos.z = p.position.z; + + //o = p.position; + //wait(0.1); + + //vitessex = (p.position.x - o.x)/0.1; + //vitessey = (p.position.y - o.y)/0.1; + //vitessez = (p.position.z - o.z)/0.1; + + } + + + void tire(point orig, float orient) + { + //float t = 3; //temps d'anticipation + float angle; + point cible; + + cible.x = pos.x;// + t*vitessex; + cible.y = pos.y;// + t*vitessey; + cible.z = pos.z;// + t*vitessez; + + if (cible.x == 0) angle = 90; else + angle = atan(cible.y / cible.x); + if (cible.x < 0) angle = angle + 180; + angle = angle - orient; + if (angle > 180) angle = angle - 360; + if (angle < -180) angle = angle + 360; + turn(angle); + + angle = atan((cible.z-orig.z) / distance2d(orig, cible)); + aim(angle); + + fire(0.1); + + } +} + +extern void object::Fourmi6() +{ + //fps(1000); + Guepe guepe = new Guepe(); + + while (true) + { + guepe.cherche(position, 50); + + guepe.tire(position, orientation); + } +} diff --git a/src/CBot/tests/TestCBot/scenarios/bug.txt b/src/CBot/tests/TestCBot/scenarios/bug.txt new file mode 100644 index 0000000..4ec6eb3 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/bug.txt @@ -0,0 +1,12 @@ +public extern void object::Bug() +{ + point a; + a = position; + TEST(); + float d=dist(a, position); +} + +float dist(point a, point b) +{ + return a.x-b.x; +} diff --git a/src/CBot/tests/TestCBot/scenarios/bugmw.txt b/src/CBot/tests/TestCBot/scenarios/bugmw.txt new file mode 100644 index 0000000..284ee43 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/bugmw.txt @@ -0,0 +1,9 @@ +extern public void main() +{ + show(fact(30)) ; +} + +public int fact(int n) +{ + return (fact(n-1)*n) ; +} diff --git a/src/CBot/tests/TestCBot/scenarios/ccc.txt b/src/CBot/tests/TestCBot/scenarios/ccc.txt new file mode 100644 index 0000000..dbcd1d5 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/ccc.txt @@ -0,0 +1,8 @@ +public extern void ccc() +{ + int a; + a = 0 ; + + if ( a == 0 ); + +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/enum.txt b/src/CBot/tests/TestCBot/scenarios/enum.txt new file mode 100644 index 0000000..a592a7f --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/enum.txt @@ -0,0 +1,9 @@ + +enum JourDeLaSemaine { + lundi = 1, + mardi, + mercredi, + jeudi, + vendredi, + samedi, + dimanche = 0 } \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/fibo.txt b/src/CBot/tests/TestCBot/scenarios/fibo.txt new file mode 100644 index 0000000..88f5357 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/fibo.txt @@ -0,0 +1,25 @@ + +extern public int Fibo( int n, boolean b ) +{ + if ( n < 2 ) return n; + int a = Fibo(n-1, b) + Fibo(n-2, false); + if ( b ) print (n + "=" + a); + return a; +} + +extern public void t() +{ + Fibo( 23, true); +} + +extern public void tt() +{ + t(); +} + +// cette routine n'est videmment pas du tout obtimise +// c'est mme un trs mauvais exemple de programmation rcursive + +// pour un test de dure, Fibo(23, true) prend +// en mode Debug 67 secondes +// en mode Release 8 secondes diff --git a/src/CBot/tests/TestCBot/scenarios/file.txt b/src/CBot/tests/TestCBot/scenarios/file.txt new file mode 100644 index 0000000..2a22dd9 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/file.txt @@ -0,0 +1,70 @@ +class CLASS22 +{ + static int nb = 2; + void T22 ( ) { nb = nb / 0 ; } +} + +public extern void object :: TEST() +{ + switch ( 1 ) + { + case 1: + { + file h(); + h.open("Mon Fichier.txt", "r"); +show ( h.filename, h.handle ); +h.filename = "xx"; +h.handle = 1 ; + h.readln(); + h.close(); + } + case 2: + { + file h("Mon Fichier.txt"); + h.open("r"); + h.readln(); + h.close(); + } + case 3: + { + file h("Mon Fichier.txt", "r"); + h.readln(); + h.close(); + } + case 4: + { + file h(); + h.filename = "Mon Fichier.txt"; + h.open("r"); + h.readln(); + h.close(); + } + case 5: + { + file h = fileopen( "Mon 2Fichier.txt", "r" ); + h.readln(); + h.close(); + } + } +{ + file h( ) ; + h.filename = "Test.h"; + h.open ( "r" ); + + + file pf ( "Mon Fichier.txt" ) ; + pf . open ( "w" ) ; + pf . writeln ( "Voici encore du texte" ) ; + pf . writeln ( "et une seconde ligne" ) ; + pf . close( ); + + pf . open ( "r" ) ; + + while ( not pf . eof( ) ) + { + string s = pf . readln ( ); + show ( s ); + } + pf.close( ); +} +} diff --git a/src/CBot/tests/TestCBot/scenarios/h.txt b/src/CBot/tests/TestCBot/scenarios/h.txt new file mode 100644 index 0000000..c395319 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/h.txt @@ -0,0 +1,5 @@ +void tf() +{ + file h; + h.handle += 1 ; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/include.txt b/src/CBot/tests/TestCBot/scenarios/include.txt new file mode 100644 index 0000000..e8f8cc9 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/include.txt @@ -0,0 +1,27 @@ +class Z +{ + static int x = 0; + private int y; + + void T( ) + { + // autoris ici + y = x ; + this.y = this.x ; + x = y ; + this.x = this.y ; + } +} + +extern public void test() +{ + Z a(); + 3 * a.x; // autoris +//vu 3 * a.y; // interdit +//vu a.y = 3; // interdit ici + a.x = 1; // autoris + + show ( a ); + a.T(); + show ( a ); +} diff --git a/src/CBot/tests/TestCBot/scenarios/intrinsic.txt b/src/CBot/tests/TestCBot/scenarios/intrinsic.txt new file mode 100644 index 0000000..f215791 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/intrinsic.txt @@ -0,0 +1,16 @@ +public extern void TestIntrinsic() +{ + point a ( 1, 2 ); + print (a); + + a.x = 3; + a.y = 4; + + point b = a; + + println ( b.x, b.y, b ) ; + if ( b == a ) b.y = 0; + println (a,b); + if ( b != a ) b.y = a.y; + println(a,b); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/methode1.txt b/src/CBot/tests/TestCBot/scenarios/methode1.txt new file mode 100644 index 0000000..080bba2 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/methode1.txt @@ -0,0 +1,57 @@ +class t { + point p; +} + +void object :: toto() +{ + show ( Position ) ; +} + +extern public void object :: XX() +{ + int test []; + test [ 9999 ] = 3; + + toto () ; +/* + Radar(); + + object test ; + test = this. Radar(); + + do { + test = this.Radar(); + } while ( test == null ); + +/* + t test [ 4 ]; + for ( int i = 0 ; i < 4 ; i++ ) test [ i ] = new t(); + test [ 3 ] .p.x = 2; + show ( test ); +/* + int a = nan; + show ( a ) ; + + a = TypeMarkPath; + show ( a, a++, --a ) ; + + if ( a != nan ) a += 1 ; + + a = TypeMarkPath; + float q = a ; + show ( a, q ) ; + +return; + + a += ++a; + show ( a ) ; + + boolean i = false; + + if ( i == true ) {} + + object p; + if ( p == null) { p = p ; } +*/ +} + diff --git a/src/CBot/tests/TestCBot/scenarios/methode2.txt b/src/CBot/tests/TestCBot/scenarios/methode2.txt new file mode 100644 index 0000000..76ce7f4 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/methode2.txt @@ -0,0 +1,50 @@ + +extern void Toto() +{ + TEST(12); + + for ( int i = 0 ; i<1000; i++) + { + int j = 1; + if (i==55) TEST(12); + } + + TEST(2); + + +// Nouveau(); + int toto[4]; + point Z[3]; + + Z[1].x = 11; Z[1].y = 12; + + toto[2] = 12; + toto[1] = nan; + +// point test, autre(2,3) ; +// object titi = Radar(); + + TEST ( 1 ) ; + + toto[0] = 11; + + TEST ( 2 ) ; + + toto[6] = 0; +} + +extern void object::Nouveau() +{ + point a; + a = np(Position); +} + +point np(point b) +{ + point c; + c.x = b.y; + c.y = b.x; + return c ; +} + + diff --git a/src/CBot/tests/TestCBot/scenarios/mp1.txt b/src/CBot/tests/TestCBot/scenarios/mp1.txt new file mode 100644 index 0000000..599cfc4 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/mp1.txt @@ -0,0 +1,25 @@ +class Guepet +{ + + float a; + float b; + + void init() + { + a = 12.34; + b = 56.78; + } + + +} + +extern void object::Fourmi6() +{ + Guepet guepe =new Guepet(); + + guepe.init(); + + + show("test "+guepe.a+" "+guepe.b); + +} diff --git a/src/CBot/tests/TestCBot/scenarios/mp2.txt b/src/CBot/tests/TestCBot/scenarios/mp2.txt new file mode 100644 index 0000000..1c2972c --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/mp2.txt @@ -0,0 +1,28 @@ +class Guepet +{ + + float a; + float b; + + void init() + { + a = 12.34; + b = 56.78; + + object x = radar(123); + show("radar "+x.position.x); + show("C'est fait"); + } + + +} + +extern void object::Fourmi6() +{ + Guepet guepe=new Guepet(); + + guepe.init(); + + show("test "+guepe.a+" "+guepe.b); + +} diff --git a/src/CBot/tests/TestCBot/scenarios/mw.txt b/src/CBot/tests/TestCBot/scenarios/mw.txt new file mode 100644 index 0000000..c237670 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/mw.txt @@ -0,0 +1,16 @@ +extern public void main() +{ +// goto( 3, 4 ); + + while( true ) + { + try { goto (12) ; } + catch( FF( ) ) + { show( "ko"); } + } +} + +boolean FF() +{ + return false; +} diff --git a/src/CBot/tests/TestCBot/scenarios/null.txt b/src/CBot/tests/TestCBot/scenarios/null.txt new file mode 100644 index 0000000..ae76b74 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/null.txt @@ -0,0 +1,5 @@ +extern public void xxx () +{ + CPoint test = null ; + if ( test == null ) show ( "NULL" ); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/opnew.txt b/src/CBot/tests/TestCBot/scenarios/opnew.txt new file mode 100644 index 0000000..7d6838c --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/opnew.txt @@ -0,0 +1,20 @@ +extern public void xx () +{ + CPoint pointeur, test = null ; + pointeur = new CPoint ( 3, 4 ); + + if ( test == null ) show ( "NULL" ); + + CPoint pp = pointeur; + +show( pointeur , pp ); + + pp.x = 33.3; + if ( pointeur.x != pp.x ) 0/0; + + pp = new CPoint(); +// pointeur = pp; + +show( pointeur , pp ); + +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/plante.txt b/src/CBot/tests/TestCBot/scenarios/plante.txt new file mode 100644 index 0000000..363461b --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/plante.txt @@ -0,0 +1,25 @@ +class Guepet +{ + + point pos; + float t = 0.1; + + void init() + { + pos.x = 12.123; + pos.y = 34.345; + + F(t); + } + + +} + +extern void object::Fourmi6() +{ + Guepet guepe=new Guepet(); + + guepe.init(); + + show ( guepe ); +} diff --git a/src/CBot/tests/TestCBot/scenarios/pointer.txt b/src/CBot/tests/TestCBot/scenarios/pointer.txt new file mode 100644 index 0000000..2d4d907 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/pointer.txt @@ -0,0 +1,41 @@ +extern public void x () +{ + show ( 3 ** 4 ); + float z = 1e-3; + show ( z ); + + CPoint b ( 4,5 ); + show ( b ); + + CPoint a ( ) ; + a.x = 21; a.y = 12; + show ( a ) ; + + CPoint test = new CPoint ( 1,1 ); + test = new CPoint ( 2, 2 ); + show ( test ); +} + +// cre un objet et retourne son pointeur +CPoint newcpoint() +{ + CPoint p = new CPoint ( 3, 3 ); + return p; +} + +extern public void y () +{ + CPoint test = newcpoint(); + println ( test ); + dontmodif( test ); + println ( test ); +} + +// ne doit pas modifier l'objet en paramtre +void dontmodif ( CPoint pp ) +{ + pp.x = 5; + pp.y = 2; + println ( pp, pp.x, pp.y ); +} + diff --git a/src/CBot/tests/TestCBot/scenarios/postinc.txt b/src/CBot/tests/TestCBot/scenarios/postinc.txt new file mode 100644 index 0000000..cdf6ab5 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/postinc.txt @@ -0,0 +1,7 @@ +extern public void X() +{ + point A [ ] ; + A[5] = new point (2,3); + int val = A[5].x++ + --A[5].y; + show ( A, val ); +} diff --git a/src/CBot/tests/TestCBot/scenarios/radar.txt b/src/CBot/tests/TestCBot/scenarios/radar.txt new file mode 100644 index 0000000..09d84a2 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/radar.txt @@ -0,0 +1,39 @@ +extern void object::Bug( ) +{ + try{ int a = 44 ; a = 12 / 0 ; } + catch(6000) { int b = 4 ; } + finally { int z = 1 ; } + +// tp ( A, B ); + +/* int a = 4, b = 2, c = nan; + float x, y = 3/2, z = nan; + boolean i, j = false, k = true; + + string s, ss = "xyz"; + + while ( false ) + { + object left, right; + + left = Radar(TypeMarkPath, -45, 120, 100); + right = Radar(TypeMarkPath, 45, 120, 100); + + if ( left == null && right == null ) + { + } + } + int t = fact ( 4 ) ;*/ +} + +void tp( point a , point b ) +{ + a.x += b.x; +} + + +int fact( int n ) +{ + if ( n < 2 ) return n; + return n * fact ( n - 1 ) ; +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/solution.txt b/src/CBot/tests/TestCBot/scenarios/solution.txt new file mode 100644 index 0000000..f78cf12 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/solution.txt @@ -0,0 +1,13 @@ +extern void object::Solution( ) +{ +show ( "Solution " + Position ); + Carr(15); + Carr(25); +} + +void object::Carr(float ct) +{ +show ( "Carr " + Position ); + Move(ct); + Turn(-90); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/test.txt b/src/CBot/tests/TestCBot/scenarios/test.txt new file mode 100644 index 0000000..a912415 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/test.txt @@ -0,0 +1,8 @@ +extern public void x() +{ + float a= 1, b = 2; + a = b * ( 2 + 2 ); +// print (a); + a += 4; +// print (a); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/test23.txt b/src/CBot/tests/TestCBot/scenarios/test23.txt new file mode 100644 index 0000000..d6e1ddd --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/test23.txt @@ -0,0 +1,10 @@ +extern public void object::TEST23() +{ + CLASS22 T; + T.T22( ) ; + + show( position ); + show( this.position ); + +// T22(); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/testmw.txt b/src/CBot/tests/TestCBot/scenarios/testmw.txt new file mode 100644 index 0000000..6570f6d --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/testmw.txt @@ -0,0 +1,14 @@ +extern public int testmw( int a) +{ + boolean b = true ; + + if (b) + return 1 ; + else + return a ; 0 * testmw(a-1) ; +} + +public int Fibo2 ( int n ) +{ + print ( " bof " ); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/this.txt b/src/CBot/tests/TestCBot/scenarios/this.txt new file mode 100644 index 0000000..b8a9e04 --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/this.txt @@ -0,0 +1,13 @@ +extern void object :: TEST22 ( ) +{ + show( position ); + show( this.position ); + + T(); +} + +public void object :: T22() +{ + show( position ); + show( this.position ); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/tt.txt b/src/CBot/tests/TestCBot/scenarios/tt.txt new file mode 100644 index 0000000..cd13c9d --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/tt.txt @@ -0,0 +1,12 @@ +extern public void T() { T1(); } + +public void T1() +{ + show( "T1" ); + T2(); +} + +public void T2() +{ + show( "T2" ); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/tt2.txt b/src/CBot/tests/TestCBot/scenarios/tt2.txt new file mode 100644 index 0000000..ad9dc1d --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/tt2.txt @@ -0,0 +1,5 @@ +extern public void TT() +{ + T1(); + T2(); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/scenarios/vide.txt b/src/CBot/tests/TestCBot/scenarios/vide.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/CBot/tests/TestCBot/scenarios/zz.txt b/src/CBot/tests/TestCBot/scenarios/zz.txt new file mode 100644 index 0000000..da764ac --- /dev/null +++ b/src/CBot/tests/TestCBot/scenarios/zz.txt @@ -0,0 +1,6 @@ +extern public void zz() +{ + MaClass TOTO (); + + show (TOTO); +} \ No newline at end of file diff --git a/src/CBot/tests/TestCBot/xTestCBot.clw b/src/CBot/tests/TestCBot/xTestCBot.clw new file mode 100644 index 0000000..5b84c16 --- /dev/null +++ b/src/CBot/tests/TestCBot/xTestCBot.clw @@ -0,0 +1,245 @@ +; CLW file contains information for the MFC ClassWizard + +[General Info] +Version=1 +LastClass=CBotConsoleDlg +LastTemplate=CDialog +NewFileInclude1=#include "stdafx.h" +NewFileInclude2=#include "TestCBot.h" +LastPage=0 + +ClassCount=7 +Class1=CTestCBotApp +Class2=CTestCBotDoc +Class3=CTestCBotView +Class4=CMainFrame + +ResourceCount=7 +Resource1=IDD_ABOUTBOX +Resource2=IDR_MAINFRAME +Resource3=IDR_TESTCBTYPE +Class5=CAboutDlg +Class6=CChildFrame +Resource4=IDD_ABOUTBOX (French (France)) +Resource5=IDR_TESTCBTYPE (French (France)) +Resource6=IDD_CONSOLE +Class7=CBotConsoleDlg +Resource7=IDR_MAINFRAME (French (France)) + +[CLS:CTestCBotApp] +Type=0 +HeaderFile=TestCBot.h +ImplementationFile=TestCBot.cpp +Filter=N + +[CLS:CTestCBotDoc] +Type=0 +HeaderFile=TestCBotDoc.h +ImplementationFile=TestCBotDoc.cpp +Filter=N +BaseClass=CDocument +VirtualFilter=DC +LastObject=IDC_EDIT2 + +[CLS:CTestCBotView] +Type=0 +HeaderFile=TestCBotView.h +ImplementationFile=TestCBotView.cpp +Filter=C +BaseClass=CView +VirtualFilter=VWC +LastObject=CTestCBotView + +[CLS:CMainFrame] +Type=0 +HeaderFile=MainFrm.h +ImplementationFile=MainFrm.cpp +Filter=T +BaseClass=CMDIFrameWnd +VirtualFilter=fWC +LastObject=CMainFrame + + +[CLS:CChildFrame] +Type=0 +HeaderFile=ChildFrm.h +ImplementationFile=ChildFrm.cpp +Filter=M + +[CLS:CAboutDlg] +Type=0 +HeaderFile=TestCBot.cpp +ImplementationFile=TestCBot.cpp +Filter=D + +[DLG:IDD_ABOUTBOX] +Type=1 +ControlCount=4 +Control1=IDC_STATIC,static,1342177283 +Control2=IDC_STATIC,static,1342308352 +Control3=IDC_STATIC,static,1342308352 +Control4=IDOK,button,1342373889 +Class=CAboutDlg + +[MNU:IDR_MAINFRAME] +Type=1 +Class=CMainFrame +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command4=ID_APP_EXIT +Command5=ID_VIEW_TOOLBAR +Command6=ID_VIEW_STATUS_BAR +Command7=ID_APP_ABOUT +CommandCount=7 +Command3=ID_FILE_MRU_FILE1 + +[TB:IDR_MAINFRAME] +Type=1 +Class=CMainFrame +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command4=ID_EDIT_CUT +Command5=ID_EDIT_COPY +Command6=ID_EDIT_PASTE +Command7=ID_FILE_PRINT +CommandCount=8 +Command8=ID_APP_ABOUT + +[MNU:IDR_TESTCBTYPE] +Type=1 +Class=CTestCBotView +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_CLOSE +Command4=ID_FILE_SAVE +Command5=ID_FILE_SAVE_AS +Command9=ID_EDIT_CUT +Command10=ID_EDIT_COPY +Command11=ID_EDIT_PASTE +Command12=ID_VIEW_TOOLBAR +Command13=ID_VIEW_STATUS_BAR +Command14=ID_WINDOW_NEW +CommandCount=18 +Command6=ID_FILE_MRU_FILE1 +Command7=ID_APP_EXIT +Command8=ID_EDIT_UNDO +Command15=ID_WINDOW_CASCADE +Command16=ID_WINDOW_TILE_HORZ +Command17=ID_WINDOW_ARRANGE +Command18=ID_APP_ABOUT + +[ACL:IDR_MAINFRAME] +Type=1 +Class=CMainFrame +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command5=ID_EDIT_CUT +Command6=ID_EDIT_COPY +Command7=ID_EDIT_PASTE +Command8=ID_EDIT_UNDO +Command9=ID_EDIT_CUT +Command10=ID_EDIT_COPY +Command11=ID_EDIT_PASTE +Command12=ID_NEXT_PANE +CommandCount=13 +Command4=ID_EDIT_UNDO +Command13=ID_PREV_PANE + + +[TB:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command4=ID_EDIT_CUT +Command5=ID_EDIT_COPY +Command6=ID_EDIT_PASTE +Command7=ID_FILE_PRINT +Command8=ID_RUN +Command9=ID_APP_ABOUT +CommandCount=9 + +[MNU:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_MRU_FILE1 +Command4=ID_APP_EXIT +Command5=ID_VIEW_TOOLBAR +Command6=ID_VIEW_STATUS_BAR +Command7=ID_APP_ABOUT +CommandCount=7 + +[MNU:IDR_TESTCBTYPE (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_CLOSE +Command4=ID_FILE_SAVE +Command5=ID_FILE_SAVE_AS +Command6=ID_FILE_MRU_FILE1 +Command7=ID_APP_EXIT +Command8=ID_EDIT_UNDO +Command9=ID_EDIT_CUT +Command10=ID_EDIT_COPY +Command11=ID_EDIT_PASTE +Command12=ID_VIEW_TOOLBAR +Command13=ID_VIEW_STATUS_BAR +Command14=ID_WINDOW_NEW +Command15=ID_WINDOW_CASCADE +Command16=ID_WINDOW_TILE_HORZ +Command17=ID_WINDOW_ARRANGE +Command18=ID_APP_ABOUT +CommandCount=18 + +[ACL:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_EDIT_COPY +Command2=ID_FILE_NEW +Command3=ID_FILE_OPEN +Command4=ID_FILE_SAVE +Command5=ID_EDIT_PASTE +Command6=ID_EDIT_UNDO +Command7=ID_EDIT_CUT +Command8=ID_RUN +Command9=ID_NEXT_PANE +Command10=ID_PREV_PANE +Command11=ID_RUN +Command12=ID_EDIT_COPY +Command13=ID_EDIT_PASTE +Command14=ID_EDIT_CUT +Command15=ID_EDIT_UNDO +CommandCount=15 + +[DLG:IDD_ABOUTBOX (French (France))] +Type=1 +Class=CAboutDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342177283 +Control2=IDC_STATIC,static,1342308480 +Control3=IDC_STATIC,static,1342308352 +Control4=IDOK,button,1342373889 + +[DLG:IDD_CONSOLE] +Type=1 +Class=CBotConsoleDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_EDIT2,edit,1350631552 +Control3=IDOK,button,1342242817 +Control4=IDC_EDIT1,edit,1352734724 + +[CLS:CBotConsoleDlg] +Type=0 +HeaderFile=CBotConsoleDlg.h +ImplementationFile=CBotConsoleDlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC + diff --git a/src/CBot/tests/old TstCBot/BotConsoleDlg.cpp b/src/CBot/tests/old TstCBot/BotConsoleDlg.cpp new file mode 100644 index 0000000..077f080 --- /dev/null +++ b/src/CBot/tests/old TstCBot/BotConsoleDlg.cpp @@ -0,0 +1,164 @@ +// BotConsoleDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "TstCBot.h" +#include "BotConsoleDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CBotConsoleDlg dialog + + +CBotConsoleDlg::CBotConsoleDlg(CWnd* pParent /*=NULL*/) + : CDialog(CBotConsoleDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(CBotConsoleDlg) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + m_pProg = NULL; + m_threadinfo.m_bRun = FALSE; +} + + +void CBotConsoleDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(CBotConsoleDlg) + DDX_Control(pDX, IDOK, m_cOK); + DDX_Control(pDX, IDC_EDIT2, m_Edit2); + DDX_Control(pDX, IDC_EDIT1, m_Edit1); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(CBotConsoleDlg, CDialog) + //{{AFX_MSG_MAP(CBotConsoleDlg) + ON_MESSAGE(WM_ENDPROG, EndProg) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CBotConsoleDlg message handlers + +UINT ThreadProc(ThreadInfo *info) +{ + CTime t0 = CTime::GetCurrentTime(); + int Cpt = 0; + + info->m_pProg->Start("LaCommande"); + while ( !info->m_bStop && !info->m_pProg->Run() ) + { + Cpt++; + if ( Cpt%20 == 0 ) info->m_pEdit1->ReplaceSel("."); + } + + if ( info->m_bStop ) + { + info->m_pEdit1->ReplaceSel("\r\nInterrompu\r\n"); + } + else if (info->m_pProg->GivError() == 0) + { + CTime t = CTime::GetCurrentTime(); + CTimeSpan ts = t - t0; + + char buffer[200]; + sprintf( buffer, "\r\nExcution termine en %d secondes.\r\nInterrompue %d fois.\r\n", + ts.GetTotalSeconds(), Cpt); + + info->m_pEdit1->ReplaceSel(buffer); + } + + info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ; + return 0 ; +} + +LONG CBotConsoleDlg::EndProg(UINT wparam, LONG lparam) +{ + m_threadinfo.m_bRun = FALSE; + + if (m_pProg->GetError(m_code, m_start, m_end)) + { + AfxMessageBox(m_code); + CDialog::OnCancel(); + return 1; + } + delete m_pProg; + m_pProg = NULL; + + m_Edit2.EnableWindow(TRUE); + m_cOK.EnableWindow(TRUE); + + m_Edit2.SetWindowText(""); + m_Edit2.SetFocus(); + return 0 ; +} + +void CBotConsoleDlg::OnOK() +{ + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + pApp->m_pConsole = &m_Edit1; + + CString Commande; + m_Edit2.GetWindowText(Commande); + + CString s = "void LaCommande() { " + Commande + " ;}"; + m_pProg = new CBotProgram(); + CBotStringArray liste; + m_pProg->Compile(s, liste); + int err, start, end; + if ( m_pProg->GetError(err, start, end) ) + { + AfxMessageBox(err); + m_Edit2.SetSel(start-20, end-20); + return; + } + + m_Edit1.ReplaceSel(Commande + " ->\r\n"); + + m_Edit2.SetWindowText(""); + m_Edit1.SetFocus(); + m_Edit2.EnableWindow(FALSE); + m_cOK.EnableWindow(FALSE); + + // lance un processus paralle pour l'excution + m_threadinfo.m_pWndMessage = this ; + + m_threadinfo.m_pEdit1 = &m_Edit1; + m_threadinfo.m_pProg = m_pProg; + m_threadinfo.m_bStop = FALSE; + m_threadinfo.m_bRun = TRUE; + + AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ; +} + +void CBotConsoleDlg::OnCancel() +{ + if (!m_threadinfo.m_bRun) CDialog::OnCancel(); + m_threadinfo.m_bStop = TRUE ; +} + + +BOOL CBotConsoleDlg::OnInitDialog() +{ + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + + CDialog::OnInitDialog(); + + m_Edit1.ReplaceSel("Les fonctions suivantes sont disponibles:\r\n"); + for ( int i = 0; i < pApp->m_Liste.RetSize(); i++ ) + { + CBotString x = CString(pApp->m_Liste[i]) + "\r\n"; + m_Edit1.ReplaceSel(x); + } + m_Edit1.ReplaceSel("Entrez une commande ci-dessous.\r\n\r\n"); + + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/CBot/tests/old TstCBot/BotConsoleDlg.h b/src/CBot/tests/old TstCBot/BotConsoleDlg.h new file mode 100644 index 0000000..9b54ff2 --- /dev/null +++ b/src/CBot/tests/old TstCBot/BotConsoleDlg.h @@ -0,0 +1,65 @@ +#if !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) +#define AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 +// BotConsoleDlg.h : header file +// + +struct ThreadInfo +{ + CEdit* m_pEdit1 ; + CBotProgram* m_pProg; + CWnd* m_pWndMessage; + BOOL m_bStop; + BOOL m_bRun; +}; + + +///////////////////////////////////////////////////////////////////////////// +// CBotConsoleDlg dialog + +class CBotConsoleDlg : public CDialog +{ +// Construction +public: + CBotConsoleDlg(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(CBotConsoleDlg) + enum { IDD = IDD_CONSOLE }; + CButton m_cOK; + CEdit m_Edit2; + CEdit m_Edit1; + //}}AFX_DATA + + CBotProgram* m_pProg; + ThreadInfo m_threadinfo; + + int m_code, m_start, m_end; + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CBotConsoleDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(CBotConsoleDlg) + virtual void OnOK(); + virtual void OnCancel(); + virtual BOOL OnInitDialog(); + afx_msg LONG EndProg(UINT wparam, LONG lparam) ; + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BOTCONSOLEDLG_H__A11450A2_8E09_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/old TstCBot/BotErrorDlg.cpp b/src/CBot/tests/old TstCBot/BotErrorDlg.cpp new file mode 100644 index 0000000..87d56f0 --- /dev/null +++ b/src/CBot/tests/old TstCBot/BotErrorDlg.cpp @@ -0,0 +1,56 @@ +// BotErrorDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "TstCBot.h" +#include "BotErrorDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CBotErrorDlg dialog + + +CBotErrorDlg::CBotErrorDlg(CWnd* pParent /*=NULL*/) + : CDialog(CBotErrorDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(CBotErrorDlg) + m_TextProgram = _T(""); + //}}AFX_DATA_INIT +} + + +void CBotErrorDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(CBotErrorDlg) + DDX_Control(pDX, IDC_EDIT1, m_eProgram); + DDX_Control(pDX, IDC_STATIC1, m_sMessage); + DDX_Text(pDX, IDC_EDIT1, m_TextProgram); + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(CBotErrorDlg, CDialog) + //{{AFX_MSG_MAP(CBotErrorDlg) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CBotErrorDlg message handlers + +BOOL CBotErrorDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_sMessage.SetWindowText(m_TextError); + m_eProgram.SetFocus(); + m_eProgram.SetSel(m_start, m_end); + + return FALSE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/src/CBot/tests/old TstCBot/BotErrorDlg.h b/src/CBot/tests/old TstCBot/BotErrorDlg.h new file mode 100644 index 0000000..522afad --- /dev/null +++ b/src/CBot/tests/old TstCBot/BotErrorDlg.h @@ -0,0 +1,51 @@ +#if !defined(AFX_BOTERRORDLG_H__80E73D20_7454_11D4_A439_00D059085115__INCLUDED_) +#define AFX_BOTERRORDLG_H__80E73D20_7454_11D4_A439_00D059085115__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 +// BotErrorDlg.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// CBotErrorDlg dialog + +class CBotErrorDlg : public CDialog +{ +// Construction +public: + CBotErrorDlg(CWnd* pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(CBotErrorDlg) + enum { IDD = IDD_DIALOG1 }; + CEdit m_eProgram; + CStatic m_sMessage; + CString m_TextProgram; + //}}AFX_DATA + + + CString m_TextError; + int m_start, m_end; + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CBotErrorDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + + // Generated message map functions + //{{AFX_MSG(CBotErrorDlg) + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BOTERRORDLG_H__80E73D20_7454_11D4_A439_00D059085115__INCLUDED_) diff --git a/src/CBot/tests/old TstCBot/CBotTest.txt b/src/CBot/tests/old TstCBot/CBotTest.txt new file mode 100644 index 0000000..ce20e26 --- /dev/null +++ b/src/CBot/tests/old TstCBot/CBotTest.txt @@ -0,0 +1,36 @@ + +extern void TheTest() +{ + for (int x = 130; x>0; x--) print (x); +} + +extern void Test() +{ + int var = 10000 ; + while (var > 0) var = var -1; +} +// excut en 30 secondes + +extern void Autre() +{ + int var = 10000 ; + while (var > 0) if ( var > 0 ) var = var -1; +} +// excut en 45 secondes + +int Y ( int n ) +{ + if ( n < 2 ) return n; + int a = Y(n-1) + Y(n-2); + return a; +} + +extern int X ( int n ) +{ + if ( n < 2 ) { print(n); return n; } + int a = X(n-1) + Y(n-2); + print (a); + return a; +} + + diff --git a/src/CBot/tests/old TstCBot/CMyThread.cpp b/src/CBot/tests/old TstCBot/CMyThread.cpp new file mode 100644 index 0000000..ca92c77 --- /dev/null +++ b/src/CBot/tests/old TstCBot/CMyThread.cpp @@ -0,0 +1,107 @@ +// CMyThread.cpp : pour crer un processus pour la console +// + +#include "stdafx.h" +#include "TstCBot.h" +#include "CMyThread.h" +#include "BotConsoleDlg.h" + + +//IMPLEMENT_DYNAMIC (CMyThread, CWinThread) +IMPLEMENT_DYNCREATE (CMyThread, CWinThread) + +///////////////////////////////////////////////////////////////////////////// +// CMyThread + +BEGIN_MESSAGE_MAP(CMyThread, CWinThread) + //{{AFX_MSG_MAP(CMyThread) + //}}AFX_MSG_MAP + // Standard file based document commands +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CMyThread construction + +CMyThread::CMyThread() +{ + // TODO: add construction code here, + // Place all significant initialization in InitInstance +} + + +///////////////////////////////////////////////////////////////////////////// +// CMyThread initialization +/* +BOOL CMyThread::InitInstance() +{ + AfxEnableControlContainer(); + + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + + // ouvre une fentre pour afficher les sorties + CRect rect; + AfxGetMainWnd()->GetClientRect( rect ); + rect += CPoint(30,30); + + CWnd* pWnd = new CWnd(); + pWnd->CreateEx( 0, + AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)), + "CBot console", WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE, + rect, + AfxGetMainWnd()->GetParent(), NULL, NULL); + m_pMainWnd = pWnd; + + pApp->m_pEdit2 = new CEdit(); + + m_pMainWnd->GetClientRect( rect ); + rect.bottom -= 40; + pApp->m_pEdit2->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN| + ES_AUTOVSCROLL|ES_READONLY, + rect, m_pMainWnd, IDC_EDIT2 ); + + pApp->m_pEdit2->ReplaceSel("Les fonctions suivantes sont disponibles:\n\r"); + for ( int i = 0; i < pApp->m_Liste.RetSize(); i++ ) + { + pApp->m_pEdit2->ReplaceSel(pApp->m_Liste[i] + "\r\n"); + } + pApp->m_pEdit2->ReplaceSel("Entrez une commande ci-dessous.\r\r"); + + +// pApp->m_pEdit2->SetFocus(); + + pApp->m_pEdit3 = new CEdit(); + m_pMainWnd->GetClientRect( rect ); + rect.top = rect.bottom-40; + pApp->m_pEdit3->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP, + rect, m_pMainWnd, IDC_EDIT1 ); + pApp->m_pEdit3->SetFocus(); + + return TRUE; +}*/ + +BOOL CMyThread::InitInstance() +{ + CBotConsoleDlg dlg; + m_pMainWnd = &dlg; // cela ferme l'application avec la DBOX ! + + int nResponse = dlg.DoModal(); + + return TRUE; +} + + +int CMyThread::ExitInstance() +{ + return 0; +} + + +///////////////////////////////////////////////////////////////////////////// +// CMyThread message handlers + + +void CMyThread::OnReturn() +{ + // TODO: Add your command handler code here + __asm int 3; +} diff --git a/src/CBot/tests/old TstCBot/CMyThread.h b/src/CBot/tests/old TstCBot/CMyThread.h new file mode 100644 index 0000000..1134077 --- /dev/null +++ b/src/CBot/tests/old TstCBot/CMyThread.h @@ -0,0 +1,44 @@ +// CMyThread.h : pour crer un processus pour la console +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_MAINFRM_H__20B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) +#define AFX_MAINFRM_H__20B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ + + +#include "stdafx.h" +#include "TstCBot.h" + +class CMyThread : public CWinThread +{ +// DECLARE_DYNAMIC(CMyThread) + DECLARE_DYNCREATE(CMyThread) + +public: + + +// Constructor + CMyThread(); + virtual BOOL InitInstance(); + virtual int ExitInstance(); // return app exit code + +// Implementation + + //{{AFX_MSG(CTstCBotApp) + afx_msg void OnAppAbout(); + // NOTE - the ClassWizard will add and remove member functions here. + // DO NOT EDIT what you see in these blocks of generated code ! + //}}AFX_MSG + +// Generated message map functions +protected: + //{{AFX_MSG(CMainFrame) + afx_msg void OnReturn(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAINFRM_H__20B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/tests/old TstCBot/MainFrm.cpp b/src/CBot/tests/old TstCBot/MainFrm.cpp new file mode 100644 index 0000000..6c0962c --- /dev/null +++ b/src/CBot/tests/old TstCBot/MainFrm.cpp @@ -0,0 +1,91 @@ +// MainFrm.cpp : implementation of the CMainFrame class +// + +#include "stdafx.h" +#include "TstCBot.h" + +#include "MainFrm.h" +#include "BotErrorDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame + +IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) + +BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) + //{{AFX_MSG_MAP(CMainFrame) + ON_WM_CREATE() + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +static UINT indicators[] = +{ + ID_SEPARATOR, // status line indicator + ID_INDICATOR_CAPS, + ID_INDICATOR_NUM, + ID_INDICATOR_SCRL, +}; + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame construction/destruction + +CMainFrame::CMainFrame() +{ + // TODO: add member initialization code here + +} + +CMainFrame::~CMainFrame() +{ +} + +int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) +{ + if (CFrameWnd::OnCreate(lpCreateStruct) == -1) + return -1; + + if (!m_wndStatusBar.Create(this) || + !m_wndStatusBar.SetIndicators(indicators, + sizeof(indicators)/sizeof(UINT))) + { + TRACE0("Failed to create status bar\n"); + return -1; // fail to create + } + + return 0; +} + +BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) +{ + // TODO: Modify the Window class or styles here by modifying + // the CREATESTRUCT cs + + return CFrameWnd::PreCreateWindow(cs); +} + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame diagnostics + +#ifdef _DEBUG +void CMainFrame::AssertValid() const +{ + CFrameWnd::AssertValid(); +} + +void CMainFrame::Dump(CDumpContext& dc) const +{ + CFrameWnd::Dump(dc); +} + +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CMainFrame message handlers + + diff --git a/src/CBot/tests/old TstCBot/MainFrm.h b/src/CBot/tests/old TstCBot/MainFrm.h new file mode 100644 index 0000000..56b9c41 --- /dev/null +++ b/src/CBot/tests/old TstCBot/MainFrm.h @@ -0,0 +1,55 @@ +// MainFrm.h : interface of the CMainFrame class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_MAINFRM_H__70B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) +#define AFX_MAINFRM_H__70B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +class CMainFrame : public CFrameWnd +{ +protected: // create from serialization only + CMainFrame(); + DECLARE_DYNCREATE(CMainFrame) + +// Attributes +public: + +// Operations +public: + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CMainFrame) + virtual BOOL PreCreateWindow(CREATESTRUCT& cs); + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CMainFrame(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +protected: // control bar embedded members + CStatusBar m_wndStatusBar; + +// Generated message map functions +protected: + //{{AFX_MSG(CMainFrame) + afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); + afx_msg void OnCp1(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAINFRM_H__70B3756C_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/tests/old TstCBot/ReadMe.txt b/src/CBot/tests/old TstCBot/ReadMe.txt new file mode 100644 index 0000000..67dc05b --- /dev/null +++ b/src/CBot/tests/old TstCBot/ReadMe.txt @@ -0,0 +1,93 @@ +======================================================================== + MICROSOFT FOUNDATION CLASS LIBRARY : TstCBot +======================================================================== + + +AppWizard has created this TstCBot application for you. This application +not only demonstrates the basics of using the Microsoft Foundation classes +but is also a starting point for writing your application. + +This file contains a summary of what you will find in each of the files that +make up your TstCBot application. + +TstCBot.h + This is the main header file for the application. It includes other + project specific headers (including Resource.h) and declares the + CTstCBotApp application class. + +TstCBot.cpp + This is the main application source file that contains the application + class CTstCBotApp. + +TstCBot.rc + This is a listing of all of the Microsoft Windows resources that the + program uses. It includes the icons, bitmaps, and cursors that are stored + in the RES subdirectory. This file can be directly edited in Microsoft + Developer Studio. + +res\TstCBot.ico + This is an icon file, which is used as the application's icon. This + icon is included by the main resource file TstCBot.rc. + +res\TstCBot.rc2 + This file contains resources that are not edited by Microsoft + Developer Studio. You should place all resources not + editable by the resource editor in this file. + +TstCBot.clw + This file contains information used by ClassWizard to edit existing + classes or add new classes. ClassWizard also uses this file to store + information needed to create and edit message maps and dialog data + maps and to create prototype member functions. + +///////////////////////////////////////////////////////////////////////////// + +For the main frame window: + +MainFrm.h, MainFrm.cpp + These files contain the frame class CMainFrame, which is derived from + CFrameWnd and controls all SDI frame features. + + +///////////////////////////////////////////////////////////////////////////// + +AppWizard creates one document type and one view: + +TstCBotDoc.h, TstCBotDoc.cpp - the document + These files contain your CTstCBotDoc class. Edit these files to + add your special document data and to implement file saving and loading + (via CTstCBotDoc::Serialize). + +TstCBotView.h, TstCBotView.cpp - the view of the document + These files contain your CTstCBotView class. + CTstCBotView objects are used to view CTstCBotDoc objects. + + + +///////////////////////////////////////////////////////////////////////////// +Other standard files: + +StdAfx.h, StdAfx.cpp + These files are used to build a precompiled header (PCH) file + named TstCBot.pch and a precompiled types file named StdAfx.obj. + +Resource.h + This is the standard header file, which defines new resource IDs. + Microsoft Developer Studio reads and updates this file. + +///////////////////////////////////////////////////////////////////////////// +Other notes: + +AppWizard uses "TODO:" to indicate parts of the source code you +should add to or customize. + +If your application uses MFC in a shared DLL, and your application is +in a language other than the operating system's current language, you +will need to copy the corresponding localized resources MFC40XXX.DLL +from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, +and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. +For example, MFC40DEU.DLL contains resources translated to German.) If you +don't do this, some of the UI elements of your application will remain in the +language of the operating system. + +///////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/tests/old TstCBot/Resource.h b/src/CBot/tests/old TstCBot/Resource.h new file mode 100644 index 0000000..6863fd8 --- /dev/null +++ b/src/CBot/tests/old TstCBot/Resource.h @@ -0,0 +1,68 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Developer Studio generated include file. +// Used by TstCBot.rc +// +#define IDD_ABOUTBOX 100 +#define IDR_MAINFRAME 128 +#define IDR_TSTCBOTYPE 129 +#define IDD_DIALOG1 130 +#define IDD_CONSOLE 131 +#define IDC_EDIT1 1000 +#define TX_TYPENAMES 1000 +#define IDC_STATIC1 1001 +#define IDC_EDIT2 1002 +#define TX_OPENPAR 5000 +#define TX_CLOSEPAR 5001 +#define TX_NOTBOOL 5002 +#define TX_UNDEFVAR 5003 +#define TX_BADLEFT 5004 +#define TX_ENDOF 5005 +#define TX_OUTCASE 5006 +#define TX_NOTERM 5007 +#define TX_CLOSEBLK 5008 +#define TX_ELSEWITHOUTIF 5009 +#define TX_OPENBLK 5010 +#define TX_BADTYPE 5011 +#define TX_REDEFVAR 5012 +#define TX_BAD2TYPE 5013 +#define TX_UNDEFCALL 5014 +#define TX_MISDOTS 5015 +#define TX_WHILE 5016 +#define TX_BREAK 5017 +#define TX_LABEL 5018 +#define TX_NOLABEL 5019 +#define TX_NOCASE 5020 +#define TX_BADNUM 5021 +#define TX_VOID 5022 +#define TX_NOTYP 5023 +#define TX_NOVAR 5024 +#define TX_NOFONC 5025 +#define TX_OVERPARAM 5026 +#define TX_REDEF 5027 +#define TX_LOWPARAM 5028 +#define TX_BADPARAM 5029 +#define TX_NUMPARAM 5030 +#define TX_NOITEM 5031 +#define TX_DOT 5032 +#define TX_NOCONST 5033 +#define TX_REDEFCLASS 5034 +#define TX_DIVZERO 6000 +#define TX_NOTINIT 6001 +#define TX_BADTHROW 6002 +#define TX_NORETVAL 6003 +#define TX_NORUN 6004 +#define TX_NOCALL 6005 +#define ID_CP1 32771 +#define ID_EXE 32772 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_3D_CONTROLS 1 +#define _APS_NEXT_RESOURCE_VALUE 132 +#define _APS_NEXT_COMMAND_VALUE 32775 +#define _APS_NEXT_CONTROL_VALUE 1002 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/src/CBot/tests/old TstCBot/StdAfx.cpp b/src/CBot/tests/old TstCBot/StdAfx.cpp new file mode 100644 index 0000000..ae0ec93 --- /dev/null +++ b/src/CBot/tests/old TstCBot/StdAfx.cpp @@ -0,0 +1,6 @@ +// stdafx.cpp : source file that includes just the standard includes +// TstCBot.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + diff --git a/src/CBot/tests/old TstCBot/StdAfx.h b/src/CBot/tests/old TstCBot/StdAfx.h new file mode 100644 index 0000000..7d46ace --- /dev/null +++ b/src/CBot/tests/old TstCBot/StdAfx.h @@ -0,0 +1,26 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#if !defined(AFX_STDAFX_H__70B3756A_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) +#define AFX_STDAFX_H__70B3756A_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers + +#include // MFC core and standard components +#include // MFC extensions +#include // MFC OLE automation classes +#ifndef _AFX_NO_AFXCMN_SUPPORT +#include // MFC support for Windows Common Controls +#endif // _AFX_NO_AFXCMN_SUPPORT + + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_STDAFX_H__70B3756A_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/tests/old TstCBot/TstCBot.clw b/src/CBot/tests/old TstCBot/TstCBot.clw new file mode 100644 index 0000000..4c54168 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBot.clw @@ -0,0 +1,189 @@ +; CLW file contains information for the MFC ClassWizard + +[General Info] +Version=1 +LastClass=CTstCBotView +LastTemplate=CDialog +NewFileInclude1=#include "stdafx.h" +NewFileInclude2=#include "TstCBot.h" +LastPage=0 + +ClassCount=7 +Class1=CTstCBotApp +Class2=CTstCBotDoc +Class3=CTstCBotView +Class4=CMainFrame + +ResourceCount=6 +Resource1=IDD_ABOUTBOX +Resource2=IDR_MAINFRAME +Class5=CAboutDlg +Resource3=IDD_ABOUTBOX (French (France)) +Resource4=IDD_CONSOLE +Class6=CBotErrorDlg +Resource5=IDD_DIALOG1 (French (Switzerland)) +Class7=CBotConsoleDlg +Resource6=IDR_MAINFRAME (French (France)) + +[CLS:CTstCBotApp] +Type=0 +HeaderFile=TstCBot.h +ImplementationFile=TstCBot.cpp +Filter=N + +[CLS:CTstCBotDoc] +Type=0 +HeaderFile=TstCBotDoc.h +ImplementationFile=TstCBotDoc.cpp +Filter=N +BaseClass=CDocument +VirtualFilter=DC +LastObject=CTstCBotDoc + +[CLS:CTstCBotView] +Type=0 +HeaderFile=TstCBotView.h +ImplementationFile=TstCBotView.cpp +Filter=C +BaseClass=CView +VirtualFilter=VWC +LastObject=CTstCBotView + +[CLS:CMainFrame] +Type=0 +HeaderFile=MainFrm.h +ImplementationFile=MainFrm.cpp +Filter=T +BaseClass=CFrameWnd +VirtualFilter=fWC +LastObject=CMainFrame + + + +[CLS:CAboutDlg] +Type=0 +HeaderFile=TstCBot.cpp +ImplementationFile=TstCBot.cpp +Filter=D + +[DLG:IDD_ABOUTBOX] +Type=1 +Class=CAboutDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342177283 +Control2=IDC_STATIC,static,1342308480 +Control3=IDC_STATIC,static,1342308352 +Control4=IDOK,button,1342373889 + +[MNU:IDR_MAINFRAME] +Type=1 +Class=CMainFrame +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command4=ID_FILE_SAVE_AS +Command5=ID_FILE_MRU_FILE1 +Command6=ID_APP_EXIT +Command7=ID_EDIT_UNDO +Command8=ID_EDIT_CUT +Command9=ID_EDIT_COPY +Command10=ID_EDIT_PASTE +Command11=ID_VIEW_STATUS_BAR +Command12=ID_CP1 +Command13=ID_EXE +Command14=ID_APP_ABOUT +CommandCount=14 + +[ACL:IDR_MAINFRAME] +Type=1 +Class=CMainFrame +Command1=ID_CP1 +Command2=ID_FILE_NEW +Command3=ID_FILE_OPEN +Command4=ID_FILE_SAVE +Command5=ID_EXE +Command6=ID_EDIT_UNDO +Command7=ID_EDIT_CUT +Command8=ID_EXE +Command9=ID_CP1 +Command10=ID_EXE +CommandCount=10 + +[MNU:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_FILE_NEW +Command2=ID_FILE_OPEN +Command3=ID_FILE_SAVE +Command4=ID_FILE_SAVE_AS +Command5=ID_FILE_MRU_FILE1 +Command6=ID_APP_EXIT +Command7=ID_EDIT_UNDO +Command8=ID_EDIT_CUT +Command9=ID_EDIT_COPY +Command10=ID_EDIT_PASTE +Command11=ID_VIEW_STATUS_BAR +Command12=ID_CP1 +Command13=ID_EXE +Command14=ID_APP_ABOUT +CommandCount=14 + +[ACL:IDR_MAINFRAME (French (France))] +Type=1 +Class=? +Command1=ID_CP1 +Command2=ID_FILE_NEW +Command3=ID_FILE_OPEN +Command4=ID_FILE_SAVE +Command5=ID_EXE +Command6=ID_EDIT_UNDO +Command7=ID_EDIT_CUT +Command8=ID_EXE +Command9=ID_CP1 +Command10=ID_EXE +CommandCount=10 + +[DLG:IDD_ABOUTBOX (French (France))] +Type=1 +Class=CAboutDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342177283 +Control2=IDC_STATIC,static,1342308480 +Control3=IDC_STATIC,static,1342308352 +Control4=IDOK,button,1342373889 + +[CLS:CBotErrorDlg] +Type=0 +HeaderFile=BotErrorDlg.h +ImplementationFile=BotErrorDlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=CBotErrorDlg + +[DLG:IDD_DIALOG1 (French (Switzerland))] +Type=1 +ControlCount=4 +Control1=IDOK,button,1342242817 +Control2=IDC_EDIT1,edit,1352728708 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_STATIC1,static,1342308352 + +[DLG:IDD_CONSOLE] +Type=1 +Class=CBotConsoleDlg +ControlCount=4 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_EDIT2,edit,1350631552 +Control3=IDOK,button,1342242817 +Control4=IDC_EDIT1,edit,1352734724 + +[CLS:CBotConsoleDlg] +Type=0 +HeaderFile=BotConsoleDlg.h +ImplementationFile=BotConsoleDlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=IDOK + diff --git a/src/CBot/tests/old TstCBot/TstCBot.cpp b/src/CBot/tests/old TstCBot/TstCBot.cpp new file mode 100644 index 0000000..8ac4557 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBot.cpp @@ -0,0 +1,412 @@ +// TstCBot.cpp : Defines the class behaviors for the application. +// + +#include "stdafx.h" +#include "TstCBot.h" + +#include "MainFrm.h" +#include "TstCBotDoc.h" +#include "TstCBotView.h" +#include "CMyThread.h" + + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + + +//////////////////////////////////////////////////////////////////// +// routine show() +// utilisable depuis le programme crit en CBot + +// excution +BOOL rShow( CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + CString s; + + if ( pVar == NULL ) + { + Exception = 22; return FALSE; + } + + while ( pVar != NULL ) + { + CString ss; + ss.LoadString( TX_TYPENAMES + pVar->RetType() ); + s += ss + " "; + + ss = pVar->RetName(); + if (ss.IsEmpty()) ss = ""; + s += ss + " = "; + + s += pVar->RetValString(); + s += "\n"; + pVar = pVar->RetNext(); + } + + AfxMessageBox(s, MB_OK|MB_ICONINFORMATION); + +// if ( pResult && pResult->RetType() == CBotTypInt) pResult->SetValInt(123); + + return TRUE; // pas d'interruption +} + +int cShow( CBotVar* &pVar, CBotString& RetClass) +{ + if ( pVar == NULL ) return 22; + return CBotTypInt; // tous paramtres accepts, un entier en retour +} + +int cErr( CBotVar* &pVar, CBotString& RetClass) +{ + pVar = pVar->RetNext(); // avance le pointeur sur l'erreur + return 6666; +} + +//////////////////////////////////////////////////////////////////// +// routine print() +// utilisable depuis le programme crit en CBot + +// excution +BOOL rPrintLn( CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + CString s; + + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + CEdit* pEdit = pApp->m_pConsole; + + if (pEdit == NULL) return TRUE; + pEdit->GetWindowText(s); + + while ( pVar != NULL ) + { + if ( !s.IsEmpty() ) s += "\r\n"; + s += pVar->RetValString(); + pVar = pVar->RetNext(); + } + + pEdit->SetWindowText(s); + pEdit->SetSel(s.GetLength(), s.GetLength()); + pEdit->SetFocus(); + return TRUE; // pas d'interruption +} + +BOOL rPrint( CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + CString s; + + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + CEdit* pEdit = pApp->m_pConsole; + + if (pEdit == NULL) return TRUE; + pEdit->GetWindowText(s); + + while ( pVar != NULL ) + { + if ( !s.IsEmpty() ) s += " "; + s += pVar->RetValString(); + pVar = pVar->RetNext(); + } + + pEdit->SetWindowText(s); + pEdit->SetSel(s.GetLength(), s.GetLength()); + pEdit->SetFocus(); + return TRUE; // pas d'interruption +} + +int cPrint( CBotVar* &pVar, CBotString& RetClass) +{ + return 0; // tous paramtres accepts, un entier en retour +} + + +////////////////////////////////////////////////////////////////// +// class CPoint pour essayer + +// excution +BOOL rCPoint( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + CString s; + + if ( pVar == NULL )return TRUE; // constructeur sans paramtres est ok + + if ( pVar->RetType() > CBotTypDouble ) + { + Exception = 6023; return FALSE; + } + + CBotVar* pX = pThis->RetItem("x"); + if ( pX == NULL ) + { + Exception = 6024; return FALSE; + } + + pX->SetValFloat( pVar->RetValFloat() ); + pVar = pVar->RetNext(); + + if ( pVar == NULL ) + { + Exception = 6022; return FALSE; + } + + if ( pVar->RetType() > CBotTypDouble ) + { + Exception = 6023; return FALSE; + } + + CBotVar* pY = pThis->RetItem("y"); + if ( pY == NULL ) + { + Exception = 6024; return FALSE; + } + + pY->SetValFloat( pVar->RetValFloat() ); + pVar = pVar->RetNext(); + + if ( pVar != NULL ) + { + Exception = 6025; return FALSE; + } + + return TRUE; // pas d'interruption +} + +int cCPoint( CBotVar* pThis, CBotVar* &pVar, CBotString& RetClass) +{ + // l'objet doit tre de la classe CPoint + if ( !pThis->IsElemOfClass("CPoint") ) return 6021; + + // ok si aucun paramtres ! + if ( pVar == NULL ) return 0; + + // paramtre de type numrique svp + if ( pVar->RetType() > CBotTypDouble ) return 6023; + pVar = pVar->RetNext(); + + // il doit y avoir un second paramtre + if ( pVar == NULL ) return 6022; + // galement de type numrique + if ( pVar->RetType() > CBotTypDouble )return 6023; + pVar = pVar->RetNext(); + + // et pas plus de 2 paramtres svp + if ( pVar != NULL ) return 6025; + + return 0; // cette fonction retourne void +} + +// mthode dterminant l'oppos +BOOL rOppose( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + CString s; + + if ( pVar != NULL ) // pas de paramtre + { + Exception = 6025; return FALSE; + } + + CBotVar* pvar = pThis->RetItemList(); // demande la chane des items + + // tous les paramtres sont des nombres + while (pvar != NULL) + { + pvar->SetValFloat( -pvar->RetValFloat() ); + pvar = pvar->RetNext(); + } + + pResult->Copy(pThis); + return TRUE; // pas d'interruption +} + +int cOppose( CBotVar* pThis, CBotVar* &pVar, CBotString& RetClass) +{ + // l'objet doit tre de la classe CPoint + if ( !pThis->IsElemOfClass("CPoint") ) return 6021; + + RetClass = "CPoint"; // l'objet rendu est de cette class + + // ok si aucun paramtres ! + if ( pVar == NULL ) return CBotTypClass; // le paramtre retourn est une instance de la classe + + return TX_OVERPARAM; // a va pas +} + + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotApp + +BEGIN_MESSAGE_MAP(CTstCBotApp, CWinApp) + //{{AFX_MSG_MAP(CTstCBotApp) + ON_COMMAND(ID_APP_ABOUT, OnAppAbout) + // NOTE - the ClassWizard will add and remove mapping macros here. + // DO NOT EDIT what you see in these blocks of generated code! + //}}AFX_MSG_MAP + // Standard file based document commands + ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) + ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotApp construction + +CTstCBotApp::CTstCBotApp() +{ + // TODO: add construction code here, + // Place all significant initialization in InitInstance +} + +///////////////////////////////////////////////////////////////////////////// +// The one and only CTstCBotApp object + +CTstCBotApp theApp; + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotApp initialization + +BOOL CTstCBotApp::InitInstance() +{ + AfxEnableControlContainer(); + + // Standard initialization + // If you are not using these features and wish to reduce the size + // of your final executable, you should remove from the following + // the specific initialization routines you do not need. + +#ifdef _AFXDLL + Enable3dControls(); // Call this when using MFC in a shared DLL +#else + Enable3dControlsStatic(); // Call this when linking to MFC statically +#endif + + // Change the registry key under which our settings are stored. + // You should modify this string to be something appropriate + // such as the name of your company or organization. + SetRegistryKey(_T("Local AppWizard-Generated Applications")); + + LoadStdProfileSettings(); // Load standard INI file options (including MRU) + + // Register the application's document templates. Document templates + // serve as the connection between documents, frame windows and views. + + CSingleDocTemplate* pDocTemplate; + pDocTemplate = new CSingleDocTemplate( + IDR_MAINFRAME, + RUNTIME_CLASS(CTstCBotDoc), + RUNTIME_CLASS(CMainFrame), // main SDI frame window + RUNTIME_CLASS(CTstCBotView)); + AddDocTemplate(pDocTemplate); + + // Parse command line for standard shell commands, DDE, file open + CCommandLineInfo cmdInfo; + ParseCommandLine(cmdInfo); + + // Dispatch commands specified on the command line + if (!ProcessShellCommand(cmdInfo)) + return FALSE; + + // The one and only window has been initialized, so show and update it. + m_pMainWnd->ShowWindow(SW_SHOW); + m_pMainWnd->UpdateWindow(); + + + +/////////////////////////////////// +// dfini la fonction "show()" +// -------------------------------- + + CBotProgram::AddFunction("show", rShow, cShow); + CBotProgram::AddFunction("err", rShow, cErr); + CBotProgram::AddFunction("print", rPrint, cPrint); + CBotProgram::AddFunction("println", rPrintLn, cPrint); + + +/////////////////////////////////// +// dfinie la classe globale CPoint +// -------------------------------- + + CBotClass* m_pClassPoint; + + m_pClassPoint = new CBotClass("CPoint", NULL); + // ajoute le composant ".x" + m_pClassPoint->AddItem("x", CBotTypFloat); + // ajoute le composant ".y" + m_pClassPoint->AddItem("y", CBotTypFloat); + + // ajoute le constructeur pour cette classe + m_pClassPoint->AddFunction("CPoint", rCPoint, cCPoint); + // ajoute la mthode Oppos + m_pClassPoint->AddFunction("Oppos", rOppose, cOppose); + + +////////////////////////////////////////////////////////////////// +// compile un bout de programme pour voir s'il est bien accessible +// depuis un autre "module" + + CBotProgram* p = new CBotProgram; + CBotStringArray Liste; + p->Compile(" public void MonProgram( ) { show (\"mon programme\") ;}", Liste ); + + // l'objet n'est pas dtruit et plus rfrenc + // je sais c'est pas bien + + + return TRUE; +} + +///////////////////////////////////////////////////////////////////////////// +// CAboutDlg dialog used for App About + +class CAboutDlg : public CDialog +{ +public: + CAboutDlg(); + +// Dialog Data + //{{AFX_DATA(CAboutDlg) + enum { IDD = IDD_ABOUTBOX }; + //}}AFX_DATA + + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CAboutDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + //{{AFX_MSG(CAboutDlg) + // No message handlers + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) +{ + //{{AFX_DATA_INIT(CAboutDlg) + //}}AFX_DATA_INIT +} + +void CAboutDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(CAboutDlg) + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) + //{{AFX_MSG_MAP(CAboutDlg) + // No message handlers + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +// App command to run the dialog +void CTstCBotApp::OnAppAbout() +{ + CAboutDlg aboutDlg; + aboutDlg.DoModal(); +} + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotApp commands diff --git a/src/CBot/tests/old TstCBot/TstCBot.dsp b/src/CBot/tests/old TstCBot/TstCBot.dsp new file mode 100644 index 0000000..35e5c0b --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBot.dsp @@ -0,0 +1,180 @@ +# Microsoft Developer Studio Project File - Name="TstCBot" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 5.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Application" 0x0101 + +CFG=TstCBot - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "TstCBot.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "TstCBot.mak" CFG="TstCBot - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "TstCBot - Win32 Release" (based on "Win32 (x86) Application") +!MESSAGE "TstCBot - Win32 Debug" (based on "Win32 (x86) Application") +!MESSAGE + +# Begin Project +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "TstCBot - Win32 Release" + +# PROP BASE Use_MFC 6 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 6 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /FR /Yu"stdafx.h" /FD /c +# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32 +# ADD BASE RSC /l 0x40c /d "NDEBUG" /d "_AFXDLL" +# ADD RSC /l 0x40c /d "NDEBUG" /d "_AFXDLL" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 /nologo /subsystem:windows /machine:I386 +# ADD LINK32 /nologo /subsystem:windows /machine:I386 + +!ELSEIF "$(CFG)" == "TstCBot - Win32 Debug" + +# PROP BASE Use_MFC 6 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 6 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MDd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /MDd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /FR /Yu"stdafx.h" /FD /c +# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32 +# ADD BASE RSC /l 0x40c /d "_DEBUG" /d "_AFXDLL" +# ADD RSC /l 0x40c /d "_DEBUG" /d "_AFXDLL" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept +# ADD LINK32 /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "TstCBot - Win32 Release" +# Name "TstCBot - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\BotConsoleDlg.cpp +# End Source File +# Begin Source File + +SOURCE=.\MainFrm.cpp +# End Source File +# Begin Source File + +SOURCE=.\StdAfx.cpp +# ADD CPP /Yc"stdafx.h" +# End Source File +# Begin Source File + +SOURCE=.\TstCBot.cpp +# End Source File +# Begin Source File + +SOURCE=.\TstCBot.rc + +!IF "$(CFG)" == "TstCBot - Win32 Release" + +!ELSEIF "$(CFG)" == "TstCBot - Win32 Debug" + +!ENDIF + +# End Source File +# Begin Source File + +SOURCE=.\TstCBotDoc.cpp +# End Source File +# Begin Source File + +SOURCE=.\TstCBotView.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\BotConsoleDlg.h +# End Source File +# Begin Source File + +SOURCE=.\MainFrm.h +# End Source File +# Begin Source File + +SOURCE=.\Resource.h +# End Source File +# Begin Source File + +SOURCE=.\StdAfx.h +# End Source File +# Begin Source File + +SOURCE=.\TstCBot.h +# End Source File +# Begin Source File + +SOURCE=.\TstCBotDoc.h +# End Source File +# Begin Source File + +SOURCE=.\TstCBotView.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe" +# Begin Source File + +SOURCE=.\res\TstCBot.ico +# End Source File +# Begin Source File + +SOURCE=.\res\TstCBot.rc2 +# End Source File +# Begin Source File + +SOURCE=.\res\TstCBotDoc.ico +# End Source File +# End Group +# Begin Source File + +SOURCE=.\ReadMe.txt +# End Source File +# End Target +# End Project diff --git a/src/CBot/tests/old TstCBot/TstCBot.h b/src/CBot/tests/old TstCBot/TstCBot.h new file mode 100644 index 0000000..616db43 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBot.h @@ -0,0 +1,62 @@ +// TstCBot.h : main header file for the TSTCBOT application +// + +#if !defined(AFX_TSTCBOT_H__70B37568_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) +#define AFX_TSTCBOT_H__70B37568_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +#ifndef __AFXWIN_H__ + #error include 'stdafx.h' before including this file for PCH +#endif + +#include "resource.h" // main symbols +#include "..\CBotDll.h" + + +class CMyThread; + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotApp: +// See TstCBot.cpp for the implementation of this class +// + +class CTstCBotApp : public CWinApp +{ +public: + CTstCBotApp(); + + CMyThread* m_pThread; + CWnd* m_pView; + CEdit* m_pConsole; + CBotStringArray m_Liste; + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CTstCBotApp) + public: + virtual BOOL InitInstance(); + //}}AFX_VIRTUAL + +// Implementation + + //{{AFX_MSG(CTstCBotApp) + afx_msg void OnAppAbout(); + // NOTE - the ClassWizard will add and remove member functions here. + // DO NOT EDIT what you see in these blocks of generated code ! + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TSTCBOT_H__70B37568_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) + +#define WM_STARTPROG WM_APP + 0 +#define WM_ENDPROG WM_APP + 1 diff --git a/src/CBot/tests/old TstCBot/TstCBot.rc b/src/CBot/tests/old TstCBot/TstCBot.rc new file mode 100644 index 0000000..9e91c76 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBot.rc @@ -0,0 +1,471 @@ +//Microsoft Developer Studio generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// French (France) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) +#ifdef _WIN32 +LANGUAGE LANG_FRENCH, SUBLANG_FRENCH +#pragma code_page(1252) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE DISCARDABLE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE DISCARDABLE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE DISCARDABLE +BEGIN + "#define _AFX_NO_SPLITTER_RESOURCES\r\n" + "#define _AFX_NO_OLE_RESOURCES\r\n" + "#define _AFX_NO_TRACKER_RESOURCES\r\n" + "#define _AFX_NO_PROPERTY_RESOURCES\r\n" + "\r\n" + "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)\r\n" + "#ifdef _WIN32\r\n" + "LANGUAGE 12, 1\r\n" + "#pragma code_page(1252)\r\n" + "#endif\r\n" + "#include ""res\\TstCBot.rc2"" // non-Microsoft Visual C++ edited resources\r\n" + "#include ""l.fra\\afxres.rc"" // Standard components\r\n" + "#endif\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Icon +// + +// Icon with lowest ID value placed first to ensure application icon +// remains consistent on all systems. +IDR_MAINFRAME ICON DISCARDABLE "res\\TstCBot.ico" +IDR_TSTCBOTYPE ICON DISCARDABLE "res\\TstCBotDoc.ico" + +///////////////////////////////////////////////////////////////////////////// +// +// Menu +// + +IDR_MAINFRAME MENU PRELOAD DISCARDABLE +BEGIN + POPUP "&Fichier" + BEGIN + MENUITEM "&Nouveau\tCtrl+N", ID_FILE_NEW + MENUITEM "&Ouvrir...\tCtrl+O", ID_FILE_OPEN + MENUITEM "&Enregistrer\tCtrl+S", ID_FILE_SAVE + MENUITEM "En®istrer sous...", ID_FILE_SAVE_AS + MENUITEM SEPARATOR + MENUITEM "Fichier rcent", ID_FILE_MRU_FILE1, GRAYED + MENUITEM SEPARATOR + MENUITEM "&Quitter", ID_APP_EXIT + END + POPUP "&Edition" + BEGIN + MENUITEM "&Annuler\tCtrl+Z", ID_EDIT_UNDO + MENUITEM SEPARATOR + MENUITEM "&Couper\tCtrl+X", ID_EDIT_CUT + MENUITEM "&Copier\tCtrl+C", ID_EDIT_COPY + MENUITEM "C&oller\tCtrl+V", ID_EDIT_PASTE + END + POPUP "&Affichage" + BEGIN + MENUITEM "Barre d'&tat", ID_VIEW_STATUS_BAR + END + POPUP "&Tests" + BEGIN + MENUITEM "&Compile\tAlt+C", ID_CP1 + MENUITEM "&Execute\tAlt+V", ID_EXE + END + POPUP "&?" + BEGIN + MENUITEM "&A propos de TstCBot...", ID_APP_ABOUT + END +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Accelerator +// + +IDR_MAINFRAME ACCELERATORS PRELOAD MOVEABLE PURE +BEGIN + "C", ID_CP1, VIRTKEY, ALT, NOINVERT + "N", ID_FILE_NEW, VIRTKEY, CONTROL, NOINVERT + "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT + "S", ID_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT + "V", ID_EXE, VIRTKEY, ALT, NOINVERT + VK_BACK, ID_EDIT_UNDO, VIRTKEY, ALT, NOINVERT + VK_DELETE, ID_EDIT_CUT, VIRTKEY, SHIFT, NOINVERT + VK_F5, ID_EXE, VIRTKEY, NOINVERT + VK_F7, ID_CP1, VIRTKEY, NOINVERT + "X", ID_EXE, VIRTKEY, ALT, NOINVERT +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 217, 55 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "A propos de TstCBot" +FONT 8, "MS Sans Serif" +BEGIN + ICON IDR_MAINFRAME,IDC_STATIC,11,17,20,20 + LTEXT "TstCBot version 1.0",IDC_STATIC,40,10,119,8,SS_NOPREFIX + LTEXT "Copyright (C) 1900",IDC_STATIC,40,25,119,8 + DEFPUSHBUTTON "OK",IDOK,178,7,32,14,WS_GROUP +END + + +#ifndef _MAC +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,0,1 + PRODUCTVERSION 1,0,0,1 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040C04B0" + BEGIN + VALUE "CompanyName", "\0" + VALUE "FileDescription", "Application MFC TstCBot\0" + VALUE "FileVersion", "1, 0, 0, 1\0" + VALUE "InternalName", "TstCBot\0" + VALUE "LegalCopyright", "Copyright (C) 1900\0" + VALUE "LegalTrademarks", "\0" + VALUE "OriginalFilename", "TstCBot.EXE\0" + VALUE "ProductName", "Application TstCBot\0" + VALUE "ProductVersion", "1, 0, 0, 1\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Traduction", 0x40c, 1200 + END +END + +#endif // !_MAC + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO DISCARDABLE +BEGIN + IDD_ABOUTBOX, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 210 + TOPMARGIN, 7 + BOTTOMMARGIN, 48 + END +END +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// String Table +// + +STRINGTABLE PRELOAD DISCARDABLE +BEGIN + IDR_MAINFRAME "TstCBot\n\nTstCBo\n\n\nTstCBot.Document\nTstCBo Document" +END + +STRINGTABLE PRELOAD DISCARDABLE +BEGIN + AFX_IDS_APP_TITLE "TstCBot" + AFX_IDS_IDLEMESSAGE "Prt" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_INDICATOR_EXT "EXT" + ID_INDICATOR_CAPS "MAJ" + ID_INDICATOR_NUM "NUM" + ID_INDICATOR_SCRL "DEF" + ID_INDICATOR_OVR "ECR" + ID_INDICATOR_REC "ENR" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_FILE_NEW "Cre un nouveau document\nNouveau" + ID_FILE_OPEN "Ouvre un document existant\nOuvrir" + ID_FILE_CLOSE "Ferme le document actif\nFermer" + ID_FILE_SAVE "Enregistre le document actif\nEnregistrer" + ID_FILE_SAVE_AS "Enregistre le document actif sous un nouveau nom\nEnregistrer sous" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_APP_ABOUT "Affiche des informations sur le programme, le numro de version et le copyright\nA propos de" + ID_APP_EXIT "Ferme l'application ; propose d'enregistrer les documents\nQuitter" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_FILE_MRU_FILE1 "Ouvre ce document" + ID_FILE_MRU_FILE2 "Ouvre ce document" + ID_FILE_MRU_FILE3 "Ouvre ce document" + ID_FILE_MRU_FILE4 "Ouvre ce document" + ID_FILE_MRU_FILE5 "Ouvre ce document" + ID_FILE_MRU_FILE6 "Ouvre ce document" + ID_FILE_MRU_FILE7 "Ouvre ce document" + ID_FILE_MRU_FILE8 "Ouvre ce document" + ID_FILE_MRU_FILE9 "Ouvre ce document" + ID_FILE_MRU_FILE10 "Ouvre ce document" + ID_FILE_MRU_FILE11 "Ouvre ce document" + ID_FILE_MRU_FILE12 "Ouvre ce document" + ID_FILE_MRU_FILE13 "Ouvre ce document" + ID_FILE_MRU_FILE14 "Ouvre ce document" + ID_FILE_MRU_FILE15 "Ouvre ce document" + ID_FILE_MRU_FILE16 "Ouvre ce document" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_NEXT_PANE "Passe au volet de fentre suivant\nVolet suivant" + ID_PREV_PANE "Revient au volet prcdent\nVolet prcdent" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_WINDOW_SPLIT "Fractionne la fentre active en deux volets\nFractionner" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_EDIT_CLEAR "Efface la slection\nEffacer" + ID_EDIT_CLEAR_ALL "Efface tout\nEffacer tout" + ID_EDIT_COPY "Copie la slection et la place dans le Presse-papiers\nCopier" + ID_EDIT_CUT "Supprime la slection et la place dans le Presse-papiers\nCopier" + ID_EDIT_FIND "Recherche le texte spcifi\nRechercher" + ID_EDIT_PASTE "Insre le contenu du Presse-papiers\nColler" + ID_EDIT_REPEAT "Rpte la dernire action\nRpter" + ID_EDIT_REPLACE "Remplace le texte spcifique par un texte diffrent\nRemplacer" + ID_EDIT_SELECT_ALL "Slectionne le document entier\nSlectionner tout" + ID_EDIT_UNDO "Annule la dernire action\nAnnuler" + ID_EDIT_REDO "Rtablit l'action prcdemment annule\nRtablir" +END + +STRINGTABLE DISCARDABLE +BEGIN + ID_VIEW_STATUS_BAR "Affiche ou masque la barre d'tat\nBarre d'tat" +END + +STRINGTABLE DISCARDABLE +BEGIN + AFX_IDS_SCSIZE "Change la taille de la fentre" + AFX_IDS_SCMOVE "Change la position de la fentre" + AFX_IDS_SCMINIMIZE "Rduit la fentre en icne" + AFX_IDS_SCMAXIMIZE "Agrandit la fentre au format de l'cran" + AFX_IDS_SCNEXTWINDOW "Passe la fentre de document suivante" + AFX_IDS_SCPREVWINDOW "Passe la fentre de document prcdente" + AFX_IDS_SCCLOSE "Ferme la fentre active et propose l'enregistrement des documents" +END + +STRINGTABLE DISCARDABLE +BEGIN + AFX_IDS_SCRESTORE "Restaure la fentre sa taille d'origine" + AFX_IDS_SCTASKLIST "Active la liste des tches" +END + +STRINGTABLE DISCARDABLE +BEGIN + TX_TYPENAMES "les diffrents types" + 1001 "Byte" + 1002 "Short" + 1003 "Char" + 1004 "Int" + 1005 "Long" + 1006 "Real" + 1007 "Double" +END + +STRINGTABLE DISCARDABLE +BEGIN + 1008 "Boolean" + 1009 "Class" + 1010 "String" +END + +STRINGTABLE DISCARDABLE +BEGIN + TX_OPENPAR "Il manque une parenthse ouvrante." + TX_CLOSEPAR "Il manque une parenthse fermante." + TX_NOTBOOL "L'expression doit tre un boolean." + TX_UNDEFVAR "Variable non dclare." + TX_BADLEFT "Assignation impossible." + TX_ENDOF "Instruction non termine." + TX_OUTCASE "Instruction ""case"" hors d'un bloc ""switch""." + TX_NOTERM "Instructions aprs la fin." +END + +STRINGTABLE DISCARDABLE +BEGIN + TX_CLOSEBLK "Il manque la fin du bloc." + TX_ELSEWITHOUTIF "Instruction ""else"" sans ""if"" correspondant." + TX_OPENBLK "Dbut d'un bloc attendu." + TX_BADTYPE "Mauvais type de rsultat pour l'assignation." + TX_REDEFVAR "Redfinition d'une variable." + TX_BAD2TYPE "Les deux oprandes ne sont pas de types compatibles." + TX_UNDEFCALL "Routine inconnue." + TX_MISDOTS "Sparateur "" : "" attendu." + TX_WHILE "Manque le mot ""while""." + TX_BREAK "Instruction ""break"" en dehors d'une boucle." + TX_LABEL "Un label ne peut se placer que devant un ""for"", un ""while"", un ""do"" ou un ""switch""." + TX_NOLABEL "Cette tiquette n'existe pas" + TX_NOCASE "Manque une instruction ""case""." + TX_BADNUM "Un nombre est attendu." + TX_VOID "Paramtre void." + TX_NOTYP "Dclaration de type attendu" +END + +STRINGTABLE DISCARDABLE +BEGIN + TX_DIVZERO "Division par zro." + TX_NOTINIT "Variable non initialise." + TX_BADTHROW "Valeur ngative refuse pour ""throw""." + TX_NORETVAL "La fonction n'a pas retourn de rsultat" + TX_NORUN "Pas de fonction en excution" + TX_NOCALL "Appel d'une fonction inexistante" +END + +STRINGTABLE DISCARDABLE +BEGIN + TX_NOVAR "Nom d'une variable attendu" + TX_NOFONC "Nom de la fonction attendu." + TX_OVERPARAM "Trop de paramtres" + TX_REDEF "Cette fonction existe dj." + TX_LOWPARAM "Pas assez de paramtres" + TX_BADPARAM "Aucune fonction de ce nom n'accepte ce(s) type(s) de paramtre(s)" + TX_NUMPARAM "Aucune fonction de ce nom n'accepte ce nombre de paramtres" + TX_NOITEM "Cet lment n'exite pas dans cette classe." + TX_DOT "L'objet n'est pas une instance d'une classe." + TX_NOCONST "Il n'y a pas de constructeur appropri." + TX_REDEFCLASS "Cette classe existe dj." +END + +#endif // French (France) resources +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// French (Switzerland) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRS) +#ifdef _WIN32 +LANGUAGE LANG_FRENCH, SUBLANG_FRENCH_SWISS +#pragma code_page(1252) +#endif //_WIN32 + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_CONSOLE DIALOG DISCARDABLE 0, 0, 401, 210 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "CBot Console" +FONT 8, "MS Sans Serif" +BEGIN + LTEXT "Commande :",IDC_STATIC,7,177,40,8 + EDITTEXT IDC_EDIT2,7,189,329,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "Excute",IDOK,344,189,50,14 + EDITTEXT IDC_EDIT1,7,7,387,167,ES_MULTILINE | ES_READONLY | + ES_WANTRETURN | WS_VSCROLL +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO DISCARDABLE +BEGIN + IDD_CONSOLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 394 + TOPMARGIN, 7 + BOTTOMMARGIN, 203 + END +END +#endif // APSTUDIO_INVOKED + +#endif // French (Switzerland) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// +#define _AFX_NO_SPLITTER_RESOURCES +#define _AFX_NO_OLE_RESOURCES +#define _AFX_NO_TRACKER_RESOURCES +#define _AFX_NO_PROPERTY_RESOURCES + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA) +#ifdef _WIN32 +LANGUAGE 12, 1 +#pragma code_page(1252) +#endif +#include "res\TstCBot.rc2" // non-Microsoft Visual C++ edited resources +#include "l.fra\afxres.rc" // Standard components +#endif +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/src/CBot/tests/old TstCBot/TstCBotDoc.cpp b/src/CBot/tests/old TstCBot/TstCBotDoc.cpp new file mode 100644 index 0000000..7d7e2ef --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBotDoc.cpp @@ -0,0 +1,83 @@ +// TstCBotDoc.cpp : implementation of the CTstCBotDoc class +// + +#include "stdafx.h" +#include "TstCBot.h" + +#include "TstCBotDoc.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotDoc + +IMPLEMENT_DYNCREATE(CTstCBotDoc, CDocument) + +BEGIN_MESSAGE_MAP(CTstCBotDoc, CDocument) + //{{AFX_MSG_MAP(CTstCBotDoc) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotDoc construction/destruction + +CTstCBotDoc::CTstCBotDoc() +{ + // TODO: add one-time construction code here + +} + +CTstCBotDoc::~CTstCBotDoc() +{ +} + +BOOL CTstCBotDoc::OnNewDocument() +{ + if (!CDocument::OnNewDocument()) + return FALSE; + + // TODO: add reinitialization code here + // (SDI documents will reuse this document) + + return TRUE; +} + + + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotDoc serialization + +void CTstCBotDoc::Serialize(CArchive& ar) +{ + if (ar.IsStoring()) + { + // TODO: add storing code here + } + else + { + // TODO: add loading code here + } +} + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotDoc diagnostics + +#ifdef _DEBUG +void CTstCBotDoc::AssertValid() const +{ + CDocument::AssertValid(); +} + +void CTstCBotDoc::Dump(CDumpContext& dc) const +{ + CDocument::Dump(dc); +} +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotDoc commands + diff --git a/src/CBot/tests/old TstCBot/TstCBotDoc.h b/src/CBot/tests/old TstCBot/TstCBotDoc.h new file mode 100644 index 0000000..ae1d0f7 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBotDoc.h @@ -0,0 +1,55 @@ +// TstCBotDoc.h : interface of the CTstCBotDoc class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_TSTCBOTDOC_H__70B3756E_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) +#define AFX_TSTCBOTDOC_H__70B3756E_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + + +class CTstCBotDoc : public CDocument +{ +protected: // create from serialization only + CTstCBotDoc(); + DECLARE_DYNCREATE(CTstCBotDoc) + +// Attributes +public: + +// Operations +public: + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CTstCBotDoc) + public: + virtual BOOL OnNewDocument(); + virtual void Serialize(CArchive& ar); + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CTstCBotDoc(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +protected: + +// Generated message map functions +protected: + //{{AFX_MSG(CTstCBotDoc) + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TSTCBOTDOC_H__70B3756E_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) diff --git a/src/CBot/tests/old TstCBot/TstCBotView.cpp b/src/CBot/tests/old TstCBot/TstCBotView.cpp new file mode 100644 index 0000000..3ee9094 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBotView.cpp @@ -0,0 +1,291 @@ +// TstCBotView.cpp : implementation of the CTstCBotView class +// + +#include "stdafx.h" +#include "TstCBot.h" + +#include "TstCBotDoc.h" +#include "TstCBotView.h" +#include "BotConsoleDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#undef THIS_FILE +static char THIS_FILE[] = __FILE__; +#endif + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotView + +IMPLEMENT_DYNCREATE(CTstCBotView, CView) + +BEGIN_MESSAGE_MAP(CTstCBotView, CView) + //{{AFX_MSG_MAP(CTstCBotView) + ON_WM_SIZE() + ON_COMMAND(ID_CP1, OnCp1) + ON_COMMAND(ID_EXE, OnExe) + ON_COMMAND(ID_FILE_SAVE, OnFileSave) + ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotView construction/destruction + +CTstCBotView::CTstCBotView() +{ + // TODO: add construction code here + m_pEdit = NULL; + m_pProg = NULL; +} + +CTstCBotView::~CTstCBotView() +{ +} + +BOOL CTstCBotView::PreCreateWindow(CREATESTRUCT& cs) +{ + // TODO: Modify the Window class or styles here by modifying + // the CREATESTRUCT cs + + return CView::PreCreateWindow(cs); +} + +void CTstCBotView::OnActivateView( BOOL bActivate, CView* pActivateView, CView* pDeactiveView ) +{ + if ( m_pEdit == NULL) + { + m_pEdit = new CEdit(); + CRect rect; + GetClientRect( rect ); + + m_pEdit->Create( WS_VISIBLE|WS_BORDER|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_NOHIDESEL|ES_AUTOVSCROLL, + rect, this, IDC_EDIT1 ); + m_pEdit->SetTabStops(12); + LoadEdition("CBotTest.txt"); + m_pEdit->SetFocus(); + } +} + + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotView drawing + +void CTstCBotView::OnDraw(CDC* pDC) +{ + CTstCBotDoc* pDoc = GetDocument(); + ASSERT_VALID(pDoc); + + // TODO: add draw code for native data here +} + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotView diagnostics + +#ifdef _DEBUG +void CTstCBotView::AssertValid() const +{ + CView::AssertValid(); +} + +void CTstCBotView::Dump(CDumpContext& dc) const +{ + CView::Dump(dc); +} + +CTstCBotDoc* CTstCBotView::GetDocument() // non-debug version is inline +{ + ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTstCBotDoc))); + return (CTstCBotDoc*)m_pDocument; +} +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// CTstCBotView message handlers + +void CTstCBotView::OnSize(UINT nType, int cx, int cy) +{ + CView::OnSize(nType, cx, cy); + + if ( m_pEdit != NULL ) + { + CRect rect; + GetClientRect( rect ); + m_pEdit->MoveWindow( rect ); + m_pEdit->SetFocus(); + } +} + +void CTstCBotView::SaveEdition(const char* filename) +{ + CString program; + + m_pEdit->GetWindowText(program); + + FILE* pf = fopen(filename, "wb"); + if (pf==NULL) return; + + fputs (program, pf); + fclose(pf); +} + +void CTstCBotView::LoadEdition(const char* filename) +{ + CString program("{ int x = 10000; while (x > 0) x = x-1; }"); + + FILE* pf = fopen(filename, "r"); + if (pf!=NULL) + { + char buffer[10000]; + program.Empty(); + + while (NULL != fgets (buffer, 100000, pf)) + { + program += buffer; + program = program.Left(program.GetLength()-1) + "\r\n"; + } + + fclose(pf); + } + + m_pEdit->SetWindowText(program); +} + + + +// compile le programme +#include + +void CTstCBotView::OnCp1() +{ + CString program; + + SaveEdition("CBotTest.txt"); + + m_pEdit->GetWindowText(program); + + CString TextError; + int code, start, end; + + if ( m_pProg == NULL ) m_pProg = new CBotProgram(); + + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + + if (m_pProg->Compile(program, pApp->m_Liste)) + { + CString done = "Compilation sans erreur.\nLes fonctions suivantes sont externes:\n"; + + for ( int i = 0; i < pApp->m_Liste.RetSize(); i++) + { + done += CString(pApp->m_Liste[i]) + "\n"; + } + + AfxMessageBox( done ); + } + else + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + + m_pEdit->SetSel( start, end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + TextError.LoadString( code ); + if (TextError.IsEmpty()) + { + char buf[100]; + sprintf(buf, "Erreur numro %d.", code); + TextError = buf; + } + AfxMessageBox( TextError ); + } + + m_pEdit->SetFocus(); +} + + +////////////////////////////////////////////////////// + + +void CTstCBotView::OnExe() +{ + CTstCBotApp* pApp = (CTstCBotApp*)AfxGetApp(); + + if( m_pProg == NULL) + { + AfxMessageBox("Pas de programme compil !"); + return; + } + + if( pApp->m_Liste.RetSize() == 0 ) + { + AfxMessageBox("Aucune fonction marque \"extern\" !"); + return; + } + + + + CBotConsoleDlg dlg; + dlg.DoModal(); // dialogue pour faire la console + + if ( dlg.m_code>0 ) + { + CString TextError; + + m_pEdit->SetSel( dlg.m_start, dlg.m_end ); + m_pEdit->SetFocus(); // met en vidence la partie avec problme + + TextError.LoadString( dlg.m_code ); + if (TextError.IsEmpty()) + { + char buf[100]; + sprintf(buf, "Erreur numro %d.", dlg.m_code); + TextError = buf; + } +// AfxMessageBox( TextError ); + } + + m_pEdit->SetFocus(); + + return; +} + + + +void CTstCBotView::OnFileSave() +{ + // TODO: Add your command handler code here + SaveEdition("CBotTest.txt"); +} + +void CTstCBotView::OnFileSaveAs() +{ + CFileDialog *pDlg; + CString s; + + pDlg = new CFileDialog(FALSE, "TXT", NULL, + OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY, + "cboxtest|*.txt", this); + if ( pDlg == NULL ) return; + + if ( pDlg->DoModal() == IDOK ) // choix du fichier ... + { + SaveEdition(pDlg->GetPathName()); + } + + delete pDlg; +} + +#if 0 +void test() +{ + int y,z; + + for (;;); + for (x = 0; y = 1; z = 3) int q = 6; + for (int x = 0; int y = 1; int z = 3) int q = 6; + // pour voir +} +#endif + diff --git a/src/CBot/tests/old TstCBot/TstCBotView.h b/src/CBot/tests/old TstCBot/TstCBotView.h new file mode 100644 index 0000000..d5aede5 --- /dev/null +++ b/src/CBot/tests/old TstCBot/TstCBotView.h @@ -0,0 +1,81 @@ +// TstCBotView.h : interface of the CTstCBotView class +// +///////////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_TSTCBOTVIEW_H__70B37570_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) +#define AFX_TSTCBOTVIEW_H__70B37570_5DFD_11D4_A15E_00E0189013DF__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + + +class CBotProgram; +class CBotClass; + + +class CTstCBotView : public CView +{ +protected: // create from serialization only + CTstCBotView(); + DECLARE_DYNCREATE(CTstCBotView) + + CEdit* m_pEdit; // texte en dition + CWnd* m_pWnd; + CBotProgram* m_pProg; // programme compil + +// Attributes +public: + CTstCBotDoc* GetDocument(); + +// Operations +public: + void LoadEdition(const char* name); + void SaveEdition(const char* name); + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CTstCBotView) + public: + virtual void OnDraw(CDC* pDC); // overridden to draw this view + virtual BOOL PreCreateWindow(CREATESTRUCT& cs); + virtual void OnActivateView( BOOL bActivate, CView* pActivateView, CView* pDeactiveView ); + protected: + //}}AFX_VIRTUAL + +// Implementation +public: + virtual ~CTstCBotView(); +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + +protected: + +// Generated message map functions +protected: + //{{AFX_MSG(CTstCBotView) + afx_msg void OnSize(UINT nType, int cx, int cy); + afx_msg void OnCp1(); + afx_msg void OnExe(); + afx_msg void OnFileSave(); + afx_msg void OnFileSaveAs(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +#ifndef _DEBUG // debug version in TstCBotView.cpp +inline CTstCBotDoc* CTstCBotView::GetDocument() + { return (CTstCBotDoc*)m_pDocument; } +#endif + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TSTCBOTVIEW_H__70B37570_5DFD_11D4_A15E_00E0189013DF__INCLUDED_) + + + diff --git a/src/CBot/tests/old TstCBot/res/TstCBot.ico b/src/CBot/tests/old TstCBot/res/TstCBot.ico new file mode 100644 index 0000000..7eef0bc Binary files /dev/null and b/src/CBot/tests/old TstCBot/res/TstCBot.ico differ diff --git a/src/CBot/tests/old TstCBot/res/TstCBot.rc2 b/src/CBot/tests/old TstCBot/res/TstCBot.rc2 new file mode 100644 index 0000000..2186272 --- /dev/null +++ b/src/CBot/tests/old TstCBot/res/TstCBot.rc2 @@ -0,0 +1,13 @@ +// +// TSTCBOT.RC2 - resources Microsoft Visual C++ does not edit directly +// + +#ifdef APSTUDIO_INVOKED + #error this file is not editable by Microsoft Visual C++ +#endif //APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// Add manually edited resources here... + +///////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/tests/old TstCBot/res/TstCBotDoc.ico b/src/CBot/tests/old TstCBot/res/TstCBotDoc.ico new file mode 100644 index 0000000..2a1f1ae Binary files /dev/null and b/src/CBot/tests/old TstCBot/res/TstCBotDoc.ico differ diff --git a/src/CBot/tests/old TstCBot/test complet 1.txt b/src/CBot/tests/old TstCBot/test complet 1.txt new file mode 100644 index 0000000..0fd4fa5 --- /dev/null +++ b/src/CBot/tests/old TstCBot/test complet 1.txt @@ -0,0 +1,213 @@ +// test de l'interprteur CBot, (c) D. Dumoulin 2000 + +Int Somme ( Int x, Int y ) +{ + return x + y; +} + +Real Somme ( Real x, Real y ) +{ + return x + y; +} + +void A_Faire() +{ + CPoint position; // utilise une classe externe + position.x = 123.5; + position.y = -45.1; + + show ( position ); +} + +/* Les nouveauts sont les suivantes + __________________________________________________ + + On peut dfinir des fonctions, avec la syntaxe habituelle au C + void MaFonction( Int x, Real y ) { ... } + + Les caractristiques sont pour l'instant les suivantes: + + - ce programme TstCBot excute la dernire fonction dfinie + + - on peut dfinir deux fonctions du mme nom, + si la liste de paramtres est diffrente. + Par exemple + Int Somme( Int x, Int y ) + Real Somme( Real x, Real y ); + Note: si la seconde n'existait pas, Somme ( 1.3, 4.8 ) + serait fait sur les nombres entier 1 + 4 + La priorit est donne la routine qui ne pert pas + de bits dans la conversion des paramtres. + + - il n'y a pas d'erreur de compilation si une routine + ne retourne pas de valeur alors qu'elle devrait, + par contre il y a une erreur "correcte" l'excution + + - il est possible d'utiliser une fonction qui est dfinie + plus bas dans le programme. + __________________________________________________ + + Tous les blocs d'instructions existent maintenant, savoir + + label : + while (condition) { instructions; break label; continue label; } + + label : + do { instructions; break label; continue label; } while (condition) + + label: + for (initial; condition; incrment) { instructions; break; continue } + + switch ( valeur ) { case 1: instructions; case 2: break ; } + + try {instructions; throw exception; } catch (exception) {instructions;} + catch (testlogique) {instructions;} + finally {instructions;} + // le bloc finally est excuter dans tous les cas + // qu'il y ait eu exception ou non, et aussi en cas de break, continue ou return + __________________________________________________ + + Les "exceptions" sont juste des numros (31 bits) + 6000 = division par zro + 6001 = variable non initialise + 6002 = valeur ngative pour un throw + 6003 = la fonction n'a pas retourn de valeur + + les autres numros sont disposition + (COLOBOT aura surement des numros d'exception propre) + l'association d'un mot clef pour ces exceptions est venir. + __________________________________________________ + + L'interprteur a t un peu optimiser, une boucle de un millon de dcrment + ne prend plus que +*/ + +void Test () +{ // dbut du bloc d'instructions + + Int z = 1000000; + while ( z>0 ) z--; + + return; + { + // test la prsance pour les assignations + Int a = 9; + a += (a = 3); + if ( a != 12 ) 1/0; // le rsultat correct est 12 + + Int b = 9; + b = b + (b = 3); + if (b != 12) 1/0; // mme chose + + // la fonction show est une fonction externe + // dfinie par TstCBot + // elle peut prendre un nombre quelconque de paramtres + show ( a, b ); + } + + { + // petit test sur les chanes + String x = "ch." ; + String y ; + x += y = x + " de la brume."; + + // concatnation de chanes, accepte des autres types + String s = 1 + 2 + " test " + 3 + 4 ; + + show( x, y, s ); + + // les tests sur les chanes ne sont pas standard en Java + // mais c'est si pratique : + + if ( s != "3 test 34" ) 1/0; // le rsultat correct est "3 test 34" + // car 1+2 est valu en premier entre 2 nombres + // et ensuite on additionne des chanes "3" "4" + } + + { + // teste toutes les oprations avec les entiers (32 bits) + Int a = 4; + Int b = 4; + + Int c = a++ * --b; // post incrment, pr dcrment + if ( c != 12 ) 1/0; + + c = ++a * b--; // pr incrment, post dcrment + if ( c!=18 ) 1/0; + + a = a+b-a*b/a%3; // 6 + 2 - ( 6 * 2 / 6 % 3 ) -> 6 + if ( a != 6 ) 1/0; + + a += 2; a-=1; a*=3; a/=4; a%=3; // (6+2 -1) *3 /4 modulo 3 = 21 / 4 modulo 3 = 2 + if ( a!= 2) 0/0; + + if (-5 << 3 != -40) 0/0; // shift gauche + if ( -5 >> 1 != -3) 0/0; // shift arithmtique droite 11111011 -> 11111101 = -3 + if ( -5 >>> 1 != 0x3ffffffd) 0/0; // shift non sign droite + + a = -10; // fait la mme chose en assignation + a <<= 1; // -20 + a >>= 2; // -5 + a >>>= 1; // pert le signe + if ( a != 0x3ffffffd) 0/0; // + + Int x = 5/3; // division d'entiers + if ( x != 1 ) 0/0; + Int xx = 5.0/3.0; // division de rels, assign un entier + if ( xx != 1 ) 0/0; + + Int y = 0xF0035678; + if ( ~y != 0x0FFCA987 ) 0/0; // NOT bit bit + if ( (0x3456 ^ 0x54f0) != 0x60A6) // XOR bit bit + 0/0; + if ( (0x23 | 0x83) != 0xA3 ) 0/0; // OR bit bit + if ( (0x23 & 0x83) != 0x03 ) 0/0; // AND bit bit + + Int z = 0x0123; + z |= 0x8010; if ( z != 0x8133) 0/0; + z &= 0xF018; if ( z != 0x8010) 0/0; + z ^= 0xFF17; if ( z != 0x7F07) 0/0; + } + + { + // test pour les boolens + Boolean a, b= true, c = false; + a = b | c & b; + if ( a != b ) 0/0; + if ( !a ) 0/0; + if ( b ^ a ) 0/0; // XOR + if ( true || 0/0<1 ) {}; + if ( false && 0/0<1) {}; + // a ? "vrai" : "faux"; + } + + { + // petit test sur les nombres rels + Real x = 1. / 3, y = 0; + + if ( 3 * x != 1 ) x = x / y; // provoque une division par zro + else y = 1.123; + } + + + // test de dure + // attention, le programme de test ne stoppe qu' la fin d'excution + // bien que la boucle est interrompue plusieures fois + + // la boucle est plus rapide si elle est au dbut du programme ! + { + Int z = 10000; + while ( z > 0 ) z = z - 1; + } + +} + +void t() +{ + A_Faire(); + + show ( Somme ( 1, 2 ) ); + show ( Somme ( 1., 2 ) ); + show ( Somme ( 4.5, 2.7 ) ); +} + diff --git a/src/CBot/tests/old TstCBot/x.txt b/src/CBot/tests/old TstCBot/x.txt new file mode 100644 index 0000000..95856e0 --- /dev/null +++ b/src/CBot/tests/old TstCBot/x.txt @@ -0,0 +1,43 @@ +// test de l'interprteur CBot, (c) D. Dumoulin 2000 + +// pour l'instant, seule les primitives suivantes sont implmentes + +// { ... ; ... ; ... } un bloc d'instructions +// int x, y = 12, z; // dclaration de nombre entier +// float a, b= 2/3, c=b+1; // dclaration de nombres rels +// boolean tst = true; // dclaration d'un boolen +// String x = "hello"; // dclaration d'une chanes + +// z = x = x * y / ( z + 1 - x ); // assignation en chane et les 4 oprations + +// while ( x >= 0 ) x = x - 1; // boucle while, et test > >= < <= == != +// if ( x < y ) x = x + 1; // test si +// else y = y + 1; // sinon + +/* et les oprations suivantes: + + plus unaire x = +y; + - moins unaire x = -y; + + || OU logique + && ET logique + ! NOT logique + | OU bit bit + & ET bit bit + ^ XOR bit bit + ~ NON bit bit + +// les commentaires sont accepts +/* y compris les commentaires + sur plusieures lignes */ + + +{ +String str ; + +str = "abc" ; + +show (str) ; + +show( str = str + "+++" , ) ; + +} -- cgit v1.2.3-1-g7c22 From bc24b9f9e516e657fcc0034808e010287fc2e393 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 8 Aug 2012 22:35:17 +0200 Subject: Whitespace fix --- src/CBot/CBotAddExpr.cpp | 144 +-- src/CBot/CBotClass.cpp | 1236 +++++++++---------- src/CBot/CBotCompExpr.cpp | 172 +-- src/CBot/CBotFunction.cpp | 2744 +++++++++++++++++++++--------------------- src/CBot/CBotIf.cpp | 224 ++-- src/CBot/CBotProgram.cpp | 1462 +++++++++++----------- src/CBot/CBotString.cpp | 1384 ++++++++++----------- src/CBot/CBotToken.cpp | 1124 ++++++++--------- src/CBot/CBotToken.h | 76 +- src/CBot/CBotTwoOpExpr.cpp | 932 +++++++------- src/CBot/CBotWhile.cpp | 2012 +++++++++++++++---------------- src/CBot/ClassFILE.cpp | 856 ++++++------- src/CBot/StringFunctions.cpp | 494 ++++---- src/CBot/resource.h | 354 +++--- 14 files changed, 6607 insertions(+), 6607 deletions(-) diff --git a/src/CBot/CBotAddExpr.cpp b/src/CBot/CBotAddExpr.cpp index 8e4ed85..231f008 100644 --- a/src/CBot/CBotAddExpr.cpp +++ b/src/CBot/CBotAddExpr.cpp @@ -16,7 +16,7 @@ /////////////////////////////////////////////////// // expressions of type Operand1 + Operand2 -// Operand1 - Operand2 +// Operand1 - Operand2 #include "CBot.h" @@ -24,15 +24,15 @@ CBotAddExpr::CBotAddExpr() { - m_leftop = - m_rightop = NULL; // NULL to be able to delete without further - name = "CBotAddExpr"; // debug + m_leftop = + m_rightop = NULL; // NULL to be able to delete without further + name = "CBotAddExpr"; // debug } CBotAddExpr::~CBotAddExpr() { - delete m_leftop; - delete m_rightop; + delete m_leftop; + delete m_rightop; } @@ -40,54 +40,54 @@ CBotAddExpr::~CBotAddExpr() CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack) { - CBotStack* pStk = pStack->TokenStack(); // one end of stack please + CBotStack* pStk = pStack->TokenStack(); // one end of stack please - // looking statements that may be suitable to the left of the operation + or - + // looking statements that may be suitable to the left of the operation + or - - CBotInstr* left = CBotMulExpr::Compile( p, pStk ); // expression A * B left - if (left == NULL) return pStack->Return(NULL, pStk); // if error, transmit + CBotInstr* left = CBotMulExpr::Compile( p, pStk ); // expression A * B left + if (left == NULL) return pStack->Return(NULL, pStk); // if error, transmit - // do we have the token + or - next? + // do we have the token + or - next? - if ( p->GetType() == ID_ADD || - p->GetType() == ID_SUB) // more or less - { - CBotAddExpr* inst = new CBotAddExpr(); // element for operation - inst->SetToken(p); // stores the operation + if ( p->GetType() == ID_ADD || + p->GetType() == ID_SUB) // more or less + { + CBotAddExpr* inst = new CBotAddExpr(); // element for operation + inst->SetToken(p); // stores the operation - int type1, type2; - type1 = pStack->GetType(); // what kind of the first operand? + int type1, type2; + type1 = pStack->GetType(); // what kind of the first operand? - p = p->Next(); // skip the token of the operation + p = p->Next(); // skip the token of the operation - // looking statements that may be suitable for right + // looking statements that may be suitable for right - if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression (...) rigth - { - // there is an acceptable second operand + if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression (...) rigth + { + // there is an acceptable second operand - type2 = pStack->GetType(); // what kind of results? + type2 = pStack->GetType(); // what kind of results? - if ( type1 == type2 ) // are the results consistent ? - { - // ok so, saves the operand in the object - inst->m_leftop = left; - // and makes the object on demand - return pStack->Return(inst, pStk); - } - } + if ( type1 == type2 ) // are the results consistent ? + { + // ok so, saves the operand in the object + inst->m_leftop = left; + // and makes the object on demand + return pStack->Return(inst, pStk); + } + } - // in case of error, free the elements - delete left; - delete inst; - // and transmits the error that is on the stack - return pStack->Return(NULL, pStk); - } + // in case of error, free the elements + delete left; + delete inst; + // and transmits the error that is on the stack + return pStack->Return(NULL, pStk); + } - // if we are not dealing with an operation + or - + // if we are not dealing with an operation + or - // goes to that requested, the operand (left) found - // place the object "addition" - return pStack->Return(left, pStk); + // place the object "addition" + return pStack->Return(left, pStk); } @@ -97,48 +97,48 @@ CBotInstr* CBotAddExpr::Compile(CBotToken* &p, CBotStack* pStack) bool CBotAddExpr::Execute(CBotStack* &pStack) { - CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack - // or is found in case of recovery -// if ( pSk1 == EOX ) return TRUE; + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or is found in case of recovery +// if ( pSk1 == EOX ) return TRUE; - // according to recovery, it may be in one of two states + // according to recovery, it may be in one of two states - if ( pStk1->GetState() == 0 && // first state, evaluates the left operand - !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here? + if ( pStk1->GetState() == 0 && // first state, evaluates the left operand + !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here? - // passes to the next step - pStk1->SetState(1); // ready for further + // passes to the next step + pStk1->SetState(1); // ready for further - // requires a little more stack to not touch the result of the left - // which is on the stack, precisely. + // requires a little more stack to not touch the result of the left + // which is on the stack, precisely. - CBotStack* pStk2 = pStk1->AddStack(); // adds an item to the stack - // or is found in case of recovery + CBotStack* pStk2 = pStk1->AddStack(); // adds an item to the stack + // or is found in case of recovery - // Second state, evaluates the right operand - if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here? + // Second state, evaluates the right operand + if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here? - int type1 = pStk1->GetType(); // what kind of results? - int type2 = pStk2->GetType(); + int type1 = pStk1->GetType(); // what kind of results? + int type2 = pStk2->GetType(); - // creates a temporary variable to put the result - CBotVar* result = new CBotVar( NULL, MAX(type1, type2)); + // creates a temporary variable to put the result + CBotVar* result = new CBotVar( NULL, MAX(type1, type2)); - // is the operation as requested - switch (GetTokenType()) - { - case ID_ADD: - result->Add(pStk1->GetVar(), pStk2->GetVar()); // addition - break; - case ID_SUB: - result->Sub(pStk1->GetVar(), pStk2->GetVar()); // subtraction - break; - } - pStk2->SetVar(result); // puts the result on the stack + // is the operation as requested + switch (GetTokenType()) + { + case ID_ADD: + result->Add(pStk1->GetVar(), pStk2->GetVar()); // addition + break; + case ID_SUB: + result->Sub(pStk1->GetVar(), pStk2->GetVar()); // subtraction + break; + } + pStk2->SetVar(result); // puts the result on the stack - pStk1->Return(pStk2); // frees the stack - return pStack->Return(pStk1); // transmits the result + pStk1->Return(pStk2); // frees the stack + return pStack->Return(pStk1); // transmits the result } diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 692d3c7..a524a8e 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -25,349 +25,349 @@ CBotClass* CBotClass::m_ExClass = NULL; CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic) { - m_pParent = pPapa; - m_name = name; - m_pVar = NULL; - m_next = NULL; - m_pCalls = NULL; - m_pMethod = NULL; - m_rMaj = NULL; - m_IsDef = true; - m_bIntrinsic= bIntrinsic; - m_cptLock = 0; - m_cptOne = 0; - m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; + m_pParent = pPapa; + m_name = name; + m_pVar = NULL; + m_next = NULL; + m_pCalls = NULL; + m_pMethod = NULL; + m_rMaj = NULL; + m_IsDef = true; + m_bIntrinsic= bIntrinsic; + m_cptLock = 0; + m_cptOne = 0; + m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; - for ( int j= 0; j< 5 ; j++ ) - { - m_ProgInLock[j] = NULL; - } + for ( int j= 0; j< 5 ; j++ ) + { + m_ProgInLock[j] = NULL; + } - // is located alone in the list - if (m_ExClass) m_ExClass->m_ExPrev = this; - m_ExNext = m_ExClass; - m_ExPrev = NULL; - m_ExClass = this; + // is located alone in the list + if (m_ExClass) m_ExClass->m_ExPrev = this; + m_ExNext = m_ExClass; + m_ExPrev = NULL; + m_ExClass = this; } CBotClass::~CBotClass() { - // removes the list of class - if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; - else m_ExClass = m_ExNext; + // removes the list of class + if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; + else m_ExClass = m_ExNext; - if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; - m_ExPrev = NULL; - m_ExNext = NULL; + if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; + m_ExPrev = NULL; + m_ExNext = NULL; - delete m_pVar; - delete m_pCalls; - delete m_pMethod; + delete m_pVar; + delete m_pCalls; + delete m_pMethod; - delete m_next; // releases all of them on this level + delete m_next; // releases all of them on this level } void CBotClass::Free() { - while ( m_ExClass != NULL ) - { - delete m_ExClass; - } + while ( m_ExClass != NULL ) + { + delete m_ExClass; + } } void CBotClass::Purge() { - if ( this == NULL ) return; + if ( this == NULL ) return; - delete m_pVar; - m_pVar = NULL; - delete m_pCalls; - m_pCalls = NULL; - delete m_pMethod; - m_pMethod = NULL; - m_IsDef = false; + delete m_pVar; + m_pVar = NULL; + delete m_pCalls; + m_pCalls = NULL; + delete m_pMethod; + m_pMethod = NULL; + m_IsDef = false; - m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; + m_nbVar = m_pParent == NULL ? 0 : m_pParent->m_nbVar; - m_next->Purge(); - m_next = NULL; // no longer belongs to this chain + m_next->Purge(); + m_next = NULL; // no longer belongs to this chain } bool CBotClass::Lock(CBotProgram* p) { - int i = m_cptLock++; - - if ( i == 0 ) - { - m_cptOne = 1; - m_ProgInLock[0] = p; - return true; - } - if ( p == m_ProgInLock[0] ) - { - m_cptOne++; - m_cptLock--; // has already been counted - return true; - } - - for ( int j = 1 ; j <= i ; j++) - { - if ( p == m_ProgInLock[j] ) - { - m_cptLock--; - return false; // already pending - } - } - - if ( i < 5 ) // max 5 in query - { - m_ProgInLock[i] = p; // located in a queue - } - else - m_cptLock--; - - return false; + int i = m_cptLock++; + + if ( i == 0 ) + { + m_cptOne = 1; + m_ProgInLock[0] = p; + return true; + } + if ( p == m_ProgInLock[0] ) + { + m_cptOne++; + m_cptLock--; // has already been counted + return true; + } + + for ( int j = 1 ; j <= i ; j++) + { + if ( p == m_ProgInLock[j] ) + { + m_cptLock--; + return false; // already pending + } + } + + if ( i < 5 ) // max 5 in query + { + m_ProgInLock[i] = p; // located in a queue + } + else + m_cptLock--; + + return false; } void CBotClass::Unlock() { - if ( --m_cptOne > 0 ) return ; + if ( --m_cptOne > 0 ) return ; - int i = --m_cptLock; - if ( i<0 ) - { - m_cptLock = 0; - return; - } + int i = --m_cptLock; + if ( i<0 ) + { + m_cptLock = 0; + return; + } - for ( int j= 0; j< i ; j++ ) - { - m_ProgInLock[j] = m_ProgInLock[j+1]; - } - m_ProgInLock[i] = 0; + for ( int j= 0; j< i ; j++ ) + { + m_ProgInLock[j] = m_ProgInLock[j+1]; + } + m_ProgInLock[i] = 0; } void CBotClass::FreeLock(CBotProgram* p) { - CBotClass* pClass = m_ExClass; + CBotClass* pClass = m_ExClass; - while ( pClass != NULL ) - { - if ( p == pClass->m_ProgInLock[0] ) - { - pClass->m_cptLock -= pClass->m_cptOne; - pClass->m_cptOne = 0; - } + while ( pClass != NULL ) + { + if ( p == pClass->m_ProgInLock[0] ) + { + pClass->m_cptLock -= pClass->m_cptOne; + pClass->m_cptOne = 0; + } - for ( int j = 1; j < 5 ; j++ ) - if ( p == pClass->m_ProgInLock[j] ) - pClass->m_cptLock--; + for ( int j = 1; j < 5 ; j++ ) + if ( p == pClass->m_ProgInLock[j] ) + pClass->m_cptLock--; - pClass = pClass->m_ExNext; - } + pClass = pClass->m_ExNext; + } } bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) { - CBotToken token(name, CBotString()); - CBotClass* pClass = type.GivClass(); + CBotToken token(name, CBotString()); + CBotClass* pClass = type.GivClass(); - CBotVar* pVar = CBotVar::Create( name, type ); -/// pVar->SetUniqNum(CBotVar::NextUniqNum()); - pVar->SetPrivate( mPrivate ); + CBotVar* pVar = CBotVar::Create( name, type ); +/// pVar->SetUniqNum(CBotVar::NextUniqNum()); + pVar->SetPrivate( mPrivate ); - if ( pClass != NULL ) - { -// pVar->SetClass(pClass); - if ( type.Eq(CBotTypClass) ) - { - // adds a new statement for the object initialization - pVar->m_InitExpr = new CBotNew() ; - CBotToken nom( pClass->GivName() ); - pVar->m_InitExpr->SetToken(&nom); - } - } - return AddItem( pVar ); + if ( pClass != NULL ) + { +// pVar->SetClass(pClass); + if ( type.Eq(CBotTypClass) ) + { + // adds a new statement for the object initialization + pVar->m_InitExpr = new CBotNew() ; + CBotToken nom( pClass->GivName() ); + pVar->m_InitExpr->SetToken(&nom); + } + } + return AddItem( pVar ); } bool CBotClass::AddItem(CBotVar* pVar) { - pVar->SetUniqNum(++m_nbVar); + pVar->SetUniqNum(++m_nbVar); - if ( m_pVar == NULL ) m_pVar = pVar; - else m_pVar->AddNext(pVar); + if ( m_pVar == NULL ) m_pVar = pVar; + else m_pVar->AddNext(pVar); - return true; + return true; } void CBotClass::AddNext(CBotClass* pClass) { - CBotClass* p = this; - while (p->m_next != NULL) p = p->m_next; + CBotClass* p = this; + while (p->m_next != NULL) p = p->m_next; - p->m_next = pClass; + p->m_next = pClass; } CBotString CBotClass::GivName() { - return m_name; + return m_name; } CBotClass* CBotClass::GivParent() { - if ( this == NULL ) return NULL; - return m_pParent; + if ( this == NULL ) return NULL; + return m_pParent; } bool CBotClass::IsChildOf(CBotClass* pClass) { - CBotClass* p = this; - while ( p != NULL ) - { - if ( p == pClass ) return true; - p = p->m_pParent; - } - return false; + CBotClass* p = this; + while ( p != NULL ) + { + if ( p == pClass ) return true; + p = p->m_pParent; + } + return false; } CBotVar* CBotClass::GivVar() { - return m_pVar; + return m_pVar; } CBotVar* CBotClass::GivItem(const char* name) { - CBotVar* p = m_pVar; + CBotVar* p = m_pVar; - while ( p != NULL ) - { - if ( p->GivName() == name ) return p; - p = p->GivNext(); - } - if ( m_pParent != NULL ) return m_pParent->GivItem(name); - return NULL; + while ( p != NULL ) + { + if ( p->GivName() == name ) return p; + p = p->GivNext(); + } + if ( m_pParent != NULL ) return m_pParent->GivItem(name); + return NULL; } CBotVar* CBotClass::GivItemRef(int nIdent) { - CBotVar* p = m_pVar; + CBotVar* p = m_pVar; - while ( p != NULL ) - { - if ( p->GivUniqNum() == nIdent ) return p; - p = p->GivNext(); - } - if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); - return NULL; + while ( p != NULL ) + { + if ( p->GivUniqNum() == nIdent ) return p; + p = p->GivNext(); + } + if ( m_pParent != NULL ) return m_pParent->GivItemRef(nIdent); + return NULL; } bool CBotClass::IsIntrinsic() { - return m_bIntrinsic; + return m_bIntrinsic; } CBotClass* CBotClass::Find(CBotToken* &pToken) { - return Find(pToken->GivString()); + return Find(pToken->GivString()); } CBotClass* CBotClass::Find(const char* name) { - CBotClass* p = m_ExClass; + CBotClass* p = m_ExClass; - while ( p != NULL ) - { - if ( p->GivName() == name ) return p; - p = p->m_ExNext; - } + while ( p != NULL ) + { + if ( p->GivName() == name ) return p; + p = p->m_ExNext; + } - return NULL; + return NULL; } bool CBotClass::AddFunction(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) -{ - // stores pointers to the two functions - CBotCallMethode* p = m_pCalls; - CBotCallMethode* pp = NULL; - - while ( p != NULL ) - { - if ( name == p->GivName() ) - { - if ( pp == NULL ) m_pCalls = p->m_next; - else pp->m_next = p->m_next; - delete p; - break; - } - pp = p; - p = p->m_next; - } - - p = new CBotCallMethode(name, rExec, rCompile); - - if (m_pCalls == NULL) m_pCalls = p; - else m_pCalls->AddNext(p); // added to the list - - return true; + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) +{ + // stores pointers to the two functions + CBotCallMethode* p = m_pCalls; + CBotCallMethode* pp = NULL; + + while ( p != NULL ) + { + if ( name == p->GivName() ) + { + if ( pp == NULL ) m_pCalls = p->m_next; + else pp->m_next = p->m_next; + delete p; + break; + } + pp = p; + p = p->m_next; + } + + p = new CBotCallMethode(name, rExec, rCompile); + + if (m_pCalls == NULL) m_pCalls = p; + else m_pCalls->AddNext(p); // added to the list + + return true; } bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ) { - m_rMaj = rMaj; - return true; + m_rMaj = rMaj; + return true; } // compiles a method associated with an instance of class // the method can be declared by the user or AddFunction CBotTypResult CBotClass::CompileMethode(const char* name, - CBotVar* pThis, CBotVar** ppParams, - CBotCStack* pStack, long& nIdent) + CBotVar* pThis, CBotVar** ppParams, + CBotCStack* pStack, long& nIdent) { - nIdent = 0; // forget the previous one if necessary + nIdent = 0; // forget the previous one if necessary - // find the methods declared by AddFunction + // find the methods declared by AddFunction - CBotTypResult r = m_pCalls->CompileCall(name, pThis, ppParams, pStack, nIdent); - if ( r.GivType() >= 0) return r; + CBotTypResult r = m_pCalls->CompileCall(name, pThis, ppParams, pStack, nIdent); + if ( r.GivType() >= 0) return r; - // find the methods declared by user + // find the methods declared by user - r = m_pMethod->CompileCall(name, ppParams, nIdent); - if ( r.Eq(TX_UNDEFCALL) && m_pParent != NULL ) - return m_pParent->m_pMethod->CompileCall(name, ppParams, nIdent); - return r; + r = m_pMethod->CompileCall(name, ppParams, nIdent); + if ( r.Eq(TX_UNDEFCALL) && m_pParent != NULL ) + return m_pParent->m_pMethod->CompileCall(name, ppParams, nIdent); + return r; } // executes a method bool CBotClass::ExecuteMethode(long& nIdent, const char* name, - CBotVar* pThis, CBotVar** ppParams, - CBotVar* &pResult, CBotStack* &pStack, - CBotToken* pToken) + CBotVar* pThis, CBotVar** ppParams, + CBotVar* &pResult, CBotStack* &pStack, + CBotToken* pToken) { - int ret = m_pCalls->DoCall(nIdent, name, pThis, ppParams, pResult, pStack, pToken); - if (ret>=0) return ret; + int ret = m_pCalls->DoCall(nIdent, name, pThis, ppParams, pResult, pStack, pToken); + if (ret>=0) return ret; - ret = m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, this); - return ret; + ret = m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, this); + return ret; } // restored the execution stack void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, - CBotVar** ppParams, CBotStack* &pStack) + CBotVar** ppParams, CBotStack* &pStack) { - m_pMethod->RestoreCall(nIdent, name, pThis, ppParams, pStack, this); + m_pMethod->RestoreCall(nIdent, name, pThis, ppParams, pStack, this); } @@ -375,75 +375,75 @@ void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, bool CBotClass::SaveStaticState(FILE* pf) { - if (!WriteWord( pf, CBOTVERSION*2)) return false; + if (!WriteWord( pf, CBOTVERSION*2)) return false; - // saves the state of static variables in classes - CBotClass* p = m_ExClass; + // saves the state of static variables in classes + CBotClass* p = m_ExClass; - while ( p != NULL ) - { - if (!WriteWord( pf, 1)) return false; - // save the name of the class - if (!WriteString( pf, p->GivName() )) return false; + while ( p != NULL ) + { + if (!WriteWord( pf, 1)) return false; + // save the name of the class + if (!WriteString( pf, p->GivName() )) return false; - CBotVar* pv = p->GivVar(); - while( pv != NULL ) - { - if ( pv->IsStatic() ) - { - if (!WriteWord( pf, 1)) return false; - if (!WriteString( pf, pv->GivName() )) return false; + CBotVar* pv = p->GivVar(); + while( pv != NULL ) + { + if ( pv->IsStatic() ) + { + if (!WriteWord( pf, 1)) return false; + if (!WriteString( pf, pv->GivName() )) return false; - if ( !pv->Save0State(pf)) return false; // common header - if ( !pv->Save1State(pf) ) return false; // saves as the child class - if ( !WriteWord( pf, 0)) return false; - } - pv = pv->GivNext(); - } + if ( !pv->Save0State(pf)) return false; // common header + if ( !pv->Save1State(pf) ) return false; // saves as the child class + if ( !WriteWord( pf, 0)) return false; + } + pv = pv->GivNext(); + } - if (!WriteWord( pf, 0)) return false; - p = p->m_ExNext; - } + if (!WriteWord( pf, 0)) return false; + p = p->m_ExNext; + } - if (!WriteWord( pf, 0)) return false; - return true; + if (!WriteWord( pf, 0)) return false; + return true; } bool CBotClass::RestoreStaticState(FILE* pf) { - CBotString ClassName, VarName; - CBotClass* pClass; - unsigned short w; + CBotString ClassName, VarName; + CBotClass* pClass; + unsigned short w; - if (!ReadWord( pf, w )) return false; - if ( w != CBOTVERSION*2 ) return false; + if (!ReadWord( pf, w )) return false; + if ( w != CBOTVERSION*2 ) return false; - while (true) - { - if (!ReadWord( pf, w )) return false; - if ( w == 0 ) return true; + while (true) + { + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) return true; - if (!ReadString( pf, ClassName )) return false; - pClass = Find(ClassName); + if (!ReadString( pf, ClassName )) return false; + pClass = Find(ClassName); - while (true) - { - if (!ReadWord( pf, w )) return false; - if ( w == 0 ) break; + while (true) + { + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) break; - CBotVar* pVar = NULL; - CBotVar* pv = NULL; + CBotVar* pVar = NULL; + CBotVar* pv = NULL; - if (!ReadString( pf, VarName )) return false; - if ( pClass != NULL ) pVar = pClass->GivItem(VarName); + if (!ReadString( pf, VarName )) return false; + if ( pClass != NULL ) pVar = pClass->GivItem(VarName); - if (!CBotVar::RestoreState(pf, pv)) return false; // the temp variable + if (!CBotVar::RestoreState(pf, pv)) return false; // the temp variable - if ( pVar != NULL ) pVar->Copy(pv); - delete pv; - } - } - return true; + if ( pVar != NULL ) pVar->Copy(pv); + delete pv; + } + } + return true; } @@ -451,19 +451,19 @@ bool CBotClass::RestoreStaticState(FILE* pf) CBotClassInst::CBotClassInst() { - m_next = NULL; - m_var = NULL; - m_Parameters = NULL; - m_expr = NULL; - m_hasParams = false; - m_nMethodeIdent = 0; - name = "CBotClassInst"; + m_next = NULL; + m_var = NULL; + m_Parameters = NULL; + m_expr = NULL; + m_hasParams = false; + m_nMethodeIdent = 0; + name = "CBotClassInst"; } CBotClassInst::~CBotClassInst() { - delete m_var; -// delete m_next; // done by the destructor of the base class ~CBotInstr() + delete m_var; +// delete m_next; // done by the destructor of the base class ~CBotInstr() } // definition of pointer (s) to an object @@ -472,388 +472,388 @@ CBotClassInst::~CBotClassInst() CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) { - // seeks the corresponding classes - if ( pClass == NULL ) - { - pStack->SetStartError(p->GivStart()); - pClass = CBotClass::Find(p); - if ( pClass == NULL ) - { - // not found? is bizare - pStack->SetError(TX_NOCLASS, p); - return NULL; - } - p = p->GivNext(); - } - - bool bIntrinsic = pClass->IsIntrinsic(); - CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); - CBotClassInst* inst = (CBotClassInst*)CompileArray(p, pStack, type); - if ( inst != NULL || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(); - - inst = new CBotClassInst(); + // seeks the corresponding classes + if ( pClass == NULL ) + { + pStack->SetStartError(p->GivStart()); + pClass = CBotClass::Find(p); + if ( pClass == NULL ) + { + // not found? is bizare + pStack->SetError(TX_NOCLASS, p); + return NULL; + } + p = p->GivNext(); + } + + bool bIntrinsic = pClass->IsIntrinsic(); + CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); + CBotClassInst* inst = (CBotClassInst*)CompileArray(p, pStack, type); + if ( inst != NULL || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(); + + inst = new CBotClassInst(); /// \TODO Need to be revised and fixed after adding unit tests CBotToken token(pClass->GivName(), CBotString(), p->GivStart(), p->GivEnd()); - inst->SetToken(&token); - CBotToken* vartoken = p; - - if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - ((CBotLeftExprVar*)inst->m_var)->m_typevar = type; - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable - { - pStk->SetStartError(vartoken->GivStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) // with any clues? - { - delete inst; // is not type CBotInt - p = vartoken; // returns to the variable name - - // compiles declaration an array - - inst = (CBotClassInst*)CBotInstArray::Compile( p, pStk, type ); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GivStart()); - goto error; - } - goto suite; // no assignment, variable already created - } - - - CBotVar* var; - var = CBotVar::Create(vartoken->GivString(), type); // creates the instance -// var->SetClass(pClass); - var->SetUniqNum( - ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); - // its attribute a unique number - pStack->AddVar(var); // placed on the stack - - // look if there are parameters - inst->m_hasParams = (p->GivType() == ID_OPENPAR); - - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStk, ppVars); - if ( !pStk->IsOk() ) goto error; - - // if there are parameters, is the equivalent to the stament "new" - // CPoint A ( 0, 0 ) is equivalent to - // CPoint A = new CPoint( 0, 0 ) - -// if ( NULL != inst->m_Parameters ) - if ( inst->m_hasParams ) - { - // the constructor is there? -// CBotString noname; - CBotTypResult r = pClass->CompileMethode(pClass->GivName(), var, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // releases the supplement stack - int typ = r.GivType(); - - if (typ == TX_UNDEFCALL) - { - // si le constructeur n'existe pas - if (inst->m_Parameters != NULL) // with parameters - { - pStk->SetError(TX_NOCONST, vartoken); - goto error; - } - typ = 0; - } - - if (typ>20) - { - pStk->SetError(typ, vartoken->GivEnd()); - goto error; - } - - } - - if (IsOfType(p, ID_ASS)) // with a assignment? - { - if (inst->m_hasParams) - { - pStk->SetError(TX_ENDOF, p->GivStart()); - goto error; - } - - if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - CBotClass* result = pStk->GivClass(); - if ( !pStk->GivTypResult(1).Eq(CBotTypNullPointer) && - ( !pStk->GivTypResult(1).Eq(CBotTypPointer) || - ( result != NULL && !pClass->IsChildOf(result) ))) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GivStart()); - goto error; - } -// if ( !bIntrinsic ) var->SetPointer(pStk->GivVar()->GivPointer()); - if ( !bIntrinsic ) - { - // does not use the result on the stack, to impose the class - CBotVar* pvar = CBotVar::Create("", pClass); - var->SetPointer( pvar ); // variable already declared instance pointer - delete pvar; // removes the second pointer - } - var->SetInit(true); // marks the pointer as init - } - else if (inst->m_hasParams) - { - // creates the object on the "job" (\TODO "tas") - // with a pointer to the object - if ( !bIntrinsic ) - { - CBotVar* pvar = CBotVar::Create("", pClass); - var->SetPointer( pvar ); // variable already declared instance pointer - delete pvar; // removes the second pointer - } - var->SetInit(2); // marks the pointer as init - } + inst->SetToken(&token); + CBotToken* vartoken = p; + + if ( NULL != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + ((CBotLeftExprVar*)inst->m_var)->m_typevar = type; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetStartError(vartoken->GivStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GivEnd()); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) // with any clues? + { + delete inst; // is not type CBotInt + p = vartoken; // returns to the variable name + + // compiles declaration an array + + inst = (CBotClassInst*)CBotInstArray::Compile( p, pStk, type ); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GivStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + + CBotVar* var; + var = CBotVar::Create(vartoken->GivString(), type); // creates the instance +// var->SetClass(pClass); + var->SetUniqNum( + ((CBotLeftExprVar*)inst->m_var)->m_nIdent = CBotVar::NextUniqNum()); + // its attribute a unique number + pStack->AddVar(var); // placed on the stack + + // look if there are parameters + inst->m_hasParams = (p->GivType() == ID_OPENPAR); + + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStk, ppVars); + if ( !pStk->IsOk() ) goto error; + + // if there are parameters, is the equivalent to the stament "new" + // CPoint A ( 0, 0 ) is equivalent to + // CPoint A = new CPoint( 0, 0 ) + +// if ( NULL != inst->m_Parameters ) + if ( inst->m_hasParams ) + { + // the constructor is there? +// CBotString noname; + CBotTypResult r = pClass->CompileMethode(pClass->GivName(), var, ppVars, pStk, inst->m_nMethodeIdent); + delete pStk->TokenStack(); // releases the supplement stack + int typ = r.GivType(); + + if (typ == TX_UNDEFCALL) + { + // si le constructeur n'existe pas + if (inst->m_Parameters != NULL) // with parameters + { + pStk->SetError(TX_NOCONST, vartoken); + goto error; + } + typ = 0; + } + + if (typ>20) + { + pStk->SetError(typ, vartoken->GivEnd()); + goto error; + } + + } + + if (IsOfType(p, ID_ASS)) // with a assignment? + { + if (inst->m_hasParams) + { + pStk->SetError(TX_ENDOF, p->GivStart()); + goto error; + } + + if ( NULL == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + CBotClass* result = pStk->GivClass(); + if ( !pStk->GivTypResult(1).Eq(CBotTypNullPointer) && + ( !pStk->GivTypResult(1).Eq(CBotTypPointer) || + ( result != NULL && !pClass->IsChildOf(result) ))) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GivStart()); + goto error; + } +// if ( !bIntrinsic ) var->SetPointer(pStk->GivVar()->GivPointer()); + if ( !bIntrinsic ) + { + // does not use the result on the stack, to impose the class + CBotVar* pvar = CBotVar::Create("", pClass); + var->SetPointer( pvar ); // variable already declared instance pointer + delete pvar; // removes the second pointer + } + var->SetInit(true); // marks the pointer as init + } + else if (inst->m_hasParams) + { + // creates the object on the "job" (\TODO "tas") + // with a pointer to the object + if ( !bIntrinsic ) + { + CBotVar* pvar = CBotVar::Create("", pClass); + var->SetPointer( pvar ); // variable already declared instance pointer + delete pvar; // removes the second pointer + } + var->SetInit(2); // marks the pointer as init + } suite: - if (IsOfType(p, ID_COMMA)) // several chained definitions - { - if ( NULL != ( inst->m_next = CBotClassInst::Compile(p, pStk, pClass) )) // compiles the following - { - return pStack->Return(inst, pStk); - } - } - - if (IsOfType(p, ID_SEP)) // complete instruction - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GivStart()); - } + if (IsOfType(p, ID_COMMA)) // several chained definitions + { + if ( NULL != ( inst->m_next = CBotClassInst::Compile(p, pStk, pClass) )) // compiles the following + { + return pStack->Return(inst, pStk); + } + } + + if (IsOfType(p, ID_SEP)) // complete instruction + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GivStart()); + } error: - delete inst; - return pStack->Return(NULL, pStk); + delete inst; + return pStack->Return(NULL, pStk); } // declaration of the instance of a class, for example: -// CPoint A, B; +// CPoint A, B; bool CBotClassInst::Execute(CBotStack* &pj) { - CBotVar* pThis = NULL; - - CBotStack* pile = pj->AddStack(this);//essential for SetState() -// if ( pile == EOX ) return true; - - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - - bool bIntrincic = pClass->IsIntrinsic(); - - // creates the variable of type pointer to the object - - if ( pile->GivState()==0) - { - CBotString name = m_var->m_token.GivString(); - if ( bIntrincic ) - { - pThis = CBotVar::Create(name, CBotTypResult( CBotTypIntrinsic, pClass )); - } - else - { - pThis = CBotVar::Create(name, CBotTypResult( CBotTypPointer, pClass )); - } - - pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // its attribute as unique number - pile->AddVar(pThis); // place on the stack - pile->IncState(); - } - - if ( pThis == NULL ) pThis = pile->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); - - if ( pile->GivState()<3) - { - // ss there an assignment or parameters (contructor) + CBotVar* pThis = NULL; + + CBotStack* pile = pj->AddStack(this);//essential for SetState() +// if ( pile == EOX ) return true; + + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + + bool bIntrincic = pClass->IsIntrinsic(); + + // creates the variable of type pointer to the object + + if ( pile->GivState()==0) + { + CBotString name = m_var->m_token.GivString(); + if ( bIntrincic ) + { + pThis = CBotVar::Create(name, CBotTypResult( CBotTypIntrinsic, pClass )); + } + else + { + pThis = CBotVar::Create(name, CBotTypResult( CBotTypPointer, pClass )); + } + + pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // its attribute as unique number + pile->AddVar(pThis); // place on the stack + pile->IncState(); + } + + if ( pThis == NULL ) pThis = pile->FindVar(((CBotLeftExprVar*)m_var)->m_nIdent); + + if ( pile->GivState()<3) + { + // ss there an assignment or parameters (contructor) -// CBotVarClass* pInstance = NULL; +// CBotVarClass* pInstance = NULL; - if ( m_expr != NULL ) - { - // evaluates the expression for the assignment - if (!m_expr->Execute(pile)) return false; - - if ( bIntrincic ) - { - CBotVar* pv = pile->GivVar(); - if ( pv == NULL || pv->GivPointer() == NULL ) - { - pile->SetError(TX_NULLPT, &m_token); - return pj->Return(pile); - } - pThis->Copy(pile->GivVar(), false); - } - else - { - CBotVarClass* pInstance; - pInstance = ((CBotVarPointer*)pile->GivVar())->GivPointer(); // value for the assignment - pThis->SetPointer(pInstance); - } - pThis->SetInit(true); - } + if ( m_expr != NULL ) + { + // evaluates the expression for the assignment + if (!m_expr->Execute(pile)) return false; + + if ( bIntrincic ) + { + CBotVar* pv = pile->GivVar(); + if ( pv == NULL || pv->GivPointer() == NULL ) + { + pile->SetError(TX_NULLPT, &m_token); + return pj->Return(pile); + } + pThis->Copy(pile->GivVar(), false); + } + else + { + CBotVarClass* pInstance; + pInstance = ((CBotVarPointer*)pile->GivVar())->GivPointer(); // value for the assignment + pThis->SetPointer(pInstance); + } + pThis->SetInit(true); + } - else if ( m_hasParams ) - { - // evaluates the constructor of an instance + else if ( m_hasParams ) + { + // evaluates the constructor of an instance - if ( !bIntrincic && pile->GivState() == 1) - { - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); + if ( !bIntrincic && pile->GivState() == 1) + { + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); - // creates an instance of the requested class + // creates an instance of the requested class - CBotVarClass* pInstance; - pInstance = (CBotVarClass*)CBotVar::Create("", pClass); - pThis->SetPointer(pInstance); - delete pInstance; + CBotVarClass* pInstance; + pInstance = (CBotVarClass*)CBotVar::Create("", pClass); + pThis->SetPointer(pInstance); + delete pInstance; - pile->IncState(); - } + pile->IncState(); + } - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; - int i = 0; + int i = 0; - CBotInstr* p = m_Parameters; - // evaluates the parameters - // and places the values ​​on the stack - // to (can) be interrupted (broken) at any time + CBotInstr* p = m_Parameters; + // evaluates the parameters + // and places the values ​​on the stack + // to (can) be interrupted (broken) at any time - if ( p != NULL) while ( true ) - { - pile2 = pile2->AddStack(); // place on the stack for the results - if ( pile2->GivState() == 0 ) - { - if (!p->Execute(pile2)) return false; // interrupted here? - pile2->SetState(1); - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; + if ( p != NULL) while ( true ) + { + pile2 = pile2->AddStack(); // place on the stack for the results + if ( pile2->GivState() == 0 ) + { + if (!p->Execute(pile2)) return false; // interrupted here? + pile2->SetState(1); + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; - // creates a variable for the result - CBotVar* pResult = NULL; // constructor still void - - if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), - pThis, ppVars, - pResult, pile2, GivToken())) return false; // interrupt - - pThis->SetInit(true); - pThis->ConstructorSet(); // indicates that the constructor has been called - pile->Return(pile2); // releases a piece of stack - -// pInstance = pThis->GivPointer(); - - } - -// if ( !bIntrincic ) pThis->SetPointer(pInstance); // a pointer to the instance + // creates a variable for the result + CBotVar* pResult = NULL; // constructor still void + + if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GivName(), + pThis, ppVars, + pResult, pile2, GivToken())) return false; // interrupt + + pThis->SetInit(true); + pThis->ConstructorSet(); // indicates that the constructor has been called + pile->Return(pile2); // releases a piece of stack + +// pInstance = pThis->GivPointer(); + + } + +// if ( !bIntrincic ) pThis->SetPointer(pInstance); // a pointer to the instance - pile->SetState(3); // finished this part - } + pile->SetState(3); // finished this part + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - if ( m_next2b != NULL && - !m_next2b->Execute(pile)) return false; // other (s) definition (s) + if ( m_next2b != NULL && + !m_next2b->Execute(pile)) return false; // other (s) definition (s) - return pj->Return( pile ); // transmits below (further) + return pj->Return( pile ); // transmits below (further) } void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) { - CBotVar* pThis = NULL; + CBotVar* pThis = NULL; - CBotStack* pile = pj; - if ( bMain ) pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj; + if ( bMain ) pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - // creates the variable of type pointer to the object - { - CBotString name = m_var->m_token.GivString(); - pThis = pile->FindVar(name); - pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // its attribute a unique number - } + // creates the variable of type pointer to the object + { + CBotString name = m_var->m_token.GivString(); + pThis = pile->FindVar(name); + pThis->SetUniqNum(((CBotLeftExprVar*)m_var)->m_nIdent); // its attribute a unique number + } - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - bool bIntrincic = pClass->IsIntrinsic(); + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + bool bIntrincic = pClass->IsIntrinsic(); - if ( bMain && pile->GivState()<3) - { - // is there an assignment or parameters (constructor) + if ( bMain && pile->GivState()<3) + { + // is there an assignment or parameters (constructor) -// CBotVarClass* pInstance = NULL; +// CBotVarClass* pInstance = NULL; - if ( m_expr != NULL ) - { - // evaluates the expression for the assignment - m_expr->RestoreState(pile, bMain); - return; - } + if ( m_expr != NULL ) + { + // evaluates the expression for the assignment + m_expr->RestoreState(pile, bMain); + return; + } - else if ( m_hasParams ) - { - // evaluates the constructor of an instance + else if ( m_hasParams ) + { + // evaluates the constructor of an instance - if ( !bIntrincic && pile->GivState() == 1) - { - return; - } + if ( !bIntrincic && pile->GivState() == 1) + { + return; + } - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; - int i = 0; + int i = 0; - CBotInstr* p = m_Parameters; - // evaluates the parameters - // and the values an the stack - // for the ability to be interrupted at any time (\TODO pour pouvoir être interrompu n'importe quand) + CBotInstr* p = m_Parameters; + // evaluates the parameters + // and the values an the stack + // for the ability to be interrupted at any time (\TODO pour pouvoir être interrompu n'importe quand) - if ( p != NULL) while ( true ) - { - pile2 = pile2->RestoreStack(); // place on the stack for the results - if ( pile2 == NULL ) return; + if ( p != NULL) while ( true ) + { + pile2 = pile2->RestoreStack(); // place on the stack for the results + if ( pile2 == NULL ) return; - if ( pile2->GivState() == 0 ) - { - p->RestoreState(pile2, bMain); // interrupted here? - return; - } - ppVars[i++] = pile2->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; + if ( pile2->GivState() == 0 ) + { + p->RestoreState(pile2, bMain); // interrupted here? + return; + } + ppVars[i++] = pile2->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; - // creates a variable for the result - CBotVar* pResult = NULL; // constructor still void + // creates a variable for the result + CBotVar* pResult = NULL; // constructor still void - pClass->RestoreMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, pile2); - return; - } - } + pClass->RestoreMethode(m_nMethodeIdent, pClass->GivName(), pThis, ppVars, pile2); + return; + } + } - if ( m_next2b != NULL ) - m_next2b->RestoreState(pile, bMain); // other(s) definition(s) + if ( m_next2b != NULL ) + m_next2b->RestoreState(pile, bMain); // other(s) definition(s) } @@ -861,22 +861,22 @@ void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { - CBotString name = pToken->GivString(); + CBotString name = pToken->GivString(); - if ( CBotCall::CheckCall(name) ) return true; + if ( CBotCall::CheckCall(name) ) return true; - CBotFunction* pp = m_pMethod; - while ( pp != NULL ) - { - if ( pToken->GivString() == pp->GivName() ) - { - // are their parameters exactly the same? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->Next(); - } + CBotFunction* pp = m_pMethod; + while ( pp != NULL ) + { + if ( pToken->GivString() == pp->GivName() ) + { + // are their parameters exactly the same? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->Next(); + } - return false; + return false; } diff --git a/src/CBot/CBotCompExpr.cpp b/src/CBot/CBotCompExpr.cpp index 8ae507f..2daf53f 100644 --- a/src/CBot/CBotCompExpr.cpp +++ b/src/CBot/CBotCompExpr.cpp @@ -16,7 +16,7 @@ /////////////////////////////////////////////////// // expression of type Opérande1 > Opérande2 -// Opérande1 != Opérande2 +// Opérande1 != Opérande2 // etc. #include "CBot.h" @@ -25,15 +25,15 @@ CBotCompExpr::CBotCompExpr() { - m_leftop = - m_rightop = NULL; - name = "CBotCompExpr"; + m_leftop = + m_rightop = NULL; + name = "CBotCompExpr"; } CBotCompExpr::~CBotCompExpr() { - delete m_leftop; - delete m_rightop; + delete m_leftop; + delete m_rightop; } fichier plus utilise; @@ -42,44 +42,44 @@ fichier plus utilise; CBotInstr* CBotCompExpr::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCStack* pStk = pStack->AddStack(); - - CBotInstr* left = CBotAddExpr::Compile( p, pStk ); // expression A + B left - if (left == NULL) return pStack->Return(NULL, pStk); // error - - if ( p->GetType() == ID_HI || - p->GetType() == ID_LO || - p->GetType() == ID_HS || - p->GetType() == ID_LS || - p->GetType() == ID_EQ || - p->GetType() == ID_NE) // the various comparisons - { - CBotCompExpr* inst = new CBotCompExpr(); // element for operation - inst->SetToken(p); // stores the operation - - int type1, type2; - type1 = pStack->GetType(); - - p = p->Next(); - if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression A + B right - { - type2 = pStack->GetType(); - // are the results compatible - if ( type1 == type2 ) - { - inst->m_leftop = left; - pStk->SetVar(new CBotVar(NULL, CBotTypBoolean)); - // the result is a boolean - return pStack->Return(inst, pStk); - } - } - - delete left; - delete inst; - return pStack->Return(NULL, pStk); - } - - return pStack->Return(left, pStk); + CBotCStack* pStk = pStack->AddStack(); + + CBotInstr* left = CBotAddExpr::Compile( p, pStk ); // expression A + B left + if (left == NULL) return pStack->Return(NULL, pStk); // error + + if ( p->GetType() == ID_HI || + p->GetType() == ID_LO || + p->GetType() == ID_HS || + p->GetType() == ID_LS || + p->GetType() == ID_EQ || + p->GetType() == ID_NE) // the various comparisons + { + CBotCompExpr* inst = new CBotCompExpr(); // element for operation + inst->SetToken(p); // stores the operation + + int type1, type2; + type1 = pStack->GetType(); + + p = p->Next(); + if ( NULL != (inst->m_rightop = CBotAddExpr::Compile( p, pStk )) ) // expression A + B right + { + type2 = pStack->GetType(); + // are the results compatible + if ( type1 == type2 ) + { + inst->m_leftop = left; + pStk->SetVar(new CBotVar(NULL, CBotTypBoolean)); + // the result is a boolean + return pStack->Return(inst, pStk); + } + } + + delete left; + delete inst; + return pStack->Return(NULL, pStk); + } + + return pStack->Return(left, pStk); } @@ -87,47 +87,47 @@ CBotInstr* CBotCompExpr::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotCompExpr::Execute(CBotStack* &pStack) { - CBotStack* pStk1 = pStack->AddStack(this); -// if ( pStk1 == EOX ) return TRUE; - - if ( pStk1->GetState() == 0 && !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here ? - - pStk1->SetState(1); // finished - - // requires a little more stack to not touch the result of the left - CBotStack* pStk2 = pStk1->AddStack(); - - if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here ? - - int type1 = pStk1->GetType(); - int type2 = pStk2->GetType(); - - CBotVar* result = new CBotVar( NULL, CBotTypBoolean ); - - switch (GetTokenType()) - { - case ID_LO: - result->Lo(pStk1->GetVar(), pStk2->GetVar()); // lower - break; - case ID_HI: - result->Hi(pStk1->GetVar(), pStk2->GetVar()); // higher - break; - case ID_LS: - result->Ls(pStk1->GetVar(), pStk2->GetVar()); // lower or equal - break; - case ID_HS: - result->Hs(pStk1->GetVar(), pStk2->GetVar()); // higher of equal - break; - case ID_EQ: - result->Eq(pStk1->GetVar(), pStk2->GetVar()); // equal - break; - case ID_NE: - result->Ne(pStk1->GetVar(), pStk2->GetVar()); // not equal - break; - } - pStk2->SetVar(result); // puts the result on the stack - - pStk1->Return(pStk2); // frees the stack - return pStack->Return(pStk1); // transmit the result + CBotStack* pStk1 = pStack->AddStack(this); +// if ( pStk1 == EOX ) return TRUE; + + if ( pStk1->GetState() == 0 && !m_leftop->Execute(pStk1) ) return FALSE; // interrupted here ? + + pStk1->SetState(1); // finished + + // requires a little more stack to not touch the result of the left + CBotStack* pStk2 = pStk1->AddStack(); + + if ( !m_rightop->Execute(pStk2) ) return FALSE; // interrupted here ? + + int type1 = pStk1->GetType(); + int type2 = pStk2->GetType(); + + CBotVar* result = new CBotVar( NULL, CBotTypBoolean ); + + switch (GetTokenType()) + { + case ID_LO: + result->Lo(pStk1->GetVar(), pStk2->GetVar()); // lower + break; + case ID_HI: + result->Hi(pStk1->GetVar(), pStk2->GetVar()); // higher + break; + case ID_LS: + result->Ls(pStk1->GetVar(), pStk2->GetVar()); // lower or equal + break; + case ID_HS: + result->Hs(pStk1->GetVar(), pStk2->GetVar()); // higher of equal + break; + case ID_EQ: + result->Eq(pStk1->GetVar(), pStk2->GetVar()); // equal + break; + case ID_NE: + result->Ne(pStk1->GetVar(), pStk2->GetVar()); // not equal + break; + } + pStk2->SetVar(result); // puts the result on the stack + + pStk1->Return(pStk2); // frees the stack + return pStack->Return(pStk1); // transmit the result } diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 756c6cb..1c94c1b 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -25,147 +25,147 @@ // pour libérer tout selon l'arbre établi CBotFunction::CBotFunction() { - m_Param = NULL; // empty parameter list - m_Block = NULL; // the instruction block - m_next = NULL; // functions can be chained - m_bPublic = false; // function not public - m_bExtern = false; // function not extern - m_nextpublic = NULL; - m_prevpublic = NULL; - m_pProg = NULL; -// m_nThisIdent = 0; - m_nFuncIdent = 0; - m_bSynchro = false; + m_Param = NULL; // empty parameter list + m_Block = NULL; // the instruction block + m_next = NULL; // functions can be chained + m_bPublic = false; // function not public + m_bExtern = false; // function not extern + m_nextpublic = NULL; + m_prevpublic = NULL; + m_pProg = NULL; +// m_nThisIdent = 0; + m_nFuncIdent = 0; + m_bSynchro = false; } CBotFunction* CBotFunction::m_listPublic = NULL; CBotFunction::~CBotFunction() { - delete m_Param; // empty parameter list - delete m_Block; // the instruction block - delete m_next; - - // remove public list if there is - if ( m_bPublic ) - { - if ( m_nextpublic != NULL ) - { - m_nextpublic->m_prevpublic = m_prevpublic; - } - if ( m_prevpublic != NULL) - { - m_prevpublic->m_nextpublic = m_nextpublic; - } - else - { - // if prev = next = null may not be in the list! - if ( m_listPublic == this ) m_listPublic = m_nextpublic; - } - } + delete m_Param; // empty parameter list + delete m_Block; // the instruction block + delete m_next; + + // remove public list if there is + if ( m_bPublic ) + { + if ( m_nextpublic != NULL ) + { + m_nextpublic->m_prevpublic = m_prevpublic; + } + if ( m_prevpublic != NULL) + { + m_prevpublic->m_nextpublic = m_nextpublic; + } + else + { + // if prev = next = null may not be in the list! + if ( m_listPublic == this ) m_listPublic = m_nextpublic; + } + } } bool CBotFunction::IsPublic() { - return m_bPublic; + return m_bPublic; } bool CBotFunction::IsExtern() { - return m_bExtern; + return m_bExtern; } bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop) { - start = m_extern.GivStart(); - stop = m_closeblk.GivEnd(); - - if (modestart == GetPosExtern) - { - start = m_extern.GivStart(); - } - if (modestop == GetPosExtern) - { - stop = m_extern.GivEnd(); - } - if (modestart == GetPosNom) - { - start = m_token.GivStart(); - } - if (modestop == GetPosNom) - { - stop = m_token.GivEnd(); - } - if (modestart == GetPosParam) - { - start = m_openpar.GivStart(); - } - if (modestop == GetPosParam) - { - stop = m_closepar.GivEnd(); - } - if (modestart == GetPosBloc) - { - start = m_openblk.GivStart(); - } - if (modestop == GetPosBloc) - { - stop = m_closeblk.GivEnd(); - } - - return true; + start = m_extern.GivStart(); + stop = m_closeblk.GivEnd(); + + if (modestart == GetPosExtern) + { + start = m_extern.GivStart(); + } + if (modestop == GetPosExtern) + { + stop = m_extern.GivEnd(); + } + if (modestart == GetPosNom) + { + start = m_token.GivStart(); + } + if (modestop == GetPosNom) + { + stop = m_token.GivEnd(); + } + if (modestart == GetPosParam) + { + start = m_openpar.GivStart(); + } + if (modestop == GetPosParam) + { + stop = m_closepar.GivEnd(); + } + if (modestart == GetPosBloc) + { + start = m_openblk.GivStart(); + } + if (modestop == GetPosBloc) + { + stop = m_closeblk.GivEnd(); + } + + return true; } -CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) +CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) { - while ( IsOfType( p, ID_OPBRK ) ) - { - if ( !IsOfType( p, ID_CLBRK ) ) - { - pile->SetError(TX_CLBRK, p->GivStart()); - return CBotTypResult( -1 ); - } - type = CBotTypResult( CBotTypArrayPointer, type ); - } - return type; + while ( IsOfType( p, ID_OPBRK ) ) + { + if ( !IsOfType( p, ID_CLBRK ) ) + { + pile->SetError(TX_CLBRK, p->GivStart()); + return CBotTypResult( -1 ); + } + type = CBotTypResult( CBotTypArrayPointer, type ); + } + return type; } -CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) +CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) { - CBotClass* pClass = NULL; - - switch (p->GivType()) - { - case ID_INT: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypInt )); - case ID_FLOAT: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypFloat )); - case ID_BOOLEAN: - case ID_BOOL: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypBoolean )); - case ID_STRING: - p = p->GivNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypString )); - case ID_VOID: - p = p->GivNext(); - return CBotTypResult( 0 ); - - case TokenTypVar: - pClass = CBotClass::Find(p); - if ( pClass != NULL) - { - p = p->GivNext(); - return ArrayType(p, pile, - pClass->IsIntrinsic() ? - CBotTypResult( CBotTypIntrinsic, pClass ) : - CBotTypResult( CBotTypPointer, pClass ) ); - } - } - return CBotTypResult( -1 ); + CBotClass* pClass = NULL; + + switch (p->GivType()) + { + case ID_INT: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypInt )); + case ID_FLOAT: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypFloat )); + case ID_BOOLEAN: + case ID_BOOL: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypBoolean )); + case ID_STRING: + p = p->GivNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypString )); + case ID_VOID: + p = p->GivNext(); + return CBotTypResult( 0 ); + + case TokenTypVar: + pClass = CBotClass::Find(p); + if ( pClass != NULL) + { + p = p->GivNext(); + return ArrayType(p, pile, + pClass->IsIntrinsic() ? + CBotTypResult( CBotTypIntrinsic, pClass ) : + CBotTypResult( CBotTypPointer, pClass ) ); + } + } + return CBotTypResult( -1 ); } // compiles a new function @@ -173,297 +173,297 @@ CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) // as the elements belonging to the class for methods CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, bool bLocal) { - CBotToken* pp; - CBotFunction* func = finput; - if ( func == NULL ) func = new CBotFunction(); - - CBotCStack* pStk = pStack->TokenStack(p, bLocal); - -// func->m_nFuncIdent = CBotVar::NextUniqNum(); - - while (true) - { - if ( IsOfType(p, ID_PUBLIC) ) - { - func->m_bPublic = true; - continue; - } - pp = p; - if ( IsOfType(p, ID_EXTERN) ) - { - func->m_extern = pp; // for the position of the word "extern" - func->m_bExtern = true; -// func->m_bPublic = true; // therefore also public! - continue; - } - break; - } - - func->m_retToken = *p; -// CBotClass* pClass; - func->m_retTyp = TypeParam(p, pStk); // type of the result - - if (func->m_retTyp.GivType() >= 0) - { - CBotToken* pp = p; - func->m_token = *p; - - if ( IsOfType(p, ID_NOT) ) - { - CBotToken d("~" + p->GivString()); - func->m_token = d; - } - - // un nom de fonction est-il là ? - if (IsOfType(p, TokenTypVar)) - { - if ( IsOfType( p, ID_DBLDOTS ) ) // method for a class - { - func->m_MasterClass = pp->GivString(); - CBotClass* pClass = CBotClass::Find(pp); - if ( pClass == NULL ) goto bad; - -// pp = p; - func->m_token = *p; - if (!IsOfType(p, TokenTypVar)) goto bad; - - } - func->m_openpar = p; - func->m_Param = CBotDefParam::Compile( p, pStk ); - func->m_closepar = p->GivPrev(); - if (pStk->IsOk()) - { - pStk->SetRetType(func->m_retTyp); // for knowledge what type returns - - if (!func->m_MasterClass.IsEmpty()) - { - // return "this" known - CBotVar* pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, func->m_MasterClass )); - pThis->SetInit(2); -// pThis->SetUniqNum(func->m_nThisIdent = -2); //CBotVar::NextUniqNum() will not - pThis->SetUniqNum(-2); - pStk->AddVar(pThis); - - // initialize variables acording to This - // only saves the pointer to the first, - // the rest is chained - CBotVar* pv = pThis->GivItemList(); -// int num = 1; - while (pv != NULL) - { - CBotVar* pcopy = CBotVar::Create(pv); -// pcopy->SetInit(2); - pcopy->Copy(pv); - pcopy->SetPrivate(pv->GivPrivate()); -// pcopy->SetUniqNum(pv->GivUniqNum()); //num++); - pStk->AddVar(pcopy); - pv = pv->GivNext(); - } - } - - // and compiles the following instruction block - func->m_openblk = p; - func->m_Block = CBotBlock::Compile(p, pStk, false); - func->m_closeblk = p->GivPrev(); - if ( pStk->IsOk() ) - { - if ( func->m_bPublic ) // public function, return known for all - { - CBotFunction::AddPublic(func); - } - return pStack->ReturnFunc(func, pStk); - } - } - } + CBotToken* pp; + CBotFunction* func = finput; + if ( func == NULL ) func = new CBotFunction(); + + CBotCStack* pStk = pStack->TokenStack(p, bLocal); + +// func->m_nFuncIdent = CBotVar::NextUniqNum(); + + while (true) + { + if ( IsOfType(p, ID_PUBLIC) ) + { + func->m_bPublic = true; + continue; + } + pp = p; + if ( IsOfType(p, ID_EXTERN) ) + { + func->m_extern = pp; // for the position of the word "extern" + func->m_bExtern = true; +// func->m_bPublic = true; // therefore also public! + continue; + } + break; + } + + func->m_retToken = *p; +// CBotClass* pClass; + func->m_retTyp = TypeParam(p, pStk); // type of the result + + if (func->m_retTyp.GivType() >= 0) + { + CBotToken* pp = p; + func->m_token = *p; + + if ( IsOfType(p, ID_NOT) ) + { + CBotToken d("~" + p->GivString()); + func->m_token = d; + } + + // un nom de fonction est-il là ? + if (IsOfType(p, TokenTypVar)) + { + if ( IsOfType( p, ID_DBLDOTS ) ) // method for a class + { + func->m_MasterClass = pp->GivString(); + CBotClass* pClass = CBotClass::Find(pp); + if ( pClass == NULL ) goto bad; + +// pp = p; + func->m_token = *p; + if (!IsOfType(p, TokenTypVar)) goto bad; + + } + func->m_openpar = p; + func->m_Param = CBotDefParam::Compile( p, pStk ); + func->m_closepar = p->GivPrev(); + if (pStk->IsOk()) + { + pStk->SetRetType(func->m_retTyp); // for knowledge what type returns + + if (!func->m_MasterClass.IsEmpty()) + { + // return "this" known + CBotVar* pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, func->m_MasterClass )); + pThis->SetInit(2); +// pThis->SetUniqNum(func->m_nThisIdent = -2); //CBotVar::NextUniqNum() will not + pThis->SetUniqNum(-2); + pStk->AddVar(pThis); + + // initialize variables acording to This + // only saves the pointer to the first, + // the rest is chained + CBotVar* pv = pThis->GivItemList(); +// int num = 1; + while (pv != NULL) + { + CBotVar* pcopy = CBotVar::Create(pv); +// pcopy->SetInit(2); + pcopy->Copy(pv); + pcopy->SetPrivate(pv->GivPrivate()); +// pcopy->SetUniqNum(pv->GivUniqNum()); //num++); + pStk->AddVar(pcopy); + pv = pv->GivNext(); + } + } + + // and compiles the following instruction block + func->m_openblk = p; + func->m_Block = CBotBlock::Compile(p, pStk, false); + func->m_closeblk = p->GivPrev(); + if ( pStk->IsOk() ) + { + if ( func->m_bPublic ) // public function, return known for all + { + CBotFunction::AddPublic(func); + } + return pStack->ReturnFunc(func, pStk); + } + } + } bad: - pStk->SetError(TX_NOFONC, p); - } - pStk->SetError(TX_NOTYP, p); - if ( finput == NULL ) delete func; - return pStack->ReturnFunc(NULL, pStk); + pStk->SetError(TX_NOFONC, p); + } + pStk->SetError(TX_NOTYP, p); + if ( finput == NULL ) delete func; + return pStack->ReturnFunc(NULL, pStk); } // pre-compile a new function -CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) +CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) { - CBotFunction* func = new CBotFunction(); - func->m_nFuncIdent = CBotVar::NextUniqNum(); - - CBotCStack* pStk = pStack->TokenStack(p, true); - - while (true) - { - if ( IsOfType(p, ID_PUBLIC) ) - { - // func->m_bPublic = true; // will be done in two passes - continue; - } - if ( IsOfType(p, ID_EXTERN) ) - { - func->m_bExtern = true; - continue; - } - break; - } - - func->m_retToken = *p; - func->m_retTyp = TypeParam(p, pStack); // type of the result - - if (func->m_retTyp.GivType() >= 0) - { - CBotToken* pp = p; - func->m_token = *p; - // un nom de fonction est-il là ? - if (IsOfType(p, TokenTypVar)) - { - if ( IsOfType( p, ID_DBLDOTS ) ) // method for a class - { - func->m_MasterClass = pp->GivString(); - CBotClass* pClass = CBotClass::Find(pp); - if ( pClass == NULL ) - { - pStk->SetError(TX_NOCLASS, pp); - goto bad; - } - - pp = p; - func->m_token = *p; - if (!IsOfType(p, TokenTypVar)) goto bad; - - } - func->m_Param = CBotDefParam::Compile( p, pStk ); - if (pStk->IsOk()) - { - // looks if the function exists elsewhere - if (( pClass != NULL || !pStack->CheckCall(pp, func->m_Param)) && - ( pClass == NULL || !pClass->CheckCall(pp, func->m_Param)) ) - { - if (IsOfType(p, ID_OPBLK)) - { - int level = 1; - // and skips the following instruction block - do - { - int type = p->GivType(); - p = p->GivNext(); - if (type == ID_OPBLK) level++; - if (type == ID_CLBLK) level--; - } - while (level > 0 && p != NULL); - - return pStack->ReturnFunc(func, pStk); - } - pStk->SetError(TX_OPENBLK, p); - } - } - pStk->SetError(TX_REDEF, pp); - } + CBotFunction* func = new CBotFunction(); + func->m_nFuncIdent = CBotVar::NextUniqNum(); + + CBotCStack* pStk = pStack->TokenStack(p, true); + + while (true) + { + if ( IsOfType(p, ID_PUBLIC) ) + { + // func->m_bPublic = true; // will be done in two passes + continue; + } + if ( IsOfType(p, ID_EXTERN) ) + { + func->m_bExtern = true; + continue; + } + break; + } + + func->m_retToken = *p; + func->m_retTyp = TypeParam(p, pStack); // type of the result + + if (func->m_retTyp.GivType() >= 0) + { + CBotToken* pp = p; + func->m_token = *p; + // un nom de fonction est-il là ? + if (IsOfType(p, TokenTypVar)) + { + if ( IsOfType( p, ID_DBLDOTS ) ) // method for a class + { + func->m_MasterClass = pp->GivString(); + CBotClass* pClass = CBotClass::Find(pp); + if ( pClass == NULL ) + { + pStk->SetError(TX_NOCLASS, pp); + goto bad; + } + + pp = p; + func->m_token = *p; + if (!IsOfType(p, TokenTypVar)) goto bad; + + } + func->m_Param = CBotDefParam::Compile( p, pStk ); + if (pStk->IsOk()) + { + // looks if the function exists elsewhere + if (( pClass != NULL || !pStack->CheckCall(pp, func->m_Param)) && + ( pClass == NULL || !pClass->CheckCall(pp, func->m_Param)) ) + { + if (IsOfType(p, ID_OPBLK)) + { + int level = 1; + // and skips the following instruction block + do + { + int type = p->GivType(); + p = p->GivNext(); + if (type == ID_OPBLK) level++; + if (type == ID_CLBLK) level--; + } + while (level > 0 && p != NULL); + + return pStack->ReturnFunc(func, pStk); + } + pStk->SetError(TX_OPENBLK, p); + } + } + pStk->SetError(TX_REDEF, pp); + } bad: - pStk->SetError(TX_NOFONC, p); - } - pStk->SetError(TX_NOTYP, p); - delete func; - return pStack->ReturnFunc(NULL, pStk); + pStk->SetError(TX_NOFONC, p); + } + pStk->SetError(TX_NOTYP, p); + delete func; + return pStack->ReturnFunc(NULL, pStk); } -#ifdef _DEBUG +#ifdef _DEBUG static int xx = 0; #endif bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) { - CBotStack* pile = pj->AddStack(this, 2); // one end of stack local to this function -// if ( pile == EOX ) return true; - - pile->SetBotCall(m_pProg); // bases for routines - - if ( pile->GivState() == 0 ) - { - if ( !m_Param->Execute(ppVars, pile) ) return false; // define parameters - pile->IncState(); - } - - if ( pile->GivState() == 1 && !m_MasterClass.IsEmpty() ) - { - // makes "this" known - CBotVar* pThis ; - if ( pInstance == NULL ) - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, m_MasterClass )); - pThis->SetInit(2); - } - else - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, m_MasterClass )); - pThis->SetPointer(pInstance); - pThis->SetInit(2); - } - -// pThis->SetUniqNum(m_nThisIdent); - pThis->SetUniqNum(-2); - pile->AddVar(pThis); - - pile->IncState(); - } - - if ( pile->IfStep() ) return false; - - if ( !m_Block->Execute(pile) ) - { - if ( pile->GivError() < 0 ) - pile->SetError( 0 ); - else - return false; - } - - return pj->Return(pile); + CBotStack* pile = pj->AddStack(this, 2); // one end of stack local to this function +// if ( pile == EOX ) return true; + + pile->SetBotCall(m_pProg); // bases for routines + + if ( pile->GivState() == 0 ) + { + if ( !m_Param->Execute(ppVars, pile) ) return false; // define parameters + pile->IncState(); + } + + if ( pile->GivState() == 1 && !m_MasterClass.IsEmpty() ) + { + // makes "this" known + CBotVar* pThis ; + if ( pInstance == NULL ) + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, m_MasterClass )); + pThis->SetInit(2); + } + else + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, m_MasterClass )); + pThis->SetPointer(pInstance); + pThis->SetInit(2); + } + +// pThis->SetUniqNum(m_nThisIdent); + pThis->SetUniqNum(-2); + pile->AddVar(pThis); + + pile->IncState(); + } + + if ( pile->IfStep() ) return false; + + if ( !m_Block->Execute(pile) ) + { + if ( pile->GivError() < 0 ) + pile->SetError( 0 ); + else + return false; + } + + return pj->Return(pile); } void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) { - CBotStack* pile = pj->RestoreStack(this); // one end of stack local to this function - if ( pile == NULL ) return; - CBotStack* pile2 = pile; - - pile->SetBotCall(m_pProg); // bases for routines - - if ( pile->GivBlock() < 2 ) - { - CBotStack* pile2 = pile->RestoreStack(NULL); // one end of stack local to this function - if ( pile2 == NULL ) return; - pile->SetState(pile->GivState() + pile2->GivState()); - pile2->Delete(); - } - - m_Param->RestoreState(pile2, true); // parameters - - if ( !m_MasterClass.IsEmpty() ) - { - CBotVar* pThis = pile->FindVar("this"); - pThis->SetInit(2); - pThis->SetUniqNum(-2); - } - - m_Block->RestoreState(pile2, true); + CBotStack* pile = pj->RestoreStack(this); // one end of stack local to this function + if ( pile == NULL ) return; + CBotStack* pile2 = pile; + + pile->SetBotCall(m_pProg); // bases for routines + + if ( pile->GivBlock() < 2 ) + { + CBotStack* pile2 = pile->RestoreStack(NULL); // one end of stack local to this function + if ( pile2 == NULL ) return; + pile->SetState(pile->GivState() + pile2->GivState()); + pile2->Delete(); + } + + m_Param->RestoreState(pile2, true); // parameters + + if ( !m_MasterClass.IsEmpty() ) + { + CBotVar* pThis = pile->FindVar("this"); + pThis->SetInit(2); + pThis->SetUniqNum(-2); + } + + m_Block->RestoreState(pile2, true); } void CBotFunction::AddNext(CBotFunction* p) { - CBotFunction* pp = this; - while (pp->m_next != NULL) pp = pp->m_next; + CBotFunction* pp = this; + while (pp->m_next != NULL) pp = pp->m_next; - pp->m_next = p; + pp->m_next = p; } CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long& nIdent) { - nIdent = 0; - CBotTypResult type; + nIdent = 0; + CBotTypResult type; - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); - return type; + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); + return type; } @@ -472,154 +472,154 @@ CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic) { - TypeOrError.SetType(TX_UNDEFCALL); // no routine of the name - CBotFunction* pt; - - if ( nIdent ) - { - if ( this != NULL ) for ( pt = this ; pt != NULL ; pt = pt->m_next ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - TypeOrError = pt->m_retTyp; - return pt; - } - } - - // search the list of public functions - - for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - TypeOrError = pt->m_retTyp; - return pt; - } - } - } - - if ( name == NULL ) return NULL; - - int delta = 99999; // seeks the lowest signature - CBotFunction* pFunc = NULL; // the best function found - - if ( this != NULL ) - { - for ( pt = this ; pt != NULL ; pt = pt->m_next ) - { - if ( pt->m_token.GivString() == name ) - { - int i = 0; - int alpha = 0; // signature of parameters - // parameters are compatible? - CBotDefParam* pv = pt->m_Param; // expected list of parameters - CBotVar* pw = ppVars[i++]; // provided list parameter - while ( pv != NULL && pw != NULL) - { - if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) - { - if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; - break; - } - int d = pv->GivType() - pw->GivType(2); - alpha += d>0 ? d : -10*d; // quality loss, 10 times more expensive! - - pv = pv->GivNext(); - pw = ppVars[i++]; - } - if ( pw != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); - continue; // too many parameters - } - if ( pv != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); - continue; // not enough parameters - } - - if (alpha == 0) // perfect signature - { - nIdent = pt->m_nFuncIdent; - TypeOrError = pt->m_retTyp; - return pt; - } - - if ( alpha < delta ) // a better signature? - { - pFunc = pt; - delta = alpha; - } - } - } - } - - if ( bPublic ) - { - for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) - { - if ( pt->m_token.GivString() == name ) - { - int i = 0; - int alpha = 0; // signature of parameters - // parameters sont-ils compatibles ? - CBotDefParam* pv = pt->m_Param; // list of expected parameters - CBotVar* pw = ppVars[i++]; // list of provided parameters - while ( pv != NULL && pw != NULL) - { - if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) - { - if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; - break; - } - int d = pv->GivType() - pw->GivType(2); - alpha += d>0 ? d : -10*d; // quality loss, 10 times more expensive! - - pv = pv->GivNext(); - pw = ppVars[i++]; - } - if ( pw != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); - continue; // to many parameters - } - if ( pv != NULL ) - { - if ( pFunc != NULL ) continue; - if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); - if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); - continue; // not enough parameters - } - - if (alpha == 0) // perfect signature - { - nIdent = pt->m_nFuncIdent; - TypeOrError = pt->m_retTyp; - return pt; - } - - if ( alpha < delta ) // a better signature? - { - pFunc = pt; - delta = alpha; - } - } - } - } - - if ( pFunc != NULL ) - { - nIdent = pFunc->m_nFuncIdent; - TypeOrError = pFunc->m_retTyp; - return pFunc; - } - return NULL; + TypeOrError.SetType(TX_UNDEFCALL); // no routine of the name + CBotFunction* pt; + + if ( nIdent ) + { + if ( this != NULL ) for ( pt = this ; pt != NULL ; pt = pt->m_next ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + TypeOrError = pt->m_retTyp; + return pt; + } + } + + // search the list of public functions + + for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + TypeOrError = pt->m_retTyp; + return pt; + } + } + } + + if ( name == NULL ) return NULL; + + int delta = 99999; // seeks the lowest signature + CBotFunction* pFunc = NULL; // the best function found + + if ( this != NULL ) + { + for ( pt = this ; pt != NULL ; pt = pt->m_next ) + { + if ( pt->m_token.GivString() == name ) + { + int i = 0; + int alpha = 0; // signature of parameters + // parameters are compatible? + CBotDefParam* pv = pt->m_Param; // expected list of parameters + CBotVar* pw = ppVars[i++]; // provided list parameter + while ( pv != NULL && pw != NULL) + { + if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) + { + if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; + break; + } + int d = pv->GivType() - pw->GivType(2); + alpha += d>0 ? d : -10*d; // quality loss, 10 times more expensive! + + pv = pv->GivNext(); + pw = ppVars[i++]; + } + if ( pw != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); + continue; // too many parameters + } + if ( pv != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); + continue; // not enough parameters + } + + if (alpha == 0) // perfect signature + { + nIdent = pt->m_nFuncIdent; + TypeOrError = pt->m_retTyp; + return pt; + } + + if ( alpha < delta ) // a better signature? + { + pFunc = pt; + delta = alpha; + } + } + } + } + + if ( bPublic ) + { + for ( pt = m_listPublic ; pt != NULL ; pt = pt->m_nextpublic ) + { + if ( pt->m_token.GivString() == name ) + { + int i = 0; + int alpha = 0; // signature of parameters + // parameters sont-ils compatibles ? + CBotDefParam* pv = pt->m_Param; // list of expected parameters + CBotVar* pw = ppVars[i++]; // list of provided parameters + while ( pv != NULL && pw != NULL) + { + if (!TypesCompatibles(pv->GivTypResult(), pw->GivTypResult())) + { + if ( pFunc == NULL ) TypeOrError = TX_BADPARAM; + break; + } + int d = pv->GivType() - pw->GivType(2); + alpha += d>0 ? d : -10*d; // quality loss, 10 times more expensive! + + pv = pv->GivNext(); + pw = ppVars[i++]; + } + if ( pw != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_LOWPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL)) TypeOrError.SetType(TX_OVERPARAM); + continue; // to many parameters + } + if ( pv != NULL ) + { + if ( pFunc != NULL ) continue; + if ( TypeOrError.Eq(TX_OVERPARAM) ) TypeOrError.SetType(TX_NUMPARAM); + if ( TypeOrError.Eq(TX_UNDEFCALL) ) TypeOrError.SetType(TX_LOWPARAM); + continue; // not enough parameters + } + + if (alpha == 0) // perfect signature + { + nIdent = pt->m_nFuncIdent; + TypeOrError = pt->m_retTyp; + return pt; + } + + if ( alpha < delta ) // a better signature? + { + pFunc = pt; + delta = alpha; + } + } + } + } + + if ( pFunc != NULL ) + { + nIdent = pFunc->m_nFuncIdent; + TypeOrError = pFunc->m_retTyp; + return pFunc; + } + return NULL; } @@ -627,128 +627,128 @@ CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CB int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken) { - CBotTypResult type; - CBotFunction* pt = NULL; - - pt = FindLocalOrPublic(nIdent, name, ppVars, type); - - if ( pt != NULL ) - { - CBotStack* pStk1 = pStack->AddStack(pt, 2); // to put "this" -// if ( pStk1 == EOX ) return true; - - pStk1->SetBotCall(pt->m_pProg); // it may have changed module - - if ( pStk1->IfStep() ) return false; - - CBotStack* pStk3 = pStk1->AddStack(NULL, true); // parameters - - // preparing parameters on the stack - - if ( pStk1->GivState() == 0 ) - { - if ( !pt->m_MasterClass.IsEmpty() ) - { - CBotVar* pInstance = m_pProg->m_pInstance; - // make "this" known - CBotVar* pThis ; - if ( pInstance == NULL ) - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, pt->m_MasterClass )); - pThis->SetInit(2); - } - else - { - pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, pt->m_MasterClass )); - pThis->SetPointer(pInstance); - pThis->SetInit(2); - } - - pThis->SetUniqNum(-2); - pStk1->AddVar(pThis); - - } - - // initializes the variables as parameters - pt->m_Param->Execute(ppVars, pStk3); // cannot be interrupted - - pStk1->IncState(); - } - - // finally execution of the found function - - if ( !pStk3->GivRetVar( // puts the result on the stack - pt->m_Block->Execute(pStk3) )) // GivRetVar said if it is interrupted - { - if ( !pStk3->IsOk() && pt->m_pProg != m_pProg ) - { + CBotTypResult type; + CBotFunction* pt = NULL; + + pt = FindLocalOrPublic(nIdent, name, ppVars, type); + + if ( pt != NULL ) + { + CBotStack* pStk1 = pStack->AddStack(pt, 2); // to put "this" +// if ( pStk1 == EOX ) return true; + + pStk1->SetBotCall(pt->m_pProg); // it may have changed module + + if ( pStk1->IfStep() ) return false; + + CBotStack* pStk3 = pStk1->AddStack(NULL, true); // parameters + + // preparing parameters on the stack + + if ( pStk1->GivState() == 0 ) + { + if ( !pt->m_MasterClass.IsEmpty() ) + { + CBotVar* pInstance = m_pProg->m_pInstance; + // make "this" known + CBotVar* pThis ; + if ( pInstance == NULL ) + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypClass, pt->m_MasterClass )); + pThis->SetInit(2); + } + else + { + pThis = CBotVar::Create("this", CBotTypResult( CBotTypPointer, pt->m_MasterClass )); + pThis->SetPointer(pInstance); + pThis->SetInit(2); + } + + pThis->SetUniqNum(-2); + pStk1->AddVar(pThis); + + } + + // initializes the variables as parameters + pt->m_Param->Execute(ppVars, pStk3); // cannot be interrupted + + pStk1->IncState(); + } + + // finally execution of the found function + + if ( !pStk3->GivRetVar( // puts the result on the stack + pt->m_Block->Execute(pStk3) )) // GivRetVar said if it is interrupted + { + if ( !pStk3->IsOk() && pt->m_pProg != m_pProg ) + { #ifdef _DEBUG - if ( m_pProg->GivFunctions()->GivName() == "LaCommande" ) return false; + if ( m_pProg->GivFunctions()->GivName() == "LaCommande" ) return false; #endif - pStk3->SetPosError(pToken); // indicates the error on the procedure call - } - return false; // interrupt ! - } - - return pStack->Return( pStk3 ); - } - return -1; + pStk3->SetPosError(pToken); // indicates the error on the procedure call + } + return false; // interrupt ! + } + + return pStack->Return( pStk3 ); + } + return -1; } void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack) { - CBotTypResult type; - CBotFunction* pt = NULL; - CBotStack* pStk1; - CBotStack* pStk3; - - // search function to return the ok identifier - - pt = FindLocalOrPublic(nIdent, name, ppVars, type); - - if ( pt != NULL ) - { - pStk1 = pStack->RestoreStack(pt); - if ( pStk1 == NULL ) return; - - pStk1->SetBotCall(pt->m_pProg); // it may have changed module - - if ( pStk1->GivBlock() < 2 ) - { - CBotStack* pStk2 = pStk1->RestoreStack(NULL); // used more - if ( pStk2 == NULL ) return; - pStk3 = pStk2->RestoreStack(NULL); - if ( pStk3 == NULL ) return; - } - else - { - pStk3 = pStk1->RestoreStack(NULL); - if ( pStk3 == NULL ) return; - } - - // preparing parameters on the stack - - { - if ( !pt->m_MasterClass.IsEmpty() ) - { - CBotVar* pInstance = m_pProg->m_pInstance; - // make "this" known - CBotVar* pThis = pStk1->FindVar("this"); - pThis->SetInit(2); - pThis->SetUniqNum(-2); - } - } - - if ( pStk1->GivState() == 0 ) - { - pt->m_Param->RestoreState(pStk3, true); - return; - } - - // initializes the variables as parameters - pt->m_Param->RestoreState(pStk3, false); - pt->m_Block->RestoreState(pStk3, true); - } + CBotTypResult type; + CBotFunction* pt = NULL; + CBotStack* pStk1; + CBotStack* pStk3; + + // search function to return the ok identifier + + pt = FindLocalOrPublic(nIdent, name, ppVars, type); + + if ( pt != NULL ) + { + pStk1 = pStack->RestoreStack(pt); + if ( pStk1 == NULL ) return; + + pStk1->SetBotCall(pt->m_pProg); // it may have changed module + + if ( pStk1->GivBlock() < 2 ) + { + CBotStack* pStk2 = pStk1->RestoreStack(NULL); // used more + if ( pStk2 == NULL ) return; + pStk3 = pStk2->RestoreStack(NULL); + if ( pStk3 == NULL ) return; + } + else + { + pStk3 = pStk1->RestoreStack(NULL); + if ( pStk3 == NULL ) return; + } + + // preparing parameters on the stack + + { + if ( !pt->m_MasterClass.IsEmpty() ) + { + CBotVar* pInstance = m_pProg->m_pInstance; + // make "this" known + CBotVar* pThis = pStk1->FindVar("this"); + pThis->SetInit(2); + pThis->SetUniqNum(-2); + } + } + + if ( pStk1->GivState() == 0 ) + { + pt->m_Param->RestoreState(pStk3, true); + return; + } + + // initializes the variables as parameters + pt->m_Param->RestoreState(pStk3, false); + pt->m_Block->RestoreState(pStk3, true); + } } @@ -758,167 +758,167 @@ void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass) { - CBotTypResult type; - CBotProgram* pProgCurrent = pStack->GivBotCall(); - - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, false); - - if ( pt != NULL ) - { -// DEBUG( "CBotFunction::DoCall" + pt->GivName(), 0, pStack); - - CBotStack* pStk = pStack->AddStack(pt, 2); -// if ( pStk == EOX ) return true; - - pStk->SetBotCall(pt->m_pProg); // it may have changed module - CBotStack* pStk3 = pStk->AddStack(NULL, true); // to set parameters passed - - // preparing parameters on the stack - - if ( pStk->GivState() == 0 ) - { - // sets the variable "this" on the stack - CBotVar* pthis = CBotVar::Create("this", CBotTypNullPointer); - pthis->Copy(pThis, false); - pthis->SetUniqNum(-2); // special value - pStk->AddVar(pthis); - - CBotClass* pClass = pThis->GivClass()->GivParent(); - if ( pClass ) - { - // sets the variable "super" on the stack - CBotVar* psuper = CBotVar::Create("super", CBotTypNullPointer); - psuper->Copy(pThis, false); // in fact identical to "this" - psuper->SetUniqNum(-3); // special value - pStk->AddVar(psuper); - } - // initializes the variables as parameters - pt->m_Param->Execute(ppVars, pStk3); // cannot be interrupted - pStk->IncState(); - } - - if ( pStk->GivState() == 1 ) - { - if ( pt->m_bSynchro ) - { - CBotProgram* pProgBase = pStk->GivBotCall(true); - if ( !pClass->Lock(pProgBase) ) return false; // expected to power \TODO attend de pouvoir - } - pStk->IncState(); - } - // finally calls the found function - - if ( !pStk3->GivRetVar( // puts the result on the stack - pt->m_Block->Execute(pStk3) )) // GivRetVar said if it is interrupted - { - if ( !pStk3->IsOk() ) - { - if ( pt->m_bSynchro ) - { - pClass->Unlock(); // release function - } - - if ( pt->m_pProg != pProgCurrent ) - { - pStk3->SetPosError(pToken); // indicates the error on the procedure call - } - } - return false; // interrupt ! - } - - if ( pt->m_bSynchro ) - { - pClass->Unlock(); // release function - } - - return pStack->Return( pStk3 ); - } - return -1; + CBotTypResult type; + CBotProgram* pProgCurrent = pStack->GivBotCall(); + + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type, false); + + if ( pt != NULL ) + { +// DEBUG( "CBotFunction::DoCall" + pt->GivName(), 0, pStack); + + CBotStack* pStk = pStack->AddStack(pt, 2); +// if ( pStk == EOX ) return true; + + pStk->SetBotCall(pt->m_pProg); // it may have changed module + CBotStack* pStk3 = pStk->AddStack(NULL, true); // to set parameters passed + + // preparing parameters on the stack + + if ( pStk->GivState() == 0 ) + { + // sets the variable "this" on the stack + CBotVar* pthis = CBotVar::Create("this", CBotTypNullPointer); + pthis->Copy(pThis, false); + pthis->SetUniqNum(-2); // special value + pStk->AddVar(pthis); + + CBotClass* pClass = pThis->GivClass()->GivParent(); + if ( pClass ) + { + // sets the variable "super" on the stack + CBotVar* psuper = CBotVar::Create("super", CBotTypNullPointer); + psuper->Copy(pThis, false); // in fact identical to "this" + psuper->SetUniqNum(-3); // special value + pStk->AddVar(psuper); + } + // initializes the variables as parameters + pt->m_Param->Execute(ppVars, pStk3); // cannot be interrupted + pStk->IncState(); + } + + if ( pStk->GivState() == 1 ) + { + if ( pt->m_bSynchro ) + { + CBotProgram* pProgBase = pStk->GivBotCall(true); + if ( !pClass->Lock(pProgBase) ) return false; // expected to power \TODO attend de pouvoir + } + pStk->IncState(); + } + // finally calls the found function + + if ( !pStk3->GivRetVar( // puts the result on the stack + pt->m_Block->Execute(pStk3) )) // GivRetVar said if it is interrupted + { + if ( !pStk3->IsOk() ) + { + if ( pt->m_bSynchro ) + { + pClass->Unlock(); // release function + } + + if ( pt->m_pProg != pProgCurrent ) + { + pStk3->SetPosError(pToken); // indicates the error on the procedure call + } + } + return false; // interrupt ! + } + + if ( pt->m_bSynchro ) + { + pClass->Unlock(); // release function + } + + return pStack->Return( pStk3 ); + } + return -1; } void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass) { - CBotTypResult type; - CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); + CBotTypResult type; + CBotFunction* pt = FindLocalOrPublic(nIdent, name, ppVars, type); - if ( pt != NULL ) - { - CBotStack* pStk = pStack->RestoreStack(pt); - if ( pStk == NULL ) return; - pStk->SetBotCall(pt->m_pProg); // it may have changed module + if ( pt != NULL ) + { + CBotStack* pStk = pStack->RestoreStack(pt); + if ( pStk == NULL ) return; + pStk->SetBotCall(pt->m_pProg); // it may have changed module - CBotVar* pthis = pStk->FindVar("this"); - pthis->SetUniqNum(-2); + CBotVar* pthis = pStk->FindVar("this"); + pthis->SetUniqNum(-2); - CBotStack* pStk3 = pStk->RestoreStack(NULL); // to set parameters passed - if ( pStk3 == NULL ) return; + CBotStack* pStk3 = pStk->RestoreStack(NULL); // to set parameters passed + if ( pStk3 == NULL ) return; - pt->m_Param->RestoreState(pStk3, true); // parameters + pt->m_Param->RestoreState(pStk3, true); // parameters - if ( pStk->GivState() > 1 && // latching is effective? - pt->m_bSynchro ) - { - CBotProgram* pProgBase = pStk->GivBotCall(true); - pClass->Lock(pProgBase); // locks the class - } + if ( pStk->GivState() > 1 && // latching is effective? + pt->m_bSynchro ) + { + CBotProgram* pProgBase = pStk->GivBotCall(true); + pClass->Lock(pProgBase); // locks the class + } - // finally calls the found function + // finally calls the found function - pt->m_Block->RestoreState(pStk3, true); // interrupt ! - } + pt->m_Block->RestoreState(pStk3, true); // interrupt ! + } } // see if the "signature" of parameters is identical bool CBotFunction::CheckParam(CBotDefParam* pParam) { - CBotDefParam* pp = m_Param; - while ( pp != NULL && pParam != NULL ) - { - CBotTypResult type1 = pp->GivType(); - CBotTypResult type2 = pParam->GivType(); - if ( !type1.Compare(type2) ) return false; - pp = pp->GivNext(); - pParam = pParam->GivNext(); - } - return ( pp == NULL && pParam == NULL ); + CBotDefParam* pp = m_Param; + while ( pp != NULL && pParam != NULL ) + { + CBotTypResult type1 = pp->GivType(); + CBotTypResult type2 = pParam->GivType(); + if ( !type1.Compare(type2) ) return false; + pp = pp->GivNext(); + pParam = pParam->GivNext(); + } + return ( pp == NULL && pParam == NULL ); } CBotString CBotFunction::GivName() { - return m_token.GivString(); + return m_token.GivString(); } CBotString CBotFunction::GivParams() { - if ( m_Param == NULL ) return CBotString("()"); + if ( m_Param == NULL ) return CBotString("()"); - CBotString params = "( "; - CBotDefParam* p = m_Param; // list of parameters + CBotString params = "( "; + CBotDefParam* p = m_Param; // list of parameters - while (p != NULL) - { - params += p->GivParamString(); - p = p->GivNext(); - if ( p != NULL ) params += ", "; - } + while (p != NULL) + { + params += p->GivParamString(); + p = p->GivNext(); + if ( p != NULL ) params += ", "; + } - params += " )"; - return params; + params += " )"; + return params; } CBotFunction* CBotFunction::Next() { - return m_next; + return m_next; } void CBotFunction::AddPublic(CBotFunction* func) { - if ( m_listPublic != NULL ) - { - func->m_nextpublic = m_listPublic; - m_listPublic->m_prevpublic = func; - } - m_listPublic = func; + if ( m_listPublic != NULL ) + { + func->m_nextpublic = m_listPublic; + m_listPublic->m_prevpublic = func; + } + m_listPublic = func; } @@ -929,172 +929,172 @@ void CBotFunction::AddPublic(CBotFunction* func) CBotDefParam::CBotDefParam() { - m_next = NULL; - m_nIdent = 0; + m_next = NULL; + m_nIdent = 0; } CBotDefParam::~CBotDefParam() { - delete m_next; + delete m_next; } // compiles a list of parameters CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) { - // mainly not pStack->TokenStack here - // declared variables must remain visible thereafter - - pStack->SetStartError(p->GivStart()); - - if (IsOfType(p, ID_OPENPAR)) - { - CBotDefParam* list = NULL; - - while (!IsOfType(p, ID_CLOSEPAR)) - { - CBotDefParam* param = new CBotDefParam(); - if (list == NULL) list = param; - else list->AddNext(param); // added to the list - - CBotClass* pClass = NULL;//= CBotClass::Find(p); - param->m_typename = p->GivString(); - CBotTypResult type = param->m_type = TypeParam(p, pStack); -// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object - - if (param->m_type.GivType() > 0) - { - CBotToken* pp = p; - param->m_token = *p; - if (pStack->IsOk() && IsOfType(p, TokenTypVar) ) - { - - // variable already declared? - if (pStack->CheckVarLocal(pp)) - { - pStack->SetError(TX_REDEFVAR, pp); - break; - } - - if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody); - CBotVar* var = CBotVar::Create(pp->GivString(), type); // creates the variable -// if ( pClass ) var->SetClass(pClass); - var->SetInit(2); // mark initialized - param->m_nIdent = CBotVar::NextUniqNum(); - var->SetUniqNum(param->m_nIdent); - pStack->AddVar(var); // place on the stack - - if (IsOfType(p, ID_COMMA) || p->GivType() == ID_CLOSEPAR) - continue; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStack->SetError(TX_NOTYP, p); - delete list; - return NULL; - } - return list; - } - pStack->SetError(TX_OPENPAR, p->GivStart()); - return NULL; + // mainly not pStack->TokenStack here + // declared variables must remain visible thereafter + + pStack->SetStartError(p->GivStart()); + + if (IsOfType(p, ID_OPENPAR)) + { + CBotDefParam* list = NULL; + + while (!IsOfType(p, ID_CLOSEPAR)) + { + CBotDefParam* param = new CBotDefParam(); + if (list == NULL) list = param; + else list->AddNext(param); // added to the list + + CBotClass* pClass = NULL;//= CBotClass::Find(p); + param->m_typename = p->GivString(); + CBotTypResult type = param->m_type = TypeParam(p, pStack); +// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object + + if (param->m_type.GivType() > 0) + { + CBotToken* pp = p; + param->m_token = *p; + if (pStack->IsOk() && IsOfType(p, TokenTypVar) ) + { + + // variable already declared? + if (pStack->CheckVarLocal(pp)) + { + pStack->SetError(TX_REDEFVAR, pp); + break; + } + + if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody); + CBotVar* var = CBotVar::Create(pp->GivString(), type); // creates the variable +// if ( pClass ) var->SetClass(pClass); + var->SetInit(2); // mark initialized + param->m_nIdent = CBotVar::NextUniqNum(); + var->SetUniqNum(param->m_nIdent); + pStack->AddVar(var); // place on the stack + + if (IsOfType(p, ID_COMMA) || p->GivType() == ID_CLOSEPAR) + continue; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStack->SetError(TX_NOTYP, p); + delete list; + return NULL; + } + return list; + } + pStack->SetError(TX_OPENPAR, p->GivStart()); + return NULL; } void CBotDefParam::AddNext(CBotDefParam* p) { - CBotDefParam* pp = this; - while (pp->m_next != NULL) pp = pp->m_next; + CBotDefParam* pp = this; + while (pp->m_next != NULL) pp = pp->m_next; - pp->m_next = p; + pp->m_next = p; } bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) { - int i = 0; - CBotDefParam* p = this; - - while ( p != NULL ) - { - // creates a local variable on the stack - CBotVar* newvar = CBotVar::Create(p->m_token.GivString(), p->m_type); - - // serves to make the transformation of types: - if ( ppVars != NULL && ppVars[i] != NULL ) - { - switch (p->m_type.GivType()) - { - case CBotTypInt: - newvar->SetValInt(ppVars[i]->GivValInt()); - break; - case CBotTypFloat: - newvar->SetValFloat(ppVars[i]->GivValFloat()); - break; - case CBotTypString: - newvar->SetValString(ppVars[i]->GivValString()); - break; - case CBotTypBoolean: - newvar->SetValInt(ppVars[i]->GivValInt()); - break; - case CBotTypIntrinsic: - ((CBotVarClass*)newvar)->Copy(ppVars[i], false); - break; - case CBotTypPointer: - case CBotTypArrayPointer: - { - newvar->SetPointer(ppVars[i]->GivPointer()); - } - break; - default: - ASM_TRAP(); - } - } - newvar->SetUniqNum(p->m_nIdent); - pj->AddVar(newvar); // add a variable - p = p->m_next; - i++; - } - - return true; + int i = 0; + CBotDefParam* p = this; + + while ( p != NULL ) + { + // creates a local variable on the stack + CBotVar* newvar = CBotVar::Create(p->m_token.GivString(), p->m_type); + + // serves to make the transformation of types: + if ( ppVars != NULL && ppVars[i] != NULL ) + { + switch (p->m_type.GivType()) + { + case CBotTypInt: + newvar->SetValInt(ppVars[i]->GivValInt()); + break; + case CBotTypFloat: + newvar->SetValFloat(ppVars[i]->GivValFloat()); + break; + case CBotTypString: + newvar->SetValString(ppVars[i]->GivValString()); + break; + case CBotTypBoolean: + newvar->SetValInt(ppVars[i]->GivValInt()); + break; + case CBotTypIntrinsic: + ((CBotVarClass*)newvar)->Copy(ppVars[i], false); + break; + case CBotTypPointer: + case CBotTypArrayPointer: + { + newvar->SetPointer(ppVars[i]->GivPointer()); + } + break; + default: + ASM_TRAP(); + } + } + newvar->SetUniqNum(p->m_nIdent); + pj->AddVar(newvar); // add a variable + p = p->m_next; + i++; + } + + return true; } void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) { - int i = 0; - CBotDefParam* p = this; - - while ( p != NULL ) - { - // creates a local variable on the stack - CBotVar* var = pj->FindVar(p->m_token.GivString()); - var->SetUniqNum(p->m_nIdent); - p = p->m_next; - } + int i = 0; + CBotDefParam* p = this; + + while ( p != NULL ) + { + // creates a local variable on the stack + CBotVar* var = pj->FindVar(p->m_token.GivString()); + var->SetUniqNum(p->m_nIdent); + p = p->m_next; + } } int CBotDefParam::GivType() { - return m_type.GivType(); + return m_type.GivType(); } CBotTypResult CBotDefParam::GivTypResult() { - return m_type; + return m_type; } CBotDefParam* CBotDefParam::GivNext() { - return m_next; + return m_next; } CBotString CBotDefParam::GivParamString() { - CBotString param; - - param = m_typename; - param += ' '; + CBotString param; + + param = m_typename; + param += ' '; - param += m_token.GivString(); - return param; + param += m_token.GivString(); + return param; } @@ -1104,80 +1104,80 @@ CBotString CBotDefParam::GivParamString() CBotReturn::CBotReturn() { - m_Instr = NULL; - name = "CBotReturn"; // debug + m_Instr = NULL; + name = "CBotReturn"; // debug } CBotReturn::~CBotReturn() { - delete m_Instr; + delete m_Instr; } CBotInstr* CBotReturn::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; - - if (!IsOfType(p, ID_RETURN)) return NULL; // should never happen - - CBotReturn* inst = new CBotReturn(); // creates the object - inst->SetToken( pp ); - - CBotTypResult type = pStack->GivRetType(); - - if ( type.GivType() == 0 ) // returned void ? - { - if ( IsOfType( p, ID_SEP ) ) return inst; - pStack->SetError( TX_BADTYPE, pp ); - return NULL; - } - - inst->m_Instr = CBotExpression::Compile(p, pStack); - if ( pStack->IsOk() ) - { - CBotTypResult retType = pStack->GivTypResult(2); - if (TypeCompatible(retType, type, ID_ASS)) - { - if ( IsOfType( p, ID_SEP ) ) - return inst; - - pStack->SetError(TX_ENDOF, p->GivStart()); - } - pStack->SetError(TX_BADTYPE, p->GivStart()); - } - - delete inst; - return NULL; // no object, the error is on the stack + CBotToken* pp = p; + + if (!IsOfType(p, ID_RETURN)) return NULL; // should never happen + + CBotReturn* inst = new CBotReturn(); // creates the object + inst->SetToken( pp ); + + CBotTypResult type = pStack->GivRetType(); + + if ( type.GivType() == 0 ) // returned void ? + { + if ( IsOfType( p, ID_SEP ) ) return inst; + pStack->SetError( TX_BADTYPE, pp ); + return NULL; + } + + inst->m_Instr = CBotExpression::Compile(p, pStack); + if ( pStack->IsOk() ) + { + CBotTypResult retType = pStack->GivTypResult(2); + if (TypeCompatible(retType, type, ID_ASS)) + { + if ( IsOfType( p, ID_SEP ) ) + return inst; + + pStack->SetError(TX_ENDOF, p->GivStart()); + } + pStack->SetError(TX_BADTYPE, p->GivStart()); + } + + delete inst; + return NULL; // no object, the error is on the stack } bool CBotReturn::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) - { - if ( m_Instr != NULL && !m_Instr->Execute(pile) ) return false; // evaluate the result - // the result is on the stack - pile->IncState(); - } + if ( pile->GivState() == 0 ) + { + if ( m_Instr != NULL && !m_Instr->Execute(pile) ) return false; // evaluate the result + // the result is on the stack + pile->IncState(); + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - pile->SetBreak(3, CBotString()); - return pj->Return(pile); + pile->SetBreak(3, CBotString()); + return pj->Return(pile); } void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; - - if ( pile->GivState() == 0 ) - { - if ( m_Instr != NULL ) m_Instr->RestoreState(pile, bMain); // evaluate the result - return; - } + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; + + if ( pile->GivState() == 0 ) + { + if ( m_Instr != NULL ) m_Instr->RestoreState(pile, bMain); // evaluate the result + return; + } } //////////////////////////////////////////////////////////////////////////////// @@ -1185,172 +1185,172 @@ void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) CBotInstrCall::CBotInstrCall() { - m_Parameters = NULL; - m_nFuncIdent = 0; - name = "CBotInstrCall"; + m_Parameters = NULL; + m_nFuncIdent = 0; + name = "CBotInstrCall"; } CBotInstrCall::~CBotInstrCall() { - delete m_Parameters; + delete m_Parameters; } CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotVar* ppVars[1000]; - - int i = 0; - - CBotToken* pp = p; - p = p->GivNext(); - - pStack->SetStartError(p->GivStart()); - CBotCStack* pile = pStack; - - if ( IsOfType(p, ID_OPENPAR) ) - { - int start, end; - CBotInstrCall* inst = new CBotInstrCall(); - inst->SetToken(pp); - - // compile la list of parameters - if (!IsOfType(p, ID_CLOSEPAR)) while (true) - { - start = p->GivStart(); - pile = pile->TokenStack(); // keeps the results on the stack - - CBotInstr* param = CBotExpression::Compile(p, pile); - end = p->GivStart(); - if ( inst->m_Parameters == NULL ) inst->m_Parameters = param; - else inst->m_Parameters->AddNext(param); // constructs the list - - if ( !pile->IsOk() ) - { - delete inst; - return pStack->Return(NULL, pile); - } - - if ( param != NULL ) - { - if ( pile->GivTypResult().Eq(99) ) - { - delete pStack->TokenStack(); - pStack->SetError(TX_VOID, p->GivStart()); - delete inst; - return NULL; - } - ppVars[i] = pile->GivVar(); - ppVars[i]->GivToken()->SetPos(start, end); - i++; - - if (IsOfType(p, ID_COMMA)) continue; // skips the comma - if (IsOfType(p, ID_CLOSEPAR)) break; - } - - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - delete pStack->TokenStack(); - delete inst; - return NULL; - } - ppVars[i] = NULL; - - // the routine is known? -// CBotClass* pClass = NULL; - inst->m_typRes = pStack->CompileCall(pp, ppVars, inst->m_nFuncIdent); - if ( inst->m_typRes.GivType() >= 20 ) - { -// if (pVar2!=NULL) pp = pVar2->RetToken(); - pStack->SetError( inst->m_typRes.GivType(), pp ); - delete pStack->TokenStack(); - delete inst; - return NULL; - } - - delete pStack->TokenStack(); - if ( inst->m_typRes.GivType() > 0 ) - { - CBotVar* pRes = CBotVar::Create("", inst->m_typRes); - pStack->SetVar(pRes); // for knowing the type of the result - } - else pStack->SetVar(NULL); // routine returns void - - return inst; - } - p = pp; - delete pStack->TokenStack(); - return NULL; + CBotVar* ppVars[1000]; + + int i = 0; + + CBotToken* pp = p; + p = p->GivNext(); + + pStack->SetStartError(p->GivStart()); + CBotCStack* pile = pStack; + + if ( IsOfType(p, ID_OPENPAR) ) + { + int start, end; + CBotInstrCall* inst = new CBotInstrCall(); + inst->SetToken(pp); + + // compile la list of parameters + if (!IsOfType(p, ID_CLOSEPAR)) while (true) + { + start = p->GivStart(); + pile = pile->TokenStack(); // keeps the results on the stack + + CBotInstr* param = CBotExpression::Compile(p, pile); + end = p->GivStart(); + if ( inst->m_Parameters == NULL ) inst->m_Parameters = param; + else inst->m_Parameters->AddNext(param); // constructs the list + + if ( !pile->IsOk() ) + { + delete inst; + return pStack->Return(NULL, pile); + } + + if ( param != NULL ) + { + if ( pile->GivTypResult().Eq(99) ) + { + delete pStack->TokenStack(); + pStack->SetError(TX_VOID, p->GivStart()); + delete inst; + return NULL; + } + ppVars[i] = pile->GivVar(); + ppVars[i]->GivToken()->SetPos(start, end); + i++; + + if (IsOfType(p, ID_COMMA)) continue; // skips the comma + if (IsOfType(p, ID_CLOSEPAR)) break; + } + + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + delete pStack->TokenStack(); + delete inst; + return NULL; + } + ppVars[i] = NULL; + + // the routine is known? +// CBotClass* pClass = NULL; + inst->m_typRes = pStack->CompileCall(pp, ppVars, inst->m_nFuncIdent); + if ( inst->m_typRes.GivType() >= 20 ) + { +// if (pVar2!=NULL) pp = pVar2->RetToken(); + pStack->SetError( inst->m_typRes.GivType(), pp ); + delete pStack->TokenStack(); + delete inst; + return NULL; + } + + delete pStack->TokenStack(); + if ( inst->m_typRes.GivType() > 0 ) + { + CBotVar* pRes = CBotVar::Create("", inst->m_typRes); + pStack->SetVar(pRes); // for knowing the type of the result + } + else pStack->SetVar(NULL); // routine returns void + + return inst; + } + p = pp; + delete pStack->TokenStack(); + return NULL; } bool CBotInstrCall::Execute(CBotStack* &pj) { - CBotVar* ppVars[1000]; - CBotStack* pile = pj->AddStack(this); - if ( pile->StackOver() ) return pj->Return( pile ); - - CBotStack* pile1 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluates parameters - // and places the values ​​on the stack - // for allow of interruption at any time - if ( p != NULL) while ( true ) - { - pile = pile->AddStack(); // place on the stack for the results - if ( pile->GivState() == 0 ) - { - if (!p->Execute(pile)) return false; // interrupted here? - pile->SetState(1); // mark as special for reknowed parameters \TODO marque spéciale pour reconnaîre parameters - } - ppVars[i++] = pile->GivVar(); - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return false; - - if ( !pile2->ExecuteCall(m_nFuncIdent, GivToken(), ppVars, m_typRes)) return false; // interrupt - - return pj->Return(pile2); // release the entire stack + CBotVar* ppVars[1000]; + CBotStack* pile = pj->AddStack(this); + if ( pile->StackOver() ) return pj->Return( pile ); + + CBotStack* pile1 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates parameters + // and places the values ​​on the stack + // for allow of interruption at any time + if ( p != NULL) while ( true ) + { + pile = pile->AddStack(); // place on the stack for the results + if ( pile->GivState() == 0 ) + { + if (!p->Execute(pile)) return false; // interrupted here? + pile->SetState(1); // mark as special for reknowed parameters \TODO marque spéciale pour reconnaîre parameters + } + ppVars[i++] = pile->GivVar(); + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotStack* pile2 = pile->AddStack(); + if ( pile2->IfStep() ) return false; + + if ( !pile2->ExecuteCall(m_nFuncIdent, GivToken(), ppVars, m_typRes)) return false; // interrupt + + return pj->Return(pile2); // release the entire stack } void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; - - CBotStack* pile1 = pile; - - int i = 0; - CBotVar* ppVars[1000]; - CBotInstr* p = m_Parameters; - // evaluate parameters - // and place the values on the stack - // for allow of interruption at any time - if ( p != NULL) while ( true ) - { - pile = pile->RestoreStack(); // place on the stack for the results - if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) - { - p->RestoreState(pile, bMain); // interrupt here! - return; - } - ppVars[i++] = pile->GivVar(); // constructs the list of parameters - p = p->GivNext(); - if ( p == NULL) break; - } - ppVars[i] = NULL; - - CBotStack* pile2 = pile->RestoreStack(); - if ( pile2 == NULL ) return; - - pile2->RestoreCall(m_nFuncIdent, GivToken(), ppVars); + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; + + CBotStack* pile1 = pile; + + int i = 0; + CBotVar* ppVars[1000]; + CBotInstr* p = m_Parameters; + // evaluate parameters + // and place the values on the stack + // for allow of interruption at any time + if ( p != NULL) while ( true ) + { + pile = pile->RestoreStack(); // place on the stack for the results + if ( pile == NULL ) return; + if ( pile->GivState() == 0 ) + { + p->RestoreState(pile, bMain); // interrupt here! + return; + } + ppVars[i++] = pile->GivVar(); // constructs the list of parameters + p = p->GivNext(); + if ( p == NULL) break; + } + ppVars[i] = NULL; + + CBotStack* pile2 = pile->RestoreStack(); + if ( pile2 == NULL ) return; + + pile2->RestoreCall(m_nFuncIdent, GivToken(), ppVars); } ////////////////////////////////////////////////////////////////////////////// @@ -1361,287 +1361,287 @@ void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) { - if ( !IsOfType(p, ID_PUBLIC) ) - { - pStack->SetError(TX_NOPUBLIC, p); - return NULL; - } - - if ( !IsOfType(p, ID_CLASS) ) return NULL; - - CBotString name = p->GivString(); - - CBotClass* pOld = CBotClass::Find(name); - if ( pOld != NULL && pOld->m_IsDef ) - { - pStack->SetError( TX_REDEFCLASS, p ); - return NULL; - } - - // a name of the class is there? - if (IsOfType(p, TokenTypVar)) - { - CBotClass* pPapa = NULL; - if ( IsOfType( p, ID_EXTENDS ) ) - { - CBotString name = p->GivString(); - pPapa = CBotClass::Find(name); - - if (!IsOfType(p, TokenTypVar) || pPapa == NULL ) - { - pStack->SetError( TX_NOCLASS, p ); - return NULL; - } - } - CBotClass* classe = (pOld == NULL) ? new CBotClass(name, pPapa) : pOld; - classe->Purge(); // emptythe old definitions - classe->m_IsDef = false; // current definition - - if ( !IsOfType( p, ID_OPBLK) ) - { - pStack->SetError(TX_OPENBLK, p); - return NULL; - } - - while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) - { - classe->CompileDefItem(p, pStack, false); - } - - if (pStack->IsOk()) return classe; - } - pStack->SetError(TX_ENDOF, p); - return NULL; + if ( !IsOfType(p, ID_PUBLIC) ) + { + pStack->SetError(TX_NOPUBLIC, p); + return NULL; + } + + if ( !IsOfType(p, ID_CLASS) ) return NULL; + + CBotString name = p->GivString(); + + CBotClass* pOld = CBotClass::Find(name); + if ( pOld != NULL && pOld->m_IsDef ) + { + pStack->SetError( TX_REDEFCLASS, p ); + return NULL; + } + + // a name of the class is there? + if (IsOfType(p, TokenTypVar)) + { + CBotClass* pPapa = NULL; + if ( IsOfType( p, ID_EXTENDS ) ) + { + CBotString name = p->GivString(); + pPapa = CBotClass::Find(name); + + if (!IsOfType(p, TokenTypVar) || pPapa == NULL ) + { + pStack->SetError( TX_NOCLASS, p ); + return NULL; + } + } + CBotClass* classe = (pOld == NULL) ? new CBotClass(name, pPapa) : pOld; + classe->Purge(); // emptythe old definitions + classe->m_IsDef = false; // current definition + + if ( !IsOfType( p, ID_OPBLK) ) + { + pStack->SetError(TX_OPENBLK, p); + return NULL; + } + + while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) + { + classe->CompileDefItem(p, pStack, false); + } + + if (pStack->IsOk()) return classe; + } + pStack->SetError(TX_ENDOF, p); + return NULL; } bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) { - bool bStatic = false; - int mProtect = PR_PUBLIC; - bool bSynchro = false; - - while (IsOfType(p, ID_SEP)) ; - - CBotTypResult type( -1 ); - - if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; - CBotToken* pBase = p; - - if ( IsOfType(p, ID_STATIC) ) bStatic = true; - if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; - if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; - if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; - if ( IsOfType(p, ID_STATIC) ) bStatic = true; - -// CBotClass* pClass = NULL; - type = TypeParam(p, pStack); // type of the result - - if ( type.Eq(-1) ) - { - pStack->SetError(TX_NOTYP, p); - return false; - } - - while (pStack->IsOk()) - { - CBotToken* pp = p; - IsOfType(p, ID_NOT); // skips ~ eventual (destructor) - - if (IsOfType(p, TokenTypVar)) - { - CBotInstr* limites = NULL; - while ( IsOfType( p, ID_OPBRK ) ) // a table? - { - CBotInstr* i = NULL; - - if ( p->GivType() != ID_CLBRK ) - i = CBotExpression::Compile( p, pStack ); // expression for the value - else - i = new CBotEmpty(); // special if not a formula - - type = CBotTypResult(CBotTypArrayPointer, type); - - if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStack->SetError(TX_CLBRK, p->GivStart()); - return false; - } - -/* CBotVar* pv = pStack->GivVar(); - if ( pv->GivType()>= CBotTypBoolean ) - { - pStack->SetError(TX_BADTYPE, p->GivStart()); - return false; - }*/ - - if (limites == NULL) limites = i; - else limites->AddNext3(i); - } - - if ( p->GivType() == ID_OPENPAR ) - { - if ( !bSecond ) - { - p = pBase; - CBotFunction* f = - CBotFunction::Compile1(p, pStack, this); - - if ( f == NULL ) return false; - - if (m_pMethod == NULL) m_pMethod = f; - else m_pMethod->AddNext(f); - } - else - { - // return a method precompiled in pass 1 - CBotFunction* pf = m_pMethod; - CBotFunction* prev = NULL; - while ( pf != NULL ) - { - if (pf->GivName() == pp->GivString()) break; - prev = pf; - pf = pf->Next(); - } - - bool bConstructor = (pp->GivString() == GivName()); - CBotCStack* pile = pStack->TokenStack(NULL, true); - - // make "this" known - CBotToken TokenThis(CBotString("this"), CBotString()); - CBotVar* pThis = CBotVar::Create(&TokenThis, CBotTypResult( CBotTypClass, this ) ); - pThis->SetUniqNum(-2); - pile->AddVar(pThis); - - if ( m_pParent ) - { - // makes "super" known - CBotToken TokenSuper(CBotString("super"), CBotString()); - CBotVar* pThis = CBotVar::Create(&TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) ); - pThis->SetUniqNum(-3); - pile->AddVar(pThis); - } - -// int num = 1; - CBotClass* my = this; - while (my != NULL) - { - // places a copy of variables of a class (this) on a stack - CBotVar* pv = my->m_pVar; - while (pv != NULL) - { - CBotVar* pcopy = CBotVar::Create(pv); - pcopy->SetInit(!bConstructor || pv->IsStatic()); - pcopy->SetUniqNum(pv->GivUniqNum()); - pile->AddVar(pcopy); - pv = pv->GivNext(); - } - my = my->m_pParent; - } - - // compiles a method - p = pBase; - CBotFunction* f = - CBotFunction::Compile(p, pile, NULL/*, false*/); - - if ( f != NULL ) - { - f->m_pProg = pStack->GivBotCall(); - f->m_bSynchro = bSynchro; - // replaces the element in the chain - f->m_next = pf->m_next; - pf->m_next = NULL; - delete pf; - if (prev == NULL) m_pMethod = f; - else prev->m_next = f; - } - pStack->Return(NULL, pile); - } - - return pStack->IsOk(); - } - - // definition of an element - if (type.Eq(0)) - { - pStack->SetError(TX_ENDOF, p); - return false; - } - - CBotInstr* i = NULL; - if ( IsOfType(p, ID_ASS ) ) - { - if ( type.Eq(CBotTypArrayPointer) ) - { - i = CBotListArray::Compile(p, pStack, type.GivTypElem()); - } - else - { - // it has an assignmet to calculate - i = CBotTwoOpExpr::Compile(p, pStack); - } - if ( !pStack->IsOk() ) return false; - } - - - if ( !bSecond ) - { - CBotVar* pv = CBotVar::Create(pp->GivString(), type); - pv -> SetStatic( bStatic ); - pv -> SetPrivate( mProtect ); - - AddItem( pv ); - - pv->m_InitExpr = i; - pv->m_LimExpr = limites; - - - if ( pv->IsStatic() && pv->m_InitExpr != NULL ) - { - CBotStack* pile = CBotStack::FirstStack(); // independent stack - while(pile->IsOk() && !pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer - pv->SetVal( pile->GivVar() ) ; - pile->Delete(); - } - } - else - delete i; - - if ( IsOfType(p, ID_COMMA) ) continue; - if ( IsOfType(p, ID_SEP) ) break; - } - pStack->SetError(TX_ENDOF, p); - } - return pStack->IsOk(); + bool bStatic = false; + int mProtect = PR_PUBLIC; + bool bSynchro = false; + + while (IsOfType(p, ID_SEP)) ; + + CBotTypResult type( -1 ); + + if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; + CBotToken* pBase = p; + + if ( IsOfType(p, ID_STATIC) ) bStatic = true; + if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; + if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; + if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; + if ( IsOfType(p, ID_STATIC) ) bStatic = true; + +// CBotClass* pClass = NULL; + type = TypeParam(p, pStack); // type of the result + + if ( type.Eq(-1) ) + { + pStack->SetError(TX_NOTYP, p); + return false; + } + + while (pStack->IsOk()) + { + CBotToken* pp = p; + IsOfType(p, ID_NOT); // skips ~ eventual (destructor) + + if (IsOfType(p, TokenTypVar)) + { + CBotInstr* limites = NULL; + while ( IsOfType( p, ID_OPBRK ) ) // a table? + { + CBotInstr* i = NULL; + + if ( p->GivType() != ID_CLBRK ) + i = CBotExpression::Compile( p, pStack ); // expression for the value + else + i = new CBotEmpty(); // special if not a formula + + type = CBotTypResult(CBotTypArrayPointer, type); + + if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) + { + pStack->SetError(TX_CLBRK, p->GivStart()); + return false; + } + +/* CBotVar* pv = pStack->GivVar(); + if ( pv->GivType()>= CBotTypBoolean ) + { + pStack->SetError(TX_BADTYPE, p->GivStart()); + return false; + }*/ + + if (limites == NULL) limites = i; + else limites->AddNext3(i); + } + + if ( p->GivType() == ID_OPENPAR ) + { + if ( !bSecond ) + { + p = pBase; + CBotFunction* f = + CBotFunction::Compile1(p, pStack, this); + + if ( f == NULL ) return false; + + if (m_pMethod == NULL) m_pMethod = f; + else m_pMethod->AddNext(f); + } + else + { + // return a method precompiled in pass 1 + CBotFunction* pf = m_pMethod; + CBotFunction* prev = NULL; + while ( pf != NULL ) + { + if (pf->GivName() == pp->GivString()) break; + prev = pf; + pf = pf->Next(); + } + + bool bConstructor = (pp->GivString() == GivName()); + CBotCStack* pile = pStack->TokenStack(NULL, true); + + // make "this" known + CBotToken TokenThis(CBotString("this"), CBotString()); + CBotVar* pThis = CBotVar::Create(&TokenThis, CBotTypResult( CBotTypClass, this ) ); + pThis->SetUniqNum(-2); + pile->AddVar(pThis); + + if ( m_pParent ) + { + // makes "super" known + CBotToken TokenSuper(CBotString("super"), CBotString()); + CBotVar* pThis = CBotVar::Create(&TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) ); + pThis->SetUniqNum(-3); + pile->AddVar(pThis); + } + +// int num = 1; + CBotClass* my = this; + while (my != NULL) + { + // places a copy of variables of a class (this) on a stack + CBotVar* pv = my->m_pVar; + while (pv != NULL) + { + CBotVar* pcopy = CBotVar::Create(pv); + pcopy->SetInit(!bConstructor || pv->IsStatic()); + pcopy->SetUniqNum(pv->GivUniqNum()); + pile->AddVar(pcopy); + pv = pv->GivNext(); + } + my = my->m_pParent; + } + + // compiles a method + p = pBase; + CBotFunction* f = + CBotFunction::Compile(p, pile, NULL/*, false*/); + + if ( f != NULL ) + { + f->m_pProg = pStack->GivBotCall(); + f->m_bSynchro = bSynchro; + // replaces the element in the chain + f->m_next = pf->m_next; + pf->m_next = NULL; + delete pf; + if (prev == NULL) m_pMethod = f; + else prev->m_next = f; + } + pStack->Return(NULL, pile); + } + + return pStack->IsOk(); + } + + // definition of an element + if (type.Eq(0)) + { + pStack->SetError(TX_ENDOF, p); + return false; + } + + CBotInstr* i = NULL; + if ( IsOfType(p, ID_ASS ) ) + { + if ( type.Eq(CBotTypArrayPointer) ) + { + i = CBotListArray::Compile(p, pStack, type.GivTypElem()); + } + else + { + // it has an assignmet to calculate + i = CBotTwoOpExpr::Compile(p, pStack); + } + if ( !pStack->IsOk() ) return false; + } + + + if ( !bSecond ) + { + CBotVar* pv = CBotVar::Create(pp->GivString(), type); + pv -> SetStatic( bStatic ); + pv -> SetPrivate( mProtect ); + + AddItem( pv ); + + pv->m_InitExpr = i; + pv->m_LimExpr = limites; + + + if ( pv->IsStatic() && pv->m_InitExpr != NULL ) + { + CBotStack* pile = CBotStack::FirstStack(); // independent stack + while(pile->IsOk() && !pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer + pv->SetVal( pile->GivVar() ) ; + pile->Delete(); + } + } + else + delete i; + + if ( IsOfType(p, ID_COMMA) ) continue; + if ( IsOfType(p, ID_SEP) ) break; + } + pStack->SetError(TX_ENDOF, p); + } + return pStack->IsOk(); } CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) { - if ( !IsOfType(p, ID_PUBLIC) ) return NULL; - if ( !IsOfType(p, ID_CLASS) ) return NULL; - - CBotString name = p->GivString(); - - // a name for the class is there? - if (IsOfType(p, TokenTypVar)) - { - // the class was created by Compile1 - CBotClass* pOld = CBotClass::Find(name); - - if ( IsOfType( p, ID_EXTENDS ) ) - { - IsOfType(p, TokenTypVar); // necessarily - } - IsOfType( p, ID_OPBLK); // necessarily - - while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) - { - pOld->CompileDefItem(p, pStack, true); - } - - pOld->m_IsDef = true; // complete definition - if (pStack->IsOk()) return pOld; - } - pStack->SetError(TX_ENDOF, p); - return NULL; + if ( !IsOfType(p, ID_PUBLIC) ) return NULL; + if ( !IsOfType(p, ID_CLASS) ) return NULL; + + CBotString name = p->GivString(); + + // a name for the class is there? + if (IsOfType(p, TokenTypVar)) + { + // the class was created by Compile1 + CBotClass* pOld = CBotClass::Find(name); + + if ( IsOfType( p, ID_EXTENDS ) ) + { + IsOfType(p, TokenTypVar); // necessarily + } + IsOfType( p, ID_OPBLK); // necessarily + + while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) + { + pOld->CompileDefItem(p, pStack, true); + } + + pOld->m_IsDef = true; // complete definition + if (pStack->IsOk()) return pOld; + } + pStack->SetError(TX_ENDOF, p); + return NULL; } diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotIf.cpp index c1dc833..84dbd6a 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotIf.cpp @@ -22,17 +22,17 @@ // various constructors / destructors CBotIf::CBotIf() { - m_Condition = - m_Block = - m_BlockElse = NULL; // NULL so that delete is not possible further - name = "CBotIf"; // debug + m_Condition = + m_Block = + m_BlockElse = NULL; // NULL so that delete is not possible further + name = "CBotIf"; // debug } CBotIf::~CBotIf() { - delete m_Condition; // frees the condition - delete m_Block; // frees the block of instruction1 - delete m_BlockElse; // frees the block of instruction2 + delete m_Condition; // frees the condition + delete m_Block; // frees the block of instruction1 + delete m_BlockElse; // frees the block of instruction2 } // compilation (static routine) @@ -40,47 +40,47 @@ CBotIf::~CBotIf() CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; // preserves at the ^ token (starting instruction) - - if (!IsOfType(p, ID_IF)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - CBotIf* inst = new CBotIf(); // create the object - inst->SetToken( pp ); - - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // the condition does exist - - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if ( pStk->IsOk() ) - { - // the statement block is ok (can be empty) - - // see if the next instruction is the token "else" - if (IsOfType(p, ID_ELSE)) - { - // if so, compiles the following statement block - inst->m_BlockElse = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if (!pStk->IsOk()) - { - // there is no correct block after the else - // frees the object, and transmits the error that is on the stack - delete inst; - return pStack->Return(NULL, pStk); - } - } - - // return the corrent object to the application - return pStack->Return(inst, pStk); - } - } - - // error, frees the object - delete inst; - // and transmits the error that is on the stack. - return pStack->Return(NULL, pStk); + CBotToken* pp = p; // preserves at the ^ token (starting instruction) + + if (!IsOfType(p, ID_IF)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + CBotIf* inst = new CBotIf(); // create the object + inst->SetToken( pp ); + + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition does exist + + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if ( pStk->IsOk() ) + { + // the statement block is ok (can be empty) + + // see if the next instruction is the token "else" + if (IsOfType(p, ID_ELSE)) + { + // if so, compiles the following statement block + inst->m_BlockElse = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if (!pStk->IsOk()) + { + // there is no correct block after the else + // frees the object, and transmits the error that is on the stack + delete inst; + return pStack->Return(NULL, pStk); + } + } + + // return the corrent object to the application + return pStack->Return(inst, pStk); + } + } + + // error, frees the object + delete inst; + // and transmits the error that is on the stack. + return pStack->Return(NULL, pStk); } @@ -88,74 +88,74 @@ CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotIf :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or found in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - // according to recovery, it may be in one of two states - if( pile->GivState() == 0 ) - { - // evaluates the condition - if ( !m_Condition->Execute(pile) ) return false; // interrupted here? - - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // returns the results and releases the stack - } - - // passes into the second state - if (!pile->SetState(1)) return false; // ready for further - } - - // second state, evaluates the associated instructions - // the result of the condition is on the stack - - if ( pile->GivVal() == true ) // condition was true? - { - if ( m_Block != NULL && // block may be absent - !m_Block->Execute(pile) ) return false; // interrupted here? - } - else - { - if ( m_BlockElse != NULL && // if there is an alternate block - !m_BlockElse->Execute(pile) ) return false; // interrupted here - } - - // sends the results and releases the stack - return pj->Return(pile); + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or found in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + // according to recovery, it may be in one of two states + if( pile->GivState() == 0 ) + { + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here? + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // returns the results and releases the stack + } + + // passes into the second state + if (!pile->SetState(1)) return false; // ready for further + } + + // second state, evaluates the associated instructions + // the result of the condition is on the stack + + if ( pile->GivVal() == true ) // condition was true? + { + if ( m_Block != NULL && // block may be absent + !m_Block->Execute(pile) ) return false; // interrupted here? + } + else + { + if ( m_BlockElse != NULL && // if there is an alternate block + !m_BlockElse->Execute(pile) ) return false; // interrupted here + } + + // sends the results and releases the stack + return pj->Return(pile); } void CBotIf :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - // according to recovery, it may be in one of two states - if( pile->GivState() == 0 ) - { - // evaluates the condition - m_Condition->RestoreState(pile, bMain); // interrupted here! - return; - } - - // second state, evaluates the associated instructions - // the result of the condition is on the stack - - if ( pile->GivVal() == true ) // condition was true? - { - if ( m_Block != NULL ) // block may be absent - m_Block->RestoreState(pile, bMain); // interrupted here! - } - else - { - if ( m_BlockElse != NULL ) // if there is an alternate block - m_BlockElse->RestoreState(pile, bMain); // interrupted here! - } + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + // according to recovery, it may be in one of two states + if( pile->GivState() == 0 ) + { + // evaluates the condition + m_Condition->RestoreState(pile, bMain); // interrupted here! + return; + } + + // second state, evaluates the associated instructions + // the result of the condition is on the stack + + if ( pile->GivVal() == true ) // condition was true? + { + if ( m_Block != NULL ) // block may be absent + m_Block->RestoreState(pile, bMain); // interrupted here! + } + else + { + if ( m_BlockElse != NULL ) // if there is an alternate block + m_BlockElse->RestoreState(pile, bMain); // interrupted here! + } } diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 5f1c021..d9be052 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -22,597 +22,597 @@ CBotProgram::CBotProgram() { - m_Prog = NULL; - m_pRun = NULL; - m_pClass = NULL; - m_pStack = NULL; - m_pInstance = NULL; - - m_ErrorCode = 0; - m_Ident = 0; - m_bDebugDD = 0; + m_Prog = NULL; + m_pRun = NULL; + m_pClass = NULL; + m_pStack = NULL; + m_pInstance = NULL; + + m_ErrorCode = 0; + m_Ident = 0; + m_bDebugDD = 0; } CBotProgram::CBotProgram(CBotVar* pInstance) { - m_Prog = NULL; - m_pRun = NULL; - m_pClass = NULL; - m_pStack = NULL; - m_pInstance = pInstance; - - m_ErrorCode = 0; - m_Ident = 0; - m_bDebugDD = 0; + m_Prog = NULL; + m_pRun = NULL; + m_pClass = NULL; + m_pStack = NULL; + m_pInstance = pInstance; + + m_ErrorCode = 0; + m_Ident = 0; + m_bDebugDD = 0; } CBotProgram::~CBotProgram() { -// delete m_pClass; - m_pClass->Purge(); - m_pClass = NULL; +// delete m_pClass; + m_pClass->Purge(); + m_pClass = NULL; - CBotClass::FreeLock(this); + CBotClass::FreeLock(this); - delete m_Prog; -#if STACKMEM - m_pStack->Delete(); + delete m_Prog; +#if STACKMEM + m_pStack->Delete(); #else - delete m_pStack; + delete m_pStack; #endif } bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser ) { - int error = 0; - Stop(); - -// delete m_pClass; - m_pClass->Purge(); // purge the old definitions of classes - // but without destroying the object - m_pClass = NULL; - delete m_Prog; m_Prog= NULL; - - ListFonctions.SetSize(0); - m_ErrorCode = 0; - - if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) - pUser = m_pInstance->m_pUserPtr; - - // transforms the program in Tokens - CBotToken* pBaseToken = CBotToken::CompileTokens(program, error); - if ( pBaseToken == NULL ) return false; - - - CBotCStack* pStack = new CBotCStack(NULL); - CBotToken* p = pBaseToken->GivNext(); // skips the first token (separator) - - pStack->SetBotCall(this); // defined used routines - CBotCall::SetPUser(pUser); - - // first made a quick pass just to take the headers of routines and classes - while ( pStack->IsOk() && p != NULL && p->GivType() != 0) - { - if ( IsOfType(p, ID_SEP) ) continue; // semicolons lurking - - if ( p->GivType() == ID_CLASS || - ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) - { - CBotClass* nxt = CBotClass::Compile1(p, pStack); - if (m_pClass == NULL ) m_pClass = nxt; - else m_pClass->AddNext(nxt); - } - else - { - CBotFunction* next = CBotFunction::Compile1(p, pStack, NULL); - if (m_Prog == NULL ) m_Prog = next; - else m_Prog->AddNext(next); - } - } - if ( !pStack->IsOk() ) - { - m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); - delete m_Prog; - m_Prog = NULL; - delete pBaseToken; - return false; - } - -// CBotFunction* temp = NULL; - CBotFunction* next = m_Prog; // rewind the list - - p = pBaseToken->GivNext(); // returns to the beginning - - while ( pStack->IsOk() && p != NULL && p->GivType() != 0 ) - { - if ( IsOfType(p, ID_SEP) ) continue; // semicolons lurking - - if ( p->GivType() == ID_CLASS || - ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) - { - m_bCompileClass = true; - CBotClass::Compile(p, pStack); // completes the definition of the class - } - else - { - m_bCompileClass = false; - CBotFunction::Compile(p, pStack, next); - if (next->IsExtern()) ListFonctions.Add(next->GivName()/* + next->GivParams()*/); - next->m_pProg = this; // keeps pointers to the module - next = next->Next(); - } - } - -// delete m_Prog; // the list of first pass -// m_Prog = temp; // list of the second pass - - if ( !pStack->IsOk() ) - { - m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); - delete m_Prog; - m_Prog = NULL; - } - - delete pBaseToken; - delete pStack; - - return (m_Prog != NULL); + int error = 0; + Stop(); + +// delete m_pClass; + m_pClass->Purge(); // purge the old definitions of classes + // but without destroying the object + m_pClass = NULL; + delete m_Prog; m_Prog= NULL; + + ListFonctions.SetSize(0); + m_ErrorCode = 0; + + if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) + pUser = m_pInstance->m_pUserPtr; + + // transforms the program in Tokens + CBotToken* pBaseToken = CBotToken::CompileTokens(program, error); + if ( pBaseToken == NULL ) return false; + + + CBotCStack* pStack = new CBotCStack(NULL); + CBotToken* p = pBaseToken->GivNext(); // skips the first token (separator) + + pStack->SetBotCall(this); // defined used routines + CBotCall::SetPUser(pUser); + + // first made a quick pass just to take the headers of routines and classes + while ( pStack->IsOk() && p != NULL && p->GivType() != 0) + { + if ( IsOfType(p, ID_SEP) ) continue; // semicolons lurking + + if ( p->GivType() == ID_CLASS || + ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) + { + CBotClass* nxt = CBotClass::Compile1(p, pStack); + if (m_pClass == NULL ) m_pClass = nxt; + else m_pClass->AddNext(nxt); + } + else + { + CBotFunction* next = CBotFunction::Compile1(p, pStack, NULL); + if (m_Prog == NULL ) m_Prog = next; + else m_Prog->AddNext(next); + } + } + if ( !pStack->IsOk() ) + { + m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); + delete m_Prog; + m_Prog = NULL; + delete pBaseToken; + return false; + } + +// CBotFunction* temp = NULL; + CBotFunction* next = m_Prog; // rewind the list + + p = pBaseToken->GivNext(); // returns to the beginning + + while ( pStack->IsOk() && p != NULL && p->GivType() != 0 ) + { + if ( IsOfType(p, ID_SEP) ) continue; // semicolons lurking + + if ( p->GivType() == ID_CLASS || + ( p->GivType() == ID_PUBLIC && p->GivNext()->GivType() == ID_CLASS )) + { + m_bCompileClass = true; + CBotClass::Compile(p, pStack); // completes the definition of the class + } + else + { + m_bCompileClass = false; + CBotFunction::Compile(p, pStack, next); + if (next->IsExtern()) ListFonctions.Add(next->GivName()/* + next->GivParams()*/); + next->m_pProg = this; // keeps pointers to the module + next = next->Next(); + } + } + +// delete m_Prog; // the list of first pass +// m_Prog = temp; // list of the second pass + + if ( !pStack->IsOk() ) + { + m_ErrorCode = pStack->GivError(m_ErrorStart, m_ErrorEnd); + delete m_Prog; + m_Prog = NULL; + } + + delete pBaseToken; + delete pStack; + + return (m_Prog != NULL); } bool CBotProgram::Start(const char* name) { -#if STACKMEM - m_pStack->Delete(); +#if STACKMEM + m_pStack->Delete(); #else - delete m_pStack; + delete m_pStack; #endif - m_pStack = NULL; - - m_pRun = m_Prog; - while (m_pRun != NULL) - { - if ( m_pRun->GivName() == name ) break; - m_pRun = m_pRun->m_next; - } - - if ( m_pRun == NULL ) - { - m_ErrorCode = TX_NORUN; - return false; - } - -#if STACKMEM - m_pStack = CBotStack::FirstStack(); + m_pStack = NULL; + + m_pRun = m_Prog; + while (m_pRun != NULL) + { + if ( m_pRun->GivName() == name ) break; + m_pRun = m_pRun->m_next; + } + + if ( m_pRun == NULL ) + { + m_ErrorCode = TX_NORUN; + return false; + } + +#if STACKMEM + m_pStack = CBotStack::FirstStack(); #else - m_pStack = new CBotStack(NULL); // creates an execution stack + m_pStack = new CBotStack(NULL); // creates an execution stack #endif - m_pStack->SetBotCall(this); // bases for routines + m_pStack->SetBotCall(this); // bases for routines - return true; // we are ready for Run () + return true; // we are ready for Run () } bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop) { - CBotFunction* p = m_Prog; - while (p != NULL) - { - if ( p->GivName() == name ) break; - p = p->m_next; - } + CBotFunction* p = m_Prog; + while (p != NULL) + { + if ( p->GivName() == name ) break; + p = p->m_next; + } - if ( p == NULL ) return false; + if ( p == NULL ) return false; - p->GetPosition(start, stop, modestart, modestop); - return true; + p->GetPosition(start, stop, modestart, modestop); + return true; } bool CBotProgram::Run(void* pUser, int timer) { - bool ok; - - if (m_pStack == NULL || m_pRun == NULL) goto error; - - m_ErrorCode = 0; - if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) - pUser = m_pInstance->m_pUserPtr; - - m_pStack->Reset(pUser); // empty the possible previous error, and resets the timer - if ( timer >= 0 ) m_pStack->SetTimer(timer); - - m_pStack->SetBotCall(this); // bases for routines - -#if STACKRUN - // resumes execution on the top of the stack - ok = m_pStack->Execute(); - if ( ok ) - { -#ifdef _DEBUG - CBotVar* ppVar[3]; - ppVar[0] = CBotVar::Create("aa", CBotTypInt); - ppVar[1] = CBotVar::Create("bb", CBotTypInt); - ppVar[2] = NULL; - ok = m_pRun->Execute(ppVar, m_pStack, m_pInstance); + bool ok; + + if (m_pStack == NULL || m_pRun == NULL) goto error; + + m_ErrorCode = 0; + if (m_pInstance != NULL && m_pInstance->m_pUserPtr != NULL) + pUser = m_pInstance->m_pUserPtr; + + m_pStack->Reset(pUser); // empty the possible previous error, and resets the timer + if ( timer >= 0 ) m_pStack->SetTimer(timer); + + m_pStack->SetBotCall(this); // bases for routines + +#if STACKRUN + // resumes execution on the top of the stack + ok = m_pStack->Execute(); + if ( ok ) + { +#ifdef _DEBUG + CBotVar* ppVar[3]; + ppVar[0] = CBotVar::Create("aa", CBotTypInt); + ppVar[1] = CBotVar::Create("bb", CBotTypInt); + ppVar[2] = NULL; + ok = m_pRun->Execute(ppVar, m_pStack, m_pInstance); #else - // returns to normal execution - ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); + // returns to normal execution + ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); #endif - } + } #else - ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); + ok = m_pRun->Execute(NULL, m_pStack, m_pInstance); #endif - // completed on a mistake? - if (!ok && !m_pStack->IsOk()) - { - m_ErrorCode = m_pStack->GivError(m_ErrorStart, m_ErrorEnd); -#if STACKMEM - m_pStack->Delete(); + // completed on a mistake? + if (!ok && !m_pStack->IsOk()) + { + m_ErrorCode = m_pStack->GivError(m_ErrorStart, m_ErrorEnd); +#if STACKMEM + m_pStack->Delete(); #else - delete m_pStack; + delete m_pStack; #endif - m_pStack = NULL; - return true; // execution is finished! - } + m_pStack = NULL; + return true; // execution is finished! + } - if ( ok ) m_pRun = NULL; // more function in execution - return ok; + if ( ok ) m_pRun = NULL; // more function in execution + return ok; error: - m_ErrorCode = TX_NORUN; - return true; + m_ErrorCode = TX_NORUN; + return true; } void CBotProgram::Stop() { -#if STACKMEM - m_pStack->Delete(); +#if STACKMEM + m_pStack->Delete(); #else - delete m_pStack; + delete m_pStack; #endif - m_pStack = NULL; - m_pRun = NULL; + m_pStack = NULL; + m_pRun = NULL; } bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) { - FunctionName = NULL; - start = end = 0; - if (m_pStack == NULL) return false; + FunctionName = NULL; + start = end = 0; + if (m_pStack == NULL) return false; - m_pStack->GetRunPos(FunctionName, start, end); - return true; + m_pStack->GetRunPos(FunctionName, start, end); + return true; } CBotVar* CBotProgram::GivStackVars(const char* &FunctionName, int level) { - FunctionName = NULL; - if (m_pStack == NULL) return NULL; + FunctionName = NULL; + if (m_pStack == NULL) return NULL; - return m_pStack->GivStackVars(FunctionName, level); + return m_pStack->GivStackVars(FunctionName, level); } void CBotProgram::SetTimer(int n) { - CBotStack::SetTimer( n ); + CBotStack::SetTimer( n ); } int CBotProgram::GivError() { - return m_ErrorCode; + return m_ErrorCode; } void CBotProgram::SetIdent(long n) { - m_Ident = n; + m_Ident = n; } long CBotProgram::GivIdent() { - return m_Ident; + return m_Ident; } bool CBotProgram::GetError(int& code, int& start, int& end) { - code = m_ErrorCode; - start = m_ErrorStart; - end = m_ErrorEnd; - return code > 0; + code = m_ErrorCode; + start = m_ErrorStart; + end = m_ErrorEnd; + return code > 0; } bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) { - code = m_ErrorCode; - start = m_ErrorStart; - end = m_ErrorEnd; - pProg = this; - return code > 0; + code = m_ErrorCode; + start = m_ErrorStart; + end = m_ErrorEnd; + pProg = this; + return code > 0; } CBotString CBotProgram::GivErrorText(int code) { - CBotString TextError; - - TextError.LoadString( code ); - if (TextError.IsEmpty()) - { - char buf[100]; - sprintf(buf, "Exception numéro %d.", code); - TextError = buf; - } - return TextError; + CBotString TextError; + + TextError.LoadString( code ); + if (TextError.IsEmpty()) + { + char buf[100]; + sprintf(buf, "Exception numéro %d.", code); + TextError = buf; + } + return TextError; } CBotFunction* CBotProgram::GivFunctions() { - return m_Prog; + return m_Prog; } bool CBotProgram::AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) { - // stores pointers to the two functions - return CBotCall::AddFunction(name, rExec, rCompile); + // stores pointers to the two functions + return CBotCall::AddFunction(name, rExec, rCompile); } bool WriteWord(FILE* pf, unsigned short w) { - size_t lg; + size_t lg; - lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); + lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); - return (lg == 1); + return (lg == 1); } bool ReadWord(FILE* pf, unsigned short& w) { - size_t lg; + size_t lg; - lg = fread(&w, sizeof( unsigned short ), 1, pf ); + lg = fread(&w, sizeof( unsigned short ), 1, pf ); - return (lg == 1); + return (lg == 1); } bool WriteFloat(FILE* pf, float w) { - size_t lg; + size_t lg; - lg = fwrite(&w, sizeof( float ), 1, pf ); + lg = fwrite(&w, sizeof( float ), 1, pf ); - return (lg == 1); + return (lg == 1); } bool ReadFloat(FILE* pf, float& w) { - size_t lg; + size_t lg; - lg = fread(&w, sizeof( float ), 1, pf ); + lg = fread(&w, sizeof( float ), 1, pf ); - return (lg == 1); + return (lg == 1); } bool WriteLong(FILE* pf, long w) { - size_t lg; + size_t lg; - lg = fwrite(&w, sizeof( long ), 1, pf ); + lg = fwrite(&w, sizeof( long ), 1, pf ); - return (lg == 1); + return (lg == 1); } bool ReadLong(FILE* pf, long& w) { - size_t lg; + size_t lg; - lg = fread(&w, sizeof( long ), 1, pf ); + lg = fread(&w, sizeof( long ), 1, pf ); - return (lg == 1); + return (lg == 1); } bool WriteString(FILE* pf, CBotString s) { - size_t lg1, lg2; + size_t lg1, lg2; - lg1 = s.GivLength(); - if (!WriteWord(pf, lg1)) return false; + lg1 = s.GivLength(); + if (!WriteWord(pf, lg1)) return false; - lg2 = fwrite(s, 1, lg1, pf ); - return (lg1 == lg2); + lg2 = fwrite(s, 1, lg1, pf ); + return (lg1 == lg2); } bool ReadString(FILE* pf, CBotString& s) { - unsigned short w; - char buf[1000]; - size_t lg1, lg2; + unsigned short w; + char buf[1000]; + size_t lg1, lg2; - if (!ReadWord(pf, w)) return false; - lg1 = w; - lg2 = fread(buf, 1, lg1, pf ); - buf[lg2] = 0; + if (!ReadWord(pf, w)) return false; + lg1 = w; + lg2 = fread(buf, 1, lg1, pf ); + buf[lg2] = 0; - s = buf; - return (lg1 == lg2); + s = buf; + return (lg1 == lg2); } bool WriteType(FILE* pf, CBotTypResult type) { - int typ = type.GivType(); - if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; - if ( !WriteWord(pf, typ) ) return false; - if ( typ == CBotTypClass ) - { - CBotClass* p = type.GivClass(); - if ( !WriteString(pf, p->GivName()) ) return false; - } - if ( type.Eq( CBotTypArrayBody ) || - type.Eq( CBotTypArrayPointer ) ) - { - if ( !WriteWord(pf, type.GivLimite()) ) return false; - if ( !WriteType(pf, type.GivTypElem()) ) return false; - } - return true; + int typ = type.GivType(); + if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; + if ( !WriteWord(pf, typ) ) return false; + if ( typ == CBotTypClass ) + { + CBotClass* p = type.GivClass(); + if ( !WriteString(pf, p->GivName()) ) return false; + } + if ( type.Eq( CBotTypArrayBody ) || + type.Eq( CBotTypArrayPointer ) ) + { + if ( !WriteWord(pf, type.GivLimite()) ) return false; + if ( !WriteType(pf, type.GivTypElem()) ) return false; + } + return true; } bool ReadType(FILE* pf, CBotTypResult& type) { - unsigned short w, ww; - if ( !ReadWord(pf, w) ) return false; - type.SetType(w); - - if ( type.Eq( CBotTypIntrinsic ) ) - { - type = CBotTypResult( w, "point" ); - } - - if ( type.Eq( CBotTypClass ) ) - { - CBotString s; - if ( !ReadString(pf, s) ) return false; - type = CBotTypResult( w, s ); - } - - if ( type.Eq( CBotTypArrayPointer ) || - type.Eq( CBotTypArrayBody ) ) - { - CBotTypResult r; - if ( !ReadWord(pf, ww) ) return false; - if ( !ReadType(pf, r) ) return false; - type = CBotTypResult( w, r ); - type.SetLimite((short)ww); - } - return true; + unsigned short w, ww; + if ( !ReadWord(pf, w) ) return false; + type.SetType(w); + + if ( type.Eq( CBotTypIntrinsic ) ) + { + type = CBotTypResult( w, "point" ); + } + + if ( type.Eq( CBotTypClass ) ) + { + CBotString s; + if ( !ReadString(pf, s) ) return false; + type = CBotTypResult( w, s ); + } + + if ( type.Eq( CBotTypArrayPointer ) || + type.Eq( CBotTypArrayBody ) ) + { + CBotTypResult r; + if ( !ReadWord(pf, ww) ) return false; + if ( !ReadType(pf, r) ) return false; + type = CBotTypResult( w, r ); + type.SetLimite((short)ww); + } + return true; } bool CBotProgram::DefineNum(const char* name, long val) { - return CBotToken::DefineNum(name, val); + return CBotToken::DefineNum(name, val); } bool CBotProgram::SaveState(FILE* pf) { - if (!WriteWord( pf, CBOTVERSION)) return false; - - - if ( m_pStack != NULL ) - { - if (!WriteWord( pf, 1)) return false; - if (!WriteString( pf, m_pRun->GivName() )) return false; - if (!m_pStack->SaveState(pf)) return false; - } - else - { - if (!WriteWord( pf, 0)) return false; - } - return true; + if (!WriteWord( pf, CBOTVERSION)) return false; + + + if ( m_pStack != NULL ) + { + if (!WriteWord( pf, 1)) return false; + if (!WriteString( pf, m_pRun->GivName() )) return false; + if (!m_pStack->SaveState(pf)) return false; + } + else + { + if (!WriteWord( pf, 0)) return false; + } + return true; } bool CBotProgram::RestoreState(FILE* pf) { - unsigned short w; - CBotString s; + unsigned short w; + CBotString s; - Stop(); + Stop(); - if (!ReadWord( pf, w )) return false; - if ( w != CBOTVERSION ) return false; + if (!ReadWord( pf, w )) return false; + if ( w != CBOTVERSION ) return false; - if (!ReadWord( pf, w )) return false; - if ( w == 0 ) return true; + if (!ReadWord( pf, w )) return false; + if ( w == 0 ) return true; - if (!ReadString( pf, s )) return false; - Start(s); // point de reprise + if (!ReadString( pf, s )) return false; + Start(s); // point de reprise -#if STACKMEM - m_pStack->Delete(); +#if STACKMEM + m_pStack->Delete(); #else - delete m_pStack; + delete m_pStack; #endif - m_pStack = NULL; + m_pStack = NULL; - // retrieves the stack from the memory - // uses a NULL pointer (m_pStack) but it's ok like that - if (!m_pStack->RestoreState(pf, m_pStack)) return false; - m_pStack->SetBotCall(this); // bases for routines + // retrieves the stack from the memory + // uses a NULL pointer (m_pStack) but it's ok like that + if (!m_pStack->RestoreState(pf, m_pStack)) return false; + m_pStack->SetBotCall(this); // bases for routines - // restored some states in the stack according to the structure - m_pRun->RestoreState(NULL, m_pStack, m_pInstance); - return true; + // restored some states in the stack according to the structure + m_pRun->RestoreState(NULL, m_pStack, m_pInstance); + return true; } int CBotProgram::GivVersion() { - return CBOTVERSION; + return CBOTVERSION; } ////////////////////////////////////////////////////////////////////////////////////////////////////// CBotCall* CBotCall::m_ListCalls = NULL; - + CBotCall::CBotCall(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) { - m_name = name; - m_rExec = rExec; - m_rComp = rCompile; - m_next = NULL; - m_nFuncIdent = CBotVar::NextUniqNum(); + m_name = name; + m_rExec = rExec; + m_rComp = rCompile; + m_next = NULL; + m_nFuncIdent = CBotVar::NextUniqNum(); } CBotCall::~CBotCall() { - if (m_next) delete m_next; - m_next = NULL; + if (m_next) delete m_next; + m_next = NULL; } void CBotCall::Free() { - delete CBotCall::m_ListCalls; + delete CBotCall::m_ListCalls; } bool CBotCall::AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) { - CBotCall* p = m_ListCalls; - CBotCall* pp = NULL; - - if ( p != NULL ) while ( p->m_next != NULL ) - { - if ( p->GivName() == name ) - { - // frees redefined function - if ( pp ) pp->m_next = p->m_next; - else m_ListCalls = p->m_next; - pp = p; - p = p->m_next; - pp->m_next = NULL; // not to destroy the following list - delete pp; - continue; - } - pp = p; // previous pointer - p = p->m_next; - } - - pp = new CBotCall(name, rExec, rCompile); - - if (p) p->m_next = pp; - else m_ListCalls = pp; - - return true; + CBotCall* p = m_ListCalls; + CBotCall* pp = NULL; + + if ( p != NULL ) while ( p->m_next != NULL ) + { + if ( p->GivName() == name ) + { + // frees redefined function + if ( pp ) pp->m_next = p->m_next; + else m_ListCalls = p->m_next; + pp = p; + p = p->m_next; + pp->m_next = NULL; // not to destroy the following list + delete pp; + continue; + } + pp = p; // previous pointer + p = p->m_next; + } + + pp = new CBotCall(name, rExec, rCompile); + + if (p) p->m_next = pp; + else m_ListCalls = pp; + + return true; } @@ -620,25 +620,25 @@ bool CBotCall::AddFunction(const char* name, // in a chained list of variables CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false) { - int i = 0; - CBotVar* pVar = NULL; - - while( true ) - { - ppVars[i]; - if ( ppVars[i] == NULL ) break; - - CBotVar* pp = CBotVar::Create(ppVars[i]); - if (bSetVal) pp->Copy(ppVars[i]); - else - if ( ppVars[i]->GivType() == CBotTypPointer ) - pp->SetClass( ppVars[i]->GivClass()); + int i = 0; + CBotVar* pVar = NULL; + + while( true ) + { + ppVars[i]; + if ( ppVars[i] == NULL ) break; + + CBotVar* pp = CBotVar::Create(ppVars[i]); + if (bSetVal) pp->Copy(ppVars[i]); + else + if ( ppVars[i]->GivType() == CBotTypPointer ) + pp->SetClass( ppVars[i]->GivClass()); // copy the pointer according to indirections - if (pVar == NULL) pVar = pp; - else pVar->AddNext(pp); - i++; - } - return pVar; + if (pVar == NULL) pVar = pp; + else pVar->AddNext(pp); + i++; + } + return pVar; } // is acceptable by a call procedure name @@ -646,212 +646,212 @@ CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false) CBotTypResult CBotCall::CompileCall(CBotToken* &p, CBotVar** ppVar, CBotCStack* pStack, long& nIdent) { - nIdent = 0; - CBotCall* pt = m_ListCalls; - CBotString name = p->GivString(); - - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - CBotVar* pVar = MakeListVars(ppVar); - CBotVar* pVar2 = pVar; - CBotTypResult r = pt->m_rComp(pVar2, m_pUser); - int ret = r.GivType(); - - // if a class is returned, it is actually a pointer - if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer ); - - if ( ret > 20 ) - { - if (pVar2) pStack->SetError(ret, p /*pVar2->GivToken()*/ ); - } - delete pVar; - nIdent = pt->m_nFuncIdent; - return r; - } - pt = pt->m_next; - } - return -1; + nIdent = 0; + CBotCall* pt = m_ListCalls; + CBotString name = p->GivString(); + + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + CBotVar* pVar = MakeListVars(ppVar); + CBotVar* pVar2 = pVar; + CBotTypResult r = pt->m_rComp(pVar2, m_pUser); + int ret = r.GivType(); + + // if a class is returned, it is actually a pointer + if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer ); + + if ( ret > 20 ) + { + if (pVar2) pStack->SetError(ret, p /*pVar2->GivToken()*/ ); + } + delete pVar; + nIdent = pt->m_nFuncIdent; + return r; + } + pt = pt->m_next; + } + return -1; } void* CBotCall::m_pUser = NULL; void CBotCall::SetPUser(void* pUser) { - m_pUser = pUser; + m_pUser = pUser; } bool CBotCall::CheckCall(const char* name) { - CBotCall* p = m_ListCalls; - - while ( p != NULL ) - { - if ( name == p->GivName() ) return true; - p = p->m_next; - } - return false; + CBotCall* p = m_ListCalls; + + while ( p != NULL ) + { + if ( name == p->GivName() ) return true; + p = p->m_next; + } + return false; } CBotString CBotCall::GivName() { - return m_name; + return m_name; } CBotCall* CBotCall::Next() { - return m_next; + return m_next; } int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype) { - CBotCall* pt = m_ListCalls; - - if ( nIdent ) while ( pt != NULL ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - goto fund; - } - pt = pt->m_next; - } - - pt = m_ListCalls; - - if ( token != NULL ) - { - CBotString name = token->GivString(); - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - nIdent = pt->m_nFuncIdent; - goto fund; - } - pt = pt->m_next; - } - } - - return -1; + CBotCall* pt = m_ListCalls; + + if ( nIdent ) while ( pt != NULL ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + goto fund; + } + pt = pt->m_next; + } + + pt = m_ListCalls; + + if ( token != NULL ) + { + CBotString name = token->GivString(); + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + nIdent = pt->m_nFuncIdent; + goto fund; + } + pt = pt->m_next; + } + } + + return -1; fund: #if !STACKRUN - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVarToDelete = pVar; - - // creates a variable to the result - CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); - - CBotVar* pRes = pResult; - int Exception = 0; - int res = pt->m_rExec(pVar, pResult, Exception, pStack->GivPUser()); - - if ( pResult != pRes ) delete pRes; // different result if made - delete pVarToDelete; - - if (res == false) - { - if (Exception!=0) - { - pStack->SetError(Exception, token); - } - delete pResult; - return false; - } - pStack->SetVar(pResult); - - if ( rettype.GivType() > 0 && pResult == NULL ) - { - pStack->SetError(TX_NORETVAL, token); - } - nIdent = pt->m_nFuncIdent; - return true; + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVarToDelete = pVar; + + // creates a variable to the result + CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); + + CBotVar* pRes = pResult; + int Exception = 0; + int res = pt->m_rExec(pVar, pResult, Exception, pStack->GivPUser()); + + if ( pResult != pRes ) delete pRes; // different result if made + delete pVarToDelete; + + if (res == false) + { + if (Exception!=0) + { + pStack->SetError(Exception, token); + } + delete pResult; + return false; + } + pStack->SetVar(pResult); + + if ( rettype.GivType() > 0 && pResult == NULL ) + { + pStack->SetError(TX_NORETVAL, token); + } + nIdent = pt->m_nFuncIdent; + return true; #else - CBotStack* pile = pStack->AddStackEOX(pt); - if ( pile == EOX ) return true; + CBotStack* pile = pStack->AddStackEOX(pt); + if ( pile == EOX ) return true; - // lists the parameters depending on the contents of the stack (pStackVar) + // lists the parameters depending on the contents of the stack (pStackVar) - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVarToDelete = pVar; + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVarToDelete = pVar; - // creates a variable to the result - CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); + // creates a variable to the result + CBotVar* pResult = rettype.Eq(0) ? NULL : CBotVar::Create("", rettype); - pile->SetVar( pVar ); + pile->SetVar( pVar ); - CBotStack* pile2 = pile->AddStack(); - pile2->SetVar( pResult ); + CBotStack* pile2 = pile->AddStack(); + pile2->SetVar( pResult ); - pile->SetError(0, token); // for the position on error + away - return pt->Run( pStack ); + pile->SetError(0, token); // for the position on error + away + return pt->Run( pStack ); #endif } -#if STACKRUN +#if STACKRUN bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) { - CBotCall* pt = m_ListCalls; - - { - CBotString name = token->GivString(); - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - nIdent = pt->m_nFuncIdent; - - CBotStack* pile = pStack->RestoreStackEOX(pt); - if ( pile == NULL ) return true; - - CBotStack* pile2 = pile->RestoreStack(); - return true; - } - pt = pt->m_next; - } - } - - return false; + CBotCall* pt = m_ListCalls; + + { + CBotString name = token->GivString(); + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + nIdent = pt->m_nFuncIdent; + + CBotStack* pile = pStack->RestoreStackEOX(pt); + if ( pile == NULL ) return true; + + CBotStack* pile2 = pile->RestoreStack(); + return true; + } + pt = pt->m_next; + } + } + + return false; } bool CBotCall::Run(CBotStack* pStack) { - CBotStack* pile = pStack->AddStackEOX(this); - if ( pile == EOX ) return true; - CBotVar* pVar = pile->GivVar(); - - CBotStack* pile2 = pile->AddStack(); - CBotVar* pResult = pile2->GivVar(); - CBotVar* pRes = pResult; - - int Exception = 0; - int res = m_rExec(pVar, pResult, Exception, pStack->GivPUser()); - - if (res == false) - { - if (Exception!=0) - { - pStack->SetError(Exception); - } - if ( pResult != pRes ) delete pResult; // different result if made - return false; - } - - if ( pResult != NULL ) pStack->SetCopyVar( pResult ); - if ( pResult != pRes ) delete pResult; // different result if made - - return true; + CBotStack* pile = pStack->AddStackEOX(this); + if ( pile == EOX ) return true; + CBotVar* pVar = pile->GivVar(); + + CBotStack* pile2 = pile->AddStack(); + CBotVar* pResult = pile2->GivVar(); + CBotVar* pRes = pResult; + + int Exception = 0; + int res = m_rExec(pVar, pResult, Exception, pStack->GivPUser()); + + if (res == false) + { + if (Exception!=0) + { + pStack->SetError(Exception); + } + if ( pResult != pRes ) delete pResult; // different result if made + return false; + } + + if ( pResult != NULL ) pStack->SetCopyVar( pResult ); + if ( pResult != pRes ) delete pResult; // different result if made + + return true; } #endif @@ -859,168 +859,168 @@ bool CBotCall::Run(CBotStack* pStack) /////////////////////////////////////////////////////////////////////////////////////// CBotCallMethode::CBotCallMethode(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) { - m_name = name; - m_rExec = rExec; - m_rComp = rCompile; - m_next = NULL; - m_nFuncIdent = CBotVar::NextUniqNum(); + m_name = name; + m_rExec = rExec; + m_rComp = rCompile; + m_next = NULL; + m_nFuncIdent = CBotVar::NextUniqNum(); } CBotCallMethode::~CBotCallMethode() { - delete m_next; - m_next = NULL; + delete m_next; + m_next = NULL; } // is acceptable by a call procedure name // and given parameters CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, - CBotVar** ppVar, CBotCStack* pStack, - long& nIdent) + CBotVar** ppVar, CBotCStack* pStack, + long& nIdent) { - CBotCallMethode* pt = this; - nIdent = 0; - - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVar2 = pVar; - CBotTypResult r = pt->m_rComp(pThis, pVar2); - int ret = r.GivType(); - if ( ret > 20 ) - { - if (pVar2) pStack->SetError(ret, pVar2->GivToken()); - } - delete pVar; - nIdent = pt->m_nFuncIdent; - return r; - } - pt = pt->m_next; - } - return CBotTypResult(-1); + CBotCallMethode* pt = this; + nIdent = 0; + + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVar2 = pVar; + CBotTypResult r = pt->m_rComp(pThis, pVar2); + int ret = r.GivType(); + if ( ret > 20 ) + { + if (pVar2) pStack->SetError(ret, pVar2->GivToken()); + } + delete pVar; + nIdent = pt->m_nFuncIdent; + return r; + } + pt = pt->m_next; + } + return CBotTypResult(-1); } CBotString CBotCallMethode::GivName() { - return m_name; + return m_name; } CBotCallMethode* CBotCallMethode::Next() { - return m_next; + return m_next; } void CBotCallMethode::AddNext(CBotCallMethode* pt) { - CBotCallMethode* p = this; - while ( p->m_next != NULL ) p = p->m_next; + CBotCallMethode* p = this; + while ( p->m_next != NULL ) p = p->m_next; - p->m_next = pt; + p->m_next = pt; } int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken) { - CBotCallMethode* pt = this; - - // search by the identifier - - if ( nIdent ) while ( pt != NULL ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVars, true); - CBotVar* pVarToDelete = pVar; - - // then calls the routine external to the module - - int Exception = 0; - int res = pt->m_rExec(pThis, pVar, pResult, Exception); - pStack->SetVar(pResult); - - if (res == false) - { - if (Exception!=0) - { -// pStack->SetError(Exception, pVar->GivToken()); - pStack->SetError(Exception, pToken); - } - delete pVarToDelete; - return false; - } - delete pVarToDelete; - return true; - } - pt = pt->m_next; - } - - // search by name - - while ( pt != NULL ) - { - if ( pt->m_name == name ) - { - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVars, true); - CBotVar* pVarToDelete = pVar; - - int Exception = 0; - int res = pt->m_rExec(pThis, pVar, pResult, Exception); - pStack->SetVar(pResult); - - if (res == false) - { - if (Exception!=0) - { -// pStack->SetError(Exception, pVar->GivToken()); - pStack->SetError(Exception, pToken); - } - delete pVarToDelete; - return false; - } - delete pVarToDelete; - nIdent = pt->m_nFuncIdent; - return true; - } - pt = pt->m_next; - } - - return -1; + CBotCallMethode* pt = this; + + // search by the identifier + + if ( nIdent ) while ( pt != NULL ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVars, true); + CBotVar* pVarToDelete = pVar; + + // then calls the routine external to the module + + int Exception = 0; + int res = pt->m_rExec(pThis, pVar, pResult, Exception); + pStack->SetVar(pResult); + + if (res == false) + { + if (Exception!=0) + { +// pStack->SetError(Exception, pVar->GivToken()); + pStack->SetError(Exception, pToken); + } + delete pVarToDelete; + return false; + } + delete pVarToDelete; + return true; + } + pt = pt->m_next; + } + + // search by name + + while ( pt != NULL ) + { + if ( pt->m_name == name ) + { + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVars, true); + CBotVar* pVarToDelete = pVar; + + int Exception = 0; + int res = pt->m_rExec(pThis, pVar, pResult, Exception); + pStack->SetVar(pResult); + + if (res == false) + { + if (Exception!=0) + { +// pStack->SetError(Exception, pVar->GivToken()); + pStack->SetError(Exception, pToken); + } + delete pVarToDelete; + return false; + } + delete pVarToDelete; + nIdent = pt->m_nFuncIdent; + return true; + } + pt = pt->m_next; + } + + return -1; } bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - if ( pVar == NULL ) return TX_LOWPARAM; + if ( pVar == NULL ) return TX_LOWPARAM; - int i = 0; - pVar = pVar->GivItemList(); + int i = 0; + pVar = pVar->GivItemList(); - while ( pVar != NULL ) - { - i++; - pVar = pVar->GivNext(); - } + while ( pVar != NULL ) + { + i++; + pVar = pVar->GivNext(); + } - pResult->SetValInt(i); - return true; + pResult->SetValInt(i); + return true; } CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) { - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - if ( pVar->GivType() != CBotTypArrayPointer ) - return CBotTypResult( TX_BADPARAM ); - return CBotTypResult( CBotTypInt ); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + if ( pVar->GivType() != CBotTypArrayPointer ) + return CBotTypResult( TX_BADPARAM ); + return CBotTypResult( CBotTypInt ); } @@ -1028,18 +1028,18 @@ CBotString CBotProgram::m_DebugVarStr = ""; bool rCBotDebug( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - pResult->SetValString( CBotProgram::m_DebugVarStr ); + pResult->SetValString( CBotProgram::m_DebugVarStr ); - return true; + return true; } CBotTypResult cCBotDebug( CBotVar* &pVar, void* pUser ) { - // no parameter - if ( pVar != NULL ) return CBotTypResult( TX_OVERPARAM ); + // no parameter + if ( pVar != NULL ) return CBotTypResult( TX_OVERPARAM ); - // function returns a result "string" - return CBotTypResult( CBotTypString ); + // function returns a result "string" + return CBotTypResult( CBotTypString ); } @@ -1047,58 +1047,58 @@ CBotTypResult cCBotDebug( CBotVar* &pVar, void* pUser ) void CBotProgram::Init() { - CBotToken::DefineNum( "CBotErrOpenPar", 5000) ; // missing the opening parenthesis - CBotToken::DefineNum( "CBotErrClosePar", 5001) ; // missing the closing parenthesis - CBotToken::DefineNum( "CBotErrNotBoolean", 5002) ; // expression must be a boolean - CBotToken::DefineNum( "CBotErrUndefVar", 5003) ; // undeclared variable - CBotToken::DefineNum( "CBotErrBadLeft", 5004) ; // impossible assignment (5 = ...) - CBotToken::DefineNum( "CBotErrNoTerminator", 5005) ;// semicolon expected - CBotToken::DefineNum( "CBotErrCaseOut", 5006) ; // case outside a switch - CBotToken::DefineNum( "CBotErrCloseBlock", 5008) ; // missing " } " - CBotToken::DefineNum( "CBotErrElseWhitoutIf", 5009) ;// else without matching if - CBotToken::DefineNum( "CBotErrOpenBlock", 5010) ; // missing " { " - CBotToken::DefineNum( "CBotErrBadType1", 5011) ; // wrong type for the assignment - CBotToken::DefineNum( "CBotErrRedefVar", 5012) ; // redefinition of the variable - CBotToken::DefineNum( "CBotErrBadType2", 5013) ; // two operands are incompatible - CBotToken::DefineNum( "CBotErrUndefCall", 5014) ; // routine unknown - CBotToken::DefineNum( "CBotErrNoDoubleDots", 5015) ;// " : " expected - CBotToken::DefineNum( "CBotErrBreakOutside", 5017) ;// break outside of a loop - CBotToken::DefineNum( "CBotErrUndefLabel", 5019) ; // unknown label - CBotToken::DefineNum( "CBotErrLabel", 5018) ; // label can not get here - CBotToken::DefineNum( "CBotErrNoCase", 5020) ; // missing " case " - CBotToken::DefineNum( "CBotErrBadNum", 5021) ; // expected number - CBotToken::DefineNum( "CBotErrVoid", 5022) ; // " void " not possble here - CBotToken::DefineNum( "CBotErrNoType", 5023) ; // type declaration expected - CBotToken::DefineNum( "CBotErrNoVar", 5024) ; // variable name expected - CBotToken::DefineNum( "CBotErrNoFunc", 5025) ; // expected function name - CBotToken::DefineNum( "CBotErrOverParam", 5026) ; // too many parameters - CBotToken::DefineNum( "CBotErrRedefFunc", 5027) ; // this function already exists - CBotToken::DefineNum( "CBotErrLowParam", 5028) ; // not enough parameters - CBotToken::DefineNum( "CBotErrBadParam", 5029) ; // mauvais types de paramètres - CBotToken::DefineNum( "CBotErrNbParam", 5030) ; // wrong number of parameters - CBotToken::DefineNum( "CBotErrUndefItem", 5031) ; // element does not exist in the class - CBotToken::DefineNum( "CBotErrUndefClass", 5032) ; // variable is not a class - CBotToken::DefineNum( "CBotErrNoConstruct", 5033) ; // no appropriate constructor - CBotToken::DefineNum( "CBotErrRedefClass", 5034) ; // Class already exists - CBotToken::DefineNum( "CBotErrCloseIndex", 5035) ; // " ] " expected - CBotToken::DefineNum( "CBotErrReserved", 5036) ; // reserved word (for a DefineNum) + CBotToken::DefineNum( "CBotErrOpenPar", 5000) ; // missing the opening parenthesis + CBotToken::DefineNum( "CBotErrClosePar", 5001) ; // missing the closing parenthesis + CBotToken::DefineNum( "CBotErrNotBoolean", 5002) ; // expression must be a boolean + CBotToken::DefineNum( "CBotErrUndefVar", 5003) ; // undeclared variable + CBotToken::DefineNum( "CBotErrBadLeft", 5004) ; // impossible assignment (5 = ...) + CBotToken::DefineNum( "CBotErrNoTerminator", 5005) ;// semicolon expected + CBotToken::DefineNum( "CBotErrCaseOut", 5006) ; // case outside a switch + CBotToken::DefineNum( "CBotErrCloseBlock", 5008) ; // missing " } " + CBotToken::DefineNum( "CBotErrElseWhitoutIf", 5009) ;// else without matching if + CBotToken::DefineNum( "CBotErrOpenBlock", 5010) ; // missing " { " + CBotToken::DefineNum( "CBotErrBadType1", 5011) ; // wrong type for the assignment + CBotToken::DefineNum( "CBotErrRedefVar", 5012) ; // redefinition of the variable + CBotToken::DefineNum( "CBotErrBadType2", 5013) ; // two operands are incompatible + CBotToken::DefineNum( "CBotErrUndefCall", 5014) ; // routine unknown + CBotToken::DefineNum( "CBotErrNoDoubleDots", 5015) ;// " : " expected + CBotToken::DefineNum( "CBotErrBreakOutside", 5017) ;// break outside of a loop + CBotToken::DefineNum( "CBotErrUndefLabel", 5019) ; // unknown label + CBotToken::DefineNum( "CBotErrLabel", 5018) ; // label can not get here + CBotToken::DefineNum( "CBotErrNoCase", 5020) ; // missing " case " + CBotToken::DefineNum( "CBotErrBadNum", 5021) ; // expected number + CBotToken::DefineNum( "CBotErrVoid", 5022) ; // " void " not possble here + CBotToken::DefineNum( "CBotErrNoType", 5023) ; // type declaration expected + CBotToken::DefineNum( "CBotErrNoVar", 5024) ; // variable name expected + CBotToken::DefineNum( "CBotErrNoFunc", 5025) ; // expected function name + CBotToken::DefineNum( "CBotErrOverParam", 5026) ; // too many parameters + CBotToken::DefineNum( "CBotErrRedefFunc", 5027) ; // this function already exists + CBotToken::DefineNum( "CBotErrLowParam", 5028) ; // not enough parameters + CBotToken::DefineNum( "CBotErrBadParam", 5029) ; // mauvais types de paramètres + CBotToken::DefineNum( "CBotErrNbParam", 5030) ; // wrong number of parameters + CBotToken::DefineNum( "CBotErrUndefItem", 5031) ; // element does not exist in the class + CBotToken::DefineNum( "CBotErrUndefClass", 5032) ; // variable is not a class + CBotToken::DefineNum( "CBotErrNoConstruct", 5033) ; // no appropriate constructor + CBotToken::DefineNum( "CBotErrRedefClass", 5034) ; // Class already exists + CBotToken::DefineNum( "CBotErrCloseIndex", 5035) ; // " ] " expected + CBotToken::DefineNum( "CBotErrReserved", 5036) ; // reserved word (for a DefineNum) // Here are the list of errors that can be returned by the module // for the execution - CBotToken::DefineNum( "CBotErrZeroDiv", 6000) ; // division by zero - CBotToken::DefineNum( "CBotErrNotInit", 6001) ; // uninitialized variable - CBotToken::DefineNum( "CBotErrBadThrow", 6002) ; // throw a negative value - CBotToken::DefineNum( "CBotErrNoRetVal", 6003) ; // function did not return results - CBotToken::DefineNum( "CBotErrNoRun", 6004) ; // active Run () without a function - CBotToken::DefineNum( "CBotErrUndefFunc", 6005) ; // Calling a function that no longer exists + CBotToken::DefineNum( "CBotErrZeroDiv", 6000) ; // division by zero + CBotToken::DefineNum( "CBotErrNotInit", 6001) ; // uninitialized variable + CBotToken::DefineNum( "CBotErrBadThrow", 6002) ; // throw a negative value + CBotToken::DefineNum( "CBotErrNoRetVal", 6003) ; // function did not return results + CBotToken::DefineNum( "CBotErrNoRun", 6004) ; // active Run () without a function + CBotToken::DefineNum( "CBotErrUndefFunc", 6005) ; // Calling a function that no longer exists - CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); + CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); - InitStringFunctions(); + InitStringFunctions(); - // just a function for various debug - CBotProgram::AddFunction("CBOTDEBUGDD", rCBotDebug, cCBotDebug); + // just a function for various debug + CBotProgram::AddFunction("CBOTDEBUGDD", rCBotDebug, cCBotDebug); //TODO implement this deletion // DeleteFile("CbotDebug.txt"); @@ -1106,8 +1106,8 @@ void CBotProgram::Init() void CBotProgram::Free() { - CBotToken::Free() ; - CBotCall ::Free() ; - CBotClass::Free() ; + CBotToken::Free() ; + CBotCall ::Free() ; + CBotClass::Free() ; } diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 6acd96e..1b42c23 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -1,692 +1,692 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. -///////////////////////////////////////////////////// - -//strings management - -#include "CBot.h" - -#include -#include -#include - -//Map is filled with id-string pars that are needed for CBot language parsing -const std::map CBotString::s_keywordString = -{ - {ID_IF, "if"}, - {ID_ELSE, "else"}, - {ID_WHILE, "while"}, - {ID_DO, "do"}, - {ID_FOR, "for"}, - {ID_BREAK, "break"}, - {ID_CONTINUE, "continue"}, - {ID_SWITCH, "switch"}, - {ID_CASE, "case"}, - {ID_DEFAULT, "default"}, - {ID_TRY, "try"}, - {ID_THROW, "throw"}, - {ID_CATCH, "catch"}, - {ID_FINALLY, "finally"}, - {ID_TXT_AND, "and"}, - {ID_TXT_OR, "or"}, - {ID_TXT_NOT, "not"}, - {ID_RETURN, "return"}, - {ID_CLASS, "class"}, - {ID_EXTENDS, "extends"}, - {ID_SYNCHO, "synchronized"}, - {ID_NEW, "new"}, - {ID_PUBLIC, "public"}, - {ID_EXTERN, "extern"}, - {ID_FINAL, "final"}, - {ID_STATIC, "static"}, - {ID_PROTECTED, "protected"}, - {ID_PRIVATE, "private"}, - {ID_REPEAT, "repeat"}, - {ID_DEBUGDD, "STARTDEBUGDD"}, - {ID_INT, "int"}, - {ID_FLOAT, "float"}, - {ID_BOOLEAN, "boolean"}, - {ID_STRING, "string"}, - {ID_VOID, "void"}, - {ID_BOOL, "bool"}, - {ID_TRUE, "true"}, - {ID_FALSE, "false"}, - {ID_NULL, "null"}, - {ID_NAN, "nan"}, - {ID_OPENPAR, "("}, - {ID_CLOSEPAR, ")"}, - {ID_OPBLK, "{"}, - {ID_CLBLK, "}"}, - {ID_SEP, "},"}, - {ID_COMMA, ","}, - {ID_DOTS, ":"}, - {ID_DOT, "."}, - {ID_OPBRK, "["}, - {ID_CLBRK, "]"}, - {ID_DBLDOTS, "::"}, - {ID_LOGIC, "?"}, - {ID_ADD, "+"}, - {ID_SUB, "-"}, - {ID_MUL, "*"}, - {ID_DIV, "/"}, - {ID_ASS, "="}, - {ID_ASSADD, "+="}, - {ID_ASSSUB, "-="}, - {ID_ASSMUL, "*="}, - {ID_ASSDIV, "/="}, - {ID_ASSOR, "|="}, - {ID_ASSAND, "&="}, - {ID_ASSXOR, "^="}, - {ID_ASSSL, "<<="}, - {ID_ASSSR, ">>>="}, - {ID_ASSASR, ">>="}, - {ID_SL, "<<"}, - {ID_SR, ">>"}, - {ID_ASR, ">>"}, - {ID_INC, "++"}, - {ID_DEC, "--"}, - {ID_LO, "<"}, - {ID_HI, ">"}, - {ID_LS, "<<"}, - {ID_HS, ">="}, - {ID_EQ, "=="}, - {ID_NE, "!="}, - {ID_AND, "&"}, - {ID_XOR, "^"}, - {ID_OR, "|"}, - {ID_LOG_AND, "&&"}, - {ID_LOG_OR, "||"}, - {ID_LOG_NOT, "!"}, - {ID_NOT, "~"}, - {ID_MODULO, "%"}, - {ID_POWER, "**"}, - {ID_ASSMODULO, "%="}, - {ID_SUPER, "super"}, - {TX_UNDEF, "undefined"}, - {TX_NAN, "not a number"} -}; - -CBotString::CBotString() -{ - m_ptr = NULL; - m_lg = 0; -} - -CBotString::~CBotString() -{ - free(m_ptr); //we can call free on null pointer as it's save -} - - -CBotString::CBotString(const char* p) -{ - m_lg = strlen(p); - - m_ptr = NULL; - if (m_lg>0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, p); - } -} - -CBotString::CBotString(const CBotString& srcString) -{ - m_lg = srcString.m_lg; - - m_ptr = NULL; - if (m_lg>0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, srcString.m_ptr); - } -} - - - - -int CBotString::GivLength() -{ - if (m_ptr == NULL) return 0; - return strlen( m_ptr ); -} - - - -CBotString CBotString::Left(int nCount) const -{ - char chain[2000]; - - size_t i; - for (i = 0; i < m_lg && i < nCount && i < 1999; ++i) - { - chain[i] = m_ptr[i]; - } - chain[i] = 0 ; - - return CBotString(chain); -} - -CBotString CBotString::Right(int nCount) const -{ - char chain[2000]; - - int i = m_lg - nCount; - if ( i < 0 ) i = 0; - - size_t j; - for (size_t j = 0 ; i < m_lg && i < 1999; ++i) - { - chain[j++] = m_ptr[i]; - } - chain[j] = 0 ; - - return CBotString(chain); -} - -CBotString CBotString::Mid(int nFirst, int nCount) const -{ - char chain[2000]; - - size_t i; - for (i = nFirst; i < m_lg && i < 1999 && i <= nFirst + nCount; ++i) - { - chain[i] = m_ptr[i]; - } - chain[i] = 0 ; - - return CBotString(chain); -} - -CBotString CBotString::Mid(int nFirst) const -{ - char chain[2000]; - - size_t i; - for (i = nFirst; i < m_lg && i < 1999 ; ++i) - { - chain[i] = m_ptr[i]; - } - chain[i] = 0 ; - - return CBotString(chain); -} - - -int CBotString::Find(const char c) -{ - for (size_t i = 0; i < m_lg; ++i) - { - if (m_ptr[i] == c) return i; - } - return -1; -} - -int CBotString::Find(const char * lpsz) -{ - int l = strlen(lpsz); - - for (size_t i = 0; i <= m_lg-l; ++i) - { - for (size_t j = 0; j < l; ++j) - { - if (m_ptr[i+j] != lpsz[j]) goto bad; - } - return i; -bad:; - } - return -1; -} - -int CBotString::ReverseFind(const char c) -{ - int i; - for (i = m_lg-1; i >= 0; --i) - { - if (m_ptr[i] == c) return i; - } - return -1; -} - -int CBotString::ReverseFind(const char * lpsz) -{ - int i, j; - int l = strlen(lpsz); - - for (i = m_lg-l; i >= 0; --i) - { - for (j = 0; j < l; ++j) - { - if (m_ptr[i+j] != lpsz[j]) goto bad; - } - return i; -bad:; - } - return -1; -} - -CBotString CBotString::Mid(int start, int lg) -{ - CBotString res; - if (start >= m_lg) return res; - - if ( lg < 0 ) lg = m_lg - start; - - char* p = (char*)malloc(m_lg+1); - strcpy(p, m_ptr+start); - p[lg] = 0; - - res = p; - free(p); - return res; -} - -void CBotString::MakeUpper() -{ - for (size_t i = 0; i < m_lg && i < 1999 ; ++i) - { - char c = m_ptr[i]; - if ( c >= 'a' && c <= 'z' ) m_ptr[i] = c - 'a' + 'A'; - } -} - -void CBotString::MakeLower() -{ - for (size_t i = 0; i < m_lg && i < 1999 ; ++i) - { - char c = m_ptr[i]; - if ( c >= 'A' && c <= 'Z' ) m_ptr[i] = c - 'A' + 'a'; - } -} - -bool CBotString::LoadString(unsigned int id) -{ - const char * str = NULL; - str = MapIdToString((EID)id); - if (m_ptr != NULL) free(m_ptr); - - m_lg = strlen(str); - m_ptr = NULL; - if (m_lg > 0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, str); - return true; - } - return false; -} - - -const CBotString& CBotString::operator=(const CBotString& stringSrc) -{ - free(m_ptr); - m_ptr = NULL; - - m_lg = stringSrc.m_lg; - - if (m_lg > 0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, stringSrc.m_ptr); - } - - return *this; -} - -CBotString operator+(const CBotString& string, const char * lpsz) -{ - CBotString s(string); - s += lpsz; - return s; -} - -const CBotString& CBotString::operator+(const CBotString& stringSrc) -{ - char* p = (char*)malloc(m_lg+stringSrc.m_lg+1); - - strcpy(p, m_ptr); - char* pp = p + m_lg; - strcpy(pp, stringSrc.m_ptr); - - free(m_ptr); - m_ptr = p; - m_lg += stringSrc.m_lg; - - return *this; -} - -const CBotString& CBotString::operator=(const char ch) -{ - free(m_ptr); - - m_lg = 1; - - m_ptr = (char*)malloc(2); - m_ptr[0] = ch; - m_ptr[1] = 0; - - return *this; -} - -const CBotString& CBotString::operator=(const char* pString) -{ - free(m_ptr); - m_ptr = NULL; - - if (pString != NULL) - { - m_lg = strlen(pString); - - if (m_lg != 0) - { - m_ptr = (char*)malloc(m_lg+1); - strcpy(m_ptr, pString); - } - } - - return *this; -} - - -const CBotString& CBotString::operator+=(const char ch) -{ - char* p = (char*)malloc(m_lg+2); - - if (m_ptr!=NULL) strcpy(p, m_ptr); - p[m_lg++] = ch; - p[m_lg] = 0; - - free(m_ptr); - - m_ptr = p; - - return *this; -} - -const CBotString& CBotString::operator+=(const CBotString& str) -{ - char* p = (char*)malloc(m_lg+str.m_lg+1); - - strcpy(p, m_ptr); - char* pp = p + m_lg; - strcpy(pp, str.m_ptr); - - m_lg = m_lg + str.m_lg; - - free(m_ptr); - - m_ptr = p; - - return *this; -} - -bool CBotString::operator==(const CBotString& str) -{ - return Compare(str) == 0; -} - -bool CBotString::operator==(const char* p) -{ - return Compare(p) == 0; -} - -bool CBotString::operator!=(const CBotString& str) -{ - return Compare(str) != 0; -} - -bool CBotString::operator!=(const char* p) -{ - return Compare(p) != 0; -} - -bool CBotString::operator>(const CBotString& str) -{ - return Compare(str) > 0; -} - -bool CBotString::operator>(const char* p) -{ - return Compare(p) > 0; -} - -bool CBotString::operator>=(const CBotString& str) -{ - return Compare(str) >= 0; -} - -bool CBotString::operator>=(const char* p) -{ - return Compare(p) >= 0; -} - -bool CBotString::operator<(const CBotString& str) -{ - return Compare(str) < 0; -} - -bool CBotString::operator<(const char* p) -{ - return Compare(p) < 0; -} - -bool CBotString::operator<=(const CBotString& str) -{ - return Compare(str) <= 0; -} - -bool CBotString::operator<=(const char* p) -{ - return Compare(p) <= 0; -} - -bool CBotString::IsEmpty() const -{ - return (m_lg == 0); -} - -void CBotString::Empty() -{ - free(m_ptr); - m_ptr = NULL; - m_lg = 0; -} - -static char emptyString[] = {0}; - -CBotString::operator const char * () const -{ - if (this == NULL || m_ptr == NULL) return emptyString; - return m_ptr; -} - - -int CBotString::Compare(const char * lpsz) const -{ - char* p = m_ptr; - if (lpsz == NULL) lpsz = emptyString; - if (m_ptr == NULL) p = emptyString; - return strcmp(p, lpsz); // wcscmp -} - -const char * CBotString::MapIdToString(EID id) -{ - if (s_keywordString.find(id) != s_keywordString.end()) - { - return s_keywordString.at(id); - } - else - { - return emptyString; - } -} - -/////////////////////////////////////////////////////////////////////////////////////////// -// arrays of strings - -CBotStringArray::CBotStringArray() -{ - m_pData = NULL; - m_nSize = m_nMaxSize = 0; -} - -CBotStringArray::~CBotStringArray() -{ - SetSize(0); // destroys data ! -} - - -int CBotStringArray::GivSize() -{ - return m_nSize; -} - -void CBotStringArray::Add(const CBotString& str) -{ - SetSize(m_nSize+1); - - m_pData[m_nSize-1] = str; -} - -/////////////////////////////////////////////////////////////////////// -// utility routines - -static inline void ConstructElement(CBotString* pNewData) -{ - memset(pNewData, 0, sizeof(CBotString)); -} - -static inline void DestructElement(CBotString* pOldData) -{ - pOldData->~CBotString(); -} - -static inline void CopyElement(CBotString* pSrc, CBotString* pDest) -{ - *pSrc = *pDest; -} - -static void ConstructElements(CBotString* pNewData, int nCount) -{ - while (nCount--) - { - ConstructElement(pNewData); - pNewData++; - } -} - -static void DestructElements(CBotString* pOldData, int nCount) -{ - while (nCount--) - { - DestructElement(pOldData); - pOldData++; - } -} - -static void CopyElements(CBotString* pDest, CBotString* pSrc, int nCount) -{ - while (nCount--) - { - *pDest = *pSrc; - ++pDest; - ++pSrc; - } -} - - - -// set the array size - -void CBotStringArray::SetSize(int nNewSize) -{ - if (nNewSize == 0) - { - // shrink to nothing - - DestructElements(m_pData, m_nSize); - delete[] (unsigned char *)m_pData; - m_pData = NULL; - m_nSize = m_nMaxSize = 0; - } - else if (m_pData == NULL) - { - // create one with exact size - m_pData = (CBotString*) new unsigned char[nNewSize * sizeof(CBotString)]; - - ConstructElements(m_pData, nNewSize); - - m_nSize = m_nMaxSize = nNewSize; - } - else if (nNewSize <= m_nMaxSize) - { - // it fits - if (nNewSize > m_nSize) - { - // initialize the new elements - - ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); - - } - - else if (m_nSize > nNewSize) // destroy the old elements - DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); - - m_nSize = nNewSize; - } - else - { - // otherwise, grow array - int nGrowBy; - { - // heuristically determine growth when nGrowBy == 0 - // (this avoids heap fragmentation in many situations) - nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); - } - int nNewMax; - if (nNewSize < m_nMaxSize + nGrowBy) - nNewMax = m_nMaxSize + nGrowBy; // granularity - else - nNewMax = nNewSize; // no slush - - CBotString* pNewData = (CBotString*) new unsigned char[nNewMax * sizeof(CBotString)]; - - // copy new data from old - memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); - - // construct remaining elements - ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); - - - // Get rid of old stuff (note: no destructors called) - delete[] (unsigned char *)m_pData; - m_pData = pNewData; - m_nSize = nNewSize; - m_nMaxSize = nNewMax; - } -} - - -CBotString& CBotStringArray::operator[](int nIndex) -{ - return ElementAt(nIndex); -} - -CBotString& CBotStringArray::ElementAt(int nIndex) -{ - return m_pData[nIndex]; -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. +///////////////////////////////////////////////////// + +//strings management + +#include "CBot.h" + +#include +#include +#include + +//Map is filled with id-string pars that are needed for CBot language parsing +const std::map CBotString::s_keywordString = +{ + {ID_IF, "if"}, + {ID_ELSE, "else"}, + {ID_WHILE, "while"}, + {ID_DO, "do"}, + {ID_FOR, "for"}, + {ID_BREAK, "break"}, + {ID_CONTINUE, "continue"}, + {ID_SWITCH, "switch"}, + {ID_CASE, "case"}, + {ID_DEFAULT, "default"}, + {ID_TRY, "try"}, + {ID_THROW, "throw"}, + {ID_CATCH, "catch"}, + {ID_FINALLY, "finally"}, + {ID_TXT_AND, "and"}, + {ID_TXT_OR, "or"}, + {ID_TXT_NOT, "not"}, + {ID_RETURN, "return"}, + {ID_CLASS, "class"}, + {ID_EXTENDS, "extends"}, + {ID_SYNCHO, "synchronized"}, + {ID_NEW, "new"}, + {ID_PUBLIC, "public"}, + {ID_EXTERN, "extern"}, + {ID_FINAL, "final"}, + {ID_STATIC, "static"}, + {ID_PROTECTED, "protected"}, + {ID_PRIVATE, "private"}, + {ID_REPEAT, "repeat"}, + {ID_DEBUGDD, "STARTDEBUGDD"}, + {ID_INT, "int"}, + {ID_FLOAT, "float"}, + {ID_BOOLEAN, "boolean"}, + {ID_STRING, "string"}, + {ID_VOID, "void"}, + {ID_BOOL, "bool"}, + {ID_TRUE, "true"}, + {ID_FALSE, "false"}, + {ID_NULL, "null"}, + {ID_NAN, "nan"}, + {ID_OPENPAR, "("}, + {ID_CLOSEPAR, ")"}, + {ID_OPBLK, "{"}, + {ID_CLBLK, "}"}, + {ID_SEP, "},"}, + {ID_COMMA, ","}, + {ID_DOTS, ":"}, + {ID_DOT, "."}, + {ID_OPBRK, "["}, + {ID_CLBRK, "]"}, + {ID_DBLDOTS, "::"}, + {ID_LOGIC, "?"}, + {ID_ADD, "+"}, + {ID_SUB, "-"}, + {ID_MUL, "*"}, + {ID_DIV, "/"}, + {ID_ASS, "="}, + {ID_ASSADD, "+="}, + {ID_ASSSUB, "-="}, + {ID_ASSMUL, "*="}, + {ID_ASSDIV, "/="}, + {ID_ASSOR, "|="}, + {ID_ASSAND, "&="}, + {ID_ASSXOR, "^="}, + {ID_ASSSL, "<<="}, + {ID_ASSSR, ">>>="}, + {ID_ASSASR, ">>="}, + {ID_SL, "<<"}, + {ID_SR, ">>"}, + {ID_ASR, ">>"}, + {ID_INC, "++"}, + {ID_DEC, "--"}, + {ID_LO, "<"}, + {ID_HI, ">"}, + {ID_LS, "<<"}, + {ID_HS, ">="}, + {ID_EQ, "=="}, + {ID_NE, "!="}, + {ID_AND, "&"}, + {ID_XOR, "^"}, + {ID_OR, "|"}, + {ID_LOG_AND, "&&"}, + {ID_LOG_OR, "||"}, + {ID_LOG_NOT, "!"}, + {ID_NOT, "~"}, + {ID_MODULO, "%"}, + {ID_POWER, "**"}, + {ID_ASSMODULO, "%="}, + {ID_SUPER, "super"}, + {TX_UNDEF, "undefined"}, + {TX_NAN, "not a number"} +}; + +CBotString::CBotString() +{ + m_ptr = NULL; + m_lg = 0; +} + +CBotString::~CBotString() +{ + free(m_ptr); //we can call free on null pointer as it's save +} + + +CBotString::CBotString(const char* p) +{ + m_lg = strlen(p); + + m_ptr = NULL; + if (m_lg>0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, p); + } +} + +CBotString::CBotString(const CBotString& srcString) +{ + m_lg = srcString.m_lg; + + m_ptr = NULL; + if (m_lg>0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, srcString.m_ptr); + } +} + + + + +int CBotString::GivLength() +{ + if (m_ptr == NULL) return 0; + return strlen( m_ptr ); +} + + + +CBotString CBotString::Left(int nCount) const +{ + char chain[2000]; + + size_t i; + for (i = 0; i < m_lg && i < nCount && i < 1999; ++i) + { + chain[i] = m_ptr[i]; + } + chain[i] = 0 ; + + return CBotString(chain); +} + +CBotString CBotString::Right(int nCount) const +{ + char chain[2000]; + + int i = m_lg - nCount; + if ( i < 0 ) i = 0; + + size_t j; + for (size_t j = 0 ; i < m_lg && i < 1999; ++i) + { + chain[j++] = m_ptr[i]; + } + chain[j] = 0 ; + + return CBotString(chain); +} + +CBotString CBotString::Mid(int nFirst, int nCount) const +{ + char chain[2000]; + + size_t i; + for (i = nFirst; i < m_lg && i < 1999 && i <= nFirst + nCount; ++i) + { + chain[i] = m_ptr[i]; + } + chain[i] = 0 ; + + return CBotString(chain); +} + +CBotString CBotString::Mid(int nFirst) const +{ + char chain[2000]; + + size_t i; + for (i = nFirst; i < m_lg && i < 1999 ; ++i) + { + chain[i] = m_ptr[i]; + } + chain[i] = 0 ; + + return CBotString(chain); +} + + +int CBotString::Find(const char c) +{ + for (size_t i = 0; i < m_lg; ++i) + { + if (m_ptr[i] == c) return i; + } + return -1; +} + +int CBotString::Find(const char * lpsz) +{ + int l = strlen(lpsz); + + for (size_t i = 0; i <= m_lg-l; ++i) + { + for (size_t j = 0; j < l; ++j) + { + if (m_ptr[i+j] != lpsz[j]) goto bad; + } + return i; +bad:; + } + return -1; +} + +int CBotString::ReverseFind(const char c) +{ + int i; + for (i = m_lg-1; i >= 0; --i) + { + if (m_ptr[i] == c) return i; + } + return -1; +} + +int CBotString::ReverseFind(const char * lpsz) +{ + int i, j; + int l = strlen(lpsz); + + for (i = m_lg-l; i >= 0; --i) + { + for (j = 0; j < l; ++j) + { + if (m_ptr[i+j] != lpsz[j]) goto bad; + } + return i; +bad:; + } + return -1; +} + +CBotString CBotString::Mid(int start, int lg) +{ + CBotString res; + if (start >= m_lg) return res; + + if ( lg < 0 ) lg = m_lg - start; + + char* p = (char*)malloc(m_lg+1); + strcpy(p, m_ptr+start); + p[lg] = 0; + + res = p; + free(p); + return res; +} + +void CBotString::MakeUpper() +{ + for (size_t i = 0; i < m_lg && i < 1999 ; ++i) + { + char c = m_ptr[i]; + if ( c >= 'a' && c <= 'z' ) m_ptr[i] = c - 'a' + 'A'; + } +} + +void CBotString::MakeLower() +{ + for (size_t i = 0; i < m_lg && i < 1999 ; ++i) + { + char c = m_ptr[i]; + if ( c >= 'A' && c <= 'Z' ) m_ptr[i] = c - 'A' + 'a'; + } +} + +bool CBotString::LoadString(unsigned int id) +{ + const char * str = NULL; + str = MapIdToString((EID)id); + if (m_ptr != NULL) free(m_ptr); + + m_lg = strlen(str); + m_ptr = NULL; + if (m_lg > 0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, str); + return true; + } + return false; +} + + +const CBotString& CBotString::operator=(const CBotString& stringSrc) +{ + free(m_ptr); + m_ptr = NULL; + + m_lg = stringSrc.m_lg; + + if (m_lg > 0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, stringSrc.m_ptr); + } + + return *this; +} + +CBotString operator+(const CBotString& string, const char * lpsz) +{ + CBotString s(string); + s += lpsz; + return s; +} + +const CBotString& CBotString::operator+(const CBotString& stringSrc) +{ + char* p = (char*)malloc(m_lg+stringSrc.m_lg+1); + + strcpy(p, m_ptr); + char* pp = p + m_lg; + strcpy(pp, stringSrc.m_ptr); + + free(m_ptr); + m_ptr = p; + m_lg += stringSrc.m_lg; + + return *this; +} + +const CBotString& CBotString::operator=(const char ch) +{ + free(m_ptr); + + m_lg = 1; + + m_ptr = (char*)malloc(2); + m_ptr[0] = ch; + m_ptr[1] = 0; + + return *this; +} + +const CBotString& CBotString::operator=(const char* pString) +{ + free(m_ptr); + m_ptr = NULL; + + if (pString != NULL) + { + m_lg = strlen(pString); + + if (m_lg != 0) + { + m_ptr = (char*)malloc(m_lg+1); + strcpy(m_ptr, pString); + } + } + + return *this; +} + + +const CBotString& CBotString::operator+=(const char ch) +{ + char* p = (char*)malloc(m_lg+2); + + if (m_ptr!=NULL) strcpy(p, m_ptr); + p[m_lg++] = ch; + p[m_lg] = 0; + + free(m_ptr); + + m_ptr = p; + + return *this; +} + +const CBotString& CBotString::operator+=(const CBotString& str) +{ + char* p = (char*)malloc(m_lg+str.m_lg+1); + + strcpy(p, m_ptr); + char* pp = p + m_lg; + strcpy(pp, str.m_ptr); + + m_lg = m_lg + str.m_lg; + + free(m_ptr); + + m_ptr = p; + + return *this; +} + +bool CBotString::operator==(const CBotString& str) +{ + return Compare(str) == 0; +} + +bool CBotString::operator==(const char* p) +{ + return Compare(p) == 0; +} + +bool CBotString::operator!=(const CBotString& str) +{ + return Compare(str) != 0; +} + +bool CBotString::operator!=(const char* p) +{ + return Compare(p) != 0; +} + +bool CBotString::operator>(const CBotString& str) +{ + return Compare(str) > 0; +} + +bool CBotString::operator>(const char* p) +{ + return Compare(p) > 0; +} + +bool CBotString::operator>=(const CBotString& str) +{ + return Compare(str) >= 0; +} + +bool CBotString::operator>=(const char* p) +{ + return Compare(p) >= 0; +} + +bool CBotString::operator<(const CBotString& str) +{ + return Compare(str) < 0; +} + +bool CBotString::operator<(const char* p) +{ + return Compare(p) < 0; +} + +bool CBotString::operator<=(const CBotString& str) +{ + return Compare(str) <= 0; +} + +bool CBotString::operator<=(const char* p) +{ + return Compare(p) <= 0; +} + +bool CBotString::IsEmpty() const +{ + return (m_lg == 0); +} + +void CBotString::Empty() +{ + free(m_ptr); + m_ptr = NULL; + m_lg = 0; +} + +static char emptyString[] = {0}; + +CBotString::operator const char * () const +{ + if (this == NULL || m_ptr == NULL) return emptyString; + return m_ptr; +} + + +int CBotString::Compare(const char * lpsz) const +{ + char* p = m_ptr; + if (lpsz == NULL) lpsz = emptyString; + if (m_ptr == NULL) p = emptyString; + return strcmp(p, lpsz); // wcscmp +} + +const char * CBotString::MapIdToString(EID id) +{ + if (s_keywordString.find(id) != s_keywordString.end()) + { + return s_keywordString.at(id); + } + else + { + return emptyString; + } +} + +/////////////////////////////////////////////////////////////////////////////////////////// +// arrays of strings + +CBotStringArray::CBotStringArray() +{ + m_pData = NULL; + m_nSize = m_nMaxSize = 0; +} + +CBotStringArray::~CBotStringArray() +{ + SetSize(0); // destroys data ! +} + + +int CBotStringArray::GivSize() +{ + return m_nSize; +} + +void CBotStringArray::Add(const CBotString& str) +{ + SetSize(m_nSize+1); + + m_pData[m_nSize-1] = str; +} + +/////////////////////////////////////////////////////////////////////// +// utility routines + +static inline void ConstructElement(CBotString* pNewData) +{ + memset(pNewData, 0, sizeof(CBotString)); +} + +static inline void DestructElement(CBotString* pOldData) +{ + pOldData->~CBotString(); +} + +static inline void CopyElement(CBotString* pSrc, CBotString* pDest) +{ + *pSrc = *pDest; +} + +static void ConstructElements(CBotString* pNewData, int nCount) +{ + while (nCount--) + { + ConstructElement(pNewData); + pNewData++; + } +} + +static void DestructElements(CBotString* pOldData, int nCount) +{ + while (nCount--) + { + DestructElement(pOldData); + pOldData++; + } +} + +static void CopyElements(CBotString* pDest, CBotString* pSrc, int nCount) +{ + while (nCount--) + { + *pDest = *pSrc; + ++pDest; + ++pSrc; + } +} + + + +// set the array size + +void CBotStringArray::SetSize(int nNewSize) +{ + if (nNewSize == 0) + { + // shrink to nothing + + DestructElements(m_pData, m_nSize); + delete[] (unsigned char *)m_pData; + m_pData = NULL; + m_nSize = m_nMaxSize = 0; + } + else if (m_pData == NULL) + { + // create one with exact size + m_pData = (CBotString*) new unsigned char[nNewSize * sizeof(CBotString)]; + + ConstructElements(m_pData, nNewSize); + + m_nSize = m_nMaxSize = nNewSize; + } + else if (nNewSize <= m_nMaxSize) + { + // it fits + if (nNewSize > m_nSize) + { + // initialize the new elements + + ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); + + } + + else if (m_nSize > nNewSize) // destroy the old elements + DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); + + m_nSize = nNewSize; + } + else + { + // otherwise, grow array + int nGrowBy; + { + // heuristically determine growth when nGrowBy == 0 + // (this avoids heap fragmentation in many situations) + nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); + } + int nNewMax; + if (nNewSize < m_nMaxSize + nGrowBy) + nNewMax = m_nMaxSize + nGrowBy; // granularity + else + nNewMax = nNewSize; // no slush + + CBotString* pNewData = (CBotString*) new unsigned char[nNewMax * sizeof(CBotString)]; + + // copy new data from old + memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); + + // construct remaining elements + ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); + + + // Get rid of old stuff (note: no destructors called) + delete[] (unsigned char *)m_pData; + m_pData = pNewData; + m_nSize = nNewSize; + m_nMaxSize = nNewMax; + } +} + + +CBotString& CBotStringArray::operator[](int nIndex) +{ + return ElementAt(nIndex); +} + +CBotString& CBotStringArray::ElementAt(int nIndex) +{ + return m_pData[nIndex]; +} + diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index c76df7f..2c0bfae 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -1,562 +1,562 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - - -//CBotToken.cpp - -////////////////////////////////////////////////////////////////// -// Managing Tokens -// the text of a program is first transformed -// into a sequence of tokens for easy interpretation -// it will only treat the case as an error -// where there is an illegal character in a string - - -#include "CBot.h" -#include - -CBotStringArray CBotToken::m_ListKeyWords; -int CBotToken::m_ListIdKeyWords[200]; -CBotStringArray CBotToken::m_ListKeyDefine; -long CBotToken::m_ListKeyNums[MAXDEFNUM]; - -//! contructors -CBotToken::CBotToken() -{ - m_next = NULL; - m_prev = NULL; - m_type = TokenTypVar; // at the beginning a default variable type - m_IdKeyWord = -1; -} - -CBotToken::CBotToken(const CBotToken* pSrc) -{ - m_next = NULL; - m_prev = NULL; - - m_Text.Empty(); - m_Sep.Empty(); - - m_type = 0; - m_IdKeyWord = 0; - - m_start = 0; - m_end = 0; - - if ( pSrc != NULL ) - { - - m_type = pSrc->m_type; - m_IdKeyWord = pSrc->m_IdKeyWord; - - m_Text = pSrc->m_Text; - m_Sep = pSrc->m_Sep; - - m_start = pSrc->m_start; - m_end = pSrc->m_end; - } -} - -CBotToken::CBotToken(const CBotString& mot, const CBotString& sep, int start, int end) -{ - m_Text = mot; // word (mot) found as token - m_Sep = sep; // separator - m_next = NULL; - m_prev = NULL; - m_start = start; - m_end = end; - - m_type = TokenTypVar; // at the beginning a default variable type - m_IdKeyWord = -1; -} - -CBotToken::CBotToken(const char* mot, const char* sep) -{ - m_Text = mot; - if ( sep != NULL ) m_Sep = sep; - m_next = NULL; - m_prev = NULL; - - m_type = TokenTypVar; // at the beginning a default variable type - m_IdKeyWord = -1; -} - -CBotToken::~CBotToken() -{ - delete m_next; // recursive - m_next = NULL; -} - -void CBotToken::Free() -{ - m_ListKeyDefine.SetSize(0); -} - -const CBotToken& CBotToken::operator=(const CBotToken& src) -{ - if (m_next != NULL) delete(m_next); - m_next = NULL; - m_prev = NULL; - - m_Text = src.m_Text; - m_Sep = src.m_Sep; - - m_type = src.m_type; - m_IdKeyWord = src.m_IdKeyWord; - - m_start = src.m_start; - m_end = src.m_end; - return *this; -} - - -int CBotToken::GivType() -{ - if (this == NULL) return 0; - if (m_type == TokenTypKeyWord) return m_IdKeyWord; - return m_type; -} - -long CBotToken::GivIdKey() -{ - return m_IdKeyWord; -} - -CBotToken* CBotToken::GivNext() -{ - if (this == NULL) return NULL; - return m_next; -} - -CBotToken* CBotToken::GivPrev() -{ - if (this == NULL) return NULL; - return m_prev; -} - -void CBotToken::AddNext(CBotToken* p) -{ - CBotToken* n = new CBotToken(p); - CBotToken* pt = this; - - while ( pt->m_next != NULL ) pt = pt->m_next; - - pt->m_next = n; - n->m_prev = pt; -} - - -CBotString& CBotToken::GivString() -{ - return m_Text; -} - -CBotString& CBotToken::GivSep() -{ - return m_Sep; -} - -void CBotToken::SetString(const char* name) -{ - m_Text = name; -} - - -int CBotToken::GivStart() -{ - if (this == NULL) return -1; - return m_start; -} - -int CBotToken::GivEnd() -{ - if (this == NULL) return -1; - return m_end; -} - -void CBotToken::SetPos(int start, int end) -{ - m_start = start; - m_end = end; -} - -bool CharInList(const char c, const char* list) -{ - int i = 0; - - while (true) - { - if (c == list[i++]) return true; - if (list[i] == 0) return false; - } -} - -bool Char2InList(const char c, const char cc, const char* list) -{ - int i = 0; - - while (true) - { - if (c == list[i++] && - cc == list[i++]) return true; - - if (list[i] == 0) return false; - } -} - -static char* sep1 = " \r\n\t,:()[]{}-+*/=;>!~^|&%."; // operational separators -static char* num = "0123456789"; // point (single) is tested separately -static char* hexnum = "0123456789ABCDEFabcdef"; -static char* nch = "\"\r\n\t"; // forbidden in chains - -//static char* duo = "+=-=*=/===!=<=>=++--///**/||&&"; // double operators - -// looking for the next token in a sentence -// do not start with separators -// which are made in the previous token -CBotToken* CBotToken::NextToken(char* &program, int& error, bool first) -{ - CBotString mot; // the word which is found - CBotString sep; // separators that are after - char c; - bool stop = first; - - if (*program == 0) return NULL; - - c = *(program++); // next character - - if (!first) - { - mot = c; // built the word - c = *(program++); // next character - - // special case for strings - if ( mot[0] == '\"' ) - { - while (c != 0 && !CharInList(c, nch)) - { - mot += c; - c = *(program++); // next character - if ( c == '\\' ) - { - c = *(program++); // next character - if ( c == 'n' ) c = '\n'; - if ( c == 'r' ) c = '\r'; - if ( c == 't' ) c = '\t'; - mot += c; - c = *(program++); // next character - } - } - if ( c == '\"' ) - { - mot += c; // string is complete - c = *(program++); // next character - } - stop = true; - } - - // special case for numbers - if ( CharInList(mot[0], num )) - { - bool bdot = false; // found a point? - bool bexp = false; // found an exponent? - - char* liste = num; - if (mot[0] == '0' && c == 'x') // hexadecimal value? - { - mot += c; - c = *(program++); // next character - liste = hexnum; - } -cw: - while (c != 0 && CharInList(c, liste)) - { -cc: mot += c; - c = *(program++); // next character - } - if ( liste == num ) // not for hexadecimal - { - if ( !bdot && c == '.' ) { bdot = true; goto cc; } - if ( !bexp && ( c == 'e' || c == 'E' ) ) - { - bexp = true; - mot += c; - c = *(program++); // next character - if ( c == '-' || - c == '+' ) goto cc; - goto cw; - } - - } - stop = true; - } - - if (CharInList(mot[0], sep3)) // an operational separator? - { - CBotString motc = mot; - while (motc += c, c != 0 && GivKeyWords(motc)>0) // operand seeks the longest possible - { - mot += c; // build the word - c = *(program++); // next character - } - - stop = true; - } - } - - - - while (true) - { - if (stop || c == 0 || CharInList(c, sep1)) - { - if (!first && mot.IsEmpty()) return NULL; // end of the analysis -bis: - while (CharInList(c, sep2)) - { - sep += c; // after all the separators - c = *(program++); - } - if (c == '/' && *program == '/') // comment on the heap? - { - while( c != '\n' && c != 0 ) - { - sep += c; - c = *(program++); // next character - } - goto bis; - } - - if (c == '/' && *program == '*') // comment on the heap? - { - while( c != 0 && (c != '*' || *program != '/')) - { - sep += c; - c = *(program++); // next character - } - if ( c != 0 ) - { - sep += c; - c = *(program++); // next character - sep += c; - c = *(program++); // next character - } - goto bis; - } - - program--; - - CBotToken* token = new CBotToken(mot, sep); - - if (CharInList( mot[0], num )) token->m_type = TokenTypNum; - if (mot[0] == '\"') token->m_type = TokenTypString; - if (first) token->m_type = 0; - - token->m_IdKeyWord = GivKeyWords(mot); - if (token->m_IdKeyWord > 0) token->m_type = TokenTypKeyWord; - else GivKeyDefNum(mot, token) ; // treats DefineNum - - return token; - } - - mot += c; // built the word - c = *(program++); // next character - } -} - -CBotToken* CBotToken::CompileTokens(const char* program, int& error) -{ - CBotToken *nxt, *prv, *tokenbase; - char* p = (char*) program; - int pos = 0; - - error = 0; - prv = tokenbase = NextToken(p, error, true); - - if (tokenbase == NULL) return NULL; - - tokenbase->m_start = pos; - pos += tokenbase->m_Text.GivLength(); - tokenbase->m_end = pos; - pos += tokenbase->m_Sep.GivLength(); - - char* pp = p; - while (NULL != (nxt = NextToken(p, error))) - { - prv->m_next = nxt; // added after - nxt->m_prev = prv; - prv = nxt; // advance - - nxt->m_start = pos; -/* pos += nxt->m_Text.GivLength(); // chain may be shorter (BOA deleted) - nxt->m_end = pos; - pos += nxt->m_Sep.GivLength();*/ - pos += (p - pp); // total size - nxt->m_end = pos - nxt->m_Sep.GivLength(); - pp = p; - } - - // adds a token as a terminator - // ( useful for the previous ) - nxt = new CBotToken(); - nxt->m_type = 0; - prv->m_next = nxt; // added after - nxt->m_prev = prv; - - return tokenbase; -} - -void CBotToken::Delete(CBotToken* pToken) -{ - delete pToken; -} - - -// search if a word is part of the keywords - -int CBotToken::GivKeyWords(const char* w) -{ - int i; - int l = m_ListKeyWords.GivSize(); - - if (l == 0) - { - LoadKeyWords(); // takes the list for the first time - l = m_ListKeyWords.GivSize(); - } - - for (i = 0; i < l; i++) - { - if (m_ListKeyWords[i] == w) return m_ListIdKeyWords[ i ]; - } - - return -1; -} - -bool CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) -{ - int i; - int l = m_ListKeyDefine.GivSize(); - - for (i = 0; i < l; i++) - { - if (m_ListKeyDefine[i] == w) - { - token->m_IdKeyWord = m_ListKeyNums[i]; - token->m_type = TokenTypDef; - return true; - } - } - - return false; -} - - -/// \todo Fixme Figure out how this should work. - -// recreates the list of keywords and its IDs basing on some resources -// defines of TokenKey.. are in CBotDll.h - -void CBotToken::LoadKeyWords() -{ - CBotString s; - int i, n = 0; - - i = TokenKeyWord; //start with keywords of the language - while (s.LoadString(i)) - { - m_ListKeyWords.Add(s); - m_ListIdKeyWords[n++] = i++; - } - - i = TokenKeyDeclare; //keywords of declarations - while (s.LoadString(i)) - { - m_ListKeyWords.Add(s); - m_ListIdKeyWords[n++] = i++; - } - - - i = TokenKeyVal; //keywords of values - while (s.LoadString(i)) - { - m_ListKeyWords.Add(s); - m_ListIdKeyWords[n++] = i++; - } - - i = TokenKeyOp; //operators - while (s.LoadString(i)) - { - m_ListKeyWords.Add(s); - m_ListIdKeyWords[n++] = i++; - } -} - -bool CBotToken::DefineNum(const char* name, long val) -{ - int i; - int l = m_ListKeyDefine.GivSize(); - - for (i = 0; i < l; i++) - { - if (m_ListKeyDefine[i] == name) return false; - } - if ( i == MAXDEFNUM ) return false; - - m_ListKeyDefine.Add( name ); - m_ListKeyNums[i] = val; - return true; -} - -bool IsOfType(CBotToken* &p, int type1, int type2) -{ - if (p->GivType() == type1 || - p->GivType() == type2 ) - { - p = p->GivNext(); - return true; - } - return false; -} -// Same with any number of arguments -// There must be a zero as the last argument -bool IsOfTypeList(CBotToken* &p, int type1, ...) -{ - int i = type1; - int max = 20; - int type = p->GivType(); - - va_list marker; - va_start( marker, type1 ); /* Initialize variable arguments. */ - - while (true) - { - if (type == i) - { - p = p->GivNext(); - va_end( marker ); /* Reset variable arguments. */ - return true; - } - if (--max == 0 || 0 == (i = va_arg( marker, int))) - { - va_end( marker ); /* Reset variable arguments. */ - return false; - } - } -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + + +//CBotToken.cpp + +////////////////////////////////////////////////////////////////// +// Managing Tokens +// the text of a program is first transformed +// into a sequence of tokens for easy interpretation +// it will only treat the case as an error +// where there is an illegal character in a string + + +#include "CBot.h" +#include + +CBotStringArray CBotToken::m_ListKeyWords; +int CBotToken::m_ListIdKeyWords[200]; +CBotStringArray CBotToken::m_ListKeyDefine; +long CBotToken::m_ListKeyNums[MAXDEFNUM]; + +//! contructors +CBotToken::CBotToken() +{ + m_next = NULL; + m_prev = NULL; + m_type = TokenTypVar; // at the beginning a default variable type + m_IdKeyWord = -1; +} + +CBotToken::CBotToken(const CBotToken* pSrc) +{ + m_next = NULL; + m_prev = NULL; + + m_Text.Empty(); + m_Sep.Empty(); + + m_type = 0; + m_IdKeyWord = 0; + + m_start = 0; + m_end = 0; + + if ( pSrc != NULL ) + { + + m_type = pSrc->m_type; + m_IdKeyWord = pSrc->m_IdKeyWord; + + m_Text = pSrc->m_Text; + m_Sep = pSrc->m_Sep; + + m_start = pSrc->m_start; + m_end = pSrc->m_end; + } +} + +CBotToken::CBotToken(const CBotString& mot, const CBotString& sep, int start, int end) +{ + m_Text = mot; // word (mot) found as token + m_Sep = sep; // separator + m_next = NULL; + m_prev = NULL; + m_start = start; + m_end = end; + + m_type = TokenTypVar; // at the beginning a default variable type + m_IdKeyWord = -1; +} + +CBotToken::CBotToken(const char* mot, const char* sep) +{ + m_Text = mot; + if ( sep != NULL ) m_Sep = sep; + m_next = NULL; + m_prev = NULL; + + m_type = TokenTypVar; // at the beginning a default variable type + m_IdKeyWord = -1; +} + +CBotToken::~CBotToken() +{ + delete m_next; // recursive + m_next = NULL; +} + +void CBotToken::Free() +{ + m_ListKeyDefine.SetSize(0); +} + +const CBotToken& CBotToken::operator=(const CBotToken& src) +{ + if (m_next != NULL) delete(m_next); + m_next = NULL; + m_prev = NULL; + + m_Text = src.m_Text; + m_Sep = src.m_Sep; + + m_type = src.m_type; + m_IdKeyWord = src.m_IdKeyWord; + + m_start = src.m_start; + m_end = src.m_end; + return *this; +} + + +int CBotToken::GivType() +{ + if (this == NULL) return 0; + if (m_type == TokenTypKeyWord) return m_IdKeyWord; + return m_type; +} + +long CBotToken::GivIdKey() +{ + return m_IdKeyWord; +} + +CBotToken* CBotToken::GivNext() +{ + if (this == NULL) return NULL; + return m_next; +} + +CBotToken* CBotToken::GivPrev() +{ + if (this == NULL) return NULL; + return m_prev; +} + +void CBotToken::AddNext(CBotToken* p) +{ + CBotToken* n = new CBotToken(p); + CBotToken* pt = this; + + while ( pt->m_next != NULL ) pt = pt->m_next; + + pt->m_next = n; + n->m_prev = pt; +} + + +CBotString& CBotToken::GivString() +{ + return m_Text; +} + +CBotString& CBotToken::GivSep() +{ + return m_Sep; +} + +void CBotToken::SetString(const char* name) +{ + m_Text = name; +} + + +int CBotToken::GivStart() +{ + if (this == NULL) return -1; + return m_start; +} + +int CBotToken::GivEnd() +{ + if (this == NULL) return -1; + return m_end; +} + +void CBotToken::SetPos(int start, int end) +{ + m_start = start; + m_end = end; +} + +bool CharInList(const char c, const char* list) +{ + int i = 0; + + while (true) + { + if (c == list[i++]) return true; + if (list[i] == 0) return false; + } +} + +bool Char2InList(const char c, const char cc, const char* list) +{ + int i = 0; + + while (true) + { + if (c == list[i++] && + cc == list[i++]) return true; + + if (list[i] == 0) return false; + } +} + +static char* sep1 = " \r\n\t,:()[]{}-+*/=;>!~^|&%."; // operational separators +static char* num = "0123456789"; // point (single) is tested separately +static char* hexnum = "0123456789ABCDEFabcdef"; +static char* nch = "\"\r\n\t"; // forbidden in chains + +//static char* duo = "+=-=*=/===!=<=>=++--///**/||&&"; // double operators + +// looking for the next token in a sentence +// do not start with separators +// which are made in the previous token +CBotToken* CBotToken::NextToken(char* &program, int& error, bool first) +{ + CBotString mot; // the word which is found + CBotString sep; // separators that are after + char c; + bool stop = first; + + if (*program == 0) return NULL; + + c = *(program++); // next character + + if (!first) + { + mot = c; // built the word + c = *(program++); // next character + + // special case for strings + if ( mot[0] == '\"' ) + { + while (c != 0 && !CharInList(c, nch)) + { + mot += c; + c = *(program++); // next character + if ( c == '\\' ) + { + c = *(program++); // next character + if ( c == 'n' ) c = '\n'; + if ( c == 'r' ) c = '\r'; + if ( c == 't' ) c = '\t'; + mot += c; + c = *(program++); // next character + } + } + if ( c == '\"' ) + { + mot += c; // string is complete + c = *(program++); // next character + } + stop = true; + } + + // special case for numbers + if ( CharInList(mot[0], num )) + { + bool bdot = false; // found a point? + bool bexp = false; // found an exponent? + + char* liste = num; + if (mot[0] == '0' && c == 'x') // hexadecimal value? + { + mot += c; + c = *(program++); // next character + liste = hexnum; + } +cw: + while (c != 0 && CharInList(c, liste)) + { +cc: mot += c; + c = *(program++); // next character + } + if ( liste == num ) // not for hexadecimal + { + if ( !bdot && c == '.' ) { bdot = true; goto cc; } + if ( !bexp && ( c == 'e' || c == 'E' ) ) + { + bexp = true; + mot += c; + c = *(program++); // next character + if ( c == '-' || + c == '+' ) goto cc; + goto cw; + } + + } + stop = true; + } + + if (CharInList(mot[0], sep3)) // an operational separator? + { + CBotString motc = mot; + while (motc += c, c != 0 && GivKeyWords(motc)>0) // operand seeks the longest possible + { + mot += c; // build the word + c = *(program++); // next character + } + + stop = true; + } + } + + + + while (true) + { + if (stop || c == 0 || CharInList(c, sep1)) + { + if (!first && mot.IsEmpty()) return NULL; // end of the analysis +bis: + while (CharInList(c, sep2)) + { + sep += c; // after all the separators + c = *(program++); + } + if (c == '/' && *program == '/') // comment on the heap? + { + while( c != '\n' && c != 0 ) + { + sep += c; + c = *(program++); // next character + } + goto bis; + } + + if (c == '/' && *program == '*') // comment on the heap? + { + while( c != 0 && (c != '*' || *program != '/')) + { + sep += c; + c = *(program++); // next character + } + if ( c != 0 ) + { + sep += c; + c = *(program++); // next character + sep += c; + c = *(program++); // next character + } + goto bis; + } + + program--; + + CBotToken* token = new CBotToken(mot, sep); + + if (CharInList( mot[0], num )) token->m_type = TokenTypNum; + if (mot[0] == '\"') token->m_type = TokenTypString; + if (first) token->m_type = 0; + + token->m_IdKeyWord = GivKeyWords(mot); + if (token->m_IdKeyWord > 0) token->m_type = TokenTypKeyWord; + else GivKeyDefNum(mot, token) ; // treats DefineNum + + return token; + } + + mot += c; // built the word + c = *(program++); // next character + } +} + +CBotToken* CBotToken::CompileTokens(const char* program, int& error) +{ + CBotToken *nxt, *prv, *tokenbase; + char* p = (char*) program; + int pos = 0; + + error = 0; + prv = tokenbase = NextToken(p, error, true); + + if (tokenbase == NULL) return NULL; + + tokenbase->m_start = pos; + pos += tokenbase->m_Text.GivLength(); + tokenbase->m_end = pos; + pos += tokenbase->m_Sep.GivLength(); + + char* pp = p; + while (NULL != (nxt = NextToken(p, error))) + { + prv->m_next = nxt; // added after + nxt->m_prev = prv; + prv = nxt; // advance + + nxt->m_start = pos; +/* pos += nxt->m_Text.GivLength(); // chain may be shorter (BOA deleted) + nxt->m_end = pos; + pos += nxt->m_Sep.GivLength();*/ + pos += (p - pp); // total size + nxt->m_end = pos - nxt->m_Sep.GivLength(); + pp = p; + } + + // adds a token as a terminator + // ( useful for the previous ) + nxt = new CBotToken(); + nxt->m_type = 0; + prv->m_next = nxt; // added after + nxt->m_prev = prv; + + return tokenbase; +} + +void CBotToken::Delete(CBotToken* pToken) +{ + delete pToken; +} + + +// search if a word is part of the keywords + +int CBotToken::GivKeyWords(const char* w) +{ + int i; + int l = m_ListKeyWords.GivSize(); + + if (l == 0) + { + LoadKeyWords(); // takes the list for the first time + l = m_ListKeyWords.GivSize(); + } + + for (i = 0; i < l; i++) + { + if (m_ListKeyWords[i] == w) return m_ListIdKeyWords[ i ]; + } + + return -1; +} + +bool CBotToken::GivKeyDefNum(const char* w, CBotToken* &token) +{ + int i; + int l = m_ListKeyDefine.GivSize(); + + for (i = 0; i < l; i++) + { + if (m_ListKeyDefine[i] == w) + { + token->m_IdKeyWord = m_ListKeyNums[i]; + token->m_type = TokenTypDef; + return true; + } + } + + return false; +} + + +/// \todo Fixme Figure out how this should work. + +// recreates the list of keywords and its IDs basing on some resources +// defines of TokenKey.. are in CBotDll.h + +void CBotToken::LoadKeyWords() +{ + CBotString s; + int i, n = 0; + + i = TokenKeyWord; //start with keywords of the language + while (s.LoadString(i)) + { + m_ListKeyWords.Add(s); + m_ListIdKeyWords[n++] = i++; + } + + i = TokenKeyDeclare; //keywords of declarations + while (s.LoadString(i)) + { + m_ListKeyWords.Add(s); + m_ListIdKeyWords[n++] = i++; + } + + + i = TokenKeyVal; //keywords of values + while (s.LoadString(i)) + { + m_ListKeyWords.Add(s); + m_ListIdKeyWords[n++] = i++; + } + + i = TokenKeyOp; //operators + while (s.LoadString(i)) + { + m_ListKeyWords.Add(s); + m_ListIdKeyWords[n++] = i++; + } +} + +bool CBotToken::DefineNum(const char* name, long val) +{ + int i; + int l = m_ListKeyDefine.GivSize(); + + for (i = 0; i < l; i++) + { + if (m_ListKeyDefine[i] == name) return false; + } + if ( i == MAXDEFNUM ) return false; + + m_ListKeyDefine.Add( name ); + m_ListKeyNums[i] = val; + return true; +} + +bool IsOfType(CBotToken* &p, int type1, int type2) +{ + if (p->GivType() == type1 || + p->GivType() == type2 ) + { + p = p->GivNext(); + return true; + } + return false; +} +// Same with any number of arguments +// There must be a zero as the last argument +bool IsOfTypeList(CBotToken* &p, int type1, ...) +{ + int i = type1; + int max = 20; + int type = p->GivType(); + + va_list marker; + va_start( marker, type1 ); /* Initialize variable arguments. */ + + while (true) + { + if (type == i) + { + p = p->GivNext(); + va_end( marker ); /* Reset variable arguments. */ + return true; + } + if (--max == 0 || 0 == (i = va_arg( marker, int))) + { + va_end( marker ); /* Reset variable arguments. */ + return false; + } + } +} + diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index 8e9d1e3..6f11bb2 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -1,38 +1,38 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - - -// interpreter of the lanuage CBot for game COLOBOT -// writing a program is first transformed into a list of tokens -// before tackling the compiler itself -// for example -// int var = 3 * ( pos.y + x ) -// is decomposed into (each line is a token) -// int -// var -// = -// 3 -// * -// ( -// pos.y -// + -// x -// ) - -#pragma once - -extern bool IsOfType(CBotToken* &p, int type1, int type2 = -1); -extern bool IsOfTypeList(CBotToken* &p, int type1, ...); +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + + +// interpreter of the lanuage CBot for game COLOBOT +// writing a program is first transformed into a list of tokens +// before tackling the compiler itself +// for example +// int var = 3 * ( pos.y + x ) +// is decomposed into (each line is a token) +// int +// var +// = +// 3 +// * +// ( +// pos.y +// + +// x +// ) + +#pragma once + +extern bool IsOfType(CBotToken* &p, int type1, int type2 = -1); +extern bool IsOfTypeList(CBotToken* &p, int type1, ...); diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotTwoOpExpr.cpp index 49cfcc8..62290da 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotTwoOpExpr.cpp @@ -16,7 +16,7 @@ /////////////////////////////////////////////////// // expression of type Opérande1 + Opérande2 -// Opérande1 > Opérande2 +// Opérande1 > Opérande2 #include "CBot.h" @@ -24,271 +24,271 @@ CBotTwoOpExpr::CBotTwoOpExpr() { - m_leftop = - m_rightop = NULL; // NULL to be able to delete without other - name = "CBotTwoOpExpr"; // debug + m_leftop = + m_rightop = NULL; // NULL to be able to delete without other + name = "CBotTwoOpExpr"; // debug } CBotTwoOpExpr::~CBotTwoOpExpr() { - delete m_leftop; - delete m_rightop; + delete m_leftop; + delete m_rightop; } CBotLogicExpr::CBotLogicExpr() { - m_condition = - m_op1 = - m_op2 = NULL; // NULL to be able to delete without other - name = "CBotLogicExpr"; // debug + m_condition = + m_op1 = + m_op2 = NULL; // NULL to be able to delete without other + name = "CBotLogicExpr"; // debug } CBotLogicExpr::~CBotLogicExpr() { - delete m_condition; - delete m_op1; - delete m_op2; + delete m_condition; + delete m_op1; + delete m_op2; } // type of operands accepted by operations -#define ENTIER ((1<TokenStack(); // one end of stack please - - // search the intructions that may be suitable to the left of the operation - CBotInstr* left = (*pOp == 0) ? - CBotParExpr::Compile( p, pStk ) : // expression (...) left - CBotTwoOpExpr::Compile( p, pStk, pOp ); // expression A * B left - - if (left == NULL) return pStack->Return(NULL, pStk); // if error, transmit - - // did we expected the operand? - int TypeOp = p->GivType(); - if ( IsInList( TypeOp, pOperations, typemasque ) ) - { - CBotTypResult type1, type2; - type1 = pStk->GivTypResult(); // what kind of the first operand? - - if ( TypeOp == ID_LOGIC ) // special case provided for: ? op1: op2; - { - if ( !type1.Eq(CBotTypBoolean) ) - { - pStk->SetError( TX_BADTYPE, p); - return pStack->Return(NULL, pStk); - } - CBotLogicExpr* inst = new CBotLogicExpr(); - inst->m_condition = left; - - p = p->GivNext(); // skip the token of the operation - inst->m_op1 = CBotExpression::Compile(p, pStk); - CBotToken* pp = p; - if ( inst->m_op1 == NULL || !IsOfType( p, ID_DOTS ) ) - { - pStk->SetError( TX_MISDOTS, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - type1 = pStk->GivTypResult(); - - inst->m_op2 = CBotExpression::Compile(p, pStk); - if ( inst->m_op2 == NULL ) - { - pStk->SetError( TX_ENDOF, p->GivStart() ); - delete inst; - return pStack->Return(NULL, pStk); - } - type2 = pStk->GivTypResult(); - if (!TypeCompatible(type1, type2)) - { - pStk->SetError( TX_BAD2TYPE, pp ); - delete inst; - return pStack->Return(NULL, pStk); - } - - pStk->SetType(type1); // the greatest of 2 types - - return pStack->Return(inst, pStk); - } - - CBotTwoOpExpr* inst = new CBotTwoOpExpr(); // element for operation - inst->SetToken(p); // stores the operation - - - p = p->GivNext(); // skip the token of the operation - - // looking statements that may be suitable for right - - if ( NULL != (inst->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp )) ) - // expression (...) right - { - // there is an second operand acceptable - - type2 = pStk->GivTypResult(); // what kind of results? - - // what kind of result? - int TypeRes = MAX( type1.GivType(3), type2.GivType(3) ); - if ( TypeOp == ID_ADD && type1.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - type2 = type1; // any type convertible chain - } - else if ( TypeOp == ID_ADD && type2.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - type1 = type2; // any type convertible chain - } - else if (!TypeOk( TypeRes, typemasque )) type1.SetType(99);// error of type - - switch ( TypeOp ) - { - case ID_LOG_OR: - case ID_LOG_AND: - case ID_TXT_OR: - case ID_TXT_AND: - case ID_EQ: - case ID_NE: - case ID_HI: - case ID_LO: - case ID_HS: - case ID_LS: - TypeRes = CBotTypBoolean; - } - if ( TypeCompatible (type1, type2, TypeOp ) ) // the results are compatible - { - // ok so, saves the operand in the object - inst->m_leftop = left; - - // special for evaluation of the operations of the same level from left to right - while ( IsInList( p->GivType(), pOperations, typemasque ) ) // same operation(s) follows? - { - TypeOp = p->GivType(); - CBotTwoOpExpr* i = new CBotTwoOpExpr(); // element for operation - i->SetToken(p); // stores the operation - i->m_leftop = inst; // left operand - type1 = TypeRes; - - p = p->GivNext(); // advance after - i->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp ); - type2 = pStk->GivTypResult(); - - if ( !TypeCompatible (type1, type2, TypeOp) ) // the results are compatible - { - pStk->SetError(TX_BAD2TYPE, &i->m_token); - delete i; - return pStack->Return(NULL, pStk); - } - - if ( TypeRes != CBotTypString ) - TypeRes = MAX(type1.GivType(), type2.GivType()); - inst = i; - } - - CBotTypResult t(type1); - t.SetType(TypeRes); - // is a variable on the stack for the type of result - pStk->SetVar(CBotVar::Create((CBotToken*)NULL, t)); - - // and returns the requested object - return pStack->Return(inst, pStk); - } - pStk->SetError(TX_BAD2TYPE, &inst->m_token); - } - - // in case of error, releases the elements - delete left; - delete inst; - // and transmits the error to the stack - return pStack->Return(NULL, pStk); - } - - // if we are not dealing with an operation + or - - // goes to that requested, the operand (left) found - // instead of the object "addition" - return pStack->Return(left, pStk); + int typemasque; + + if ( pOperations == NULL ) pOperations = ListOp; + int* pOp = pOperations; + while ( *pOp++ != 0 ); // follows the table + + CBotCStack* pStk = pStack->TokenStack(); // one end of stack please + + // search the intructions that may be suitable to the left of the operation + CBotInstr* left = (*pOp == 0) ? + CBotParExpr::Compile( p, pStk ) : // expression (...) left + CBotTwoOpExpr::Compile( p, pStk, pOp ); // expression A * B left + + if (left == NULL) return pStack->Return(NULL, pStk); // if error, transmit + + // did we expected the operand? + int TypeOp = p->GivType(); + if ( IsInList( TypeOp, pOperations, typemasque ) ) + { + CBotTypResult type1, type2; + type1 = pStk->GivTypResult(); // what kind of the first operand? + + if ( TypeOp == ID_LOGIC ) // special case provided for: ? op1: op2; + { + if ( !type1.Eq(CBotTypBoolean) ) + { + pStk->SetError( TX_BADTYPE, p); + return pStack->Return(NULL, pStk); + } + CBotLogicExpr* inst = new CBotLogicExpr(); + inst->m_condition = left; + + p = p->GivNext(); // skip the token of the operation + inst->m_op1 = CBotExpression::Compile(p, pStk); + CBotToken* pp = p; + if ( inst->m_op1 == NULL || !IsOfType( p, ID_DOTS ) ) + { + pStk->SetError( TX_MISDOTS, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + type1 = pStk->GivTypResult(); + + inst->m_op2 = CBotExpression::Compile(p, pStk); + if ( inst->m_op2 == NULL ) + { + pStk->SetError( TX_ENDOF, p->GivStart() ); + delete inst; + return pStack->Return(NULL, pStk); + } + type2 = pStk->GivTypResult(); + if (!TypeCompatible(type1, type2)) + { + pStk->SetError( TX_BAD2TYPE, pp ); + delete inst; + return pStack->Return(NULL, pStk); + } + + pStk->SetType(type1); // the greatest of 2 types + + return pStack->Return(inst, pStk); + } + + CBotTwoOpExpr* inst = new CBotTwoOpExpr(); // element for operation + inst->SetToken(p); // stores the operation + + + p = p->GivNext(); // skip the token of the operation + + // looking statements that may be suitable for right + + if ( NULL != (inst->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp )) ) + // expression (...) right + { + // there is an second operand acceptable + + type2 = pStk->GivTypResult(); // what kind of results? + + // what kind of result? + int TypeRes = MAX( type1.GivType(3), type2.GivType(3) ); + if ( TypeOp == ID_ADD && type1.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + type2 = type1; // any type convertible chain + } + else if ( TypeOp == ID_ADD && type2.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + type1 = type2; // any type convertible chain + } + else if (!TypeOk( TypeRes, typemasque )) type1.SetType(99);// error of type + + switch ( TypeOp ) + { + case ID_LOG_OR: + case ID_LOG_AND: + case ID_TXT_OR: + case ID_TXT_AND: + case ID_EQ: + case ID_NE: + case ID_HI: + case ID_LO: + case ID_HS: + case ID_LS: + TypeRes = CBotTypBoolean; + } + if ( TypeCompatible (type1, type2, TypeOp ) ) // the results are compatible + { + // ok so, saves the operand in the object + inst->m_leftop = left; + + // special for evaluation of the operations of the same level from left to right + while ( IsInList( p->GivType(), pOperations, typemasque ) ) // same operation(s) follows? + { + TypeOp = p->GivType(); + CBotTwoOpExpr* i = new CBotTwoOpExpr(); // element for operation + i->SetToken(p); // stores the operation + i->m_leftop = inst; // left operand + type1 = TypeRes; + + p = p->GivNext(); // advance after + i->m_rightop = CBotTwoOpExpr::Compile( p, pStk, pOp ); + type2 = pStk->GivTypResult(); + + if ( !TypeCompatible (type1, type2, TypeOp) ) // the results are compatible + { + pStk->SetError(TX_BAD2TYPE, &i->m_token); + delete i; + return pStack->Return(NULL, pStk); + } + + if ( TypeRes != CBotTypString ) + TypeRes = MAX(type1.GivType(), type2.GivType()); + inst = i; + } + + CBotTypResult t(type1); + t.SetType(TypeRes); + // is a variable on the stack for the type of result + pStk->SetVar(CBotVar::Create((CBotToken*)NULL, t)); + + // and returns the requested object + return pStack->Return(inst, pStk); + } + pStk->SetError(TX_BAD2TYPE, &inst->m_token); + } + + // in case of error, releases the elements + delete left; + delete inst; + // and transmits the error to the stack + return pStack->Return(NULL, pStk); + } + + // if we are not dealing with an operation + or - + // goes to that requested, the operand (left) found + // instead of the object "addition" + return pStack->Return(left, pStk); } bool IsNan(CBotVar* left, CBotVar* right, int* err = NULL) { - if ( left ->GivInit() > IS_DEF || right->GivInit() > IS_DEF ) - { - if ( err != NULL ) *err = TX_OPNAN ; - return true; - } - return false; + if ( left ->GivInit() > IS_DEF || right->GivInit() > IS_DEF ) + { + if ( err != NULL ) *err = TX_OPNAN ; + return true; + } + return false; } @@ -296,273 +296,273 @@ bool IsNan(CBotVar* left, CBotVar* right, int* err = NULL) bool CBotTwoOpExpr::Execute(CBotStack* &pStack) { - CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack - // or return in case of recovery -// if ( pStk1 == EOX ) return true; - - // according to recovery, it may be in one of two states - - if ( pStk1->GivState() == 0 ) // first state, evaluates the left operand - { - if (!m_leftop->Execute(pStk1) ) return false; // interrupted here? - - // for OR and AND logic does not evaluate the second expression if not necessary - if ( (GivTokenType() == ID_LOG_AND || GivTokenType() == ID_TXT_AND ) && pStk1->GivVal() == false ) - { - CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); - res->SetValInt(false); - pStk1->SetVar(res); - return pStack->Return(pStk1); // transmits the result - } - if ( (GivTokenType() == ID_LOG_OR||GivTokenType() == ID_TXT_OR) && pStk1->GivVal() == true ) - { - CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); - res->SetValInt(true); - pStk1->SetVar(res); - return pStack->Return(pStk1); // transmits the result - } - - // passes to the next step - pStk1->SetState(1); // ready for further - } + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or return in case of recovery +// if ( pStk1 == EOX ) return true; + + // according to recovery, it may be in one of two states + + if ( pStk1->GivState() == 0 ) // first state, evaluates the left operand + { + if (!m_leftop->Execute(pStk1) ) return false; // interrupted here? + + // for OR and AND logic does not evaluate the second expression if not necessary + if ( (GivTokenType() == ID_LOG_AND || GivTokenType() == ID_TXT_AND ) && pStk1->GivVal() == false ) + { + CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); + res->SetValInt(false); + pStk1->SetVar(res); + return pStack->Return(pStk1); // transmits the result + } + if ( (GivTokenType() == ID_LOG_OR||GivTokenType() == ID_TXT_OR) && pStk1->GivVal() == true ) + { + CBotVar* res = CBotVar::Create( (CBotToken*)NULL, CBotTypBoolean); + res->SetValInt(true); + pStk1->SetVar(res); + return pStack->Return(pStk1); // transmits the result + } + + // passes to the next step + pStk1->SetState(1); // ready for further + } // requires a little more stack to avoid touching the result // of which is left on the stack, precisely - CBotStack* pStk2 = pStk1->AddStack(); // adds an item to the stack - // or return in case of recovery - - // 2e état, évalue l'opérande de droite - if ( pStk2->GivState() == 0 ) - { - if ( !m_rightop->Execute(pStk2) ) return false; // interrupted here? - pStk2->IncState(); - } - - CBotTypResult type1 = pStk1->GivTypResult(); // what kind of results? - CBotTypResult type2 = pStk2->GivTypResult(); - - CBotStack* pStk3 = pStk2->AddStack(this); // adds an item to the stack - if ( pStk3->IfStep() ) return false; // shows the operation if step by step - - // creates a temporary variable to put the result - // what kind of result? - int TypeRes = MAX(type1.GivType(), type2.GivType()); - - if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - } - - switch ( GivTokenType() ) - { - case ID_LOG_OR: - case ID_LOG_AND: - case ID_TXT_OR: - case ID_TXT_AND: - case ID_EQ: - case ID_NE: - case ID_HI: - case ID_LO: - case ID_HS: - case ID_LS: - TypeRes = CBotTypBoolean; - break; - case ID_DIV: - TypeRes = MAX(TypeRes, CBotTypFloat); - } - - // creates a variable for the result - CBotVar* result = CBotVar::Create( (CBotToken*)NULL, TypeRes); - - // creates a variable to perform the calculation in the appropriate type - TypeRes = MAX(type1.GivType(), type2.GivType()); - - if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) - { - TypeRes = CBotTypString; - } - - CBotVar* temp; - - if ( TypeRes == CBotTypPointer ) TypeRes = CBotTypNullPointer; - if ( TypeRes == CBotTypClass ) temp = CBotVar::Create( (CBotToken*)NULL, CBotTypResult(CBotTypIntrinsic, type1.GivClass() ) ); - else temp = CBotVar::Create( (CBotToken*)NULL, TypeRes ); - - int err = 0; - // is a operation according to request - CBotVar* left = pStk1->GivVar(); - CBotVar* right = pStk2->GivVar(); - - switch (GivTokenType()) - { - case ID_ADD: - if ( !IsNan(left, right, &err) ) result->Add(left , right); // addition - break; - case ID_SUB: - if ( !IsNan(left, right, &err) ) result->Sub(left , right); // substraction - break; - case ID_MUL: - if ( !IsNan(left, right, &err) ) result->Mul(left , right); // multiplies - break; - case ID_POWER: - if ( !IsNan(left, right, &err) ) result->Power(left , right); // power - break; - case ID_DIV: - if ( !IsNan(left, right, &err) ) err = result->Div(left , right);// division - break; - case ID_MODULO: - if ( !IsNan(left, right, &err) ) err = result->Modulo(left , right);// remainder of division - break; - case ID_LO: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Lo(left , right)); // lower - break; - case ID_HI: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Hi(left , right)); // top - break; - case ID_LS: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Ls(left , right)); // less than or equal - break; - case ID_HS: - if ( !IsNan(left, right, &err) ) - result->SetValInt(temp->Hs(left , right)); // greater than or equal - break; - case ID_EQ: - if ( IsNan(left, right) ) - result->SetValInt(left->GivInit() == right->GivInit()) ; - else - result->SetValInt(temp->Eq(left , right)); // equal - break; - case ID_NE: - if ( IsNan(left, right) ) - result->SetValInt(left ->GivInit() != right->GivInit()) ; - else - result->SetValInt(temp->Ne(left , right)); // different - break; - case ID_TXT_AND: - case ID_LOG_AND: - case ID_AND: - if ( !IsNan(left, right, &err) ) result->And(left , right); // AND - break; - case ID_TXT_OR: - case ID_LOG_OR: - case ID_OR: - if ( !IsNan(left, right, &err) ) result->Or(left , right); // OR - break; - case ID_XOR: - if ( !IsNan(left, right, &err) ) result->XOr(left , right); // exclusive OR - break; - case ID_ASR: - if ( !IsNan(left, right, &err) ) result->ASR(left , right); - break; - case ID_SR: - if ( !IsNan(left, right, &err) ) result->SR(left , right); - break; - case ID_SL: - if ( !IsNan(left, right, &err) ) result->SL(left , right); - break; - default: - ASM_TRAP(); - } - delete temp; - - pStk2->SetVar(result); // puts the result on the stack - if ( err ) pStk2->SetError(err, &m_token); // and the possible error (division by zero) - -// pStk1->Return(pStk2); // releases the stack - return pStack->Return(pStk2); // transmits the result + CBotStack* pStk2 = pStk1->AddStack(); // adds an item to the stack + // or return in case of recovery + + // 2e état, évalue l'opérande de droite + if ( pStk2->GivState() == 0 ) + { + if ( !m_rightop->Execute(pStk2) ) return false; // interrupted here? + pStk2->IncState(); + } + + CBotTypResult type1 = pStk1->GivTypResult(); // what kind of results? + CBotTypResult type2 = pStk2->GivTypResult(); + + CBotStack* pStk3 = pStk2->AddStack(this); // adds an item to the stack + if ( pStk3->IfStep() ) return false; // shows the operation if step by step + + // creates a temporary variable to put the result + // what kind of result? + int TypeRes = MAX(type1.GivType(), type2.GivType()); + + if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + } + + switch ( GivTokenType() ) + { + case ID_LOG_OR: + case ID_LOG_AND: + case ID_TXT_OR: + case ID_TXT_AND: + case ID_EQ: + case ID_NE: + case ID_HI: + case ID_LO: + case ID_HS: + case ID_LS: + TypeRes = CBotTypBoolean; + break; + case ID_DIV: + TypeRes = MAX(TypeRes, CBotTypFloat); + } + + // creates a variable for the result + CBotVar* result = CBotVar::Create( (CBotToken*)NULL, TypeRes); + + // creates a variable to perform the calculation in the appropriate type + TypeRes = MAX(type1.GivType(), type2.GivType()); + + if ( GivTokenType() == ID_ADD && type1.Eq(CBotTypString) ) + { + TypeRes = CBotTypString; + } + + CBotVar* temp; + + if ( TypeRes == CBotTypPointer ) TypeRes = CBotTypNullPointer; + if ( TypeRes == CBotTypClass ) temp = CBotVar::Create( (CBotToken*)NULL, CBotTypResult(CBotTypIntrinsic, type1.GivClass() ) ); + else temp = CBotVar::Create( (CBotToken*)NULL, TypeRes ); + + int err = 0; + // is a operation according to request + CBotVar* left = pStk1->GivVar(); + CBotVar* right = pStk2->GivVar(); + + switch (GivTokenType()) + { + case ID_ADD: + if ( !IsNan(left, right, &err) ) result->Add(left , right); // addition + break; + case ID_SUB: + if ( !IsNan(left, right, &err) ) result->Sub(left , right); // substraction + break; + case ID_MUL: + if ( !IsNan(left, right, &err) ) result->Mul(left , right); // multiplies + break; + case ID_POWER: + if ( !IsNan(left, right, &err) ) result->Power(left , right); // power + break; + case ID_DIV: + if ( !IsNan(left, right, &err) ) err = result->Div(left , right);// division + break; + case ID_MODULO: + if ( !IsNan(left, right, &err) ) err = result->Modulo(left , right);// remainder of division + break; + case ID_LO: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Lo(left , right)); // lower + break; + case ID_HI: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Hi(left , right)); // top + break; + case ID_LS: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Ls(left , right)); // less than or equal + break; + case ID_HS: + if ( !IsNan(left, right, &err) ) + result->SetValInt(temp->Hs(left , right)); // greater than or equal + break; + case ID_EQ: + if ( IsNan(left, right) ) + result->SetValInt(left->GivInit() == right->GivInit()) ; + else + result->SetValInt(temp->Eq(left , right)); // equal + break; + case ID_NE: + if ( IsNan(left, right) ) + result->SetValInt(left ->GivInit() != right->GivInit()) ; + else + result->SetValInt(temp->Ne(left , right)); // different + break; + case ID_TXT_AND: + case ID_LOG_AND: + case ID_AND: + if ( !IsNan(left, right, &err) ) result->And(left , right); // AND + break; + case ID_TXT_OR: + case ID_LOG_OR: + case ID_OR: + if ( !IsNan(left, right, &err) ) result->Or(left , right); // OR + break; + case ID_XOR: + if ( !IsNan(left, right, &err) ) result->XOr(left , right); // exclusive OR + break; + case ID_ASR: + if ( !IsNan(left, right, &err) ) result->ASR(left , right); + break; + case ID_SR: + if ( !IsNan(left, right, &err) ) result->SR(left , right); + break; + case ID_SL: + if ( !IsNan(left, right, &err) ) result->SL(left , right); + break; + default: + ASM_TRAP(); + } + delete temp; + + pStk2->SetVar(result); // puts the result on the stack + if ( err ) pStk2->SetError(err, &m_token); // and the possible error (division by zero) + +// pStk1->Return(pStk2); // releases the stack + return pStack->Return(pStk2); // transmits the result } void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, bool bMain) { - if ( !bMain ) return; - CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack - if ( pStk1 == NULL ) return; - - // according to recovery, it may be in one of two states - - if ( pStk1->GivState() == 0 ) // first state, evaluates the left operand - { - m_leftop->RestoreState(pStk1, bMain); // interrupted here! - return; - } - - CBotStack* pStk2 = pStk1->RestoreStack(); // adds an item to the stack - if ( pStk2 == NULL ) return; - - // second state, evaluates the right operand - if ( pStk2->GivState() == 0 ) - { - m_rightop->RestoreState(pStk2, bMain); // interrupted here! - return; - } + if ( !bMain ) return; + CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack + if ( pStk1 == NULL ) return; + + // according to recovery, it may be in one of two states + + if ( pStk1->GivState() == 0 ) // first state, evaluates the left operand + { + m_leftop->RestoreState(pStk1, bMain); // interrupted here! + return; + } + + CBotStack* pStk2 = pStk1->RestoreStack(); // adds an item to the stack + if ( pStk2 == NULL ) return; + + // second state, evaluates the right operand + if ( pStk2->GivState() == 0 ) + { + m_rightop->RestoreState(pStk2, bMain); // interrupted here! + return; + } } bool CBotLogicExpr::Execute(CBotStack* &pStack) { - CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack - // or return in case of recovery -// if ( pStk1 == EOX ) return true; - - if ( pStk1->GivState() == 0 ) - { - if ( !m_condition->Execute(pStk1) ) return false; - if (!pStk1->SetState(1)) return false; - } - - if ( pStk1->GivVal() == true ) - { - if ( !m_op1->Execute(pStk1) ) return false; - } - else - { - if ( !m_op2->Execute(pStk1) ) return false; - } - - return pStack->Return(pStk1); // transmits the result + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or return in case of recovery +// if ( pStk1 == EOX ) return true; + + if ( pStk1->GivState() == 0 ) + { + if ( !m_condition->Execute(pStk1) ) return false; + if (!pStk1->SetState(1)) return false; + } + + if ( pStk1->GivVal() == true ) + { + if ( !m_op1->Execute(pStk1) ) return false; + } + else + { + if ( !m_op2->Execute(pStk1) ) return false; + } + + return pStack->Return(pStk1); // transmits the result } void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) { - if ( !bMain ) return; - - CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack - if ( pStk1 == NULL ) return; - - if ( pStk1->GivState() == 0 ) - { - m_condition->RestoreState(pStk1, bMain); - return; - } - - if ( pStk1->GivVal() == true ) - { - m_op1->RestoreState(pStk1, bMain); - } - else - { - m_op2->RestoreState(pStk1, bMain); - } + if ( !bMain ) return; + + CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack + if ( pStk1 == NULL ) return; + + if ( pStk1->GivState() == 0 ) + { + m_condition->RestoreState(pStk1, bMain); + return; + } + + if ( pStk1->GivVal() == true ) + { + m_op1->RestoreState(pStk1, bMain); + } + else + { + m_op2->RestoreState(pStk1, bMain); + } } #if 0 void t() { - int x,y; - 1>0 ? x = 0 : y = 0; + int x,y; + 1>0 ? x = 0 : y = 0; } #endif #if 01 void t(bool t) { - int x; - x = 1 + t ? 1 : 3 + 4 * 2 ; - t ? 0 : "test"; + int x; + x = 1 + t ? 1 : 3 + 4 * 2 ; + t ? 0 : "test"; } #endif diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 36923ae..51310a9 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -16,15 +16,15 @@ /////////////////////////////////////////////////////////////////////// // This file defined the following statements: -// CBotWhile "while (condition) {instructions}" -// CBotDo "do {instructions} while (condition)" -// CBotFor "for (init, condition, incr) {instructions}" -// CBotSwitch "switch (val) {instructions}" -// CBotCase "case val:" -// CBotBreak "break", "break label", "continu", "continu label" -// CBotTry "try {instructions}" -// CBotCatch "catch (condition) {instructions}" or "finally" -// CBotThrow "throw execption" +// CBotWhile "while (condition) {instructions}" +// CBotDo "do {instructions} while (condition)" +// CBotFor "for (init, condition, incr) {instructions}" +// CBotSwitch "switch (val) {instructions}" +// CBotCase "case val:" +// CBotBreak "break", "break label", "continu", "continu label" +// CBotTry "try {instructions}" +// CBotCatch "catch (condition) {instructions}" or "finally" +// CBotThrow "throw execption" #include "CBot.h" @@ -36,122 +36,122 @@ CBotWhile::CBotWhile() { - m_Condition = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotWhile"; // debug + m_Condition = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotWhile"; // debug } CBotWhile::~CBotWhile() { - delete m_Condition; // frees the condition - delete m_Block; // releases the block instruction + delete m_Condition; // frees the condition + delete m_Block; // releases the block instruction } CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotWhile* inst = new CBotWhile(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) + CBotWhile* inst = new CBotWhile(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // records the name of the label - } + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // records the name of the label + } - inst->SetToken(p); - if (!IsOfType(p, ID_WHILE)) return NULL; // should never happen + inst->SetToken(p); + if (!IsOfType(p, ID_WHILE)) return NULL; // should never happen - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp // a bit of battery please (??) - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // the condition exists + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); - if ( pStk->IsOk() ) - { - // the statement block is ok (it may be empty! + if ( pStk->IsOk() ) + { + // the statement block is ok (it may be empty! - return pStack->Return(inst, pStk); // return an object to the application + return pStack->Return(inst, pStk); // return an object to the application // makes the object to which the application - } - } + } + } - delete inst; // error, frees the place - return pStack->Return(NULL, pStk); // no object, the error is on the stack + delete inst; // error, frees the place + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // executes a "while" instruction bool CBotWhile :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - while( true ) switch( pile->GivState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the condition - if ( !m_Condition->Execute(pile) ) return false; // interrupted here? + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here? - // the result of the condition is on the stack + // the result of the condition is on the stack - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } - // the condition is true, pass in the second mode + // the condition is true, pass in the second mode - if (!pile->SetState(1)) return false; // ready for further + if (!pile->SetState(1)) return false; // ready for further - case 1: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(0, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } + case 1: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(0, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to the test again - if (!pile->SetState(0, 0)) return false; - continue; - } + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to the test again + if (!pile->SetState(0, 0)) return false; + continue; + } } void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are two possible states (depending on recovery) - case 0: - // evaluates the condition - m_Condition->RestoreState(pile, bMain); - return; + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + m_Condition->RestoreState(pile, bMain); + return; - case 1: - // evaluates the associated statement block - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - } + case 1: + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + } } @@ -162,140 +162,140 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) CBotRepeat::CBotRepeat() { - m_NbIter = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotRepeat"; // debug + m_NbIter = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotRepeat"; // debug } CBotRepeat::~CBotRepeat() { - delete m_NbIter; // frees the condition - delete m_Block; // frees the instruction block + delete m_NbIter; // frees the condition + delete m_Block; // frees the instruction block } CBotInstr* CBotRepeat::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotRepeat* inst = new CBotRepeat(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_REPEAT)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - CBotToken* ppp = p; // preserves the ^ token (starting position) - if ( NULL != (inst->m_NbIter = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GivType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - // the statement block is ok (it may be empty! - - return pStack->Return(inst, pStk); // return an object to the application - } - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStk->SetStartError(ppp->GivStart()); - pStk->SetError( TX_BADTYPE, p->GivStart() ); - } - pStack->SetError(TX_ENDOF, p); - } - pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotRepeat* inst = new CBotRepeat(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_REPEAT)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + CBotToken* ppp = p; // preserves the ^ token (starting position) + if ( NULL != (inst->m_NbIter = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GivType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + // the statement block is ok (it may be empty! + + return pStack->Return(inst, pStk); // return an object to the application + } + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStk->SetStartError(ppp->GivStart()); + pStk->SetError( TX_BADTYPE, p->GivStart() ); + } + pStack->SetError(TX_ENDOF, p); + } + pStack->SetError(TX_OPENPAR, p->GivStart()); // missing parenthesis + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // execution of intruction "repeat" bool CBotRepeat :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - while( true ) switch( pile->GivState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the number of iterations - if ( !m_NbIter->Execute(pile) ) return false; // interrupted here ? + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the number of iterations + if ( !m_NbIter->Execute(pile) ) return false; // interrupted here ? - // the result of the condition is on the stack + // the result of the condition is on the stack - // terminates if an error or if the condition is false - int n; - if ( !pile->IsOk() || ( n = pile->GivVal() ) < 1 ) - { - return pj->Return(pile); // sends the results and releases the stack - } + // terminates if an error or if the condition is false + int n; + if ( !pile->IsOk() || ( n = pile->GivVal() ) < 1 ) + { + return pj->Return(pile); // sends the results and releases the stack + } - // puts the number of iterations +1 to the "state" + // puts the number of iterations +1 to the "state" - if (!pile->SetState(n+1)) return false; // ready for further - continue; // continue as a result + if (!pile->SetState(n+1)) return false; // ready for further + continue; // continue as a result - case 1: - // normal end of the loop - return pj->Return(pile); // sends the results and releases the stack + case 1: + // normal end of the loop + return pj->Return(pile); // sends the results and releases the stack - default: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(pile->GivState()-1, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } + default: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(pile->GivState()-1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to the test again - if (!pile->SetState(pile->GivState()-1, 0)) return false; - continue; - } + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to the test again + if (!pile->SetState(pile->GivState()-1, 0)) return false; + continue; + } } void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are two possible states (depending on recovery) - case 0: - // evaluates the condition - m_NbIter->RestoreState(pile, bMain); - return; + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // evaluates the condition + m_NbIter->RestoreState(pile, bMain); + return; - case 1: - // evaluates the associated statement block - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - } + case 1: + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + } } /////////////////////////////////////////////////////////////////////////// @@ -305,127 +305,127 @@ void CBotRepeat :: RestoreState(CBotStack* &pj, bool bMain) CBotDo::CBotDo() { - m_Condition = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotDo"; // debug + m_Condition = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotDo"; // debug } CBotDo::~CBotDo() { - delete m_Condition; // frees the condition - delete m_Block; // frees the instruction block + delete m_Condition; // frees the condition + delete m_Block; // frees the instruction block } CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotDo* inst = new CBotDo(); // creates the object - - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_DO)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - - // looking for a statement block after the do - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - if (IsOfType(p, ID_WHILE)) - { - if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // the condition exists - if (IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); // return an object to the application - } - pStk->SetError(TX_ENDOF, p->GivStart()); - } - } - pStk->SetError(TX_WHILE, p->GivStart()); - } - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotDo* inst = new CBotDo(); // creates the object + + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_DO)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + + // looking for a statement block after the do + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + if (IsOfType(p, ID_WHILE)) + { + if ( NULL != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists + if (IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError(TX_ENDOF, p->GivStart()); + } + } + pStk->SetError(TX_WHILE, p->GivStart()); + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // executes instruction "do" bool CBotDo :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } - if (!pile->SetState(1)) return false; // ready for further + if (!pile->SetState(1)) return false; // ready for further - case 1: - // evaluates the condition - if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? - - // the result of the condition is on the stack - - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to instruction block to start - if (!pile->SetState(0, 0)) return false; - continue; - } + case 1: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to instruction block to start + if (!pile->SetState(0, 0)) return false; + continue; + } } void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are two possible states (depending on recovery) - case 0: - // restores the assosiated statement's block - if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); - return; - - case 1: - // restores the condition - m_Condition->RestoreState(pile, bMain); - return; - } + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are two possible states (depending on recovery) + case 0: + // restores the assosiated statement's block + if ( m_Block != NULL ) m_Block->RestoreState(pile, bMain); + return; + + case 1: + // restores the condition + m_Condition->RestoreState(pile, bMain); + return; + } } @@ -436,181 +436,181 @@ void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) CBotFor::CBotFor() { - m_Init = - m_Test = - m_Incr = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotFor"; // debug + m_Init = + m_Test = + m_Incr = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotFor"; // debug } CBotFor::~CBotFor() { - delete m_Init; - delete m_Test; - delete m_Incr; - delete m_Block; // frees the instruction block + delete m_Init; + delete m_Test; + delete m_Incr; + delete m_Block; // frees the instruction block } CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotFor* inst = new CBotFor(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GivString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_FOR)) return NULL; // should never happen - - if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - return NULL; - } - - CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp - - // compiles instructions for initialization - inst->m_Init = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // lack the semicolon? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); // no object, the error is on the stack - } - inst->m_Test = CBotBoolExpr::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // lack the semicolon? - { - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); // no object, the error is on the stack - } - inst->m_Incr = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? - { - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - if ( pStk->IsOk() ) - return pStack->Return(inst, pStk);; - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - } - } - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotFor* inst = new CBotFor(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GivString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_FOR)) return NULL; // should never happen + + if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + return NULL; + } + + CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp + + // compiles instructions for initialization + inst->m_Init = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); // no object, the error is on the stack + } + inst->m_Test = CBotBoolExpr::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); // no object, the error is on the stack + } + inst->m_Incr = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? + { + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + if ( pStk->IsOk() ) + return pStack->Return(inst, pStk);; + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + } + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // execution of instruction "for" bool CBotFor :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) - // or find in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GivState() ) // executes the loop - { // there are four possible states (depending on recovery) - case 0: - // initialize - if ( m_Init != NULL && - !m_Init->Execute(pile) ) return false; // interrupted here ? - if (!pile->SetState(1)) return false; // ready for further + CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GivState() ) // executes the loop + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != NULL && + !m_Init->Execute(pile) ) return false; // interrupted here ? + if (!pile->SetState(1)) return false; // ready for further - case 1: - // evaluates the condition - if ( m_Test != NULL ) // no strings attached? -> True! - { - if (!m_Test->Execute(pile) ) return false; // interrupted here ? - - // the result of the condition is on the stack - - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GivVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } - } - - // la condition est vrai, passe à la suite - if (!pile->SetState(2)) return false; // ready for further - - case 2: - // evaluates the associated statement block - if ( m_Block != NULL && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } - - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - if (!pile->SetState(3)) return false; // ready for further - - case 3: - // evalutate the incrementation - if ( m_Incr != NULL && - !m_Incr->Execute(pile) ) return false; // interrupted here ? - - // returns to the test again - if (!pile->SetState(1, 0)) return false; // returns to the test - continue; - } + case 1: + // evaluates the condition + if ( m_Test != NULL ) // no strings attached? -> True! + { + if (!m_Test->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GivVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + } + + // la condition est vrai, passe à la suite + if (!pile->SetState(2)) return false; // ready for further + + case 2: + // evaluates the associated statement block + if ( m_Block != NULL && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + if (!pile->SetState(3)) return false; // ready for further + + case 3: + // evalutate the incrementation + if ( m_Incr != NULL && + !m_Incr->Execute(pile) ) return false; // interrupted here ? + + // returns to the test again + if (!pile->SetState(1, 0)) return false; // returns to the test + continue; + } } void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) - if ( pile == NULL ) return; - - switch( pile->GivState() ) - { // there are four possible states (depending on recovery) - case 0: - // initialize - if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrupted here ! - return; - - case 1: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variables definitions - - // evaluates the condition - if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrupted here ! - return; - - case 2: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions - - // evaluates the associated statement block - if ( m_Block != NULL ) m_Block->RestoreState(pile, true); - return; - - case 3: - if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions - - // evaluate the incrementation - if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrupted here ! - return; - } + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) + if ( pile == NULL ) return; + + switch( pile->GivState() ) + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != NULL ) m_Init->RestoreState(pile, true); // interrupted here ! + return; + + case 1: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variables definitions + + // evaluates the condition + if ( m_Test != NULL ) m_Test->RestoreState(pile, true); // interrupted here ! + return; + + case 2: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluates the associated statement block + if ( m_Block != NULL ) m_Block->RestoreState(pile, true); + return; + + case 3: + if ( m_Init != NULL ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluate the incrementation + if ( m_Incr != NULL ) m_Incr->RestoreState(pile, true); // interrupted here ! + return; + } } ////////////////////////////////////////////////////////////////////////////////////// @@ -620,92 +620,92 @@ void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) CBotListExpression::CBotListExpression() { - m_Expr = NULL; - name = "CBotListExpression"; + m_Expr = NULL; + name = "CBotListExpression"; } CBotListExpression::~CBotListExpression() { - delete m_Expr; + delete m_Expr; } // seeks a declaration of variable or expression static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) { - CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? - if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? - if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? - if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? - if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compiles an expression - return i; + CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? + if ( i== NULL ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? + if ( i== NULL ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? + if ( i== NULL ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? + if ( i== NULL ) i = CBotExpression::Compile( p, pStack ); // compiles an expression + return i; } CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotListExpression* inst = new CBotListExpression(); - - inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list - if (pStack->IsOk()) - { - while ( IsOfType(p, ID_COMMA) ) // more instructions? - { - CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? - inst->m_Expr->AddNext(i); // added after - if ( !pStack->IsOk() ) - { - delete inst; - return NULL; // no object, the error is on the stack - } - } - return inst; - } - delete inst; - return NULL; + CBotListExpression* inst = new CBotListExpression(); + + inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list + if (pStack->IsOk()) + { + while ( IsOfType(p, ID_COMMA) ) // more instructions? + { + CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? + inst->m_Expr->AddNext(i); // added after + if ( !pStack->IsOk() ) + { + delete inst; + return NULL; // no object, the error is on the stack + } + } + return inst; + } + delete inst; + return NULL; } bool CBotListExpression::Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(); // essential - CBotInstr* p = m_Expr; // the first expression - - int state = pile->GivState(); - while (state-->0) p = p->GivNext(); // returns to the interrupted operation - - if ( p != NULL ) while (true) - { - if ( !p->Execute(pile) ) return false; - p = p->GivNext(); - if ( p == NULL ) break; - if (!pile->IncState()) return false; // ready for next - } - return pj->Return(pile); + CBotStack* pile = pj->AddStack(); // essential + CBotInstr* p = m_Expr; // the first expression + + int state = pile->GivState(); + while (state-->0) p = p->GivNext(); // returns to the interrupted operation + + if ( p != NULL ) while (true) + { + if ( !p->Execute(pile) ) return false; + p = p->GivNext(); + if ( p == NULL ) break; + if (!pile->IncState()) return false; // ready for next + } + return pj->Return(pile); } void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) { - CBotStack* pile = pj; - int state = 0x7000; - - if ( bMain ) - { - pile = pj->RestoreStack(); - if ( pile == NULL ) return; - state = pile->GivState(); - } - - CBotInstr* p = m_Expr; // the first expression - - while (p != NULL && state-->0) - { - p->RestoreState(pile, false); - p = p->GivNext(); // returns to the interrupted operation - } - - if ( p != NULL ) - { - p->RestoreState(pile, bMain); - } + CBotStack* pile = pj; + int state = 0x7000; + + if ( bMain ) + { + pile = pj->RestoreStack(); + if ( pile == NULL ) return; + state = pile->GivState(); + } + + CBotInstr* p = m_Expr; // the first expression + + while (p != NULL && state-->0) + { + p->RestoreState(pile, false); + p = p->GivNext(); // returns to the interrupted operation + } + + if ( p != NULL ) + { + p->RestoreState(pile, bMain); + } } /////////////////////////////////////////////////////////////////////////// @@ -715,186 +715,186 @@ void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) CBotSwitch::CBotSwitch() { - m_Value = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotSwitch"; // debug + m_Value = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotSwitch"; // debug } CBotSwitch::~CBotSwitch() { - delete m_Value; // frees the value - delete m_Block; // frees the instruction block + delete m_Value; // frees the value + delete m_Block; // frees the instruction block } CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotSwitch* inst = new CBotSwitch(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_SWITCH)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - if ( NULL != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GivType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - if ( IsOfType(p, ID_OPBLK ) ) - { - IncLvl(); - - while( !IsOfType( p, ID_CLBLK ) ) - { - if ( p->GivType() == ID_CASE || p->GivType() == ID_DEFAULT) - { - CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp - - CBotInstr* i = CBotCase::Compile( p, pStk2 ); - if (i == NULL) - { - delete inst; - return pStack->Return(NULL, pStk2); - } - delete pStk2; - if ( inst->m_Block == NULL ) inst->m_Block = i; - else inst->m_Block->AddNext(i); - continue; - } - - if ( inst->m_Block == NULL ) - { - pStk->SetError(TX_NOCASE, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if ( !pStk->IsOk() ) - { - delete inst; - return pStack->Return(NULL, pStk); - } - inst->m_Block->AddNext(i); - - if ( p == NULL ) - { - pStk->SetError(TX_CLOSEBLK, -1); - delete inst; - return pStack->Return(NULL, pStk); - } - } - DecLvl(); - - if ( inst->m_Block == NULL ) - { - pStk->SetError(TX_NOCASE, p->GivStart()); - delete inst; - return pStack->Return(NULL, pStk); - } - // the statement block is ok - return pStack->Return(inst, pStk); // return an object to the application - } - pStk->SetError( TX_OPENBLK, p->GivStart() ); - } - pStk->SetError( TX_CLOSEPAR, p->GivStart() ); - } - pStk->SetError( TX_BADTYPE, p->GivStart() ); - } - } - pStk->SetError( TX_OPENPAR, p->GivStart()); - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotSwitch* inst = new CBotSwitch(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_SWITCH)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + if ( NULL != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GivType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + if ( IsOfType(p, ID_OPBLK ) ) + { + IncLvl(); + + while( !IsOfType( p, ID_CLBLK ) ) + { + if ( p->GivType() == ID_CASE || p->GivType() == ID_DEFAULT) + { + CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp + + CBotInstr* i = CBotCase::Compile( p, pStk2 ); + if (i == NULL) + { + delete inst; + return pStack->Return(NULL, pStk2); + } + delete pStk2; + if ( inst->m_Block == NULL ) inst->m_Block = i; + else inst->m_Block->AddNext(i); + continue; + } + + if ( inst->m_Block == NULL ) + { + pStk->SetError(TX_NOCASE, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + + CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if ( !pStk->IsOk() ) + { + delete inst; + return pStack->Return(NULL, pStk); + } + inst->m_Block->AddNext(i); + + if ( p == NULL ) + { + pStk->SetError(TX_CLOSEBLK, -1); + delete inst; + return pStack->Return(NULL, pStk); + } + } + DecLvl(); + + if ( inst->m_Block == NULL ) + { + pStk->SetError(TX_NOCASE, p->GivStart()); + delete inst; + return pStack->Return(NULL, pStk); + } + // the statement block is ok + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError( TX_OPENBLK, p->GivStart() ); + } + pStk->SetError( TX_CLOSEPAR, p->GivStart() ); + } + pStk->SetError( TX_BADTYPE, p->GivStart() ); + } + } + pStk->SetError( TX_OPENPAR, p->GivStart()); + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // executes instruction "switch" bool CBotSwitch :: Execute(CBotStack* &pj) { - CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack -// if ( pile1 == EOX ) return true; - - CBotInstr* p = m_Block; // first expression - - int state = pile1->GivState(); - if (state == 0) - { - if ( !m_Value->Execute(pile1) ) return false; - pile1->SetState(state = -1); - } - - if ( pile1->IfStep() ) return false; - - if ( state == -1 ) - { - state = 0; - int val = pile1->GivVal(); // result of the value - - CBotStack* pile2 = pile1->AddStack(); - while ( p != NULL ) // search for the corresponding case in a list - { - state++; - if ( p->CompCase( pile2, val ) ) break; // found the case - p = p->GivNext(); - } - pile2->Delete(); - - if ( p == NULL ) return pj->Return(pile1); // completed if nothing - - if ( !pile1->SetState(state) ) return false; - } - - p = m_Block; // returns to the beginning - while (state-->0) p = p->GivNext(); // advance in the list - - while( p != NULL ) - { - if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); - if ( !pile1->IncState() ) return false; - p = p->GivNext(); - } - return pj->Return(pile1); + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GivState(); + if (state == 0) + { + if ( !m_Value->Execute(pile1) ) return false; + pile1->SetState(state = -1); + } + + if ( pile1->IfStep() ) return false; + + if ( state == -1 ) + { + state = 0; + int val = pile1->GivVal(); // result of the value + + CBotStack* pile2 = pile1->AddStack(); + while ( p != NULL ) // search for the corresponding case in a list + { + state++; + if ( p->CompCase( pile2, val ) ) break; // found the case + p = p->GivNext(); + } + pile2->Delete(); + + if ( p == NULL ) return pj->Return(pile1); // completed if nothing + + if ( !pile1->SetState(state) ) return false; + } + + p = m_Block; // returns to the beginning + while (state-->0) p = p->GivNext(); // advance in the list + + while( p != NULL ) + { + if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); + if ( !pile1->IncState() ) return false; + p = p->GivNext(); + } + return pj->Return(pile1); } void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack - if ( pile1 == NULL ) return; - - CBotInstr* p = m_Block; // first expression - - int state = pile1->GivState(); - if (state == 0) - { - m_Value->RestoreState(pile1, bMain); - return; - } - - if ( state == -1 ) - { - return; - } - -// p = m_Block; // returns to the beginning - while ( p != NULL && state-- > 0 ) - { - p->RestoreState(pile1, false); - p = p->GivNext(); // advance in the list - } - - if( p != NULL ) - { - p->RestoreState(pile1, true); - return; - } + if ( !bMain ) return; + + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == NULL ) return; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GivState(); + if (state == 0) + { + m_Value->RestoreState(pile1, bMain); + return; + } + + if ( state == -1 ) + { + return; + } + +// p = m_Block; // returns to the beginning + while ( p != NULL && state-- > 0 ) + { + p->RestoreState(pile1, false); + p = p->GivNext(); // advance in the list + } + + if( p != NULL ) + { + p->RestoreState(pile1, true); + return; + } } /////////////////////////////////////////////////////////////////////////// @@ -905,50 +905,50 @@ void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) CBotCase::CBotCase() { - m_Value = NULL; // NULL so that delete is not possible further - name = "CBotCase"; // debug + m_Value = NULL; // NULL so that delete is not possible further + name = "CBotCase"; // debug } CBotCase::~CBotCase() { - delete m_Value; // frees the value + delete m_Value; // frees the value } CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCase* inst = new CBotCase(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return NULL; // should never happen - - if ( pp->GivType() == ID_CASE ) - { - pp = p; - inst->m_Value = CBotExprNum::Compile(p, pStack); - if ( inst->m_Value == NULL ) - { - pStack->SetError( TX_BADNUM, pp ); - delete inst; - return NULL; - } - } - if ( !IsOfType( p, ID_DOTS )) - { - pStack->SetError( TX_MISDOTS, p->GivStart() ); - delete inst; - return NULL; - } - - return inst; + CBotCase* inst = new CBotCase(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return NULL; // should never happen + + if ( pp->GivType() == ID_CASE ) + { + pp = p; + inst->m_Value = CBotExprNum::Compile(p, pStack); + if ( inst->m_Value == NULL ) + { + pStack->SetError( TX_BADNUM, pp ); + delete inst; + return NULL; + } + } + if ( !IsOfType( p, ID_DOTS )) + { + pStack->SetError( TX_MISDOTS, p->GivStart() ); + delete inst; + return NULL; + } + + return inst; } // execution of instruction "case" bool CBotCase::Execute(CBotStack* &pj) { - return true; // the "case" statement does nothing! + return true; // the "case" statement does nothing! } void CBotCase::RestoreState(CBotStack* &pj, bool bMain) @@ -960,10 +960,10 @@ void CBotCase::RestoreState(CBotStack* &pj, bool bMain) bool CBotCase::CompCase(CBotStack* &pile, int val) { - if ( m_Value == NULL ) return true; // "default" case + if ( m_Value == NULL ) return true; // "default" case - while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) - return (pile->GivVal() == val); // compared with the given value + while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) + return (pile->GivVal() == val); // compared with the given value } /////////////////////////////////////////////////////////////////////////// @@ -973,7 +973,7 @@ bool CBotCase::CompCase(CBotStack* &pile, int val) CBotBreak::CBotBreak() { - name = "CBotBreak"; // debug + name = "CBotBreak"; // debug } CBotBreak::~CBotBreak() @@ -982,58 +982,58 @@ CBotBreak::~CBotBreak() CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotToken* pp = p; // preserves at the ^ token (starting position) - int type = p->GivType(); - - if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return NULL; // should never happen - - if ( !ChkLvl(CBotString(), type ) ) - { - pStack->SetError(TX_BREAK, pp); - return NULL; // no object, the error is on the stack - } - - CBotBreak* inst = new CBotBreak(); // creates the object - inst->SetToken(pp); // keeps the operation - - pp = p; - if ( IsOfType( p, TokenTypVar ) ) - { - inst->m_label = pp->GivString(); // register the name of label - if ( !ChkLvl(inst->m_label, type ) ) - { - delete inst; - pStack->SetError(TX_NOLABEL, pp); - return NULL; // no object, the error is on the stack - } - } - - if (IsOfType(p, ID_SEP)) - { - return inst; // return what it wants - } - delete inst; - - pStack->SetError(TX_ENDOF, p->GivStart()); - return NULL; // no object, the error is on the stack + CBotToken* pp = p; // preserves at the ^ token (starting position) + int type = p->GivType(); + + if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return NULL; // should never happen + + if ( !ChkLvl(CBotString(), type ) ) + { + pStack->SetError(TX_BREAK, pp); + return NULL; // no object, the error is on the stack + } + + CBotBreak* inst = new CBotBreak(); // creates the object + inst->SetToken(pp); // keeps the operation + + pp = p; + if ( IsOfType( p, TokenTypVar ) ) + { + inst->m_label = pp->GivString(); // register the name of label + if ( !ChkLvl(inst->m_label, type ) ) + { + delete inst; + pStack->SetError(TX_NOLABEL, pp); + return NULL; // no object, the error is on the stack + } + } + + if (IsOfType(p, ID_SEP)) + { + return inst; // return what it wants + } + delete inst; + + pStack->SetError(TX_ENDOF, p->GivStart()); + return NULL; // no object, the error is on the stack } // execution of statement "break" or "continu" bool CBotBreak :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); - return pj->Return(pile); + pile->SetBreak(m_token.GivType()==ID_BREAK ? 1 : 2, m_label); + return pj->Return(pile); } void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain ) pj->RestoreStack(this); + if ( bMain ) pj->RestoreStack(this); } @@ -1044,51 +1044,51 @@ void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) CBotTry::CBotTry() { - m_ListCatch = NULL; - m_FinalInst = - m_Block = NULL; // NULL so that delete is not possible further - name = "CBotTry"; // debug + m_ListCatch = NULL; + m_FinalInst = + m_Block = NULL; // NULL so that delete is not possible further + name = "CBotTry"; // debug } CBotTry::~CBotTry() { - delete m_ListCatch; // frees the list - delete m_Block; // frees the instruction block - delete m_FinalInst; -} + delete m_ListCatch; // frees the list + delete m_Block; // frees the instruction block + delete m_FinalInst; +} CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotTry* inst = new CBotTry(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_TRY)) return NULL; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); - CBotCatch** pn = &inst->m_ListCatch; - - while (pStk->IsOk() && p->GivType() == ID_CATCH) - { - CBotCatch* i = CBotCatch::Compile(p, pStk); - *pn = i; - pn = &i->m_next; - } - - if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) - { - inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); - } - - if (pStk->IsOk()) - { - return pStack->Return(inst, pStk); // return an object to the application - } - - delete inst; // error, frees up - return pStack->Return(NULL, pStk); // no object, the error is on the stack + CBotTry* inst = new CBotTry(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_TRY)) return NULL; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); + CBotCatch** pn = &inst->m_ListCatch; + + while (pStk->IsOk() && p->GivType() == ID_CATCH) + { + CBotCatch* i = CBotCatch::Compile(p, pStk); + *pn = i; + pn = &i->m_next; + } + + if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) + { + inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); + } + + if (pStk->IsOk()) + { + return pStack->Return(inst, pStk); // return an object to the application + } + + delete inst; // error, frees up + return pStack->Return(NULL, pStk); // no object, the error is on the stack } // execution of instruction Try @@ -1098,145 +1098,145 @@ CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotTry :: Execute(CBotStack* &pj) { - int val; - - CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack -// if ( pile1 == EOX ) return true; - - if ( pile1->IfStep() ) return false; - // or find in case of recovery - CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack - CBotStack* pile2 = pile0->AddStack(); - - if ( pile1->GivState() == 0 ) - { - if ( m_Block->Execute(pile1) ) - { - if ( m_FinalInst == NULL ) return pj->Return(pile1); - pile1->SetState(-2); // passes final - } - - val = pile1->GivError(); - if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? - return false; // does not make the catch - - pile1->IncState(); - pile2->SetState(val); // stores the error number - pile1->SetError(0); // for now there is are more errors! - - if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? - return false; // does not make the catch - } - - // there was an interruption - // see what it returns - - CBotCatch* pc = m_ListCatch; - int state = (short)pile1->GivState(); // where were we? - val = pile2->GivState(); // what error? - pile0->SetState(1); // marking the GetRunPos - - if ( val >= 0 && state > 0 ) while ( pc != NULL ) - { - if ( --state <= 0 ) - { + int val; + + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + if ( pile1->IfStep() ) return false; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack + CBotStack* pile2 = pile0->AddStack(); + + if ( pile1->GivState() == 0 ) + { + if ( m_Block->Execute(pile1) ) + { + if ( m_FinalInst == NULL ) return pj->Return(pile1); + pile1->SetState(-2); // passes final + } + + val = pile1->GivError(); + if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? + return false; // does not make the catch + + pile1->IncState(); + pile2->SetState(val); // stores the error number + pile1->SetError(0); // for now there is are more errors! + + if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? + return false; // does not make the catch + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = (short)pile1->GivState(); // where were we? + val = pile2->GivState(); // what error? + pile0->SetState(1); // marking the GetRunPos + + if ( val >= 0 && state > 0 ) while ( pc != NULL ) + { + if ( --state <= 0 ) + { // request to the catch block if they feel concerned - // demande au bloc catch s'il se sent concerné - if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! - pile1->IncState(); - } - if ( --state <= 0 ) - { - if ( pile2->GivVal() == true ) - { -// pile0->SetState(1); - - if ( !pc->Execute(pile2) ) return false; // performs the operation - if ( m_FinalInst == NULL ) - return pj->Return(pile2); // ends the try - - pile1->SetState(-2); // passes final - break; - } - pile1->IncState(); - } - pc = pc->m_next; - } - if ( m_FinalInst != NULL && - pile1->GivState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final - - if (pile1->GivState() <= -1) - { -// pile0->SetState(1); - - if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; - if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception - pile2->SetError(pile1->GivState()==-1 ? val : 0); // gives the initial error - return pj->Return(pile2); - } - - pile1->SetState(0); // returns to the evaluation - pile0->SetState(0); // returns to the evaluation - if ( val != 0 && m_ListCatch == NULL && m_FinalInst == NULL ) - return pj->Return(pile2); // ends the try without exception - - pile1->SetError(val); // gives the error - return false; // it's not for us + // demande au bloc catch s'il se sent concerné + if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! + pile1->IncState(); + } + if ( --state <= 0 ) + { + if ( pile2->GivVal() == true ) + { +// pile0->SetState(1); + + if ( !pc->Execute(pile2) ) return false; // performs the operation + if ( m_FinalInst == NULL ) + return pj->Return(pile2); // ends the try + + pile1->SetState(-2); // passes final + break; + } + pile1->IncState(); + } + pc = pc->m_next; + } + if ( m_FinalInst != NULL && + pile1->GivState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final + + if (pile1->GivState() <= -1) + { +// pile0->SetState(1); + + if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; + if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception + pile2->SetError(pile1->GivState()==-1 ? val : 0); // gives the initial error + return pj->Return(pile2); + } + + pile1->SetState(0); // returns to the evaluation + pile0->SetState(0); // returns to the evaluation + if ( val != 0 && m_ListCatch == NULL && m_FinalInst == NULL ) + return pj->Return(pile2); // ends the try without exception + + pile1->SetError(val); // gives the error + return false; // it's not for us } void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; - - int val; - CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack - if ( pile1 == NULL ) return; - // or find in case of recovery - CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack - if ( pile0 == NULL ) return; - - CBotStack* pile2 = pile0->RestoreStack(); - if ( pile2 == NULL ) return; - - m_Block->RestoreState(pile1, bMain); - if ( pile0->GivState() == 0 ) - { - return; - } - - // there was an interruption - // see what it returns - - CBotCatch* pc = m_ListCatch; - int state = pile1->GivState(); // where were we ? - val = pile2->GivState(); // what error ? - - if ( val >= 0 && state > 0 ) while ( pc != NULL ) - { - if ( --state <= 0 ) - { + if ( !bMain ) return; + + int val; + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == NULL ) return; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack + if ( pile0 == NULL ) return; + + CBotStack* pile2 = pile0->RestoreStack(); + if ( pile2 == NULL ) return; + + m_Block->RestoreState(pile1, bMain); + if ( pile0->GivState() == 0 ) + { + return; + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = pile1->GivState(); // where were we ? + val = pile2->GivState(); // what error ? + + if ( val >= 0 && state > 0 ) while ( pc != NULL ) + { + if ( --state <= 0 ) + { // request to the catch block if they feel concerned - // demande au bloc catch s'il se sent concerné - pc->RestoreCondState(pile2, bMain); // suspend ! - return; - } - if ( --state <= 0 ) - { - if ( pile2->GivVal() == true ) - { - pc->RestoreState(pile2, bMain); // execute the operation - return; - } - } - pc = pc->m_next; - } - - if (pile1->GivState() <= -1) - { - m_FinalInst->RestoreState(pile2, bMain); - return; - } + // demande au bloc catch s'il se sent concerné + pc->RestoreCondState(pile2, bMain); // suspend ! + return; + } + if ( --state <= 0 ) + { + if ( pile2->GivVal() == true ) + { + pc->RestoreState(pile2, bMain); // execute the operation + return; + } + } + pc = pc->m_next; + } + + if (pile1->GivState() <= -1) + { + m_FinalInst->RestoreState(pile2, bMain); + return; + } } /////////////////////////////////////////////////////////////////////////// @@ -1246,81 +1246,81 @@ void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) CBotCatch::CBotCatch() { - m_Cond = - m_Block = NULL; // NULL so that delete is not possible further - m_next = NULL; + m_Cond = + m_Block = NULL; // NULL so that delete is not possible further + m_next = NULL; - name = "CBotCatch"; // debug + name = "CBotCatch"; // debug } CBotCatch::~CBotCatch() { - delete m_Cond; // frees the list - delete m_Block; // frees the instruction block - delete m_next; // and subsequent + delete m_Cond; // frees the list + delete m_Block; // frees the instruction block + delete m_next; // and subsequent } CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) { - CBotCatch* inst = new CBotCatch(); // creates the object - pStack->SetStartError(p->GivStart()); - - inst->SetToken(p); - if (!IsOfType(p, ID_CATCH)) return NULL; // should never happen - - if (IsOfType(p, ID_OPENPAR)) - { - inst->m_Cond = CBotExpression::Compile(p, pStack); - if (( pStack->GivType() < CBotTypLong || - pStack->GivTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); - if ( pStack->IsOk() ) - return inst; // return an object to the application - } - pStack->SetError(TX_CLOSEPAR, p->GivStart()); - } - pStack->SetError(TX_BADTYPE, p->GivStart()); - } - pStack->SetError(TX_OPENPAR, p->GivStart()); - delete inst; // error, frees up - return NULL; // no object, the error is on the stack + CBotCatch* inst = new CBotCatch(); // creates the object + pStack->SetStartError(p->GivStart()); + + inst->SetToken(p); + if (!IsOfType(p, ID_CATCH)) return NULL; // should never happen + + if (IsOfType(p, ID_OPENPAR)) + { + inst->m_Cond = CBotExpression::Compile(p, pStack); + if (( pStack->GivType() < CBotTypLong || + pStack->GivTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); + if ( pStack->IsOk() ) + return inst; // return an object to the application + } + pStack->SetError(TX_CLOSEPAR, p->GivStart()); + } + pStack->SetError(TX_BADTYPE, p->GivStart()); + } + pStack->SetError(TX_OPENPAR, p->GivStart()); + delete inst; // error, frees up + return NULL; // no object, the error is on the stack } // execution of "catch" bool CBotCatch :: Execute(CBotStack* &pj) { - if ( m_Block == NULL ) return true; - return m_Block->Execute(pj); // executes the associated block + if ( m_Block == NULL ) return true; + return m_Block->Execute(pj); // executes the associated block } void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) { - if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); + if ( bMain && m_Block != NULL ) m_Block->RestoreState(pj, bMain); } void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) { - m_Cond->RestoreState(pj, bMain); + m_Cond->RestoreState(pj, bMain); } // routine to see if the catch is to do or not bool CBotCatch :: TestCatch(CBotStack* &pile, int val) { - if ( !m_Cond->Execute(pile) ) return false; - - if ( val > 0 || pile->GivType() != CBotTypBoolean ) - { - CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); - var->SetValInt( pile->GivVal() == val ); - pile->SetVar(var); // calls on the stack - } - - return true; + if ( !m_Cond->Execute(pile) ) return false; + + if ( val > 0 || pile->GivType() != CBotTypBoolean ) + { + CBotVar* var = CBotVar::Create((CBotToken*)NULL, CBotTypBoolean); + var->SetValInt( pile->GivVal() == val ); + pile->SetVar(var); // calls on the stack + } + + return true; } /////////////////////////////////////////////////////////////////////////// @@ -1330,72 +1330,72 @@ bool CBotCatch :: TestCatch(CBotStack* &pile, int val) CBotThrow::CBotThrow() { - m_Value = NULL; // NULL so that delete is not possible further + m_Value = NULL; // NULL so that delete is not possible further - name = "CBotThrow"; // debug + name = "CBotThrow"; // debug } CBotThrow::~CBotThrow() { - delete m_Value; + delete m_Value; } CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) { - pStack->SetStartError(p->GivStart()); + pStack->SetStartError(p->GivStart()); - CBotThrow* inst = new CBotThrow(); // creates the object - inst->SetToken(p); + CBotThrow* inst = new CBotThrow(); // creates the object + inst->SetToken(p); - CBotToken* pp = p; // preserves at the ^ token (starting position) + CBotToken* pp = p; // preserves at the ^ token (starting position) - if (!IsOfType(p, ID_THROW)) return NULL; // should never happen + if (!IsOfType(p, ID_THROW)) return NULL; // should never happen - inst->m_Value = CBotExpression::Compile( p, pStack ); + inst->m_Value = CBotExpression::Compile( p, pStack ); - if (pStack->GivType() < CBotTypLong && pStack->IsOk()) - { - return inst; // return an object to the application - } - pStack->SetError(TX_BADTYPE, pp); + if (pStack->GivType() < CBotTypLong && pStack->IsOk()) + { + return inst; // return an object to the application + } + pStack->SetError(TX_BADTYPE, pp); - delete inst; // error, frees up - return NULL; // no object, the error is on the stack + delete inst; // error, frees up + return NULL; // no object, the error is on the stack } // execution of instruction "throw" bool CBotThrow :: Execute(CBotStack* &pj) { - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; - if ( pile->GivState() == 0 ) - { - if ( !m_Value->Execute(pile) ) return false; - pile->IncState(); - } + if ( pile->GivState() == 0 ) + { + if ( !m_Value->Execute(pile) ) return false; + pile->IncState(); + } - if ( pile->IfStep() ) return false; + if ( pile->IfStep() ) return false; - int val = pile->GivVal(); - if ( val < 0 ) val = TX_BADTHROW; - pile->SetError( val, &m_token ); - return pj->Return( pile ); + int val = pile->GivVal(); + if ( val < 0 ) val = TX_BADTHROW; + pile->SetError( val, &m_token ); + return pj->Return( pile ); } void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) { - if ( !bMain ) return; + if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == NULL ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == NULL ) return; - if ( pile->GivState() == 0 ) - { - m_Value->RestoreState(pile, bMain); - return; - } + if ( pile->GivState() == 0 ) + { + m_Value->RestoreState(pile, bMain); + return; + } } @@ -1405,7 +1405,7 @@ void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) CBotStartDebugDD::CBotStartDebugDD() { - name = "CBotStartDebugDD"; // debug + name = "CBotStartDebugDD"; // debug } CBotStartDebugDD::~CBotStartDebugDD() @@ -1415,9 +1415,9 @@ CBotStartDebugDD::~CBotStartDebugDD() CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) { - if (!IsOfType(p, ID_DEBUGDD)) return NULL; // should never happen + if (!IsOfType(p, ID_DEBUGDD)) return NULL; // should never happen - return new CBotStartDebugDD(); // creates the object + return new CBotStartDebugDD(); // creates the object } @@ -1425,9 +1425,9 @@ CBotInstr* CBotStartDebugDD::Compile(CBotToken* &p, CBotCStack* pStack) bool CBotStartDebugDD :: Execute(CBotStack* &pj) { - CBotProgram* p = pj->GivBotCall(); - p->m_bDebugDD = true; + CBotProgram* p = pj->GivBotCall(); + p->m_bDebugDD = true; - return true; + return true; } diff --git a/src/CBot/ClassFILE.cpp b/src/CBot/ClassFILE.cpp index 418ddb3..f73a1ac 100644 --- a/src/CBot/ClassFILE.cpp +++ b/src/CBot/ClassFILE.cpp @@ -1,428 +1,428 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -// ClassFile.cpp -// -// definition of methods for class FILE - - - -// Static variables - -static CBotClass* m_pClassFILE; -static CBotProgram* m_pFuncFile; -static int m_CompteurFileOpen = 0; - - - -// Prepares a file name. - -void PrepareFilename(CBotString &filename) //DD! -{ - int pos; - - pos = filename.ReverseFind('\\'); - if ( pos > 0 ) - { - filename = filename.Mid(pos+1); // remove the records (files)?? - } - - pos = filename.ReverseFind('/'); - if ( pos > 0 ) - { - filename = filename.Mid(pos+1); // also those with / - } - - pos = filename.ReverseFind(':'); - if ( pos > 0 ) - { - filename = filename.Mid(pos+1); // also removes the drive letter C: - } - - filename = CBotString("files\\") + filename; -} - - -// constructor of the class -// gets the filename as a parameter - -// execution -bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - CBotString mode; - - // accepts no parameters - if ( pVar == NULL ) return true; - - // must be a string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } - - CBotString filename = pVar->GivValString(); - PrepareFilename(filename); //DR - - // there may be a second parameter - pVar = pVar->GivNext(); - if ( pVar != NULL ) - { - // recovers the mode - mode = pVar->GivValString(); - if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } - - // no third parameter, only two or one possible - if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } - } - - // save the file name - pVar = pThis->GivItem("filename"); - pVar->SetValString(filename); - - if ( ! mode.IsEmpty() ) - { - // open the called file - FILE* pFile = fopen( filename, mode ); - if ( pFile == NULL ) { Exception = CBotErrFileOpen; return false; } - - m_CompteurFileOpen ++; - - // save the handle of file - pVar = pThis->GivItem("handle"); - pVar->SetValInt((long)pFile); - } - - return true; -} - -// compilation -CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) -{ - // accepts no parameters - if ( pVar == NULL ) return CBotTypResult( 0 ); - - // must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - - // there may be a second parameter - pVar = pVar->GivNext(); - if ( pVar != NULL ) - { - // must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - // no third parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - } - - // le r�sultat est de type void (constructeur) - return CBotTypResult( 0 ); -} - - -// destructor of the class - -// execution -bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // retrieves the element "handle" - pVar = pThis->GivItem("handle"); - - // not open? no problem - if ( pVar->GivInit() != IS_DEF) return true; - - FILE* pFile= (FILE*)pVar->GivValInt(); - fclose(pFile); - m_CompteurFileOpen --; - - pVar->SetInit(IS_NAN); - - return true; -} - - -// FILE :: open method -// get the r / w mode as a parameter - -// execution -bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // there must be a parameter - if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } - - // must be a string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } - - // there may be a second parameter - if ( pVar->GivNext() != NULL ) - { - // in this case the first parameter is the file name - CBotString filename = pVar->GivValString(); - PrepareFilename(filename); //DR - - // saves the file name - CBotVar* pVar2 = pThis->GivItem("filename"); - pVar2->SetValString(filename); - - // next parameter is the mode - pVar = pVar -> GivNext(); - } - - CBotString mode = pVar->GivValString(); - if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } - - // No third parameter - if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } - - // retrieves the element "handle" - pVar = pThis->GivItem("handle"); - - // which must not be initialized - if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return false; } - - // contains filename - pVar = pThis->GivItem("filename"); - CBotString filename = pVar->GivValString(); - - PrepareFilename(filename); //DD! (if the name was assigned by h.filename = "..."; - - // open requsted file - FILE* pFile = fopen( filename, mode ); - if ( pFile == NULL ) //DR - { - pResult->SetValInt(false); //DR - return true; //DR - } - - m_CompteurFileOpen ++; - - // saves the handle of file - pVar = pThis->GivItem("handle"); - pVar->SetValInt((long)pFile); - - pResult->SetValInt(true); //DR - return true; -} - -// compilation -CBotTypResult cfopen (CBotVar* pThis, CBotVar* &pVar) -{ - // there must be a parameter - if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); - - // must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - - // there may be a second parameter - pVar = pVar->GivNext(); - if ( pVar != NULL ) - { - // must be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( CBotErrBadString ); - - // no third parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - } - - // the result is of type bool - return CBotTypResult(CBotTypBoolean); //DR -} - - -// FILE :: close method - -// execution -bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // it should not be any parameter - if ( pVar != NULL ) return CBotErrOverParam; - - // retrieves the element "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - fclose(pFile); - m_CompteurFileOpen --; - - pVar->SetInit(IS_NAN); - - return true; -} - -// compilation -CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) -{ - // it should not be any parameter - if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - - // function returns a result "void" - return CBotTypResult( 0 ); -} - -// FILE :: writeln method - -// execution -bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // there must be a parameter - if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } - - // must be a string - if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } - - CBotString param = pVar->GivValString(); - - //retrieves the element "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - - int res = fputs(param+CBotString("\n"), pFile); - - // on error throws an exception - if ( res < 0 ) { Exception = CBotErrWrite; return false; } - - return true; -} - -// compilation -CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) -{ - // there must be a parameter - if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); - - // must be a string - if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); - - // no other parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); - - // function returns "void" result - return CBotTypResult( 0 ); -} - -// FILE :: readln method - -// execution -bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // there shouldn't be any parameter - if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } - - //retrieves the element "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - - char chaine[2000]; - int i; - for ( i = 0 ; i < 2000 ; i++ ) chaine[i] = 0; - - fgets(chaine, 1999, pFile); - - for ( i = 0 ; i < 2000 ; i++ ) if (chaine[i] == '\n') chaine[i] = 0; - - // on error throws an exception - if ( ferror(pFile) ) { Exception = CBotErrRead; return false; } - - pResult->SetValString( chaine ); - - return true; -} - -// compilation -CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) -{ - // there shouldn't be any parameter - if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - - // function return "string" result - return CBotTypResult( CBotTypString ); -} -// FILE :: readln method - - -// execution -bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - // there shouldn't be any parameter - if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } - - // retrieves the element "handle" - pVar = pThis->GivItem("handle"); - - if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } - - FILE* pFile= (FILE*)pVar->GivValInt(); - - pResult->SetValInt( feof( pFile ) ); - - return true; -} - -// compilation -CBotTypResult cfeof (CBotVar* pThis, CBotVar* &pVar) -{ - // there shouldn't be any parameter - if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); - - // function return boolean result - return CBotTypResult( CBotTypBoolean ); -} - - - - - -void InitClassFILE() -{ -// creates a class for file management -// the usage is as follows: -// file canal( "NomFichier.txt" ) -// canal.open( "r" ); // open reading -// s = canal.readln( ); // reads a line -// canal.close(); // closes the file - - // create class FILE - m_pClassFILE = new CBotClass("file", NULL); - // add the component ".filename" - m_pClassFILE->AddItem("filename", CBotTypString); - // add the component ".handle" - m_pClassFILE->AddItem("handle", CBotTypInt, PR_PRIVATE); - - // define a constructor and destructor - m_pClassFILE->AddFunction("file", rfconstruct, cfconstruct ); - m_pClassFILE->AddFunction("~file", rfdestruct, NULL ); - - // defined associated methods - m_pClassFILE->AddFunction("open", rfopen, cfopen ); - m_pClassFILE->AddFunction("close", rfclose, cfclose ); - m_pClassFILE->AddFunction("writeln", rfwrite, cfwrite ); - m_pClassFILE->AddFunction("readln", rfread, cfread ); - m_pClassFILE->AddFunction("eof", rfeof, cfeof ); - - m_pFuncFile = new CBotProgram( ); - CBotStringArray ListFonctions; - m_pFuncFile->Compile( "public file openfile(string name, string mode) {return new file(name, mode);}", ListFonctions); - m_pFuncFile->SetIdent(-2); // restoreState as a special identifier for this function -} - +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +// ClassFile.cpp +// +// definition of methods for class FILE + + + +// Static variables + +static CBotClass* m_pClassFILE; +static CBotProgram* m_pFuncFile; +static int m_CompteurFileOpen = 0; + + + +// Prepares a file name. + +void PrepareFilename(CBotString &filename) //DD! +{ + int pos; + + pos = filename.ReverseFind('\\'); + if ( pos > 0 ) + { + filename = filename.Mid(pos+1); // remove the records (files)?? + } + + pos = filename.ReverseFind('/'); + if ( pos > 0 ) + { + filename = filename.Mid(pos+1); // also those with / + } + + pos = filename.ReverseFind(':'); + if ( pos > 0 ) + { + filename = filename.Mid(pos+1); // also removes the drive letter C: + } + + filename = CBotString("files\\") + filename; +} + + +// constructor of the class +// gets the filename as a parameter + +// execution +bool rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + CBotString mode; + + // accepts no parameters + if ( pVar == NULL ) return true; + + // must be a string + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } + + CBotString filename = pVar->GivValString(); + PrepareFilename(filename); //DR + + // there may be a second parameter + pVar = pVar->GivNext(); + if ( pVar != NULL ) + { + // recovers the mode + mode = pVar->GivValString(); + if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } + + // no third parameter, only two or one possible + if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } + } + + // save the file name + pVar = pThis->GivItem("filename"); + pVar->SetValString(filename); + + if ( ! mode.IsEmpty() ) + { + // open the called file + FILE* pFile = fopen( filename, mode ); + if ( pFile == NULL ) { Exception = CBotErrFileOpen; return false; } + + m_CompteurFileOpen ++; + + // save the handle of file + pVar = pThis->GivItem("handle"); + pVar->SetValInt((long)pFile); + } + + return true; +} + +// compilation +CBotTypResult cfconstruct (CBotVar* pThis, CBotVar* &pVar) +{ + // accepts no parameters + if ( pVar == NULL ) return CBotTypResult( 0 ); + + // must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + + // there may be a second parameter + pVar = pVar->GivNext(); + if ( pVar != NULL ) + { + // must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); + } + + // le r�sultat est de type void (constructeur) + return CBotTypResult( 0 ); +} + + +// destructor of the class + +// execution +bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // retrieves the element "handle" + pVar = pThis->GivItem("handle"); + + // not open? no problem + if ( pVar->GivInit() != IS_DEF) return true; + + FILE* pFile= (FILE*)pVar->GivValInt(); + fclose(pFile); + m_CompteurFileOpen --; + + pVar->SetInit(IS_NAN); + + return true; +} + + +// FILE :: open method +// get the r / w mode as a parameter + +// execution +bool rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // there must be a parameter + if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } + + // must be a string + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } + + // there may be a second parameter + if ( pVar->GivNext() != NULL ) + { + // in this case the first parameter is the file name + CBotString filename = pVar->GivValString(); + PrepareFilename(filename); //DR + + // saves the file name + CBotVar* pVar2 = pThis->GivItem("filename"); + pVar2->SetValString(filename); + + // next parameter is the mode + pVar = pVar -> GivNext(); + } + + CBotString mode = pVar->GivValString(); + if ( mode != "r" && mode != "w" ) { Exception = CBotErrBadParam; return false; } + + // No third parameter + if ( pVar->GivNext() != NULL ) { Exception = CBotErrOverParam; return false; } + + // retrieves the element "handle" + pVar = pThis->GivItem("handle"); + + // which must not be initialized + if ( pVar->GivInit() == IS_DEF) { Exception = CBotErrFileOpen; return false; } + + // contains filename + pVar = pThis->GivItem("filename"); + CBotString filename = pVar->GivValString(); + + PrepareFilename(filename); //DD! (if the name was assigned by h.filename = "..."; + + // open requsted file + FILE* pFile = fopen( filename, mode ); + if ( pFile == NULL ) //DR + { + pResult->SetValInt(false); //DR + return true; //DR + } + + m_CompteurFileOpen ++; + + // saves the handle of file + pVar = pThis->GivItem("handle"); + pVar->SetValInt((long)pFile); + + pResult->SetValInt(true); //DR + return true; +} + +// compilation +CBotTypResult cfopen (CBotVar* pThis, CBotVar* &pVar) +{ + // there must be a parameter + if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); + + // must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + + // there may be a second parameter + pVar = pVar->GivNext(); + if ( pVar != NULL ) + { + // must be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( CBotErrBadString ); + + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); + } + + // the result is of type bool + return CBotTypResult(CBotTypBoolean); //DR +} + + +// FILE :: close method + +// execution +bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // it should not be any parameter + if ( pVar != NULL ) return CBotErrOverParam; + + // retrieves the element "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + fclose(pFile); + m_CompteurFileOpen --; + + pVar->SetInit(IS_NAN); + + return true; +} + +// compilation +CBotTypResult cfclose (CBotVar* pThis, CBotVar* &pVar) +{ + // it should not be any parameter + if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); + + // function returns a result "void" + return CBotTypResult( 0 ); +} + +// FILE :: writeln method + +// execution +bool rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // there must be a parameter + if ( pVar == NULL ) { Exception = CBotErrLowParam; return false; } + + // must be a string + if ( pVar->GivType() != CBotTypString ) { Exception = CBotErrBadString; return false; } + + CBotString param = pVar->GivValString(); + + //retrieves the element "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + + int res = fputs(param+CBotString("\n"), pFile); + + // on error throws an exception + if ( res < 0 ) { Exception = CBotErrWrite; return false; } + + return true; +} + +// compilation +CBotTypResult cfwrite (CBotVar* pThis, CBotVar* &pVar) +{ + // there must be a parameter + if ( pVar == NULL ) return CBotTypResult( CBotErrLowParam ); + + // must be a string + if ( pVar->GivType() != CBotTypString ) return CBotTypResult( CBotErrBadString ); + + // no other parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( CBotErrOverParam ); + + // function returns "void" result + return CBotTypResult( 0 ); +} + +// FILE :: readln method + +// execution +bool rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // there shouldn't be any parameter + if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } + + //retrieves the element "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + + char chaine[2000]; + int i; + for ( i = 0 ; i < 2000 ; i++ ) chaine[i] = 0; + + fgets(chaine, 1999, pFile); + + for ( i = 0 ; i < 2000 ; i++ ) if (chaine[i] == '\n') chaine[i] = 0; + + // on error throws an exception + if ( ferror(pFile) ) { Exception = CBotErrRead; return false; } + + pResult->SetValString( chaine ); + + return true; +} + +// compilation +CBotTypResult cfread (CBotVar* pThis, CBotVar* &pVar) +{ + // there shouldn't be any parameter + if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); + + // function return "string" result + return CBotTypResult( CBotTypString ); +} +// FILE :: readln method + + +// execution +bool rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + // there shouldn't be any parameter + if ( pVar != NULL ) { Exception = CBotErrOverParam; return false; } + + // retrieves the element "handle" + pVar = pThis->GivItem("handle"); + + if ( pVar->GivInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; } + + FILE* pFile= (FILE*)pVar->GivValInt(); + + pResult->SetValInt( feof( pFile ) ); + + return true; +} + +// compilation +CBotTypResult cfeof (CBotVar* pThis, CBotVar* &pVar) +{ + // there shouldn't be any parameter + if ( pVar != NULL ) return CBotTypResult( CBotErrOverParam ); + + // function return boolean result + return CBotTypResult( CBotTypBoolean ); +} + + + + + +void InitClassFILE() +{ +// creates a class for file management +// the usage is as follows: +// file canal( "NomFichier.txt" ) +// canal.open( "r" ); // open reading +// s = canal.readln( ); // reads a line +// canal.close(); // closes the file + + // create class FILE + m_pClassFILE = new CBotClass("file", NULL); + // add the component ".filename" + m_pClassFILE->AddItem("filename", CBotTypString); + // add the component ".handle" + m_pClassFILE->AddItem("handle", CBotTypInt, PR_PRIVATE); + + // define a constructor and destructor + m_pClassFILE->AddFunction("file", rfconstruct, cfconstruct ); + m_pClassFILE->AddFunction("~file", rfdestruct, NULL ); + + // defined associated methods + m_pClassFILE->AddFunction("open", rfopen, cfopen ); + m_pClassFILE->AddFunction("close", rfclose, cfclose ); + m_pClassFILE->AddFunction("writeln", rfwrite, cfwrite ); + m_pClassFILE->AddFunction("readln", rfread, cfread ); + m_pClassFILE->AddFunction("eof", rfeof, cfeof ); + + m_pFuncFile = new CBotProgram( ); + CBotStringArray ListFonctions; + m_pFuncFile->Compile( "public file openfile(string name, string mode) {return new file(name, mode);}", ListFonctions); + m_pFuncFile->SetIdent(-2); // restoreState as a special identifier for this function +} + diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 213b956..39bafca 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -22,21 +22,21 @@ bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // no second parameter - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + // no second parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // puts the length of the stack - pResult->SetValInt( s.GivLength() ); - return true; + // puts the length of the stack + pResult->SetValInt( s.GivLength() ); + return true; } // int xxx ( string ) @@ -44,18 +44,18 @@ bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADPARAM ); + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADPARAM ); - // no second parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - // the end result is an integer - return CBotTypResult( CBotTypInt ); + // the end result is an integer + return CBotTypResult( CBotTypInt ); } @@ -64,34 +64,34 @@ CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - // retrieves this number - int n = pVar->GivValInt(); + // retrieves this number + int n = pVar->GivValInt(); - // no third parameter - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - // takes the interesting part - s = s.Left( n ); + // takes the interesting part + s = s.Left( n ); - // puts on the stack - pResult->SetValString( s ); - return true; + // puts on the stack + pResult->SetValString( s ); + return true; } // string xxx ( string, int ) @@ -99,26 +99,26 @@ bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) - return CBotTypResult( TX_BADNUM ); + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); - // no third parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - // the end result is a string - return CBotTypResult( CBotTypString ); + // the end result is a string + return CBotTypResult( CBotTypString ); } // gives the right of a string @@ -126,34 +126,34 @@ CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - // retrieves this number - int n = pVar->GivValInt(); + // retrieves this number + int n = pVar->GivValInt(); - // no third parameter - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - // takes the interesting part - s = s.Right( n ); + // takes the interesting part + s = s.Right( n ); - // puts on the stack - pResult->SetValString( s ); - return true; + // puts on the stack + pResult->SetValString( s ); + return true; } // gives the central part of a chain @@ -161,51 +161,51 @@ bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - - // get the contents of the string - CBotString s = pVar->GivValString(); - - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - - // retrieves this number - int n = pVar->GivValInt(); - - // third parameter optional - if ( pVar->GivNext() != NULL ) - { - pVar = pVar->GivNext(); - - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } - - // retrieves this number - int l = pVar->GivValInt(); - - // but no fourth parameter - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - - // takes the interesting part - s = s.Mid( n, l ); - } - else - { - // takes the interesting part - s = s.Mid( n ); - } - - // puts on the stack - pResult->SetValString( s ); - return true; + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + + // get the contents of the string + CBotString s = pVar->GivValString(); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int n = pVar->GivValInt(); + + // third parameter optional + if ( pVar->GivNext() != NULL ) + { + pVar = pVar->GivNext(); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) { ex = TX_BADNUM ; return true; } + + // retrieves this number + int l = pVar->GivValInt(); + + // but no fourth parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + + // takes the interesting part + s = s.Mid( n, l ); + } + else + { + // takes the interesting part + s = s.Mid( n ); + } + + // puts on the stack + pResult->SetValString( s ); + return true; } // gives the central part of a chain @@ -213,36 +213,36 @@ bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); - - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) - return CBotTypResult( TX_BADNUM ); - - // third parameter optional - if ( pVar->GivNext() != NULL ) - { - - pVar = pVar->GivNext(); - // which must be a number - if ( pVar->GivType() > CBotTypDouble ) - return CBotTypResult( TX_BADNUM ); - - // no fourth parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - } - - // the end result is a string - return CBotTypResult( CBotTypString ); + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); + + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // third parameter optional + if ( pVar->GivNext() != NULL ) + { + + pVar = pVar->GivNext(); + // which must be a number + if ( pVar->GivType() > CBotTypDouble ) + return CBotTypResult( TX_BADNUM ); + + // no fourth parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + } + + // the end result is a string + return CBotTypResult( CBotTypString ); } @@ -251,23 +251,23 @@ CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // but no second parameter - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - float val = GivNumFloat(s); + float val = GivNumFloat(s); - // puts the value on the stack - pResult->SetValFloat( val ); - return true; + // puts the value on the stack + pResult->SetValFloat( val ); + return true; } // float xxx ( string ) @@ -275,18 +275,18 @@ bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); - // no second parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - // the end result is a number - return CBotTypResult( CBotTypFloat ); + // the end result is a number + return CBotTypResult( CBotTypFloat ); } @@ -295,33 +295,33 @@ CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // retrieves this number - CBotString s2 = pVar->GivValString(); + // retrieves this number + CBotString s2 = pVar->GivValString(); - // no third parameter - if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } + // no third parameter + if ( pVar->GivNext() != NULL ) { ex = TX_OVERPARAM ; return true; } - // puts the result on the stack - int res = s.Find(s2); - pResult->SetValInt( res ); - if ( res < 0 ) pResult->SetInit( IS_NAN ); - return true; + // puts the result on the stack + int res = s.Find(s2); + pResult->SetValInt( res ); + if ( res < 0 ) pResult->SetInit( IS_NAN ); + return true; } // int xxx ( string, string ) @@ -329,26 +329,26 @@ bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); - // it takes a second parameter - pVar = pVar->GivNext(); - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a second parameter + pVar = pVar->GivNext(); + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); - // no third parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + // no third parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - // the end result is a number - return CBotTypResult( CBotTypInt ); + // the end result is a number + return CBotTypResult( CBotTypInt ); } // gives a string to uppercase @@ -356,24 +356,24 @@ CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // but no second parameter - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - s.MakeUpper(); + s.MakeUpper(); - // puts the value on the stack - pResult->SetValString( s ); - return true; + // puts the value on the stack + pResult->SetValString( s ); + return true; } // gives a string to lowercase @@ -381,24 +381,24 @@ bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } + // it takes a parameter + if ( pVar == NULL ) { ex = TX_LOWPARAM ; return true; } - // to be a string - if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } + // to be a string + if ( pVar->GivType() != CBotTypString ) { ex = TX_BADSTRING ; return true; } - // get the contents of the string - CBotString s = pVar->GivValString(); + // get the contents of the string + CBotString s = pVar->GivValString(); - // but no second parameter - if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } + // but no second parameter + if ( pVar->GivNext() != NULL ){ ex = TX_OVERPARAM ; return true; } - s.MakeLower(); + s.MakeLower(); - // puts the value on the stack - pResult->SetValString( s ); - return true; + // puts the value on the stack + pResult->SetValString( s ); + return true; } // string xxx ( string ) @@ -406,31 +406,31 @@ bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) { - // it takes a parameter - if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); + // it takes a parameter + if ( pVar == NULL ) return CBotTypResult( TX_LOWPARAM ); - // to be a string - if ( pVar->GivType() != CBotTypString ) - return CBotTypResult( TX_BADSTRING ); + // to be a string + if ( pVar->GivType() != CBotTypString ) + return CBotTypResult( TX_BADSTRING ); - // no second parameter - if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); + // no second parameter + if ( pVar->GivNext() != NULL ) return CBotTypResult( TX_OVERPARAM ); - // the end result is a string - return CBotTypResult( CBotTypString ); + // the end result is a string + return CBotTypResult( CBotTypString ); } void InitStringFunctions() { - CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); - CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt ); - CBotProgram::AddFunction("strright", rStrRight, cStrStrInt ); - CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt ); + CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); + CBotProgram::AddFunction("strleft", rStrLeft, cStrStrInt ); + CBotProgram::AddFunction("strright", rStrRight, cStrStrInt ); + CBotProgram::AddFunction("strmid", rStrMid, cStrStrIntInt ); - CBotProgram::AddFunction("strval", rStrVal, cFloatStr ); - CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr ); + CBotProgram::AddFunction("strval", rStrVal, cFloatStr ); + CBotProgram::AddFunction("strfind", rStrFind, cIntStrStr ); - CBotProgram::AddFunction("strupper", rStrUpper, cStrStr ); - CBotProgram::AddFunction("strlower", rStrLower, cStrStr ); + CBotProgram::AddFunction("strupper", rStrUpper, cStrStr ); + CBotProgram::AddFunction("strlower", rStrLower, cStrStr ); } diff --git a/src/CBot/resource.h b/src/CBot/resource.h index da1ad23..96a01ba 100644 --- a/src/CBot/resource.h +++ b/src/CBot/resource.h @@ -1,177 +1,177 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch -// * -// * This program 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 3 of the License, or -// * (at your option) any later version. -// * -// * This program 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 this program. If not, see http://www.gnu.org/licenses/. - -#pragma once -#ifndef _RESOURCE_H_ -#define _RESOURCE_H_ - -enum EID -{ - ID_IF = 2000, - ID_ELSE, - ID_WHILE, - ID_DO, - ID_FOR, - ID_BREAK, - ID_CONTINUE, - ID_SWITCH, - ID_CASE, - ID_DEFAULT, - ID_TRY, - ID_THROW, - ID_CATCH, - ID_FINALLY, - ID_TXT_AND, - ID_TXT_OR, - ID_TXT_NOT, - ID_RETURN, - ID_CLASS, - ID_EXTENDS, - ID_SYNCHO, - ID_NEW, - ID_PUBLIC, - ID_EXTERN, - ID_FINAL, - ID_STATIC, - ID_PROTECTED, - ID_PRIVATE, - ID_REPEAT, - ID_DEBUGDD, - ID_INT, - ID_FLOAT, - ID_BOOLEAN, - ID_STRING, - ID_VOID, - ID_BOOL, - - ID_TRUE = 2200, - ID_FALSE, - ID_NULL, - ID_NAN, - - ID_OPENPAR = 2300, - ID_CLOSEPAR, - ID_OPBLK, - ID_CLBLK, - ID_SEP, - ID_COMMA, - ID_DOTS, - ID_DOT, - ID_OPBRK, - ID_CLBRK, - ID_DBLDOTS, - ID_LOGIC, - ID_ADD, - ID_SUB, - ID_MUL, - ID_DIV, - ID_ASS, - ID_ASSADD, - ID_ASSSUB, - ID_ASSMUL, - ID_ASSDIV, - ID_ASSOR, - ID_ASSAND, - ID_ASSXOR, - ID_ASSSL, - ID_ASSSR, - ID_ASSASR, - ID_SL, - ID_SR, - ID_ASR, - ID_INC, - ID_DEC, - ID_LO, - ID_HI, - ID_LS, - ID_HS, - ID_EQ, - ID_NE, - ID_AND, - ID_XOR, - ID_OR, - ID_LOG_AND, - ID_LOG_OR, - ID_LOG_NOT, - ID_NOT, - ID_MODULO, - ID_POWER, - ID_ASSMODULO, - TX_UNDEF = 4000, - TX_NAN, - ID_SUPER = 6000 -}; -#define TX_OPENPAR 5000 -#define TX_CLOSEPAR 5001 -#define TX_NOTBOOL 5002 -#define TX_UNDEFVAR 5003 -#define TX_BADLEFT 5004 -#define TX_ENDOF 5005 -#define TX_OUTCASE 5006 -#define TX_NOTERM 5007 -#define TX_CLOSEBLK 5008 -#define TX_ELSEWITHOUTIF 5009 -#define TX_OPENBLK 5010 -#define TX_BADTYPE 5011 -#define TX_REDEFVAR 5012 -#define TX_BAD2TYPE 5013 -#define TX_UNDEFCALL 5014 -#define TX_MISDOTS 5015 -#define TX_WHILE 5016 -#define TX_BREAK 5017 -#define TX_LABEL 5018 -#define TX_NOLABEL 5019 -#define TX_NOCASE 5020 -#define TX_BADNUM 5021 -#define TX_VOID 5022 -#define TX_NOTYP 5023 -#define TX_NOVAR 5024 -#define TX_NOFONC 5025 -#define TX_OVERPARAM 5026 -#define TX_REDEF 5027 -#define TX_LOWPARAM 5028 -#define TX_BADPARAM 5029 -#define TX_NUMPARAM 5030 -#define TX_NOITEM 5031 -#define TX_DOT 5032 -#define TX_NOCONST 5033 -#define TX_REDEFCLASS 5034 -#define TX_CLBRK 5035 -#define TX_RESERVED 5036 -#define TX_BADNEW 5037 -#define TX_OPBRK 5038 -#define TX_BADSTRING 5039 -#define TX_BADINDEX 5040 -#define TX_PRIVATE 5041 -#define TX_NOPUBLIC 5042 -#define TX_DIVZERO 6000 -#define TX_NOTINIT 6001 -#define TX_BADTHROW 6002 -#define TX_NORETVAL 6003 -#define TX_NORUN 6004 -#define TX_NOCALL 6005 -#define TX_NOCLASS 6006 -#define TX_NULLPT 6007 -#define TX_OPNAN 6008 -#define TX_OUTARRAY 6009 -#define TX_STACKOVER 6010 -#define TX_DELETEDPT 6011 -#define TX_FILEOPEN 6012 -#define TX_NOTOPEN 6013 -#define TX_ERRREAD 6014 -#define TX_ERRWRITE 6015 - -#endif //_RESOURCE_H_ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program 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 3 of the License, or +// * (at your option) any later version. +// * +// * This program 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 this program. If not, see http://www.gnu.org/licenses/. + +#pragma once +#ifndef _RESOURCE_H_ +#define _RESOURCE_H_ + +enum EID +{ + ID_IF = 2000, + ID_ELSE, + ID_WHILE, + ID_DO, + ID_FOR, + ID_BREAK, + ID_CONTINUE, + ID_SWITCH, + ID_CASE, + ID_DEFAULT, + ID_TRY, + ID_THROW, + ID_CATCH, + ID_FINALLY, + ID_TXT_AND, + ID_TXT_OR, + ID_TXT_NOT, + ID_RETURN, + ID_CLASS, + ID_EXTENDS, + ID_SYNCHO, + ID_NEW, + ID_PUBLIC, + ID_EXTERN, + ID_FINAL, + ID_STATIC, + ID_PROTECTED, + ID_PRIVATE, + ID_REPEAT, + ID_DEBUGDD, + ID_INT, + ID_FLOAT, + ID_BOOLEAN, + ID_STRING, + ID_VOID, + ID_BOOL, + + ID_TRUE = 2200, + ID_FALSE, + ID_NULL, + ID_NAN, + + ID_OPENPAR = 2300, + ID_CLOSEPAR, + ID_OPBLK, + ID_CLBLK, + ID_SEP, + ID_COMMA, + ID_DOTS, + ID_DOT, + ID_OPBRK, + ID_CLBRK, + ID_DBLDOTS, + ID_LOGIC, + ID_ADD, + ID_SUB, + ID_MUL, + ID_DIV, + ID_ASS, + ID_ASSADD, + ID_ASSSUB, + ID_ASSMUL, + ID_ASSDIV, + ID_ASSOR, + ID_ASSAND, + ID_ASSXOR, + ID_ASSSL, + ID_ASSSR, + ID_ASSASR, + ID_SL, + ID_SR, + ID_ASR, + ID_INC, + ID_DEC, + ID_LO, + ID_HI, + ID_LS, + ID_HS, + ID_EQ, + ID_NE, + ID_AND, + ID_XOR, + ID_OR, + ID_LOG_AND, + ID_LOG_OR, + ID_LOG_NOT, + ID_NOT, + ID_MODULO, + ID_POWER, + ID_ASSMODULO, + TX_UNDEF = 4000, + TX_NAN, + ID_SUPER = 6000 +}; +#define TX_OPENPAR 5000 +#define TX_CLOSEPAR 5001 +#define TX_NOTBOOL 5002 +#define TX_UNDEFVAR 5003 +#define TX_BADLEFT 5004 +#define TX_ENDOF 5005 +#define TX_OUTCASE 5006 +#define TX_NOTERM 5007 +#define TX_CLOSEBLK 5008 +#define TX_ELSEWITHOUTIF 5009 +#define TX_OPENBLK 5010 +#define TX_BADTYPE 5011 +#define TX_REDEFVAR 5012 +#define TX_BAD2TYPE 5013 +#define TX_UNDEFCALL 5014 +#define TX_MISDOTS 5015 +#define TX_WHILE 5016 +#define TX_BREAK 5017 +#define TX_LABEL 5018 +#define TX_NOLABEL 5019 +#define TX_NOCASE 5020 +#define TX_BADNUM 5021 +#define TX_VOID 5022 +#define TX_NOTYP 5023 +#define TX_NOVAR 5024 +#define TX_NOFONC 5025 +#define TX_OVERPARAM 5026 +#define TX_REDEF 5027 +#define TX_LOWPARAM 5028 +#define TX_BADPARAM 5029 +#define TX_NUMPARAM 5030 +#define TX_NOITEM 5031 +#define TX_DOT 5032 +#define TX_NOCONST 5033 +#define TX_REDEFCLASS 5034 +#define TX_CLBRK 5035 +#define TX_RESERVED 5036 +#define TX_BADNEW 5037 +#define TX_OPBRK 5038 +#define TX_BADSTRING 5039 +#define TX_BADINDEX 5040 +#define TX_PRIVATE 5041 +#define TX_NOPUBLIC 5042 +#define TX_DIVZERO 6000 +#define TX_NOTINIT 6001 +#define TX_BADTHROW 6002 +#define TX_NORETVAL 6003 +#define TX_NORUN 6004 +#define TX_NOCALL 6005 +#define TX_NOCLASS 6006 +#define TX_NULLPT 6007 +#define TX_OPNAN 6008 +#define TX_OUTARRAY 6009 +#define TX_STACKOVER 6010 +#define TX_DELETEDPT 6011 +#define TX_FILEOPEN 6012 +#define TX_NOTOPEN 6013 +#define TX_ERRREAD 6014 +#define TX_ERRWRITE 6015 + +#endif //_RESOURCE_H_ -- cgit v1.2.3-1-g7c22